TreatmentType and ProsthesisType database shcema and data updated. Lab dispatch wired through new data.

This commit is contained in:
2026-07-06 20:40:19 +03:30
parent 3e81c110a3
commit 667b08ed0c
48 changed files with 1813 additions and 275 deletions

View File

@@ -0,0 +1,26 @@
import { Controller, Get, Query, Req, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { ProsthesisCatalogService } from './prosthesis-catalog.service';
@ApiTags('prosthesis-catalog')
@ApiBearerAuth('JWT-auth')
@UseGuards(JwtAuthGuard)
@Controller('prosthesis-catalog')
export class ProsthesisCatalogController {
constructor(private readonly prosthesisCatalogService: ProsthesisCatalogService) {}
@Get()
@ApiOperation({
summary: 'List prosthesis types (optionally scoped to lab — v1 returns all types)',
})
list(
@Req() req: { user?: { language?: string | null } },
@Query('labOrganizationId') _labOrganizationId?: string,
) {
return this.prosthesisCatalogService.list(req.user?.language).then((data) => ({
success: true,
data,
}));
}
}

View File

@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { PrismaService } from '../../../prisma/prisma.service';
import { ProsthesisCatalogController } from './prosthesis-catalog.controller';
import { ProsthesisCatalogService } from './prosthesis-catalog.service';
@Module({
controllers: [ProsthesisCatalogController],
providers: [ProsthesisCatalogService, PrismaService],
exports: [ProsthesisCatalogService],
})
export class ProsthesisCatalogModule {}

View File

@@ -0,0 +1,98 @@
import { Injectable, BadRequestException, OnModuleInit } from '@nestjs/common';
import { CatalogEntityKind } from '@prisma/client';
import { PrismaService } from '../../../prisma/prisma.service';
import {
CatalogLabelService,
CatalogLocale,
normalizeCatalogLocale,
} from '../catalog/catalog-label.service';
export type ProsthesisTypeCatalogEntry = {
code: string;
sortOrder: number;
label: string;
};
@Injectable()
export class ProsthesisCatalogService implements OnModuleInit {
private loaded = false;
private byCode = new Map<string, { sortOrder: number }>();
constructor(
private readonly prisma: PrismaService,
private readonly catalogLabels: CatalogLabelService,
) {}
async onModuleInit() {
await this.refresh();
}
async refresh(): Promise<void> {
const rows = await this.prisma.prosthesisType.findMany({
where: { isActive: true },
orderBy: [{ sortOrder: 'asc' }, { code: 'asc' }],
select: { code: true, sortOrder: true },
});
this.byCode = new Map(rows.map((row) => [row.code, { sortOrder: row.sortOrder }]));
this.loaded = true;
}
async list(localeInput?: string | null): Promise<ProsthesisTypeCatalogEntry[]> {
this.ensureLoaded();
const locale = normalizeCatalogLocale(localeInput);
const codes = [...this.byCode.keys()];
const labels = await this.catalogLabels.resolveLabels(
CatalogEntityKind.PROSTHESIS_TYPE,
codes,
locale,
);
return codes
.map((code) => ({
code,
sortOrder: this.byCode.get(code)!.sortOrder,
label: labels.get(code) ?? code,
}))
.sort((a, b) => a.sortOrder - b.sortOrder || a.code.localeCompare(b.code));
}
assertKnownProsthesisType(code: string): void {
this.ensureLoaded();
if (!this.byCode.has(code)) {
throw new BadRequestException(`Unknown prosthesis type: ${code}`);
}
}
async getStepCodesForProsthesisType(prosthesisTypeCode: string): Promise<string[]> {
const type = await this.prisma.prosthesisType.findUnique({
where: { code: prosthesisTypeCode },
select: {
steps: {
orderBy: { stepOrder: 'asc' },
select: { labWorkflowStep: { select: { code: true } } },
},
},
});
if (!type) {
return [];
}
return type.steps.map((s) => s.labWorkflowStep.code);
}
async resolveStepLabels(stepCodes: string[], locale: CatalogLocale): Promise<Map<string, string>> {
return this.catalogLabels.resolveLabels(
CatalogEntityKind.LAB_WORKFLOW_STEP,
stepCodes,
locale,
);
}
private ensureLoaded() {
if (!this.loaded) {
throw new Error('Prosthesis catalog is not loaded yet');
}
}
}