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

38 lines
1.7 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";
/**
* Stok kategori ağacı (Bölüm 6.10), gelir/gider kategorilerinden ayrı. Silme yok; arşivleme.
* `expenseCategoryId`: alışveriş tamamlanınca (Faz 13) kalemlerin yazılacağı gider kategorisi;
* null ise üst kategoriden devralınır. `countEveryDays`: periyodik sayım sıklığı (Faz 11b);
* null ise üstten devralınır.
*/
const stockCategorySchema = new Schema(
{
householdId: { type: Schema.Types.ObjectId, required: true },
name: { type: String, required: true, trim: true, maxlength: 80 },
nameKey: { type: String, required: true },
parentId: { type: Schema.Types.ObjectId, default: null },
ancestors: { type: [Schema.Types.ObjectId], default: [] },
order: { type: Number, default: 0 },
expenseCategoryId: { type: Schema.Types.ObjectId, default: null },
countEveryDays: { type: Number, default: null, min: 1, max: 365 },
archivedAt: { type: Date, default: null },
defaultKey: { type: String, default: null },
createdBy: { type: Schema.Types.ObjectId, required: true },
updatedBy: { type: Schema.Types.ObjectId, required: true },
},
{ timestamps: true, collection: "stockCategories" },
);
stockCategorySchema.index({ householdId: 1, parentId: 1, order: 1 });
stockCategorySchema.index(
{ householdId: 1, defaultKey: 1 },
{ unique: true, partialFilterExpression: { defaultKey: { $type: "string" } } },
);
export type StockCategory = InferSchemaType<typeof stockCategorySchema>;
export const StockCategoryModel: Model<StockCategory> =
(models.StockCategory as Model<StockCategory> | undefined) ??
model<StockCategory>("StockCategory", stockCategorySchema);