feat: add institution support for code gen and grouping

This commit is contained in:
Methapon Metanipat 2024-11-07 08:45:46 +07:00
parent 2a6cb6ddc0
commit da372dc6fd
2 changed files with 27 additions and 6 deletions

View file

@ -930,8 +930,10 @@ model EmployeeOtherInfo {
}
model Institution {
id String @id @default(cuid())
name String
id String @id @default(cuid())
code String
group String // Use for grouping, but not use relation
name String
address String
addressEN String

View file

@ -27,6 +27,8 @@ function globalAllow(user: RequestWithUser["user"]) {
type InstitutionPayload = {
name: string;
code: string;
addressEN: string;
address: string;
soi?: string | null;
@ -69,17 +71,34 @@ export class InstitutionController extends Controller {
@Get("{institutionId}")
@Security("keycloak")
async getInstitution(@Path() institutionId: string) {
async getInstitution(@Path() institutionId: string, @Query() group?: string) {
return await prisma.institution.findFirst({
where: { id: institutionId },
where: { id: institutionId, group },
});
}
@Post()
@Security("keycloak")
async createInstitution(@Body() body: InstitutionPayload) {
return await prisma.institution.create({
data: body,
return await prisma.$transaction(async (tx) => {
const last = await tx.runningNo.upsert({
where: {
key: `INST_${body.code}`,
},
create: {
key: `INST_${body.code}`,
value: 1,
},
update: { value: { increment: 1 } },
});
return await tx.institution.create({
data: {
...body,
code: `${body.code}${last.value.toString().padStart(5, "0")}`,
group: body.code,
},
});
});
}