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

46 lines
2.1 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 { MOVEMENT_KINDS } from "@/shared/portfolio";
/**
* Varlık hareketi (lot): alım, gelen hediye, satış, hediye/çıkış. Alım ve satış bir hesaba
* bağlıysa transfer işlemi oluşturulur (`transactionId`; gelir/gider raporlarına girmez).
* Sıra: tarih, sonra oluşturulma anı.
*/
const assetMovementSchema = new Schema(
{
householdId: { type: Schema.Types.ObjectId, required: true },
assetId: { type: Schema.Types.ObjectId, required: true },
kind: { type: String, enum: MOVEMENT_KINDS, required: true },
/** "YYYY-MM-DD" (İstanbul). */
date: { type: String, required: true },
/** Binde bir. */
quantity: { type: Number, required: true, min: 1 },
unitPriceMinor: { type: Number, default: null },
/** Alımda ödenen, satışta alınan, gelen hediyede değer; çıkışta 0. */
amountMinor: { type: Number, default: 0, min: 0 },
accountId: { type: Schema.Types.ObjectId, default: null },
transactionId: { type: Schema.Types.ObjectId, default: null },
/** Saklama yeri (kasa, banka kiralık kasası …). */
storage: { type: String, default: "", maxlength: 120 },
note: { type: String, default: "", maxlength: 300 },
/** Aynî borç/alacak ödemesinden gelen hareket (Faz 9b). */
inKindDebtId: { 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: "assetMovements" },
);
assetMovementSchema.index({ householdId: 1, assetId: 1, deletedAt: 1, date: 1, createdAt: 1 });
assetMovementSchema.index(
{ householdId: 1, seedRef: 1 },
{ unique: true, partialFilterExpression: { seedRef: { $type: "string" } } },
);
export type AssetMovement = InferSchemaType<typeof assetMovementSchema>;
export const AssetMovementModel: Model<AssetMovement> =
(models.AssetMovement as Model<AssetMovement> | undefined) ??
model<AssetMovement>("AssetMovement", assetMovementSchema);