d9de29c18d
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
38 lines
1.7 KiB
TypeScript
38 lines
1.7 KiB
TypeScript
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);
|