feat(M08-A): 接入权益和余额下单抵扣
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import type { PoolConnection, ResultSetHeader, RowDataPacket } from 'mysql2/promise';
|
||||
import type { AccessProfile } from '../auth/rbac-repository.js';
|
||||
import type { MySqlPool } from '../db/mysql.js';
|
||||
import type { MarketingBenefitService } from '../wallets/marketing-benefit-service.js';
|
||||
|
||||
export const orderActions = [
|
||||
'SUBMIT', 'CONFIRM_PAYMENT', 'RESERVE', 'START', 'FINISH', 'CANCEL',
|
||||
@@ -71,7 +72,10 @@ export class OrderStateError extends Error {
|
||||
}
|
||||
|
||||
export class OrderStateRepository {
|
||||
constructor(private readonly pool: MySqlPool) {}
|
||||
constructor(
|
||||
private readonly pool: MySqlPool,
|
||||
private readonly benefits?: Pick<MarketingBenefitService, 'releaseReservedInTransaction'>
|
||||
) {}
|
||||
|
||||
async transition(actor: OrderActor, orderId: string, action: OrderAction, reason = '') {
|
||||
return this.transaction(async (connection) => {
|
||||
@@ -223,6 +227,21 @@ export class OrderStateRepository {
|
||||
WHERE tenant_id = ? AND order_id = ?`,
|
||||
[tenantId, orderId]
|
||||
);
|
||||
if (this.benefits) {
|
||||
const [users] = await connection.execute<RowDataPacket[]>(
|
||||
`SELECT user_id AS userId FROM qipai_order_user_access
|
||||
WHERE tenant_id = ? AND order_id = ? ORDER BY id LIMIT 1`,
|
||||
[tenantId, orderId]
|
||||
);
|
||||
if (users[0]?.userId) {
|
||||
await this.benefits.releaseReservedInTransaction(connection, {
|
||||
tenantId,
|
||||
userId: String(users[0].userId),
|
||||
orderId,
|
||||
traceId: `order-benefit-release-${orderId}`
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user