package.json test:e2e:prepare 现在是 migrate reset --force && prisma generate && seed 为 seed 运行时补充 JS Prisma client 生成器: schema.prisma 修复 seed 在 ESM/CJS 下的 Prisma 导入兼容: seed.mjs 修复 Jest 环境未加载 .env 导致连到 127.0.0.1 的问题: e2e-app.helper.ts 修复夹具依赖“名称”导致被组织测试改名后失效的问题(改为按 seed openId 反查): e2e-fixtures.helper.ts 修复组织测试的状态污染与清理逻辑,并收敛 afterAll 资源释放: organization.e2e-spec.ts e2e-context.helper.ts
196 lines
5.0 KiB
TypeScript
196 lines
5.0 KiB
TypeScript
import { NotFoundException } from '@nestjs/common';
|
|
import { PrismaService } from '../../../src/prisma.service.js';
|
|
|
|
export interface E2ESeedFixtures {
|
|
hospitalAId: number;
|
|
hospitalBId: number;
|
|
departmentA1Id: number;
|
|
departmentA2Id: number;
|
|
departmentB1Id: number;
|
|
groupA1Id: number;
|
|
groupA2Id: number;
|
|
groupB1Id: number;
|
|
users: {
|
|
systemAdminId: number;
|
|
hospitalAdminAId: number;
|
|
directorAId: number;
|
|
leaderAId: number;
|
|
doctorAId: number;
|
|
doctorA2Id: number;
|
|
doctorA3Id: number;
|
|
doctorBId: number;
|
|
engineerAId: number;
|
|
engineerBId: number;
|
|
};
|
|
patients: {
|
|
patientA1Id: number;
|
|
patientA2Id: number;
|
|
patientA3Id: number;
|
|
patientB1Id: number;
|
|
};
|
|
devices: {
|
|
deviceA1Id: number;
|
|
deviceA2Id: number;
|
|
deviceA3Id: number;
|
|
deviceA4InactiveId: number;
|
|
deviceB1Id: number;
|
|
};
|
|
}
|
|
|
|
interface SeedUserScope {
|
|
id: number;
|
|
hospitalId: number | null;
|
|
departmentId: number | null;
|
|
groupId: number | null;
|
|
}
|
|
|
|
async function requireUserScope(
|
|
prisma: PrismaService,
|
|
openId: string,
|
|
): Promise<SeedUserScope> {
|
|
const user = await prisma.user.findUnique({
|
|
where: { openId },
|
|
select: {
|
|
id: true,
|
|
hospitalId: true,
|
|
departmentId: true,
|
|
groupId: true,
|
|
},
|
|
});
|
|
if (!user) {
|
|
throw new NotFoundException(`Seed user not found: ${openId}`);
|
|
}
|
|
return user;
|
|
}
|
|
|
|
async function requireDeviceId(
|
|
prisma: PrismaService,
|
|
snCode: string,
|
|
): Promise<number> {
|
|
const device = await prisma.device.findUnique({
|
|
where: { snCode },
|
|
select: { id: true },
|
|
});
|
|
if (!device) {
|
|
throw new NotFoundException(`Seed device not found: ${snCode}`);
|
|
}
|
|
return device.id;
|
|
}
|
|
|
|
async function requirePatientId(
|
|
prisma: PrismaService,
|
|
hospitalId: number,
|
|
phone: string,
|
|
idCardHash: string,
|
|
): Promise<number> {
|
|
const patient = await prisma.patient.findFirst({
|
|
where: { hospitalId, phone, idCardHash },
|
|
select: { id: true },
|
|
});
|
|
if (!patient) {
|
|
throw new NotFoundException(
|
|
`Seed patient not found: ${phone}/${idCardHash}`,
|
|
);
|
|
}
|
|
return patient.id;
|
|
}
|
|
|
|
export async function loadSeedFixtures(
|
|
prisma: PrismaService,
|
|
): Promise<E2ESeedFixtures> {
|
|
const systemAdmin = await requireUserScope(
|
|
prisma,
|
|
'seed-system-admin-openid',
|
|
);
|
|
const hospitalAdminA = await requireUserScope(
|
|
prisma,
|
|
'seed-hospital-admin-a-openid',
|
|
);
|
|
const directorA = await requireUserScope(prisma, 'seed-director-a-openid');
|
|
const leaderA = await requireUserScope(prisma, 'seed-leader-a-openid');
|
|
const doctorA = await requireUserScope(prisma, 'seed-doctor-a-openid');
|
|
const doctorA2 = await requireUserScope(prisma, 'seed-doctor-a2-openid');
|
|
const doctorA3 = await requireUserScope(prisma, 'seed-doctor-a3-openid');
|
|
const doctorB = await requireUserScope(prisma, 'seed-doctor-b-openid');
|
|
const engineerA = await requireUserScope(prisma, 'seed-engineer-a-openid');
|
|
const engineerB = await requireUserScope(prisma, 'seed-engineer-b-openid');
|
|
|
|
const hospitalAId = hospitalAdminA.hospitalId;
|
|
const hospitalBId = doctorB.hospitalId;
|
|
const departmentA1Id = doctorA.departmentId;
|
|
const departmentA2Id = doctorA3.departmentId;
|
|
const departmentB1Id = doctorB.departmentId;
|
|
const groupA1Id = doctorA.groupId;
|
|
const groupA2Id = doctorA3.groupId;
|
|
const groupB1Id = doctorB.groupId;
|
|
|
|
if (
|
|
hospitalAId == null ||
|
|
hospitalBId == null ||
|
|
departmentA1Id == null ||
|
|
departmentA2Id == null ||
|
|
departmentB1Id == null ||
|
|
groupA1Id == null ||
|
|
groupA2Id == null ||
|
|
groupB1Id == null
|
|
) {
|
|
throw new NotFoundException('Seed user scope is incomplete');
|
|
}
|
|
|
|
return {
|
|
hospitalAId,
|
|
hospitalBId,
|
|
departmentA1Id,
|
|
departmentA2Id,
|
|
departmentB1Id,
|
|
groupA1Id,
|
|
groupA2Id,
|
|
groupB1Id,
|
|
users: {
|
|
systemAdminId: systemAdmin.id,
|
|
hospitalAdminAId: hospitalAdminA.id,
|
|
directorAId: directorA.id,
|
|
leaderAId: leaderA.id,
|
|
doctorAId: doctorA.id,
|
|
doctorA2Id: doctorA2.id,
|
|
doctorA3Id: doctorA3.id,
|
|
doctorBId: doctorB.id,
|
|
engineerAId: engineerA.id,
|
|
engineerBId: engineerB.id,
|
|
},
|
|
patients: {
|
|
patientA1Id: await requirePatientId(
|
|
prisma,
|
|
hospitalAId,
|
|
'13800002001',
|
|
'seed-id-card-cross-hospital',
|
|
),
|
|
patientA2Id: await requirePatientId(
|
|
prisma,
|
|
hospitalAId,
|
|
'13800002002',
|
|
'seed-id-card-a2',
|
|
),
|
|
patientA3Id: await requirePatientId(
|
|
prisma,
|
|
hospitalAId,
|
|
'13800002003',
|
|
'seed-id-card-a3',
|
|
),
|
|
patientB1Id: await requirePatientId(
|
|
prisma,
|
|
hospitalBId,
|
|
'13800002001',
|
|
'seed-id-card-cross-hospital',
|
|
),
|
|
},
|
|
devices: {
|
|
deviceA1Id: await requireDeviceId(prisma, 'SEED-SN-A-001'),
|
|
deviceA2Id: await requireDeviceId(prisma, 'SEED-SN-A-002'),
|
|
deviceA3Id: await requireDeviceId(prisma, 'SEED-SN-A-003'),
|
|
deviceA4InactiveId: await requireDeviceId(prisma, 'SEED-SN-A-004'),
|
|
deviceB1Id: await requireDeviceId(prisma, 'SEED-SN-B-001'),
|
|
},
|
|
};
|
|
}
|