Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(BE): 한의과 예진 조회 API 수정 #103

Merged
merged 1 commit into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions backend/src/km-charts/km-charts.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
BadRequestException,
Body,
Controller,
Get,
Expand All @@ -11,16 +12,19 @@ import {
import { KmChartsService } from './km-charts.service';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { CreatePrediagnosisDto } from './dto/create-prediagnosis.dto';
import { KmPrescriptionsService } from 'src/km-prescriptions/km-prescriptions.service';
import { CreateKMPrescriptionDto } from 'src/km-prescriptions/dto/create-km-prescription.dto';
import { KmMedicinesService } from 'src/km-medicines/km-medicines.service';
import { KmPrescriptionsService } from '../km-prescriptions/km-prescriptions.service';
import { CreateKMPrescriptionDto } from '../km-prescriptions/dto/create-km-prescription.dto';
import { KmMedicinesService } from '../km-medicines/km-medicines.service';
import { OrdersService } from '../orders/orders.service';
import { Department } from '../orders/const/department.const';

@ApiTags('한의과')
@Controller('km/charts')
export class KmChartsController {
constructor(
private readonly chartsService: KmChartsService,
private readonly prescriptionsService: KmPrescriptionsService,
private readonly ordersService: OrdersService,
private readonly medicinesService: KmMedicinesService,
) {}

Expand Down Expand Up @@ -66,8 +70,27 @@ export class KmChartsController {
status: HttpStatus.OK,
description: '예진이 조회되었습니다',
})
getPrediagnosis(@Param('chartId') chartId: number) {
return this.chartsService.getPrediagnosis(chartId);
async getPrediagnosis(@Param('chartId') chartId: number) {
const chart = await this.chartsService.getPrediagnosis(chartId);

if (chart.status === 1) {
const chartNumber = await this.ordersService.checkTodayChart(
chart.patient.id,
Department.M,
);
const vitalSign =
await this.chartsService.getVitalSignByChartNumber(chartNumber);
const history = await this.chartsService.getHistory(chartId);
if (chartNumber) {
return { vitalSign, history };
} else {
return history;
}
} else if (chart.status === 2) {
return chart;
}

throw new BadRequestException();
}

@Get('past/:patientId')
Expand Down
8 changes: 6 additions & 2 deletions backend/src/km-charts/km-charts.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { Histories } from 'src/patients/histories/entity/histories.entity';
import { KM_Complaints } from 'src/km-complaints/entity/km-complaints.entity';
import { Orders } from 'src/orders/entity/orders.entity';
import { MComplaintsService } from 'src/m-complaints/m-complaints.service';
import { KmPrescriptionsService } from 'src/km-prescriptions/km-prescriptions.service';
import { KmMedicinesService } from 'src/km-medicines/km-medicines.service';
import { KM_Prescriptions } from 'src/km-prescriptions/entity/km-prescriptions.entity';
import { KM_Medicines } from 'src/km-medicines/entity/km-medicines.entity';
import { CommonService } from 'src/common/common.service';
import { M_Charts } from 'src/m-charts/entity/m-charts.entity';
import { KmComplaintsService } from 'src/km-complaints/km-complaints.service';
import { OrdersService } from 'src/orders/orders.service';

@Module({
imports: [
TypeOrmModule.forFeature([
KM_Charts,
M_Charts,
Histories,
KM_Complaints,
Orders,
Expand All @@ -27,9 +30,10 @@ import { CommonService } from 'src/common/common.service';
controllers: [KmChartsController],
providers: [
KmChartsService,
MComplaintsService,
KmComplaintsService,
KmPrescriptionsService,
KmMedicinesService,
OrdersService,
CommonService,
],
})
Expand Down
71 changes: 44 additions & 27 deletions backend/src/km-charts/km-charts.service.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import {
BadRequestException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { KM_Charts } from './entity/km-charts.entity';
import { KM_Complaints } from 'src/km-complaints/entity/km-complaints.entity';
import { Histories } from 'src/patients/histories/entity/histories.entity';
import { Orders } from 'src/orders/entity/orders.entity';
import { KM_Complaints } from '../km-complaints/entity/km-complaints.entity';
import { Histories } from '../patients/histories/entity/histories.entity';
import { Orders } from '../orders/entity/orders.entity';
import { Repository } from 'typeorm';
import { CreateVitalSignDto } from './dto/create-vital-sign.dto';
import { CreateKMComplaintDto } from '../km-complaints/dto/create-km-complaint.dto';
import { CreateHistoryDto } from '../patients/histories/dto/create-history.dto';
import { DEFAULT_KM_CHART_FIND_OPTIONS } from './const/default-km-chart-find-options.const';
import { M_Charts } from '../m-charts/entity/m-charts.entity';

@Injectable()
export class KmChartsService {
constructor(
@InjectRepository(KM_Charts)
private readonly chartsRepository: Repository<KM_Charts>,
private readonly kmChartsRepository: Repository<KM_Charts>,
@InjectRepository(M_Charts)
private readonly mChartsRepository: Repository<M_Charts>,
@InjectRepository(KM_Complaints)
private readonly complaintsRepository: Repository<KM_Complaints>,
@InjectRepository(Histories)
Expand All @@ -27,22 +26,22 @@ export class KmChartsService {
private readonly ordersRepository: Repository<Orders>,
) {}
async createVitalSign(chartId: number, vitalSignDto: CreateVitalSignDto) {
const chart = await this.chartsRepository.findOne({
const chart = await this.kmChartsRepository.findOne({
where: { id: chartId },
});

if (!chart) {
throw new NotFoundException();
}

return await this.chartsRepository.save({
return await this.kmChartsRepository.save({
...chart,
...vitalSignDto,
});
}

async createComplaint(chartId: number, complaintDto: CreateKMComplaintDto) {
const chart = await this.chartsRepository.findOne({
const chart = await this.kmChartsRepository.findOne({
where: { id: chartId },
relations: { patient: true },
});
Expand All @@ -60,7 +59,7 @@ export class KmChartsService {
}

async createHistory(chartId: number, historyDto: CreateHistoryDto) {
const chart = await this.chartsRepository.findOne({
const chart = await this.kmChartsRepository.findOne({
where: { id: chartId },
relations: { patient: { history: true } },
});
Expand All @@ -79,7 +78,7 @@ export class KmChartsService {
}

async updateStatus(chartId: number, status: number) {
const chart = await this.chartsRepository.findOne({
const chart = await this.kmChartsRepository.findOne({
where: { id: chartId },
});

Expand All @@ -88,7 +87,7 @@ export class KmChartsService {
}

chart.status = status;
await this.chartsRepository.save(chart);
await this.kmChartsRepository.save(chart);

const order = await this.ordersRepository.findOne({
where: { kmChart: { id: chartId } },
Expand All @@ -101,25 +100,17 @@ export class KmChartsService {
}

async getPrediagnosis(chartId: number) {
const chart = await this.chartsRepository.findOne({
return await this.kmChartsRepository.findOne({
where: { id: chartId },
relations: {
complaints: true,
patient: { history: true },
},
});

if (chart.status < 2) {
throw new BadRequestException('예진이 완료되지 않았습니다');
}

if (chart.status >= 2) {
return chart;
}
}

async getPastCharts(patientId: number) {
const charts = await this.chartsRepository.find({
const charts = await this.kmChartsRepository.find({
where: {
patient: { id: patientId },
status: 6,
Expand All @@ -130,21 +121,47 @@ export class KmChartsService {
return charts;
}

async getVitalSignByChartNumber(chartNumber: string) {
return await this.mChartsRepository.findOne({
where: { chartNumber },
select: [
'spO2',
'heartRate',
'bodyTemperature',
'systoleBloodPressure',
'diastoleBloodPressure',
'bloodGlucose',
'afterMeals',
'vsMemo',
'createdAt',
],
});
}

async getComplaint(chartId: number) {
return this.complaintsRepository.find({
where: { chart: { id: chartId } },
});
}

async getHistory(chartId: number) {
const chart = await this.kmChartsRepository.findOne({
where: { id: chartId },
relations: { patient: { history: true } },
});

return chart?.patient?.history;
}

async getPastChart(chartId: number) {
return await this.chartsRepository.findOne({
return await this.kmChartsRepository.findOne({
...DEFAULT_KM_CHART_FIND_OPTIONS,
where: { id: chartId },
});
}

async checkChartExistsById(id: number) {
return this.chartsRepository.exists({
return this.kmChartsRepository.exists({
where: { id },
});
}
Expand Down
Loading