核销功能
This commit is contained in:
@@ -13,17 +13,46 @@ export class AdminRedeemDebugService {
|
||||
private readonly redeemService: RedeemService,
|
||||
) {}
|
||||
|
||||
private parseId(value: string, label: string): bigint {
|
||||
const normalized = value?.trim();
|
||||
private parseStoreId(value: string): bigint {
|
||||
const normalized = String(value ?? '').trim();
|
||||
if (!normalized || !/^\d+$/.test(normalized)) {
|
||||
throw new BadRequestException(`${label}格式无效`);
|
||||
throw new BadRequestException('门店 ID 格式无效');
|
||||
}
|
||||
return BigInt(normalized);
|
||||
}
|
||||
|
||||
private async resolveUserId(identifier: string): Promise<bigint> {
|
||||
const normalized = String(identifier ?? '').trim();
|
||||
if (!normalized) {
|
||||
throw new BadRequestException('请填写用户 ID、用户编号或手机号');
|
||||
}
|
||||
|
||||
if (/^\d+$/.test(normalized)) {
|
||||
const byId = await this.prisma.user.findUnique({
|
||||
where: { id: BigInt(normalized) },
|
||||
select: { id: true },
|
||||
});
|
||||
if (byId) return byId.id;
|
||||
|
||||
const byPhone = await this.prisma.user.findFirst({
|
||||
where: { phone: normalized },
|
||||
select: { id: true },
|
||||
});
|
||||
if (byPhone) return byPhone.id;
|
||||
} else {
|
||||
const byNo = await this.prisma.user.findFirst({
|
||||
where: { userNo: normalized },
|
||||
select: { id: true },
|
||||
});
|
||||
if (byNo) return byNo.id;
|
||||
}
|
||||
|
||||
throw new NotFoundException('用户不存在,请检查 ID、用户编号或手机号');
|
||||
}
|
||||
|
||||
private async resolveStoreAccountId(storeId: string): Promise<bigint> {
|
||||
const account = await this.prisma.storeAccount.findFirst({
|
||||
where: { storeId: this.parseId(storeId, '门店 ID'), status: 'ACTIVE' },
|
||||
where: { storeId: this.parseStoreId(storeId), status: 'ACTIVE' },
|
||||
orderBy: { id: 'asc' },
|
||||
select: { id: true, store: { select: { name: true } } },
|
||||
});
|
||||
@@ -34,10 +63,11 @@ export class AdminRedeemDebugService {
|
||||
}
|
||||
|
||||
async createToken(dto: AdminRedeemDebugCreateTokenDto) {
|
||||
return this.redeemService.createToken(this.parseId(dto.userId, '用户 ID'), {
|
||||
const userId = await this.resolveUserId(dto.userId);
|
||||
return this.redeemService.createToken(userId, {
|
||||
amount: dto.amount,
|
||||
couponId: dto.couponId,
|
||||
storeId: dto.storeId,
|
||||
couponId: dto.couponId?.trim() || undefined,
|
||||
storeId: dto.storeId?.trim() || undefined,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user