Skip to content

Commit

Permalink
feat(BE): 한의과 처방 생성 API 구현 (#96)
Browse files Browse the repository at this point in the history
* fix: KM_Prescriptions dosesTime 컬럼 타입 수정

* feat: 한의과 처방 생성 DTO 추가

* feat: 한의과 처방 생성 API 구현

* rename: Prescriptions entity 오타 수정

* refactor: 의과 처방 생성 API 수정
  • Loading branch information
eeseung authored Sep 7, 2024
1 parent 6b5145a commit 265b2e6
Show file tree
Hide file tree
Showing 20 changed files with 216 additions and 28 deletions.
4 changes: 2 additions & 2 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { M_Charts } from './m-charts/entity/m-charts.entity';
import { MComplaintsModule } from './m-complaints/m-complaints.module';
import { M_Complaints } from './m-complaints/entity/m-complaints.entity';
import { MPrescriptionsModule } from './m-prescriptions/m-prescriptions.module';
import { M_Prescriptions } from './m-prescriptions/entity/m-prescriotions.entity';
import { M_Prescriptions } from './m-prescriptions/entity/m-prescriptions.entity';
import { HistoriesModule } from './patients/histories/histories.module';
import { Histories } from './patients/histories/entity/histories.entity';
import { Users } from './users/entity/users.entity';
Expand All @@ -30,7 +30,7 @@ import { KmComplaintsModule } from './km-complaints/km-complaints.module';
import { KmPrescriptionsModule } from './km-prescriptions/km-prescriptions.module';
import { KmMedicinesModule } from './km-medicines/km-medicines.module';
import { KM_Complaints } from './km-complaints/entity/km-complaints.entity';
import { KM_Prescriptions } from './km-prescriptions/entity/km-prescriotions.entity';
import { KM_Prescriptions } from './km-prescriptions/entity/km-prescriptions.entity';
import { KM_Medicines } from './km-medicines/entity/km-medicines.entity';
import {
ENV_DB_DATABASE_KEY,
Expand Down
2 changes: 1 addition & 1 deletion backend/src/km-charts/entity/km-charts.entity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseModel } from 'src/common/entity/base.entity';
import { KM_Complaints } from 'src/km-complaints/entity/km-complaints.entity';
import { KM_Prescriptions } from 'src/km-prescriptions/entity/km-prescriotions.entity';
import { KM_Prescriptions } from 'src/km-prescriptions/entity/km-prescriptions.entity';
import { Patients } from 'src/patients/entity/patients.entity';
import { Column, Entity, ManyToOne, OneToMany } from 'typeorm';

Expand Down
54 changes: 52 additions & 2 deletions backend/src/km-charts/km-charts.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import { Body, Controller, Get, HttpStatus, Param, Post } from '@nestjs/common';
import {
Body,
Controller,
Get,
HttpStatus,
NotFoundException,
Param,
ParseIntPipe,
Post,
} from '@nestjs/common';
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';

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

@Post('/:chartId/prediagnosis')
@ApiOperation({
Expand Down Expand Up @@ -89,4 +105,38 @@ export class KmChartsController {
getPastChart(@Param('chartId') chartId: number) {
return this.chartsService.getPastChart(chartId);
}

@Post(':chartId/prescriptions')
@ApiOperation({
summary: '처방 생성',
})
@ApiResponse({
status: HttpStatus.BAD_REQUEST,
description:
'존재하지 않는 약품입니다. <small>medicineId에 해당하는 약품이 없는 경우</small>',
})
@ApiResponse({
status: HttpStatus.NOT_FOUND,
})
async postPrescription(
@Param('chartId', ParseIntPipe) chartId: number,
@Body() createPrescriptionDto: CreateKMPrescriptionDto,
) {
const chartExists = await this.chartsService.checkChartExistsById(chartId);
if (!chartExists) {
throw new NotFoundException();
}

const medicineExists = await this.medicinesService.checkMedicineExistsById(
createPrescriptionDto.medicineId,
);
if (!medicineExists) {
throw new NotFoundException('존재하지 않는 약품입니다.');
}

return await this.prescriptionsService.createPrescription(
chartId,
createPrescriptionDto,
);
}
}
22 changes: 20 additions & 2 deletions backend/src/km-charts/km-charts.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,30 @@ 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';

@Module({
imports: [
TypeOrmModule.forFeature([KM_Charts, Histories, KM_Complaints, Orders]),
TypeOrmModule.forFeature([
KM_Charts,
Histories,
KM_Complaints,
Orders,
KM_Prescriptions,
KM_Medicines,
]),
],
controllers: [KmChartsController],
providers: [KmChartsService, MComplaintsService],
providers: [
KmChartsService,
MComplaintsService,
KmPrescriptionsService,
KmMedicinesService,
CommonService,
],
})
export class KmChartsModule {}
6 changes: 6 additions & 0 deletions backend/src/km-charts/km-charts.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,10 @@ export class KmChartsService {
where: { id: chartId },
});
}

async checkChartExistsById(id: number) {
return this.chartsRepository.exists({
where: { id },
});
}
}
2 changes: 1 addition & 1 deletion backend/src/km-medicines/entity/km-medicines.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseModel } from 'src/common/entity/base.entity';
import { KM_Prescriptions } from 'src/km-prescriptions/entity/km-prescriotions.entity';
import { KM_Prescriptions } from 'src/km-prescriptions/entity/km-prescriptions.entity';
import { Column, DeleteDateColumn, Entity, OneToMany } from 'typeorm';

@Entity()
Expand Down
6 changes: 6 additions & 0 deletions backend/src/km-medicines/km-medicines.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,10 @@ export class KmMedicinesService {
throw new BadRequestException(error);
}
}

async checkMedicineExistsById(id: number) {
return this.medicinesRepository.exists({
where: { id },
});
}
}
55 changes: 55 additions & 0 deletions backend/src/km-prescriptions/dto/create-km-prescription.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { PickType } from '@nestjs/mapped-types';
import { KM_Prescriptions } from '../entity/km-prescriptions.entity';
import { IsInt, IsNumber, IsOptional, IsString } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';

export class CreateKMPrescriptionDto extends PickType(KM_Prescriptions, [
'doses',
'dosesCountByDay',
'dosesDay',
'dosesTime',
'memo',
]) {
@ApiProperty({
description: '1회 투약량',
example: 1,
})
@IsNumber()
doses: number;

@ApiProperty({
description: '1일 복용횟수',
example: 'tid',
})
@IsString()
dosesCountByDay: string;

@ApiProperty({
description: '복용 일수',
example: 5,
})
@IsInt()
dosesDay: number;

@ApiProperty({
description: '복용 시간',
example: '식전',
})
@IsString()
dosesTime: string;

@ApiProperty({
description: '메모',
example: '',
})
@IsString()
@IsOptional()
memo: string;

@ApiProperty({
description: '약품 ID',
example: 1,
})
@IsNumber()
medicineId: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ export class KM_Prescriptions extends BaseModel {
@IsInt()
dosesDay: number;

@Column()
@IsInt()
dosesTime: number;
@Column('char', {
length: 10,
})
@IsString()
dosesTime: string;

@Column('float')
@IsNumber()
Expand Down
2 changes: 1 addition & 1 deletion backend/src/km-prescriptions/km-prescriptions.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Module } from '@nestjs/common';
import { KmPrescriptionsService } from './km-prescriptions.service';
import { KmPrescriptionsController } from './km-prescriptions.controller';
import { KM_Prescriptions } from './entity/km-prescriotions.entity';
import { KM_Prescriptions } from './entity/km-prescriptions.entity';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
Expand Down
29 changes: 28 additions & 1 deletion backend/src/km-prescriptions/km-prescriptions.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { KM_Prescriptions } from './entity/km-prescriptions.entity';
import { Repository } from 'typeorm';
import { CreateKMPrescriptionDto } from './dto/create-km-prescription.dto';
import { convertDosesCountByDay } from 'src/common/util/convert.util';

@Injectable()
export class KmPrescriptionsService {}
export class KmPrescriptionsService {
constructor(
@InjectRepository(KM_Prescriptions)
private readonly prescriptionsRepository: Repository<KM_Prescriptions>,
) {}

async createPrescription(
chartId: number,
prescriptionDto: CreateKMPrescriptionDto,
) {
const { medicineId, ...restPrescriptionDto } = prescriptionDto;

return await this.prescriptionsRepository.save({
...restPrescriptionDto,
dosesTotal:
restPrescriptionDto.doses *
convertDosesCountByDay(restPrescriptionDto.dosesCountByDay) *
restPrescriptionDto.dosesDay,
chart: { id: chartId },
medicine: { id: medicineId },
});
}
}
2 changes: 1 addition & 1 deletion backend/src/m-charts/entity/m-charts.entity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseModel } from 'src/common/entity/base.entity';
import { M_Complaints } from 'src/m-complaints/entity/m-complaints.entity';
import { M_Prescriptions } from 'src/m-prescriptions/entity/m-prescriotions.entity';
import { M_Prescriptions } from 'src/m-prescriptions/entity/m-prescriptions.entity';
import { Patients } from 'src/patients/entity/patients.entity';
import { Column, Entity, ManyToOne, OneToMany } from 'typeorm';

Expand Down
27 changes: 20 additions & 7 deletions backend/src/m-charts/m-charts.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ParseIntPipe,
Patch,
BadRequestException,
NotFoundException,
} from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { MChartsService } from './m-charts.service';
Expand All @@ -24,7 +25,7 @@ export class MChartsController {
constructor(
private readonly chartsService: MChartsService,
private readonly prescriptionsService: MPrescriptionsService,
private readonly medicineService: MMedicinesService,
private readonly medicinesService: MMedicinesService,
) {}

@Post('/:chartId/prediagnosis')
Expand Down Expand Up @@ -150,7 +151,7 @@ export class MChartsController {

const prescriptions = await Promise.all([
...createMDiagnosisDto.prescriptions.map((prescription) => {
return this.prescriptionsService.createMPrescription(
return this.prescriptionsService.createPrescription(
chartId,
prescription,
);
Expand Down Expand Up @@ -223,7 +224,7 @@ export class MChartsController {
const prescriptions =
await this.prescriptionsService.getPrescriptions(chartId);
prescriptions.map(async (prescription) => {
await this.medicineService.updateMedicineTotalAmount(
await this.medicinesService.updateMedicineTotalAmount(
prescription.medicine.id,
prescription,
);
Expand Down Expand Up @@ -251,13 +252,25 @@ export class MChartsController {
@ApiResponse({
status: HttpStatus.NOT_FOUND,
})
async postMPrescription(
async postPrescription(
@Param('chartId', ParseIntPipe) chartId: number,
@Body() createMPrescriptionDto: CreateMPrescriptionDto,
@Body() createPrescriptionDto: CreateMPrescriptionDto,
) {
return await this.prescriptionsService.createMPrescription(
const chartExists = await this.chartsService.checkChartExistsById(chartId);
if (!chartExists) {
throw new NotFoundException();
}

const medicineExists = await this.medicinesService.checkMedicineExistsById(
createPrescriptionDto.medicineId,
);
if (!medicineExists) {
throw new NotFoundException('존재하지 않는 약품입니다.');
}

return await this.prescriptionsService.createPrescription(
chartId,
createMPrescriptionDto,
createPrescriptionDto,
);
}
}
6 changes: 6 additions & 0 deletions backend/src/m-charts/m-charts.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,10 @@ export class MChartsService {
relations: { prescriptions: { medicine: true } },
});
}

async checkChartExistsById(id: number) {
return this.chartsRepository.exists({
where: { id },
});
}
}
2 changes: 1 addition & 1 deletion backend/src/m-medicines/entity/m-medicines.entity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseModel } from 'src/common/entity/base.entity';
import { M_Medicine_Categories } from 'src/m-medicine-categories/entity/m_medicine_categories.entity';
import { M_Prescriptions } from 'src/m-prescriptions/entity/m-prescriotions.entity';
import { M_Prescriptions } from 'src/m-prescriptions/entity/m-prescriptions.entity';
import {
Column,
DeleteDateColumn,
Expand Down
8 changes: 7 additions & 1 deletion backend/src/m-medicines/m-medicines.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { UpdateMMedicineDto } from './dto/update-m-medicine.dto';
import { PaginateMMedicineDto } from './dto/paginate-m-medicine.dto';
import { CommonService } from 'src/common/common.service';
import { convertDosesCountByDay } from 'src/common/util/convert.util';
import { M_Prescriptions } from 'src/m-prescriptions/entity/m-prescriotions.entity';
import { M_Prescriptions } from 'src/m-prescriptions/entity/m-prescriptions.entity';

@Injectable()
export class MMedicinesService {
Expand Down Expand Up @@ -211,6 +211,12 @@ export class MMedicinesService {
}
}

async checkMedicineExistsById(id: number) {
return this.medicinesRepository.exists({
where: { id },
});
}

async checkDeletedMedicineByCategoryId(categoryId: number) {
const [, deletedMedicineCount] =
await this.medicinesRepository.findAndCount({
Expand Down
3 changes: 1 addition & 2 deletions backend/src/m-prescriptions/dto/create-m-prescription.dto.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { PickType } from '@nestjs/mapped-types';
import { M_Prescriptions } from '../entity/m-prescriotions.entity';
import { M_Prescriptions } from '../entity/m-prescriptions.entity';
import { IsInt, IsNumber, IsOptional, IsString } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';

export class CreateMPrescriptionDto extends PickType(M_Prescriptions, [
'doses',
'dosesCountByDay',
'dosesDay',
'dosesTotal',
'bundle',
'memo',
]) {
Expand Down
Loading

0 comments on commit 265b2e6

Please sign in to comment.