Files
dukang/server/dukang-api/src/common/event/event.helpers.ts
T
2026-08-04 21:38:49 +08:00

118 lines
2.9 KiB
TypeScript

import type { Prisma } from '@prisma/client';
type BenefitLedgerType = 'GRANT' | 'REDEEM' | 'REFUND_VOID' | 'ADJUST';
export function buildBenefitLedgerEvent(data: {
userId: bigint;
couponId: bigint;
type: BenefitLedgerType;
amount: number;
balanceAfter: number;
refType: string;
refId?: bigint;
remark?: string;
}): Prisma.CommonEventCreateInput {
return {
eventType: 'BENEFIT_LEDGER',
refType: data.refType,
refId: data.refId ?? data.couponId,
actorType: 'USER',
actorId: data.userId,
param1: data.type,
param1Desc: 'ledger_type',
param2: data.couponId.toString(),
param2Desc: 'coupon_id',
amount1: data.amount,
amount2: data.balanceAfter,
remark: data.remark,
};
}
export function buildOrderStatusEvent(data: {
orderId: bigint;
fromStatus: string;
toStatus: string;
operator: string;
remark?: string;
}): Prisma.CommonEventCreateInput {
return {
eventType: 'ORDER_STATUS',
refType: 'ORDER',
refId: data.orderId,
actorType: 'SYSTEM',
param1: data.fromStatus,
param1Desc: 'from_status',
param2: data.toStatus,
param2Desc: 'to_status',
param3: data.operator,
param3Desc: 'operator',
remark: data.remark,
};
}
export function benefitLedgerWhere(userId?: bigint, couponId?: bigint): Prisma.CommonEventWhereInput {
return {
eventType: 'BENEFIT_LEDGER',
...(userId ? { actorType: 'USER', actorId: userId } : {}),
...(couponId ? { param2: couponId.toString() } : {}),
};
}
export function orderStatusLogWhere(orderId: bigint): Prisma.CommonEventWhereInput {
return {
eventType: 'ORDER_STATUS',
refType: 'ORDER',
refId: orderId,
};
}
export function buildHqOperationEvent(data: {
hqAccountId: bigint;
action: string;
refType: string;
refId: bigint;
status?: string;
detail?: Record<string, unknown>;
remark?: string;
}): Prisma.CommonEventCreateInput {
return {
eventType: 'HQ_OPERATION',
refType: data.refType,
refId: data.refId,
actorType: 'HQ',
actorId: data.hqAccountId,
status: data.status,
param1: data.action,
param1Desc: 'action',
param2: data.refType,
param2Desc: 'target_type',
param3: data.refId.toString(),
param3Desc: 'target_id',
remark: data.remark,
extraJson: data.detail as Prisma.InputJsonValue,
};
}
export function hqOperationLogWhere(filters?: {
hqAccountId?: bigint;
action?: string;
refType?: string;
from?: Date;
to?: Date;
}): Prisma.CommonEventWhereInput {
return {
eventType: 'HQ_OPERATION',
...(filters?.hqAccountId ? { actorType: 'HQ' as const, actorId: filters.hqAccountId } : {}),
...(filters?.action ? { param1: filters.action } : {}),
...(filters?.refType ? { param2: filters.refType } : {}),
...(filters?.from || filters?.to
? {
createdAt: {
...(filters.from ? { gte: filters.from } : {}),
...(filters.to ? { lte: filters.to } : {}),
},
}
: {}),
};
}