Files
tehran_taj/apps/api/src/modules/accounting-sync/accounting-hooks.service.ts
T
mahmoodandCursor 7dd74ff4f7 feat: product design surveys, cart expiry/buy-later, and catalog polish
Add like/dislike surveys with majority alerts for manufacturing decisions, clearer accounting mapping copy, and related cart/catalog admin-app improvements.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-12 17:55:52 +03:30

231 lines
10 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { AccountingEntityType, AccountingOutboxKind, AccountingOutboxStatus, Prisma } from '@prisma/client';
import { PrismaService } from '../../prisma/prisma.service';
import { AccountingConfigService } from './accounting-config.service';
import { AccountingFeatures, documentStatusRank } from './accounting-config';
import { AccountingSyncService } from './accounting-sync.service';
import { AccountingWorkerService } from './accounting-worker.service';
import { ACCOUNTING_PROVIDERS } from './providers';
import { accountingDebug } from './accounting-debug-log';
type Tx = Prisma.TransactionClient | PrismaService;
type StockFields = {
accountingStockAt: Date | null;
accountingStockWeight: Prisma.Decimal | null;
accountingStockCount: Prisma.Decimal | null;
};
const OPEN: AccountingOutboxStatus[] = ['PENDING', 'SENDING'];
/**
* The narrow surface the rest of the app calls into (D27): orders, customer
* admin, product admin, catalog. Every write goes through the caller's own
* transaction client, so the outbox row commits or rolls back with the change
* that caused it. Nothing here talks to the accounting software.
*/
@Injectable()
export class AccountingHooksService {
constructor(
private readonly prisma: PrismaService,
private readonly config: AccountingConfigService,
private readonly sync: AccountingSyncService,
private readonly worker: AccountingWorkerService,
) {}
private async enqueue(tx: Tx, feature: keyof AccountingFeatures, kind: AccountingOutboxKind, entityId: string) {
const c = await this.config.get();
if (c.provider === 'none' || !c.features[feature]) return false;
const open = await tx.accountingOutbox.findFirst({
where: { provider: c.provider, kind, entityId, status: { in: OPEN } },
select: { id: true },
});
if (open) return false;
await tx.accountingOutbox.create({ data: { provider: c.provider, kind, entityId } });
this.worker.kickSoon();
return true;
}
/**
* F3 — the order is now in `status` (created, advanced, or rolled back).
* The owner's rule (`documents.triggerStatus`, «اتصال حسابداری») says from
* which status on the temporary document (پیش‌فاکتور) belongs in the books:
* at or past it the document is queued once; back below it, a document
* already sent is withdrawn like a cancellation.
*/
async orderStatusReached(tx: Tx, orderId: string, status: string) {
const c = await this.config.get();
const trace = (decision: string) =>
accountingDebug('order.status', { orderId, status, provider: c.provider, triggerStatus: c.documents.triggerStatus, decision });
if (c.provider === 'none' || !c.features.documents) return trace('ignored: provider none or documents switched off');
const rank = documentStatusRank(status);
if (!rank) return trace('ignored: status never triggers a document');
if (rank >= documentStatusRank(c.documents.triggerStatus)) {
await this.queueOrderDocument(tx, c.provider, orderId);
trace('document queued (unless one was already queued)');
} else {
await this.orderCancelled(tx, orderId);
trace('below trigger status: any sent document withdrawn');
}
}
private async queueOrderDocument(tx: Tx, provider: string, orderId: string) {
const existing = await tx.accountingOutbox.findFirst({
where: { provider, kind: 'ORDER_DOCUMENT', entityId: orderId, status: { notIn: ['RESOLVED'] } },
select: { id: true },
});
if (existing) {
// Withdrawn and wanted again: Tahesab deletes first (older job), then
// the new one posts — and a duplicate is refused by its Factor_Code key.
// Zarrin has no such key, so its document is never queued twice.
const deleting = await tx.accountingOutbox.findFirst({
where: { provider, kind: 'ORDER_DOCUMENT_DELETE', entityId: orderId, status: { in: ['PENDING', 'SENDING'] } },
select: { id: true },
});
if (!(provider === 'tahesab' && deleting)) return;
}
await this.enqueue(tx, 'documents', 'ORDER_DOCUMENT', orderId);
}
/**
* F3 — the order was cancelled. A document still waiting is simply dropped;
* one that may already be in the books gets a delete job, which itself only
* deletes a document that is still temporary (worker).
*/
async orderCancelled(tx: Tx, orderId: string) {
const docs = await tx.accountingOutbox.findMany({
where: { kind: 'ORDER_DOCUMENT', entityId: orderId, status: { not: 'RESOLVED' } },
});
for (const doc of docs) {
if (doc.status === 'PENDING') {
await tx.accountingOutbox.update({
where: { id: doc.id },
data: { status: 'RESOLVED', lastError: 'سفارش پیش از ارسال سند لغو شد' },
});
continue;
}
const open = await tx.accountingOutbox.findFirst({
where: { provider: doc.provider, kind: 'ORDER_DOCUMENT_DELETE', entityId: orderId, status: { not: 'RESOLVED' } },
select: { id: true },
});
if (!open) {
await tx.accountingOutbox.create({
data: { provider: doc.provider, kind: 'ORDER_DOCUMENT_DELETE', entityId: orderId },
});
}
}
}
/**
* F3 — may this order's lines or weights be corrected now? A document that is
* being sent, or is waiting for a person to look at it, cannot be reconciled
* with a change automatically, so the correction is refused until it is
* settled. Returns the reason, or null when the correction may go ahead.
*/
async orderDocumentBlocksEdit(tx: Tx, orderId: string): Promise<string | null> {
const c = await this.config.get();
if (c.provider === 'none' || !c.features.documents) return null;
const stuck = await tx.accountingOutbox.findFirst({
where: { provider: c.provider, kind: 'ORDER_DOCUMENT', entityId: orderId, status: { in: ['SENDING', 'NEEDS_REVIEW'] } },
select: { id: true },
});
return stuck
? 'سند حسابداری این سفارش در حال ارسال است یا منتظر بررسی؛ پس از روشن شدن وضعیتش دوباره تلاش کنید.'
: null;
}
/**
* F3 — the order's lines or weights were corrected after it was placed.
*
* A document still waiting (or failed and due for a retry) is built from the
* order at the moment it is sent, so it picks the correction up by itself.
* One already in the books no longer matches: it is withdrawn and a corrected
* one is queued behind the delete (the worker runs the older job first).
*/
async orderEdited(tx: Tx, orderId: string) {
const c = await this.config.get();
if (c.provider === 'none' || !c.features.documents) return;
const sent = await tx.accountingOutbox.findFirst({
where: { provider: c.provider, kind: 'ORDER_DOCUMENT', entityId: orderId, status: { in: ['SENT'] } },
select: { id: true },
});
if (!sent) return;
// Already withdrawn and re-queued by an earlier correction: that new job
// will read the latest figures too.
const requeued = await tx.accountingOutbox.findFirst({
where: { provider: c.provider, kind: 'ORDER_DOCUMENT', entityId: orderId, status: { in: ['PENDING'] } },
select: { id: true },
});
if (requeued) return;
await this.orderCancelled(tx, orderId);
await tx.accountingOutbox.create({ data: { provider: c.provider, kind: 'ORDER_DOCUMENT', entityId: orderId } });
this.worker.kickSoon();
}
/** F1 — an admin marked the customer VERIFIED. */
async customerVerified(tx: Tx, customerId: string) {
await this.enqueue(tx, 'customers', 'CUSTOMER_PUSH', customerId);
}
/** F1 — a linked customer's contact details changed. */
async customerEdited(tx: Tx, customerId: string) {
const c = await this.config.get();
if (c.provider === 'none' || !c.features.customers) return;
const link = await tx.accountingLink.findUnique({
where: { provider_entityType_entityId: { provider: c.provider, entityType: 'CUSTOMER', entityId: customerId } },
select: { id: true },
});
if (link) await this.enqueue(tx, 'customers', 'CUSTOMER_PUSH', customerId);
}
/** F4 — a product was created in the admin panel. */
async productCreated(tx: Tx, productId: string) {
const c = await this.config.get();
if (c.provider === 'none' || !ACCOUNTING_PROVIDERS[c.provider].capabilities.createKarSakhte) return;
await this.enqueue(tx, 'products', 'PRODUCT_PUSH', productId);
}
/**
* F4 — title and code of a linked product belong to the accounting software
* (D27); the admin form locks them. Null when the product is not linked.
*/
async productLink(productId: string) {
const c = await this.config.get();
if (c.provider === 'none' || !c.features.products) return null;
return this.prisma.accountingLink.findUnique({
where: { provider_entityType_entityId: { provider: c.provider, entityType: AccountingEntityType.PRODUCT, entityId: productId } },
});
}
/**
* Order time: the product with its stock read live from the accounting
* software, or as last stored when that cannot answer in a moment.
*/
liveStock<T extends { id: string; accountingStockAt: Date | null; accountingStockWeight: Prisma.Decimal | null; accountingStockCount: Prisma.Decimal | null }>(product: T) {
return this.sync.liveStock(product);
}
/**
* F4 — is this product out of stock in the accounting software? Only
* meaningful while the product sync is on; stock fields of products that are
* not linked under the active provider are cleared by every product pull.
*/
async stockBlocked(product: StockFields) {
return (await this.stockBlocker())(product);
}
/**
* The same rule as stockBlocked, with the configuration read once — for
* checking a whole list without an await per product.
*/
async stockBlocker(): Promise<(product: StockFields) => boolean> {
const c = await this.config.get();
const active = c.provider !== 'none' && c.features.products;
return (product) => {
if (!active || !product.accountingStockAt) return false;
const stock = product.accountingStockWeight ?? product.accountingStockCount;
return stock !== null && new Prisma.Decimal(stock).lte(0);
};
}
}