Files
butcem/src/models/stock-count.ts
T

46 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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);