feat: complete reporting and filing workflows
This commit is contained in:
@@ -10,7 +10,8 @@ function createPrismaMock() {
|
||||
createdAt: new Date('2026-07-03T00:00:00.000Z'),
|
||||
updatedAt: new Date('2026-07-03T00:00:00.000Z'),
|
||||
};
|
||||
return {
|
||||
const prisma = {
|
||||
$executeRaw: jest.fn().mockResolvedValue(1),
|
||||
tenant: {
|
||||
findMany: jest.fn().mockResolvedValue([{ ...tenant, enterpriseCertifications: [] }]),
|
||||
findUnique: jest.fn().mockResolvedValue({ ...tenant, enterpriseCertifications: [] }),
|
||||
@@ -27,6 +28,7 @@ function createPrismaMock() {
|
||||
},
|
||||
tenantAccount: {
|
||||
findMany: jest.fn().mockResolvedValue([{ tenantId: 'tenant-1', balanceCents: 12000, status: 'active' }]),
|
||||
findUnique: jest.fn().mockResolvedValue({ balanceCents: 0 }),
|
||||
},
|
||||
smsMessageRecord: {
|
||||
groupBy: jest.fn().mockResolvedValue([{ tenantId: 'tenant-1', _sum: { amountCents: 350 } }]),
|
||||
@@ -35,6 +37,10 @@ function createPrismaMock() {
|
||||
groupBy: jest.fn().mockResolvedValue([{ tenantId: 'tenant-1', _sum: { amountCents: 125 } }]),
|
||||
},
|
||||
};
|
||||
return {
|
||||
...prisma,
|
||||
$transaction: jest.fn((callback: (tx: typeof prisma) => unknown) => callback(prisma)),
|
||||
};
|
||||
}
|
||||
|
||||
describe('TenantsService', () => {
|
||||
@@ -96,6 +102,29 @@ describe('TenantsService', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('blocks enterprise deletion until a non-zero account balance is cleared', async () => {
|
||||
const prisma = createPrismaMock();
|
||||
prisma.tenantAccount.findUnique.mockResolvedValue({ balanceCents: -1250 });
|
||||
const service = new TenantsService(prisma as never);
|
||||
|
||||
await expect(service.delete('tenant-1')).rejects.toThrow('完成余额清算后方可删除,请给企业充值到金额为0');
|
||||
expect(prisma.tenant.update).not.toHaveBeenCalled();
|
||||
expect(prisma.smsApplication.count).not.toHaveBeenCalled();
|
||||
expect(prisma.$executeRaw).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('allows enterprise deletion only when the account balance is zero and no applications are active', async () => {
|
||||
const prisma = createPrismaMock();
|
||||
prisma.tenantAccount.findUnique.mockResolvedValue({ balanceCents: 0 });
|
||||
const service = new TenantsService(prisma as never);
|
||||
|
||||
await expect(service.delete('tenant-1')).resolves.toMatchObject({ id: 'tenant-1' });
|
||||
expect(prisma.tenant.update).toHaveBeenCalledWith({
|
||||
where: { id: 'tenant-1' },
|
||||
data: { status: 'deleted' },
|
||||
});
|
||||
});
|
||||
|
||||
it('rejects enterprise credit codes containing non-alphanumeric characters', async () => {
|
||||
const prisma = createPrismaMock();
|
||||
const service = new TenantsService(prisma as never);
|
||||
|
||||
@@ -127,16 +127,29 @@ export class TenantsService {
|
||||
}
|
||||
|
||||
private async deleteAfterApplicationCheck(id: string) {
|
||||
await this.ensureTenant(id);
|
||||
const blockingApplications = await this.prisma.smsApplication.count({
|
||||
where: { tenantId: id, status: { in: ['active', 'disabling'] } },
|
||||
});
|
||||
if (blockingApplications > 0) {
|
||||
throw new BadRequestException(`该企业还有 ${blockingApplications} 个启用或停用中的企业应用,请先完成应用停用`);
|
||||
}
|
||||
return this.prisma.tenant.update({
|
||||
where: { id },
|
||||
data: { status: 'deleted' },
|
||||
return this.prisma.$transaction(async (tx) => {
|
||||
await tx.$executeRaw`SELECT pg_advisory_xact_lock(hashtextextended(${'tenant-account:' + id}, 0))`;
|
||||
const tenant = await tx.tenant.findUnique({ where: { id } });
|
||||
if (!tenant) {
|
||||
throw new NotFoundException('Tenant not found');
|
||||
}
|
||||
const account = await tx.tenantAccount.findUnique({
|
||||
where: { tenantId: id },
|
||||
select: { balanceCents: true },
|
||||
});
|
||||
if (moneyToNumber(account?.balanceCents) !== 0) {
|
||||
throw new BadRequestException('完成余额清算后方可删除,请给企业充值到金额为0');
|
||||
}
|
||||
const blockingApplications = await tx.smsApplication.count({
|
||||
where: { tenantId: id, status: { in: ['active', 'disabling'] } },
|
||||
});
|
||||
if (blockingApplications > 0) {
|
||||
throw new BadRequestException(`该企业还有 ${blockingApplications} 个启用或停用中的企业应用,请先完成应用停用`);
|
||||
}
|
||||
return tx.tenant.update({
|
||||
where: { id },
|
||||
data: { status: 'deleted' },
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user