-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
65 changed files
with
1,215 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
apps/hcdc-access-service/src/domain/entity/instrument/auth.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import '@casl/mongoose' | ||
|
||
export enum InstrumentAction { | ||
Create = 'Create', | ||
Read = 'Read', | ||
Update = 'Update', | ||
Delete = 'Delete', | ||
} | ||
|
||
declare module '@casl/mongoose' { | ||
interface RecordTypes { | ||
Instrument: true | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
apps/hcdc-access-service/src/domain/entity/instrument/entity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { BaseEntity } from '../base-entity' | ||
import { Branch } from '../branch' | ||
|
||
export type Instrument = BaseEntity & { | ||
displayIndex: number | ||
name: string | ||
|
||
branchId: string | ||
branch?: Branch | null | ||
} |
17 changes: 17 additions & 0 deletions
17
apps/hcdc-access-service/src/domain/entity/instrument/example.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { exampleMongoObjectId } from '@diut/nest-core' | ||
|
||
import { EntityDataExample } from '../base-entity' | ||
import { Instrument } from './entity' | ||
|
||
export const exampleInstrument = { | ||
displayIndex: { | ||
example: 1, | ||
}, | ||
name: { | ||
example: 'máy xét nghiệm A', | ||
}, | ||
branchId: exampleMongoObjectId, | ||
branch: { | ||
required: false, | ||
}, | ||
} satisfies EntityDataExample<Instrument> |
3 changes: 3 additions & 0 deletions
3
apps/hcdc-access-service/src/domain/entity/instrument/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './entity' | ||
export * from './example' | ||
export * from './auth' |
14 changes: 14 additions & 0 deletions
14
apps/hcdc-access-service/src/domain/entity/sample-type/auth.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import '@casl/mongoose' | ||
|
||
export enum SampleTypeAction { | ||
Create = 'Create', | ||
Read = 'Read', | ||
Update = 'Update', | ||
Delete = 'Delete', | ||
} | ||
|
||
declare module '@casl/mongoose' { | ||
interface RecordTypes { | ||
SampleType: true | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
apps/hcdc-access-service/src/domain/entity/sample-type/entity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { BaseEntity } from '../base-entity' | ||
import { Branch } from '../branch' | ||
|
||
export type SampleType = BaseEntity & { | ||
displayIndex: number | ||
name: string | ||
|
||
branchId: string | ||
branch?: Branch | null | ||
} |
17 changes: 17 additions & 0 deletions
17
apps/hcdc-access-service/src/domain/entity/sample-type/example.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { exampleMongoObjectId } from '@diut/nest-core' | ||
|
||
import { EntityDataExample } from '../base-entity' | ||
import { SampleType } from './entity' | ||
|
||
export const exampleSampleType = { | ||
displayIndex: { | ||
example: 1, | ||
}, | ||
name: { | ||
example: 'máu', | ||
}, | ||
branchId: exampleMongoObjectId, | ||
branch: { | ||
required: false, | ||
}, | ||
} satisfies EntityDataExample<SampleType> |
3 changes: 3 additions & 0 deletions
3
apps/hcdc-access-service/src/domain/entity/sample-type/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './entity' | ||
export * from './example' | ||
export * from './auth' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
apps/hcdc-access-service/src/domain/interface/repository/instrument.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Instrument } from 'src/domain/entity' | ||
import { IRepository } from './interface' | ||
|
||
export const InstrumentRepositoryToken = Symbol('InstrumentRepository') | ||
|
||
export interface IInstrumentRepository extends IRepository<Instrument> {} |
6 changes: 6 additions & 0 deletions
6
apps/hcdc-access-service/src/domain/interface/repository/sample-type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { SampleType } from 'src/domain/entity' | ||
import { IRepository } from './interface' | ||
|
||
export const SampleTypeRepositoryToken = Symbol('SampleTypeRepository') | ||
|
||
export interface ISampleTypeRepository extends IRepository<SampleType> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
apps/hcdc-access-service/src/domain/use-case/instrument/assert-exists.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Inject, Injectable } from '@nestjs/common' | ||
|
||
import { Instrument } from 'src/domain/entity' | ||
import { EEntityNotFound } from 'src/domain/exception' | ||
import { | ||
InstrumentRepositoryToken, | ||
EntityFindOneOptions, | ||
IInstrumentRepository, | ||
} from 'src/domain/interface' | ||
|
||
@Injectable() | ||
export class InstrumentAssertExistsUseCase { | ||
constructor( | ||
@Inject(InstrumentRepositoryToken) | ||
private readonly instrumentRepository: IInstrumentRepository, | ||
) {} | ||
|
||
async execute(input: EntityFindOneOptions<Instrument>['filter']) { | ||
const rv = await this.instrumentRepository.findOne({ filter: input }) | ||
|
||
if (rv === null) { | ||
throw new EEntityNotFound(`Instrument ${JSON.stringify(input)}`) | ||
} | ||
|
||
return rv | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
apps/hcdc-access-service/src/domain/use-case/instrument/authorize-populates.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Inject, Injectable } from '@nestjs/common' | ||
|
||
import { AuthSubject, authorizePopulates } from 'src/domain/auth' | ||
import { BranchAction, Instrument } from 'src/domain/entity' | ||
import { EEntityPopulatePathUnknown } from 'src/domain/exception' | ||
import { | ||
AuthContextToken, | ||
EntityFindOneOptions, | ||
IAuthContext, | ||
} from 'src/domain/interface' | ||
|
||
@Injectable() | ||
export class InstrumentAuthorizePopulatesUseCase { | ||
constructor( | ||
@Inject(AuthContextToken) | ||
private readonly authContext: IAuthContext, | ||
) {} | ||
execute(input: EntityFindOneOptions<Instrument>['populates']) { | ||
const { ability } = this.authContext.getData() | ||
|
||
return authorizePopulates(ability, input, (path) => { | ||
switch (path) { | ||
case 'branch': | ||
return { subject: AuthSubject.Branch, action: BranchAction.Read } | ||
default: | ||
throw new EEntityPopulatePathUnknown(path) | ||
} | ||
}) | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
apps/hcdc-access-service/src/domain/use-case/instrument/create.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Inject, Injectable } from '@nestjs/common' | ||
|
||
import { | ||
AuthContextToken, | ||
InstrumentRepositoryToken, | ||
IAuthContext, | ||
IInstrumentRepository, | ||
} from 'src/domain/interface' | ||
import { Instrument, InstrumentAction, EntityData } from 'src/domain/entity' | ||
import { AuthSubject, assertPermission } from 'src/domain/auth' | ||
import { InstrumentValidateUseCase } from './validate' | ||
|
||
@Injectable() | ||
export class InstrumentCreateUseCase { | ||
constructor( | ||
@Inject(AuthContextToken) | ||
private readonly authContext: IAuthContext, | ||
@Inject(InstrumentRepositoryToken) | ||
private readonly instrumentRepository: IInstrumentRepository, | ||
private readonly instrumentValidateUseCase: InstrumentValidateUseCase, | ||
) {} | ||
|
||
async execute(input: EntityData<Instrument>) { | ||
const { ability } = this.authContext.getData() | ||
assertPermission( | ||
ability, | ||
AuthSubject.Instrument, | ||
InstrumentAction.Create, | ||
input, | ||
) | ||
await this.instrumentValidateUseCase.execute(input) | ||
|
||
const entity = await this.instrumentRepository.create(input) | ||
|
||
return entity | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
apps/hcdc-access-service/src/domain/use-case/instrument/delete.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Inject, Injectable } from '@nestjs/common' | ||
|
||
import { InstrumentAction } from 'src/domain/entity' | ||
import { AuthSubject, assertPermission } from 'src/domain/auth' | ||
import { | ||
AuthContextToken, | ||
InstrumentRepositoryToken, | ||
IAuthContext, | ||
IInstrumentRepository, | ||
} from 'src/domain/interface' | ||
import { InstrumentAssertExistsUseCase } from './assert-exists' | ||
|
||
@Injectable() | ||
export class InstrumentDeleteUseCase { | ||
constructor( | ||
@Inject(AuthContextToken) | ||
private readonly authContext: IAuthContext, | ||
@Inject(InstrumentRepositoryToken) | ||
private readonly instrumentRepository: IInstrumentRepository, | ||
private readonly instrumentAssertExistsUseCase: InstrumentAssertExistsUseCase, | ||
) {} | ||
|
||
async execute(input: { id: string }) { | ||
const entity = await this.instrumentAssertExistsUseCase.execute({ | ||
_id: input.id, | ||
}) | ||
const { ability } = this.authContext.getData() | ||
assertPermission( | ||
ability, | ||
AuthSubject.Instrument, | ||
InstrumentAction.Delete, | ||
entity, | ||
) | ||
|
||
await this.instrumentRepository.deleteById(input.id) | ||
|
||
return entity | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
apps/hcdc-access-service/src/domain/use-case/instrument/find-one.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Inject, Injectable } from '@nestjs/common' | ||
|
||
import { Instrument, InstrumentAction } from 'src/domain/entity' | ||
import { AuthSubject, assertPermission } from 'src/domain/auth' | ||
import { | ||
AuthContextToken, | ||
InstrumentRepositoryToken, | ||
EntityFindOneOptions, | ||
IAuthContext, | ||
IInstrumentRepository, | ||
} from 'src/domain/interface' | ||
import { InstrumentAuthorizePopulatesUseCase } from './authorize-populates' | ||
|
||
@Injectable() | ||
export class InstrumentFindOneUseCase { | ||
constructor( | ||
@Inject(InstrumentRepositoryToken) | ||
private readonly instrumentRepository: IInstrumentRepository, | ||
@Inject(AuthContextToken) | ||
private readonly authContext: IAuthContext, | ||
private readonly instrumentAuthorizePopulatesUseCase: InstrumentAuthorizePopulatesUseCase, | ||
) {} | ||
|
||
async execute(input: EntityFindOneOptions<Instrument>) { | ||
input.populates = this.instrumentAuthorizePopulatesUseCase.execute( | ||
input.populates, | ||
) | ||
const entity = await this.instrumentRepository.findOne(input) | ||
const { ability } = this.authContext.getData() | ||
assertPermission( | ||
ability, | ||
AuthSubject.Instrument, | ||
InstrumentAction.Read, | ||
entity, | ||
) | ||
|
||
return entity | ||
} | ||
} |
Oops, something went wrong.