@@ -5,18 +5,19 @@ import {
5
5
} from '@nestjs/common' ;
6
6
import { M_Medicine_Categories } from './entity/m_medicine_categories.entity' ;
7
7
import { InjectRepository } from '@nestjs/typeorm' ;
8
- import { Repository } from 'typeorm' ;
8
+ import { IsNull , Not , Repository } from 'typeorm' ;
9
9
import { CreateMMedicineCategoryDto } from './dto/create-m-medicine-category.dto' ;
10
10
import { UpdateMMedicineSubCategoryDto } from './dto/update-m-medicine-sub-category.dto' ;
11
- import { MMedicinesService } from 'src/m-medicines/m-medicines.service' ;
12
11
import { UpdateMMedicineMainCategoryDto } from './dto/update-m-medicine-main-category.dto' ;
12
+ import { M_Medicines } from 'src/m-medicines/entity/m-medicines.entity' ;
13
13
14
14
@Injectable ( )
15
15
export class MMedicineCategoriesService {
16
16
constructor (
17
17
@InjectRepository ( M_Medicine_Categories )
18
18
private readonly mMedicineCategoriesRepository : Repository < M_Medicine_Categories > ,
19
- private readonly mMedicinesService : MMedicinesService ,
19
+ @InjectRepository ( M_Medicines )
20
+ private readonly mMedicinesRepository : Repository < M_Medicines > ,
20
21
) { }
21
22
22
23
async getCategories ( ) {
@@ -164,10 +165,7 @@ export class MMedicineCategoriesService {
164
165
throw new NotFoundException ( ) ;
165
166
}
166
167
167
- const isDeleted =
168
- await this . mMedicinesService . checkDeletedMMedicinesByCategoryId (
169
- categoryId ,
170
- ) ;
168
+ const isDeleted = await this . checkDeletedMMedicinesByCategoryId ( categoryId ) ;
171
169
172
170
if ( ! isDeleted ) {
173
171
throw new BadRequestException ( '삭제되지 않은 약품이 존재합니다.' ) ;
@@ -177,4 +175,28 @@ export class MMedicineCategoriesService {
177
175
178
176
return categoryId ;
179
177
}
178
+
179
+ async checkDeletedMMedicinesByCategoryId ( categoryId : number ) {
180
+ const [ , deletedMedicineCount ] =
181
+ await this . mMedicinesRepository . findAndCount ( {
182
+ where : {
183
+ category : {
184
+ id : categoryId ,
185
+ } ,
186
+ deletedAt : Not ( IsNull ( ) ) ,
187
+ } ,
188
+ withDeleted : true ,
189
+ } ) ;
190
+
191
+ const [ , medicineCount ] = await this . mMedicinesRepository . findAndCount ( {
192
+ where : {
193
+ category : {
194
+ id : categoryId ,
195
+ } ,
196
+ } ,
197
+ withDeleted : true ,
198
+ } ) ;
199
+
200
+ return deletedMedicineCount === medicineCount ;
201
+ }
180
202
}
0 commit comments