后端增加发放好客权益券的接口
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import type { Prisma } from '@prisma/client';
|
||||
import { calcBenefitAmount, calcBenefitSummary, generateCouponNo } from '@dukang/domain';
|
||||
import { PrismaService } from '../../common/prisma/prisma.module';
|
||||
@@ -50,6 +50,51 @@ export class BenefitService {
|
||||
return serializeBigInt(coupon);
|
||||
}
|
||||
|
||||
/** HQ 手动发放权益(无关联订单) */
|
||||
async grantManual(params: {
|
||||
userId: bigint;
|
||||
amount: number;
|
||||
remark?: string;
|
||||
sourceProduct?: string;
|
||||
}) {
|
||||
const amount = Number(params.amount);
|
||||
if (!Number.isFinite(amount) || amount <= 0) {
|
||||
throw new BadRequestException('权益金额须大于 0');
|
||||
}
|
||||
if (amount > 999_999.99) {
|
||||
throw new BadRequestException('权益金额超出上限');
|
||||
}
|
||||
|
||||
const sourceProduct = params.sourceProduct?.trim() || '总部手动发放';
|
||||
const remark = params.remark?.trim() || '总部手动发放';
|
||||
|
||||
const coupon = await this.prisma.$transaction(async (tx) => {
|
||||
const created = await tx.benefitCoupon.create({
|
||||
data: {
|
||||
couponNo: generateCouponNo(),
|
||||
userId: params.userId,
|
||||
totalAmount: amount,
|
||||
balance: amount,
|
||||
sourceProduct,
|
||||
},
|
||||
});
|
||||
await tx.commonEvent.create({
|
||||
data: buildBenefitLedgerEvent({
|
||||
userId: params.userId,
|
||||
couponId: created.id,
|
||||
type: 'GRANT',
|
||||
amount,
|
||||
balanceAfter: amount,
|
||||
refType: 'ADMIN_GRANT',
|
||||
remark,
|
||||
}),
|
||||
});
|
||||
return created;
|
||||
});
|
||||
|
||||
return serializeBigInt(coupon);
|
||||
}
|
||||
|
||||
async listCoupons(userId: bigint) {
|
||||
const list = await this.prisma.benefitCoupon.findMany({
|
||||
where: { userId, status: { in: ['ACTIVE', 'USED_UP'] } },
|
||||
|
||||
Reference in New Issue
Block a user