Files
dyolink/backend/src/modules/cases/cases.controller.ts

104 lines
3.4 KiB
TypeScript
Raw Normal View History

import {
Body,
Controller,
Get,
Param,
Patch,
Query,
Req,
Res,
UseGuards,
} from '@nestjs/common';
import type { Response } from 'express';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { LabOrgGuard } from '../../common/guards/lab-org.guard';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { CasesService } from './cases.service';
import { ListLabCasesDto, UpdateLabCaseImportantDto, AssignLabCaseTaskDto } from './dto/cases.dto';
@ApiTags('cases')
@ApiBearerAuth('JWT-auth')
@UseGuards(JwtAuthGuard, LabOrgGuard)
@Controller('cases')
export class CasesController {
constructor(private readonly casesService: CasesService) {}
@Get()
@ApiOperation({ summary: 'List lab cases received by this organization' })
list(@Query() query: ListLabCasesDto, @Req() req) {
const organizationId = this.casesService.getOrganizationIdFromUser(req.user);
return this.casesService.list(organizationId, req.user.id, query);
}
@Get('assignable-staff')
@ApiOperation({ summary: 'Staff who can be assigned lab tasks' })
listAssignableStaff(@Req() req) {
const organizationId = this.casesService.getOrganizationIdFromUser(req.user);
return this.casesService.listAssignableStaff(organizationId, req.user.id);
}
@Get('filter-options')
@ApiOperation({ summary: 'Clinics and treatment types for inbox filters' })
listFilterOptions(@Req() req) {
const organizationId = this.casesService.getOrganizationIdFromUser(req.user);
return this.casesService.listFilterOptions(organizationId, req.user.id);
}
@Get(':id')
@ApiOperation({ summary: 'Get one lab case with tasks grouped by tooth' })
getOne(@Param('id') id: string, @Req() req) {
const organizationId = this.casesService.getOrganizationIdFromUser(req.user);
return this.casesService.getOne(id, organizationId, req.user.id, req.user.language);
}
@Get(':id/attachments/:attachmentId/file')
@ApiOperation({ summary: 'Download an attachment shared with this lab case' })
async downloadAttachment(
@Param('id') id: string,
@Param('attachmentId') attachmentId: string,
@Req() req,
@Res() res: Response,
) {
const organizationId = this.casesService.getOrganizationIdFromUser(req.user);
const file = await this.casesService.streamCaseAttachment(
id,
attachmentId,
organizationId,
req.user.id,
);
res.setHeader('Content-Type', file.mimeType);
res.setHeader('Content-Disposition', `inline; filename="${file.fileName}"`);
file.stream.pipe(res);
}
@Patch(':id/important')
@ApiOperation({ summary: 'Toggle the important flag for a whole case' })
setCaseImportant(
@Param('id') id: string,
@Body() dto: UpdateLabCaseImportantDto,
@Req() req,
) {
const organizationId = this.casesService.getOrganizationIdFromUser(req.user);
return this.casesService.setCaseImportant(id, dto, organizationId, req.user.id, req.user.language);
}
@Patch(':caseId/tasks/:taskId/assign')
@ApiOperation({ summary: 'Assign or unassign a task to lab staff' })
assignTask(
@Param('caseId') caseId: string,
@Param('taskId') taskId: string,
@Body() dto: AssignLabCaseTaskDto,
@Req() req,
) {
const organizationId = this.casesService.getOrganizationIdFromUser(req.user);
return this.casesService.assignTask(
caseId,
taskId,
dto,
organizationId,
req.user.id,
req.user.language,
);
}
}