54 lines
1.8 KiB
TypeScript
54 lines
1.8 KiB
TypeScript
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
|
import { PrismaService } from '../../common/prisma/prisma.module';
|
|
import { RedeemService } from '../redeem/redeem.service';
|
|
import type {
|
|
AdminRedeemDebugCreateTokenDto,
|
|
AdminRedeemDebugStoreTokenDto,
|
|
} from './dto/admin-mutate.dto';
|
|
|
|
@Injectable()
|
|
export class AdminRedeemDebugService {
|
|
constructor(
|
|
private readonly prisma: PrismaService,
|
|
private readonly redeemService: RedeemService,
|
|
) {}
|
|
|
|
private parseId(value: string, label: string): bigint {
|
|
const normalized = value?.trim();
|
|
if (!normalized || !/^\d+$/.test(normalized)) {
|
|
throw new BadRequestException(`${label}格式无效`);
|
|
}
|
|
return BigInt(normalized);
|
|
}
|
|
|
|
private async resolveStoreAccountId(storeId: string): Promise<bigint> {
|
|
const account = await this.prisma.storeAccount.findFirst({
|
|
where: { storeId: this.parseId(storeId, '门店 ID'), status: 'ACTIVE' },
|
|
orderBy: { id: 'asc' },
|
|
select: { id: true, store: { select: { name: true } } },
|
|
});
|
|
if (!account) {
|
|
throw new NotFoundException('该门店无可用账户,请先创建门店账户');
|
|
}
|
|
return account.id;
|
|
}
|
|
|
|
async createToken(dto: AdminRedeemDebugCreateTokenDto) {
|
|
return this.redeemService.createToken(this.parseId(dto.userId, '用户 ID'), {
|
|
amount: dto.amount,
|
|
couponId: dto.couponId,
|
|
storeId: dto.storeId,
|
|
});
|
|
}
|
|
|
|
async preview(dto: AdminRedeemDebugStoreTokenDto) {
|
|
const storeAccountId = await this.resolveStoreAccountId(dto.storeId);
|
|
return this.redeemService.previewRedeem(storeAccountId, dto.token);
|
|
}
|
|
|
|
async confirm(dto: AdminRedeemDebugStoreTokenDto) {
|
|
const storeAccountId = await this.resolveStoreAccountId(dto.storeId);
|
|
return this.redeemService.confirmRedeem(storeAccountId, { token: dto.token });
|
|
}
|
|
}
|