d9de29c18d
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
49 lines
2.0 KiB
TypeScript
49 lines
2.0 KiB
TypeScript
import { model, models, Schema, type InferSchemaType, type Model } from "mongoose";
|
||
import { STOCK_MOVEMENT_KINDS, WASTE_REASONS } from "@/shared/stock";
|
||
|
||
/**
|
||
* Stok hareketi: alım, tüketim, fire (neden ile), sayım düzeltmesi. Mevcut miktar hareketlerden
|
||
* türetilir. Miktar binde bir; yalnızca sayım düzeltmesi işaretlidir. Sıra: tarih, oluşturulma.
|
||
*/
|
||
const stockMovementSchema = new Schema(
|
||
{
|
||
householdId: { type: Schema.Types.ObjectId, required: true },
|
||
productTypeId: { type: Schema.Types.ObjectId, required: true },
|
||
kind: { type: String, enum: STOCK_MOVEMENT_KINDS, required: true },
|
||
/** "YYYY-MM-DD" (İstanbul). */
|
||
date: { type: String, required: true },
|
||
quantity: { type: Number, required: true },
|
||
wasteReason: { type: String, enum: [...WASTE_REASONS, null], default: null },
|
||
/** Son kullanma tarihi (yalnızca alımda, isteğe bağlı). */
|
||
expiryDate: { type: String, default: null },
|
||
note: { type: String, default: "", maxlength: 300 },
|
||
/** İlk kurulumda girilen başlangıç miktarı (sayım düzeltmesi olarak). */
|
||
initial: { type: Boolean, default: false },
|
||
/** Sayım oturumu (Faz 11b). */
|
||
countId: { type: Schema.Types.ObjectId, default: null },
|
||
deletedAt: { type: Date, default: null },
|
||
seedRef: { type: String, default: null },
|
||
createdBy: { type: Schema.Types.ObjectId, required: true },
|
||
},
|
||
{ timestamps: true, collection: "stockMovements" },
|
||
);
|
||
|
||
stockMovementSchema.index({
|
||
householdId: 1,
|
||
productTypeId: 1,
|
||
deletedAt: 1,
|
||
date: 1,
|
||
createdAt: 1,
|
||
});
|
||
stockMovementSchema.index({ householdId: 1, kind: 1, date: -1 });
|
||
stockMovementSchema.index(
|
||
{ householdId: 1, seedRef: 1 },
|
||
{ unique: true, partialFilterExpression: { seedRef: { $type: "string" } } },
|
||
);
|
||
|
||
export type StockMovement = InferSchemaType<typeof stockMovementSchema>;
|
||
|
||
export const StockMovementModel: Model<StockMovement> =
|
||
(models.StockMovement as Model<StockMovement> | undefined) ??
|
||
model<StockMovement>("StockMovement", stockMovementSchema);
|