814bdbbc2a
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
46 lines
1.8 KiB
TypeScript
46 lines
1.8 KiB
TypeScript
import { model, models, Schema, type InferSchemaType, type Model } from "mongoose";
|
||
|
||
/**
|
||
* Sayım oturumu (Bölüm 6.10, periyodik sayım). Her sayılan ürün için beklenen ve sayılan miktar,
|
||
* fireye ayrılan kısım. Fark sıfır olsa da kayıt tutulur: "son sayım" ve sayım takvimi buradan
|
||
* türetilir. Oluşan hareketler `countId` ile bağlanır.
|
||
*/
|
||
const stockCountItemSchema = new Schema(
|
||
{
|
||
productTypeId: { type: Schema.Types.ObjectId, required: true },
|
||
expected: { type: Number, required: true },
|
||
actual: { type: Number, required: true },
|
||
/** Eksiğin fire olarak işaretlenen kısmı (≥ 0). */
|
||
wasted: { type: Number, default: 0 },
|
||
},
|
||
{ _id: false },
|
||
);
|
||
|
||
const stockCountSchema = new Schema(
|
||
{
|
||
householdId: { type: Schema.Types.ObjectId, required: true },
|
||
/** "YYYY-MM-DD" (İstanbul). */
|
||
date: { type: String, required: true },
|
||
/** Sayılan stok kategorisi (alt ağacıyla); null: tüm stok veya tek ürün. */
|
||
categoryId: { type: Schema.Types.ObjectId, default: null },
|
||
items: { type: [stockCountItemSchema], default: [] },
|
||
note: { type: String, default: "", maxlength: 300 },
|
||
seedRef: { type: String, default: null },
|
||
createdBy: { type: Schema.Types.ObjectId, required: true },
|
||
},
|
||
{ timestamps: true, collection: "stockCounts" },
|
||
);
|
||
|
||
stockCountSchema.index({ householdId: 1, "items.productTypeId": 1, date: -1 });
|
||
stockCountSchema.index({ householdId: 1, date: -1 });
|
||
stockCountSchema.index(
|
||
{ householdId: 1, seedRef: 1 },
|
||
{ unique: true, partialFilterExpression: { seedRef: { $type: "string" } } },
|
||
);
|
||
|
||
export type StockCount = InferSchemaType<typeof stockCountSchema>;
|
||
|
||
export const StockCountModel: Model<StockCount> =
|
||
(models.StockCount as Model<StockCount> | undefined) ??
|
||
model<StockCount>("StockCount", stockCountSchema);
|