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

49 lines
2.0 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";
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);