feat(assets): varlık lotları, ortalama maliyet ve kâr/zarar servisi

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-05 23:17:44 +03:00
parent baec70d324
commit 890ba9f521
14 changed files with 1415 additions and 3 deletions
+45
View File
@@ -0,0 +1,45 @@
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);
+34
View File
@@ -0,0 +1,34 @@
import { model, models, Schema, type InferSchemaType, type Model } from "mongoose";
import { ASSET_KEYS } from "@/shared/assets";
/**
* Varlık (Bölüm 6.6): bir varlık türünde tutulan birikim, ör. "Gram altın birikimi" veya
* "Düğün ziynetleri". Miktar ve maliyet `assetMovements`ten hesaplanır (ağırlıklı ortalama).
* "Yalnızca ben" işaretli varlık ve hareketleri yalnızca sahibine görünür.
*/
const assetSchema = new Schema(
{
householdId: { type: Schema.Types.ObjectId, required: true },
assetKey: { type: String, enum: ASSET_KEYS, required: true },
name: { type: String, required: true, trim: true, maxlength: 80 },
note: { type: String, default: "", maxlength: 500 },
isPrivate: { type: Boolean, default: false },
ownerUserId: { type: Schema.Types.ObjectId, default: null },
deletedAt: { type: Date, default: null },
seedRef: { type: String, default: null },
createdBy: { type: Schema.Types.ObjectId, required: true },
updatedBy: { type: Schema.Types.ObjectId, required: true },
},
{ timestamps: true, collection: "assets" },
);
assetSchema.index({ householdId: 1, deletedAt: 1, name: 1 });
assetSchema.index(
{ householdId: 1, seedRef: 1 },
{ unique: true, partialFilterExpression: { seedRef: { $type: "string" } } },
);
export type Asset = InferSchemaType<typeof assetSchema>;
export const AssetModel: Model<Asset> =
(models.Asset as Model<Asset> | undefined) ?? model<Asset>("Asset", assetSchema);
+2
View File
@@ -33,6 +33,8 @@ export const AUDIT_ACTIONS = [
"inkind.payment",
"inkind.close",
"inkind.delete",
"asset.delete",
"asset.movement.delete",
] as const;
export type AuditAction = (typeof AUDIT_ACTIONS)[number];
+6
View File
@@ -64,6 +64,11 @@ const transactionSchema = new Schema(
*/
inKindDebtId: { type: Schema.Types.ObjectId, default: null },
inKindRole: { type: String, enum: ["contribution", "payment", null], default: null },
/**
* Varlık alımı (hesap → varlık) veya satışı (varlık → hesap): karşı hesap boş transfer.
* Hareketi varlık sayfasından yönetilir; görünürlük varlığa bağlıdır.
*/
assetId: { type: Schema.Types.ObjectId, default: null },
/**
* Mutabakatı yapılmış hesaplar (Bölüm 6.1). Transfer iki hesaba dokunduğu için hesap bazında
* tutulur. Tutar, tarih, tür veya hesap değişirse işaret kalkar.
@@ -88,6 +93,7 @@ transactionSchema.index({ householdId: 1, effectiveCategoryIds: 1, date: -1 });
transactionSchema.index({ householdId: 1, tags: 1 });
transactionSchema.index({ householdId: 1, contactId: 1, date: 1 }, { sparse: true });
transactionSchema.index({ "installment.planId": 1 }, { sparse: true });
transactionSchema.index({ householdId: 1, assetId: 1 }, { sparse: true });
transactionSchema.index(
{ recurringRuleId: 1, occurrenceIndex: 1 },
{ unique: true, partialFilterExpression: { recurringRuleId: { $type: "objectId" } } },