Files
dukang/server/dukang-api/scripts/test-redeem-confirm.ts
T
2026-07-06 22:10:45 +08:00

101 lines
3.1 KiB
TypeScript

import '../src/load-env';
import { NestFactory } from '@nestjs/core';
import { AppModule } from '../src/app.module';
import { PrismaService } from '../src/common/prisma/prisma.module';
import { RedeemService } from '../src/modules/redeem/redeem.service';
async function main() {
const app = await NestFactory.createApplicationContext(AppModule, { logger: ['error', 'warn'] });
const prisma = app.get(PrismaService);
const redeem = app.get(RedeemService);
let coupon = await prisma.benefitCoupon.findFirst({
where: { status: 'ACTIVE', balance: { gt: 10 } },
orderBy: { createdAt: 'desc' },
});
if (!coupon) {
const user = await prisma.user.findFirst({ orderBy: { id: 'asc' } });
const city = await prisma.commonCity.findFirst();
const product = await prisma.commonProductItem.findFirst();
if (!user || !city || !product) throw new Error('seed base data missing');
const order = await prisma.order.create({
data: {
orderNo: `T${Date.now()}`,
userId: user.id,
cityId: city.id,
status: 'COMPLETED',
payStatus: 'PAID',
deliveryType: 'LOCAL',
productId: product.id,
barcode69: product.barcode69,
productName: product.name,
productSpec: product.spec ?? '500ml',
quantity: 2,
listUnitPrice: 100,
listAmount: 200,
productAmount: 200,
payAmount: 200,
benefitAmount: 200,
receiverName: 'test',
receiverPhone: user.phone ?? '13800000001',
receiverProvince: '河南',
receiverCity: '郑州',
receiverDistrict: '金水',
receiverAddress: 'test addr',
},
});
coupon = await prisma.benefitCoupon.create({
data: {
couponNo: `TEST${Date.now()}`,
userId: user.id,
orderId: order.id,
totalAmount: 200,
usedAmount: 0,
balance: 200,
status: 'ACTIVE',
sourceProduct: product.name,
},
});
console.log('created test coupon', coupon.id.toString());
}
const storeAccount = await prisma.storeAccount.findFirst({
where: { status: 'ACTIVE', store: { status: 'OPEN' } },
include: { store: true },
});
if (!storeAccount) throw new Error('no active store account on OPEN store');
console.log('userId', coupon.userId.toString());
console.log('storeId', storeAccount.storeId.toString());
console.log('coupon balance', coupon.balance.toString());
const tokenRes = await redeem.createToken(coupon.userId, {
amount: 10,
storeId: storeAccount.storeId.toString(),
});
console.log('token created', tokenRes.token);
try {
const preview = await redeem.previewRedeem(storeAccount.id, tokenRes.token);
console.log('preview ok', JSON.stringify(preview));
} catch (e) {
console.error('preview failed:', e);
throw e;
}
try {
const record = await redeem.confirmRedeem(storeAccount.id, { token: tokenRes.token });
console.log('confirm ok', JSON.stringify(record));
} catch (e) {
console.error('confirm failed:', e);
throw e;
}
await app.close();
}
main().catch((e) => {
console.error(e);
process.exit(1);
});