Show cached rial and weight balances on balance-claims activity cards and details.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -5,6 +5,7 @@ import {
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import type {
|
||||
ActivityAccountBalanceDto,
|
||||
ActivityDto,
|
||||
ActivityFieldValues,
|
||||
ActivityFilter,
|
||||
@@ -15,6 +16,12 @@ import type {
|
||||
PaginatedResponse,
|
||||
UpdateActivityRequest,
|
||||
} from '@mento/shared';
|
||||
import {
|
||||
ACCOUNT_BALANCE_CLAIMS_TAG,
|
||||
ACCOUNT_BALANCE_WEIGHT_KEYS,
|
||||
ACCOUNT_BALANCE_WEIGHT_LABELS,
|
||||
parseAccountBalanceWeightFields,
|
||||
} from '@mento/shared';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import * as path from 'node:path';
|
||||
@@ -38,6 +45,7 @@ import { tehranDayKey } from '../routines/routine-dates';
|
||||
import { buildSearchWhere, ci } from '../common/search';
|
||||
import { activityPriorWhere } from '../common/prior-archive';
|
||||
import { leadsDeepWhere } from '../common/data-access';
|
||||
import { extractMandehRaw } from '../integrations/tahesab/tahesab-methods';
|
||||
import { ActivityStagesService, type BoardStageContext } from './activity-stages.service';
|
||||
import {
|
||||
ActivityTypesService,
|
||||
@@ -363,10 +371,24 @@ export class ActivitiesService {
|
||||
]);
|
||||
|
||||
const ctx = await this.ctx(organizationId);
|
||||
const [todayDto, overdueDto, upcomingDto] = await Promise.all([
|
||||
this.withAccountBalances(
|
||||
organizationId,
|
||||
today.map((row) => this.toDto(row, ctx)),
|
||||
),
|
||||
this.withAccountBalances(
|
||||
organizationId,
|
||||
overdue.map((row) => this.toDto(row, ctx)),
|
||||
),
|
||||
this.withAccountBalances(
|
||||
organizationId,
|
||||
upcoming.map((row) => this.toDto(row, ctx)),
|
||||
),
|
||||
]);
|
||||
return {
|
||||
today: today.map((row) => this.toDto(row, ctx)),
|
||||
overdue: overdue.map((row) => this.toDto(row, ctx)),
|
||||
upcoming: upcoming.map((row) => this.toDto(row, ctx)),
|
||||
today: todayDto,
|
||||
overdue: overdueDto,
|
||||
upcoming: upcomingDto,
|
||||
todayCount,
|
||||
overdueCount,
|
||||
};
|
||||
@@ -418,7 +440,10 @@ export class ActivitiesService {
|
||||
]);
|
||||
|
||||
return {
|
||||
items: rows.map((row) => this.toDto(row, ctx)),
|
||||
items: await this.withAccountBalances(
|
||||
organizationId,
|
||||
rows.map((row) => this.toDto(row, ctx)),
|
||||
),
|
||||
total,
|
||||
page,
|
||||
limit: take,
|
||||
@@ -689,7 +714,8 @@ export class ActivitiesService {
|
||||
orderBy: { fullName: 'asc' },
|
||||
});
|
||||
}
|
||||
return dto;
|
||||
const [enriched] = await this.withAccountBalances(organizationId, [dto]);
|
||||
return enriched;
|
||||
}
|
||||
|
||||
async create(
|
||||
@@ -1635,6 +1661,105 @@ export class ActivitiesService {
|
||||
}
|
||||
}
|
||||
|
||||
/** فقط فعالیتهای تگ مطالبات؛ مانده از کش تهحساب بدون فراخوانی زنده */
|
||||
private async withAccountBalances(
|
||||
organizationId: string,
|
||||
items: ActivityDto[],
|
||||
): Promise<ActivityDto[]> {
|
||||
if (!items.length) return items;
|
||||
const claimItems = items.filter((item) =>
|
||||
(item.tags ?? []).includes(ACCOUNT_BALANCE_CLAIMS_TAG),
|
||||
);
|
||||
if (!claimItems.length) return items;
|
||||
|
||||
const personIds = [
|
||||
...new Set(
|
||||
claimItems
|
||||
.map((item) => item.person?.id)
|
||||
.filter((id): id is string => Boolean(id)),
|
||||
),
|
||||
];
|
||||
const balanceByPerson = new Map<string, ActivityAccountBalanceDto | null>();
|
||||
if (personIds.length) {
|
||||
const links = await this.prisma.personAccountingLink.findMany({
|
||||
where: {
|
||||
personId: { in: personIds },
|
||||
person: { organizationId },
|
||||
},
|
||||
include: {
|
||||
balanceCache: true,
|
||||
connection: { select: { isDefault: true } },
|
||||
},
|
||||
orderBy: [{ linkedAt: 'desc' }],
|
||||
});
|
||||
const bestByPerson = new Map<string, (typeof links)[number]>();
|
||||
for (const link of links) {
|
||||
const prev = bestByPerson.get(link.personId);
|
||||
if (!prev) {
|
||||
bestByPerson.set(link.personId, link);
|
||||
continue;
|
||||
}
|
||||
const prevScore =
|
||||
(prev.balanceCache ? 2 : 0) + (prev.connection.isDefault ? 1 : 0);
|
||||
const nextScore =
|
||||
(link.balanceCache ? 2 : 0) + (link.connection.isDefault ? 1 : 0);
|
||||
if (nextScore > prevScore) bestByPerson.set(link.personId, link);
|
||||
}
|
||||
for (const personId of personIds) {
|
||||
const link = bestByPerson.get(personId);
|
||||
balanceByPerson.set(
|
||||
personId,
|
||||
link?.balanceCache
|
||||
? this.cacheToAccountBalance(link.balanceCache)
|
||||
: null,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return items.map((item) => {
|
||||
if (!(item.tags ?? []).includes(ACCOUNT_BALANCE_CLAIMS_TAG)) return item;
|
||||
const personId = item.person?.id;
|
||||
return {
|
||||
...item,
|
||||
accountBalance: personId
|
||||
? (balanceByPerson.get(personId) ?? null)
|
||||
: null,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
private cacheToAccountBalance(cache: {
|
||||
rialBalance: number | null;
|
||||
goldBalance: number | null;
|
||||
rawResponse: unknown;
|
||||
fetchedAt: Date;
|
||||
}): ActivityAccountBalanceDto {
|
||||
const fields = parseAccountBalanceWeightFields(
|
||||
extractMandehRaw(cache.rawResponse).fields,
|
||||
);
|
||||
const weights = ACCOUNT_BALANCE_WEIGHT_KEYS.map((key) => {
|
||||
const value =
|
||||
fields[key] ??
|
||||
(key === 'MandeyeVazni' ? cache.goldBalance : null) ??
|
||||
null;
|
||||
if (value == null || !Number.isFinite(value)) return null;
|
||||
return {
|
||||
key,
|
||||
label: ACCOUNT_BALANCE_WEIGHT_LABELS[key] ?? key,
|
||||
value,
|
||||
};
|
||||
}).filter((row): row is NonNullable<typeof row> => row != null);
|
||||
|
||||
return {
|
||||
rialBalance:
|
||||
cache.rialBalance != null && Number.isFinite(cache.rialBalance)
|
||||
? cache.rialBalance
|
||||
: null,
|
||||
weights,
|
||||
fetchedAt: cache.fetchedAt.toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
private toDto(row: ActivityRow, ctx?: DtoContext): ActivityDto {
|
||||
const fields =
|
||||
row.fields && typeof row.fields === 'object' && !Array.isArray(row.fields)
|
||||
|
||||
Reference in New Issue
Block a user