feat: add student wallet utility billing
This commit is contained in:
52
apps/server/src/wallets/wallets.service.spec.ts
Normal file
52
apps/server/src/wallets/wallets.service.spec.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { BadRequestException } from '@nestjs/common';
|
||||
import { WalletsService } from './wallets.service';
|
||||
import { Bill } from '../entities/bill.entity';
|
||||
|
||||
const manager = (walletBalance: number) => {
|
||||
const wallet = { id: 1, studentId: 10, balance: walletBalance };
|
||||
const saved: any[] = [];
|
||||
return {
|
||||
wallet,
|
||||
saved,
|
||||
value: {
|
||||
findOne: jest.fn(async () => wallet),
|
||||
findOneByOrFail: jest.fn(async () => wallet),
|
||||
save: jest.fn(async (value: any) => { saved.push(value); return value; }),
|
||||
create: jest.fn((_entity: unknown, value: unknown) => value),
|
||||
createQueryBuilder: jest.fn(),
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
describe('WalletsService payment rules', () => {
|
||||
const service = new WalletsService({} as any, {} as any, {} as any, {} as any);
|
||||
|
||||
it('partially pays a bill when balance is insufficient', async () => {
|
||||
const ctx = manager(40);
|
||||
const bill = { id: 9, studentId: 10, totalAmount: 100, paidAmount: 0, outstandingAmount: 100, status: 'unpaid' } as Bill;
|
||||
const result = await service.debitBill(ctx.value as any, bill, 1);
|
||||
expect(result.status).toBe('partially_paid');
|
||||
expect(Number(result.paidAmount)).toBe(40);
|
||||
expect(Number(result.outstandingAmount)).toBe(60);
|
||||
expect(Number(ctx.wallet.balance)).toBe(0);
|
||||
expect(ctx.saved.some((row) => row.type === 'bill_payment' && Number(row.amount) === -40)).toBe(true);
|
||||
});
|
||||
|
||||
it('marks a bill paid when balance covers it', async () => {
|
||||
const ctx = manager(120);
|
||||
const bill = { id: 9, studentId: 10, totalAmount: 100, paidAmount: 0, outstandingAmount: 100, status: 'unpaid' } as Bill;
|
||||
const result = await service.debitBill(ctx.value as any, bill);
|
||||
expect(result.status).toBe('paid');
|
||||
expect(Number(result.outstandingAmount)).toBe(0);
|
||||
expect(Number(ctx.wallet.balance)).toBe(20);
|
||||
});
|
||||
|
||||
it('refunds paid amount and cancels the bill', async () => {
|
||||
const ctx = manager(10);
|
||||
const bill = { id: 9, studentId: 10, totalAmount: 100, paidAmount: 40, outstandingAmount: 60, status: 'partially_paid' } as Bill;
|
||||
const result = await service.refundBill(ctx.value as any, bill, '录入错误', 1);
|
||||
expect(result.status).toBe('cancelled');
|
||||
expect(Number(ctx.wallet.balance)).toBe(50);
|
||||
expect(ctx.saved.some((row) => row.type === 'bill_refund' && Number(row.amount) === 40)).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user