Skip to content

Commit

Permalink
feat(be): branch delete
Browse files Browse the repository at this point in the history
  • Loading branch information
wermarter committed Dec 31, 2024
1 parent 1d41309 commit 3ff4602
Show file tree
Hide file tree
Showing 90 changed files with 745 additions and 533 deletions.
43 changes: 21 additions & 22 deletions apps/hcdc-access-service/src/app/bio-product/use-case/delete.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { AuthSubject, BioProductAction } from '@diut/hcdc'
import { AuthSubject, BioProduct, BioProductAction } from '@diut/hcdc'
import { Inject, Injectable } from '@nestjs/common'
import { FilterQuery } from 'mongoose'
import { assertPermission } from 'src/app/auth/common'
import {
AUTH_CONTEXT_TOKEN,
BIOPRODUCT_REPO_TOKEN,
EEntityCannotDelete,
IAuthContext,
IBioProductRepository,
ITestRepository,
TEST_REPO_TOKEN,
} from 'src/domain'
import { BioProductAssertExistsUseCase } from './assert-exists'
import { BioProductSearchUseCase } from './search'

@Injectable()
export class BioProductDeleteUseCase {
Expand All @@ -21,30 +21,29 @@ export class BioProductDeleteUseCase {
private readonly bioProductRepository: IBioProductRepository,
@Inject(TEST_REPO_TOKEN)
private readonly testRepository: ITestRepository,
private readonly bioProductAssertExistsUseCase: BioProductAssertExistsUseCase,
private readonly bioProductSearchUseCase: BioProductSearchUseCase,
) {}

async execute(input: { id: string }) {
const entity = await this.bioProductAssertExistsUseCase.execute({
_id: input.id,
})
async execute(input: FilterQuery<BioProduct>) {
const { ability } = this.authContext.getData()
assertPermission(
ability,
AuthSubject.BioProduct,
BioProductAction.Delete,
entity,
)

const connectedTestCount = await this.testRepository.count({
bioProductId: input.id,
const { items: bioProducts } = await this.bioProductSearchUseCase.execute({
filter: input,
})
if (connectedTestCount > 0) {
throw new EEntityCannotDelete(`${connectedTestCount} connected Test`)
}

await this.bioProductRepository.deleteById(input.id)
for (const bioProduct of bioProducts) {
assertPermission(
ability,
AuthSubject.BioProduct,
BioProductAction.Delete,
bioProduct,
)

return entity
await this.testRepository.updateMany(
{ bioProductId: bioProduct._id },
{ bioProductId: null },
)

await this.bioProductRepository.deleteById(bioProduct._id)
}
}
}
53 changes: 41 additions & 12 deletions apps/hcdc-access-service/src/app/branch/use-case/delete.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import { AuthSubject, BranchAction } from '@diut/hcdc'
import { AuthSubject, Branch, BranchAction } from '@diut/hcdc'
import { Inject, Injectable } from '@nestjs/common'
import { FilterQuery } from 'mongoose'
import { assertPermission } from 'src/app/auth/common'
import { DiagnosisDeleteUseCase } from 'src/app/diagnosis/use-case/delete'
import { DoctorDeleteUseCase } from 'src/app/doctor/use-case/delete'
import { PatientTypeDeleteUseCase } from 'src/app/patient-type/use-case/delete'
import { PatientDeleteUseCase } from 'src/app/patient/use-case/delete'
import { PrintFormDeleteUseCase } from 'src/app/print-form/use-case/delete'
import { RoleDeleteUseCase } from 'src/app/role/use-case/delete'
import { TestComboDeleteUseCase } from 'src/app/test-combo/use-case/delete'
import { TestDeleteUseCase } from 'src/app/test/use-case/delete'
import { UserDeleteUseCase } from 'src/app/user/use-case/delete'
import {
AUTH_CONTEXT_TOKEN,
BRANCH_REPO_TOKEN,
EEntityCannotDelete,
IAuthContext,
IBranchRepository,
} from 'src/domain'
import { BranchAssertExistsUseCase } from './assert-exists'
import { BranchSearchUseCase } from './search'

@Injectable()
export class BranchDeleteUseCase {
Expand All @@ -17,20 +26,40 @@ export class BranchDeleteUseCase {
private readonly authContext: IAuthContext,
@Inject(BRANCH_REPO_TOKEN)
private readonly branchRepository: IBranchRepository,
private readonly branchAssertExistsUseCase: BranchAssertExistsUseCase,
private readonly branchSearchUseCase: BranchSearchUseCase,
private readonly patientDeleteUseCase: PatientDeleteUseCase,
private readonly testDeleteUseCase: TestDeleteUseCase,
private readonly testComboDeleteUseCase: TestComboDeleteUseCase,
private readonly diagnosisDeleteUseCase: DiagnosisDeleteUseCase,
private readonly doctorDeleteUseCase: DoctorDeleteUseCase,
private readonly patientTypeDeleteUseCase: PatientTypeDeleteUseCase,
private readonly printFormDeleteUseCase: PrintFormDeleteUseCase,
private readonly userDeleteUseCase: UserDeleteUseCase,
private readonly roleDeleteUseCase: RoleDeleteUseCase,
) {}

async execute(input: { id: string }) {
const entity = await this.branchAssertExistsUseCase.execute({
_id: input.id,
})
async execute(input: FilterQuery<Branch>) {
const { ability } = this.authContext.getData()
assertPermission(ability, AuthSubject.Branch, BranchAction.Delete, entity)
const { items: branches } = await this.branchSearchUseCase.execute(input)

throw new EEntityCannotDelete(`there are infinitely connected resources`)
for (const branch of branches) {
assertPermission(ability, AuthSubject.Branch, BranchAction.Delete, branch)

await this.branchRepository.deleteById(input.id)
// patient and all related sample
await this.patientDeleteUseCase.execute({ branchId: branch._id })

return entity
// infra
await this.testComboDeleteUseCase.execute({ branchId: branch._id })
await this.testDeleteUseCase.execute({ branchId: branch._id })
await this.diagnosisDeleteUseCase.execute({ branchId: branch._id })
await this.doctorDeleteUseCase.execute({ branchId: branch._id })
await this.patientTypeDeleteUseCase.execute({ branchId: branch._id })
await this.printFormDeleteUseCase.execute({ branchId: branch._id })
await this.userDeleteUseCase.execute({ branchId: branch._id })
await this.roleDeleteUseCase.execute({ branchId: branch._id })

// finally
await this.branchRepository.deleteById(branch._id)
}
}
}
46 changes: 25 additions & 21 deletions apps/hcdc-access-service/src/app/diagnosis/use-case/delete.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AuthSubject, DiagnosisAction } from '@diut/hcdc'
import { AuthSubject, Diagnosis, DiagnosisAction } from '@diut/hcdc'
import { Inject, Injectable } from '@nestjs/common'
import { FilterQuery } from 'mongoose'
import { assertPermission } from 'src/app/auth/common'
import {
AUTH_CONTEXT_TOKEN,
Expand All @@ -10,7 +11,7 @@ import {
ISampleRepository,
SAMPLE_REPO_TOKEN,
} from 'src/domain'
import { DiagnosisAssertExistsUseCase } from './assert-exists'
import { DiagnosisSearchUseCase } from './search'

@Injectable()
export class DiagnosisDeleteUseCase {
Expand All @@ -21,30 +22,33 @@ export class DiagnosisDeleteUseCase {
private readonly diagnosisRepository: IDiagnosisRepository,
@Inject(SAMPLE_REPO_TOKEN)
private readonly sampleRepository: ISampleRepository,
private readonly diagnosisAssertExistsUseCase: DiagnosisAssertExistsUseCase,
private readonly diagnosisSearchUseCase: DiagnosisSearchUseCase,
) {}

async execute(input: { id: string }) {
const entity = await this.diagnosisAssertExistsUseCase.execute({
_id: input.id,
})
async execute(input: FilterQuery<Diagnosis>) {
const { ability } = this.authContext.getData()
assertPermission(
ability,
AuthSubject.Diagnosis,
DiagnosisAction.Delete,
entity,
)

const connectedSampleCount = await this.sampleRepository.count({
diagnosisId: input.id,
const { items: diagnoses } = await this.diagnosisSearchUseCase.execute({
filter: input,
})
if (connectedSampleCount > 0) {
throw new EEntityCannotDelete(`${connectedSampleCount} connected Sample`)
}

await this.diagnosisRepository.deleteById(input.id)
for (const diagnosis of diagnoses) {
assertPermission(
ability,
AuthSubject.Diagnosis,
DiagnosisAction.Delete,
diagnosis,
)

return entity
const connectedSampleCount = await this.sampleRepository.count({
diagnosisId: diagnosis._id,
})
if (connectedSampleCount > 0) {
throw new EEntityCannotDelete(
`${connectedSampleCount} connected Sample`,
)
}

await this.diagnosisRepository.deleteById(diagnosis._id)
}
}
}
36 changes: 20 additions & 16 deletions apps/hcdc-access-service/src/app/doctor/use-case/delete.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AuthSubject, DoctorAction } from '@diut/hcdc'
import { AuthSubject, Doctor, DoctorAction } from '@diut/hcdc'
import { Inject, Injectable } from '@nestjs/common'
import { FilterQuery } from 'mongoose'
import { assertPermission } from 'src/app/auth/common'
import {
AUTH_CONTEXT_TOKEN,
Expand All @@ -10,7 +11,7 @@ import {
ISampleRepository,
SAMPLE_REPO_TOKEN,
} from 'src/domain'
import { DoctorAssertExistsUseCase } from './assert-exists'
import { DoctorSearchUseCase } from './search'

@Injectable()
export class DoctorDeleteUseCase {
Expand All @@ -21,25 +22,28 @@ export class DoctorDeleteUseCase {
private readonly doctorRepository: IDoctorRepository,
@Inject(SAMPLE_REPO_TOKEN)
private readonly sampleRepository: ISampleRepository,
private readonly doctorAssertExistsUseCase: DoctorAssertExistsUseCase,
private readonly doctorSearchUseCase: DoctorSearchUseCase,
) {}

async execute(input: { id: string }) {
const entity = await this.doctorAssertExistsUseCase.execute({
_id: input.id,
})
async execute(input: FilterQuery<Doctor>) {
const { ability } = this.authContext.getData()
assertPermission(ability, AuthSubject.Doctor, DoctorAction.Delete, entity)

const connectedSampleCount = await this.sampleRepository.count({
doctorId: input.id,
const { items: doctors } = await this.doctorSearchUseCase.execute({
filter: input,
})
if (connectedSampleCount > 0) {
throw new EEntityCannotDelete(`${connectedSampleCount} connected Sample`)
}

await this.doctorRepository.deleteById(input.id)
for (const doctor of doctors) {
assertPermission(ability, AuthSubject.Doctor, DoctorAction.Delete, doctor)

return entity
const connectedSampleCount = await this.sampleRepository.count({
doctorId: doctor._id,
})
if (connectedSampleCount > 0) {
throw new EEntityCannotDelete(
`${connectedSampleCount} connected Sample`,
)
}

await this.doctorRepository.deleteById(doctor._id)
}
}
}
44 changes: 23 additions & 21 deletions apps/hcdc-access-service/src/app/instrument/use-case/delete.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AuthSubject, InstrumentAction } from '@diut/hcdc'
import { AuthSubject, Instrument, InstrumentAction } from '@diut/hcdc'
import { Inject, Injectable } from '@nestjs/common'
import { FilterQuery } from 'mongoose'
import { assertPermission } from 'src/app/auth/common'
import {
AUTH_CONTEXT_TOKEN,
Expand All @@ -10,7 +11,7 @@ import {
ITestRepository,
TEST_REPO_TOKEN,
} from 'src/domain'
import { InstrumentAssertExistsUseCase } from './assert-exists'
import { InstrumentSearchUseCase } from './search'

@Injectable()
export class InstrumentDeleteUseCase {
Expand All @@ -21,30 +22,31 @@ export class InstrumentDeleteUseCase {
private readonly instrumentRepository: IInstrumentRepository,
@Inject(TEST_REPO_TOKEN)
private readonly testRepository: ITestRepository,
private readonly instrumentAssertExistsUseCase: InstrumentAssertExistsUseCase,
private readonly instrumentSearchUseCase: InstrumentSearchUseCase,
) {}

async execute(input: { id: string }) {
const entity = await this.instrumentAssertExistsUseCase.execute({
_id: input.id,
})
async execute(input: FilterQuery<Instrument>) {
const { ability } = this.authContext.getData()
assertPermission(
ability,
AuthSubject.Instrument,
InstrumentAction.Delete,
entity,
)

const connectedTestCount = await this.testRepository.count({
instrumentId: input.id,
const { items: instruments } = await this.instrumentSearchUseCase.execute({
filter: input,
})
if (connectedTestCount > 0) {
throw new EEntityCannotDelete(`${connectedTestCount} connected Test`)
}

await this.instrumentRepository.deleteById(input.id)
for (const instrument of instruments) {
assertPermission(
ability,
AuthSubject.Instrument,
InstrumentAction.Delete,
instrument,
)

return entity
const connectedTestCount = await this.testRepository.count({
instrumentId: instrument._id,
})
if (connectedTestCount > 0) {
throw new EEntityCannotDelete(`${connectedTestCount} connected Test`)
}

await this.instrumentRepository.deleteById(instrument._id)
}
}
}
Loading

0 comments on commit 3ff4602

Please sign in to comment.