Files
butcem/src/models/payee.ts
T

26 lines
1.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 { TRANSACTION_TYPES } from "@/shared/transactions";
/** Satıcı / karşı taraf ve "akıllı giriş" kuralı: son kullanılan kategori ve hesap (Bölüm 6.3). */
const payeeSchema = new Schema(
{
householdId: { type: Schema.Types.ObjectId, required: true },
name: { type: String, required: true, trim: true, maxlength: 80 },
nameKey: { type: String, required: true },
lastType: { type: String, enum: TRANSACTION_TYPES, default: "expense" },
lastCategoryId: { type: Schema.Types.ObjectId, default: null },
lastAccountId: { type: Schema.Types.ObjectId, default: null },
useCount: { type: Number, default: 0 },
lastUsedAt: { type: Date, default: () => new Date() },
},
{ timestamps: true, collection: "payees" },
);
payeeSchema.index({ householdId: 1, nameKey: 1 }, { unique: true });
payeeSchema.index({ householdId: 1, useCount: -1 });
export type Payee = InferSchemaType<typeof payeeSchema>;
export const PayeeModel: Model<Payee> =
(models.Payee as Model<Payee> | undefined) ?? model<Payee>("Payee", payeeSchema);