fix: harden admin and CMPP delivery workflows
This commit is contained in:
@@ -29,6 +29,11 @@ export class DictionariesController {
|
||||
return this.dictionaries.createPhoneSegment(body);
|
||||
}
|
||||
|
||||
@Delete('phone-segments/:id')
|
||||
deletePhoneSegment(@Param('id') id: string) {
|
||||
return this.dictionaries.deletePhoneSegment(id);
|
||||
}
|
||||
|
||||
@Get('phone-carrier-rules')
|
||||
listPhoneCarrierRules(@Query('keyword') keyword?: string, @Query('page') page?: string, @Query('pageSize') pageSize?: string) {
|
||||
return this.dictionaries.listPhoneCarrierRules({ keyword, page: Number(page) || undefined, pageSize: Number(pageSize) || undefined });
|
||||
@@ -108,4 +113,9 @@ export class DictionariesController {
|
||||
createDrainageField(@Body() body: CreateDrainageFieldDto) {
|
||||
return this.dictionaries.createDrainageField(body);
|
||||
}
|
||||
|
||||
@Delete('drainage-fields/:id')
|
||||
deleteDrainageField(@Param('id') id: string) {
|
||||
return this.dictionaries.deleteDrainageField(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ function createPrismaMock() {
|
||||
phoneSegment: {
|
||||
findMany: jest.fn(),
|
||||
count: jest.fn().mockResolvedValue(3),
|
||||
delete: jest.fn().mockResolvedValue({ id: 'segment-1' }),
|
||||
},
|
||||
phoneCarrierRule: {
|
||||
findMany: jest.fn().mockResolvedValue([]),
|
||||
@@ -26,7 +27,12 @@ function createPrismaMock() {
|
||||
update: jest.fn().mockImplementation(({ data }) => Promise.resolve({ id: 'enterprise-1', ...data })),
|
||||
},
|
||||
drainageField: {
|
||||
findMany: jest.fn().mockResolvedValue([]),
|
||||
create: jest.fn().mockImplementation(({ data }) => Promise.resolve({ id: 'field-1', ...data })),
|
||||
delete: jest.fn().mockResolvedValue({ id: 'field-1' }),
|
||||
},
|
||||
channelReportField: {
|
||||
count: jest.fn().mockResolvedValue(0),
|
||||
},
|
||||
smsApplication: {
|
||||
findUnique: jest.fn().mockResolvedValue({ id: 'app-1', tenantId: 'tenant-1' }),
|
||||
@@ -38,6 +44,25 @@ function createPrismaMock() {
|
||||
}
|
||||
|
||||
describe('DictionariesService', () => {
|
||||
it('deletes a phone segment from the real dictionary table', async () => {
|
||||
const prisma = createPrismaMock();
|
||||
const service = new DictionariesService(prisma as never);
|
||||
|
||||
await service.deletePhoneSegment('segment-1');
|
||||
|
||||
expect(prisma.phoneSegment.delete).toHaveBeenCalledWith({ where: { id: 'segment-1' } });
|
||||
});
|
||||
|
||||
it('returns drainage field usage counts and blocks deleting fields used by channels', async () => {
|
||||
const prisma = createPrismaMock();
|
||||
prisma.drainageField.findMany.mockResolvedValue([{ id: 'field-1', code: 'license', _count: { channelReportFields: 2 } }]);
|
||||
prisma.channelReportField.count.mockResolvedValue(2);
|
||||
const service = new DictionariesService(prisma as never);
|
||||
|
||||
await expect(service.listDrainageFields()).resolves.toEqual([{ id: 'field-1', code: 'license', usageCount: 2 }]);
|
||||
await expect(service.deleteDrainageField('field-1')).rejects.toThrow('不能删除');
|
||||
expect(prisma.drainageField.delete).not.toHaveBeenCalled();
|
||||
});
|
||||
it('paginates phone segments with a real database count', async () => {
|
||||
const prisma = createPrismaMock();
|
||||
prisma.phoneSegment.findMany.mockResolvedValue([
|
||||
|
||||
@@ -97,6 +97,10 @@ export class DictionariesService {
|
||||
return this.prisma.phoneSegment.create({ data });
|
||||
}
|
||||
|
||||
deletePhoneSegment(id: string) {
|
||||
return this.prisma.phoneSegment.delete({ where: { id } });
|
||||
}
|
||||
|
||||
async listPhoneCarrierRules(query: PageQuery = {}) {
|
||||
const page = Math.max(1, Number(query.page ?? 1));
|
||||
const pageSize = Math.min(100, Math.max(1, Number(query.pageSize ?? 25)));
|
||||
@@ -258,8 +262,12 @@ export class DictionariesService {
|
||||
return updated;
|
||||
}
|
||||
|
||||
listDrainageFields() {
|
||||
return this.prisma.drainageField.findMany({ orderBy: { createdAt: 'desc' } });
|
||||
async listDrainageFields() {
|
||||
const fields = await this.prisma.drainageField.findMany({
|
||||
include: { _count: { select: { channelReportFields: true } } },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
return fields.map(({ _count, ...field }) => ({ ...field, usageCount: _count.channelReportFields }));
|
||||
}
|
||||
|
||||
createDrainageField(data: CreateDrainageFieldDto) {
|
||||
@@ -282,6 +290,14 @@ export class DictionariesService {
|
||||
});
|
||||
}
|
||||
|
||||
async deleteDrainageField(id: string) {
|
||||
const usageCount = await this.prisma.channelReportField.count({ where: { drainageFieldId: id } });
|
||||
if (usageCount > 0) {
|
||||
throw new BadRequestException(`该字段已被 ${usageCount} 个通道使用,不能删除`);
|
||||
}
|
||||
return this.prisma.drainageField.delete({ where: { id } });
|
||||
}
|
||||
|
||||
private writeOperationLog(userId: string | undefined, action: string, resource: string, resourceId: string, detail: Record<string, unknown>) {
|
||||
return this.prisma.operationLog.create({
|
||||
data: {
|
||||
|
||||
Reference in New Issue
Block a user