-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Timon Masberg <[email protected]> Co-authored-by: Jasper Herzberg <[email protected]>
- Loading branch information
1 parent
64926b2
commit 0ddc556
Showing
162 changed files
with
14,536 additions
and
7,068 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
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
103 changes: 103 additions & 0 deletions
103
libs/api/deployment/src/lib/core/command/reset-rescue-stations.command.spec.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,103 @@ | ||
import { createMock } from '@golevelup/ts-jest'; | ||
import { EventBus } from '@nestjs/cqrs'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
|
||
import { uowMockProvider } from '@kordis/api/test-helpers'; | ||
|
||
import { | ||
RescueStationDeploymentEntity, | ||
RescueStationStrength, | ||
} from '../entity/rescue-station-deployment.entity'; | ||
import { RescueStationsResetEvent } from '../event/rescue-stations-reset.event'; | ||
import { | ||
DEPLOYMENT_ASSIGNMENT_REPOSITORY, | ||
DeploymentAssignmentRepository, | ||
} from '../repository/deployment-assignment.repository'; | ||
import { | ||
RESCUE_STATION_DEPLOYMENT_REPOSITORY, | ||
RescueStationDeploymentRepository, | ||
} from '../repository/rescue-station-deployment.repository'; | ||
import { | ||
ResetRescueStationsCommand, | ||
ResetRescueStationsHandler, | ||
} from './reset-rescue-stations.command'; | ||
|
||
describe('ResetRescueStationsHandler', () => { | ||
let handler: ResetRescueStationsHandler; | ||
const mockRescueStationDeploymentRepository = | ||
createMock<RescueStationDeploymentRepository>(); | ||
const mockDeploymentAssignmentRepository = | ||
createMock<DeploymentAssignmentRepository>(); | ||
const mockEventBus = createMock<EventBus>(); | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
ResetRescueStationsHandler, | ||
{ | ||
provide: RESCUE_STATION_DEPLOYMENT_REPOSITORY, | ||
useValue: mockRescueStationDeploymentRepository, | ||
}, | ||
{ | ||
provide: DEPLOYMENT_ASSIGNMENT_REPOSITORY, | ||
useValue: mockDeploymentAssignmentRepository, | ||
}, | ||
{ | ||
provide: EventBus, | ||
useValue: mockEventBus, | ||
}, | ||
uowMockProvider(), | ||
], | ||
}).compile(); | ||
|
||
handler = module.get<ResetRescueStationsHandler>( | ||
ResetRescueStationsHandler, | ||
); | ||
}); | ||
|
||
it('should reset rescue stations and publish event', async () => { | ||
const orgId = 'orgId'; | ||
const rescueStations = [ | ||
{ id: 'rescueStationId1' }, | ||
{ id: 'rescueStationId2' }, | ||
] as RescueStationDeploymentEntity[]; | ||
mockRescueStationDeploymentRepository.findByOrgId.mockResolvedValue( | ||
rescueStations, | ||
); | ||
|
||
const command = new ResetRescueStationsCommand(orgId); | ||
await handler.execute(command); | ||
|
||
expect( | ||
mockRescueStationDeploymentRepository.findByOrgId, | ||
).toHaveBeenCalledWith(orgId, undefined, expect.anything()); | ||
|
||
expect( | ||
mockDeploymentAssignmentRepository.removeAssignmentsOfDeployments, | ||
).toHaveBeenCalledWith( | ||
orgId, | ||
['rescueStationId1', 'rescueStationId2'], | ||
expect.anything(), | ||
); | ||
const expectedStrength = new RescueStationStrength(); | ||
expectedStrength.leaders = | ||
expectedStrength.helpers = | ||
expectedStrength.subLeaders = | ||
0; | ||
|
||
expect( | ||
mockRescueStationDeploymentRepository.updateAll, | ||
).toHaveBeenCalledWith( | ||
orgId, | ||
{ | ||
signedIn: false, | ||
note: '', | ||
strength: expectedStrength, | ||
}, | ||
expect.anything(), | ||
); | ||
expect(mockEventBus.publish).toHaveBeenCalledWith( | ||
new RescueStationsResetEvent(orgId), | ||
); | ||
}); | ||
}); |
70 changes: 70 additions & 0 deletions
70
libs/api/deployment/src/lib/core/command/reset-rescue-stations.command.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,70 @@ | ||
import { Inject } from '@nestjs/common'; | ||
import { CommandHandler, EventBus, ICommandHandler } from '@nestjs/cqrs'; | ||
import { plainToInstance } from 'class-transformer'; | ||
|
||
import { UNIT_OF_WORK_SERVICE, UnitOfWorkService } from '@kordis/api/shared'; | ||
|
||
import { RescueStationStrength } from '../entity/rescue-station-deployment.entity'; | ||
import { RescueStationsResetEvent } from '../event/rescue-stations-reset.event'; | ||
import { | ||
DEPLOYMENT_ASSIGNMENT_REPOSITORY, | ||
DeploymentAssignmentRepository, | ||
} from '../repository/deployment-assignment.repository'; | ||
import { | ||
RESCUE_STATION_DEPLOYMENT_REPOSITORY, | ||
RescueStationDeploymentRepository, | ||
} from '../repository/rescue-station-deployment.repository'; | ||
|
||
export class ResetRescueStationsCommand { | ||
constructor(readonly orgId: string) {} | ||
} | ||
|
||
@CommandHandler(ResetRescueStationsCommand) | ||
export class ResetRescueStationsHandler | ||
implements ICommandHandler<ResetRescueStationsCommand> | ||
{ | ||
constructor( | ||
@Inject(RESCUE_STATION_DEPLOYMENT_REPOSITORY) | ||
private readonly rescueStationDeploymentRepository: RescueStationDeploymentRepository, | ||
@Inject(DEPLOYMENT_ASSIGNMENT_REPOSITORY) | ||
private readonly deploymentsAssignmentsRepository: DeploymentAssignmentRepository, | ||
@Inject(UNIT_OF_WORK_SERVICE) | ||
private readonly uow: UnitOfWorkService, | ||
private readonly eventBus: EventBus, | ||
) {} | ||
|
||
async execute({ orgId }: ResetRescueStationsCommand): Promise<void> { | ||
await this.uow.asTransaction(async (uow) => { | ||
const rescueStations = | ||
await this.rescueStationDeploymentRepository.findByOrgId( | ||
orgId, | ||
undefined, | ||
uow, | ||
); | ||
|
||
// remove assignments of all rescue stations | ||
await this.deploymentsAssignmentsRepository.removeAssignmentsOfDeployments( | ||
orgId, | ||
rescueStations.map((rescueStation) => rescueStation.id), | ||
uow, | ||
); | ||
|
||
// reset all rescue stations (sign out, reset note and strength) | ||
await this.rescueStationDeploymentRepository.updateAll( | ||
orgId, | ||
{ | ||
signedIn: false, | ||
note: '', | ||
strength: plainToInstance(RescueStationStrength, { | ||
leaders: 0, | ||
helpers: 0, | ||
subLeaders: 0, | ||
}), | ||
}, | ||
uow, | ||
); | ||
}, 3); | ||
|
||
this.eventBus.publish(new RescueStationsResetEvent(orgId)); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
libs/api/deployment/src/lib/core/command/update-rescue-station-note.command.spec.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,60 @@ | ||
import { createMock } from '@golevelup/ts-jest'; | ||
import { EventBus } from '@nestjs/cqrs'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
|
||
import { RescueStationNoteUpdatedEvent } from '../event/rescue-station-note-updated.event'; | ||
import { | ||
RESCUE_STATION_DEPLOYMENT_REPOSITORY, | ||
RescueStationDeploymentRepository, | ||
} from '../repository/rescue-station-deployment.repository'; | ||
import { | ||
UpdateRescueStationNoteCommand, | ||
UpdateRescueStationNoteHandler, | ||
} from './update-rescue-station-note.command'; | ||
|
||
describe('UpdateRescueStationNoteHandler', () => { | ||
let handler: UpdateRescueStationNoteHandler; | ||
const mockRescueStationDeploymentRepository = | ||
createMock<RescueStationDeploymentRepository>(); | ||
const mockEventBus = createMock<EventBus>(); | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
UpdateRescueStationNoteHandler, | ||
{ | ||
provide: RESCUE_STATION_DEPLOYMENT_REPOSITORY, | ||
useValue: mockRescueStationDeploymentRepository, | ||
}, | ||
{ | ||
provide: EventBus, | ||
useValue: mockEventBus, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
handler = module.get<UpdateRescueStationNoteHandler>( | ||
UpdateRescueStationNoteHandler, | ||
); | ||
}); | ||
|
||
it('should update the note and publish an event', async () => { | ||
const orgId = 'orgId'; | ||
const rescueStationId = 'rescueStationId'; | ||
const note = 'new note'; | ||
|
||
const command = new UpdateRescueStationNoteCommand( | ||
orgId, | ||
rescueStationId, | ||
note, | ||
); | ||
await handler.execute(command); | ||
|
||
expect( | ||
mockRescueStationDeploymentRepository.updateOne, | ||
).toHaveBeenCalledWith(orgId, rescueStationId, { note }); | ||
expect(mockEventBus.publish).toHaveBeenCalledWith( | ||
new RescueStationNoteUpdatedEvent(orgId, rescueStationId), | ||
); | ||
}); | ||
}); |
45 changes: 45 additions & 0 deletions
45
libs/api/deployment/src/lib/core/command/update-rescue-station-note.command.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,45 @@ | ||
import { Inject } from '@nestjs/common'; | ||
import { CommandHandler, EventBus, ICommandHandler } from '@nestjs/cqrs'; | ||
|
||
import { RescueStationNoteUpdatedEvent } from '../event/rescue-station-note-updated.event'; | ||
import { | ||
RESCUE_STATION_DEPLOYMENT_REPOSITORY, | ||
RescueStationDeploymentRepository, | ||
} from '../repository/rescue-station-deployment.repository'; | ||
|
||
export class UpdateRescueStationNoteCommand { | ||
constructor( | ||
readonly orgId: string, | ||
readonly rescueStationId: string, | ||
readonly note: string, | ||
) {} | ||
} | ||
|
||
@CommandHandler(UpdateRescueStationNoteCommand) | ||
export class UpdateRescueStationNoteHandler | ||
implements ICommandHandler<UpdateRescueStationNoteCommand> | ||
{ | ||
constructor( | ||
@Inject(RESCUE_STATION_DEPLOYMENT_REPOSITORY) | ||
private readonly rescueStationDeploymentRepository: RescueStationDeploymentRepository, | ||
private readonly eventBus: EventBus, | ||
) {} | ||
|
||
async execute({ | ||
orgId, | ||
rescueStationId, | ||
note, | ||
}: UpdateRescueStationNoteCommand): Promise<void> { | ||
await this.rescueStationDeploymentRepository.updateOne( | ||
orgId, | ||
rescueStationId, | ||
{ | ||
note, | ||
}, | ||
); | ||
|
||
this.eventBus.publish( | ||
new RescueStationNoteUpdatedEvent(orgId, rescueStationId), | ||
); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
libs/api/deployment/src/lib/core/event/rescue-station-note-updated.event.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 @@ | ||
export class RescueStationNoteUpdatedEvent { | ||
constructor( | ||
readonly orgId: string, | ||
readonly rescueStationId: string, | ||
) {} | ||
} |
3 changes: 3 additions & 0 deletions
3
libs/api/deployment/src/lib/core/event/rescue-stations-reset.event.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 class RescueStationsResetEvent { | ||
constructor(readonly orgId: string) {} | ||
} |
43 changes: 43 additions & 0 deletions
43
libs/api/deployment/src/lib/core/query/get-alert-group-assigned-units.query.spec.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,43 @@ | ||
import { createMock } from '@golevelup/ts-jest'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
|
||
import { | ||
UNIT_ASSIGNMENT_REPOSITORY, | ||
UnitAssignmentRepository, | ||
} from '../repository/unit-assignment.repository'; | ||
import { | ||
GetAlertGroupAssignedUnitsHandler, | ||
GetAlertGroupAssignedUnitsQuery, | ||
} from './get-alert-group-assigned-units.query'; | ||
|
||
describe('GetAlertGroupAssignedUnitsHandler', () => { | ||
let handler: GetAlertGroupAssignedUnitsHandler; | ||
const mockUnitAssignmentRepository = createMock<UnitAssignmentRepository>(); | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
GetAlertGroupAssignedUnitsHandler, | ||
{ | ||
provide: UNIT_ASSIGNMENT_REPOSITORY, | ||
useValue: mockUnitAssignmentRepository, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
handler = module.get<GetAlertGroupAssignedUnitsHandler>( | ||
GetAlertGroupAssignedUnitsHandler, | ||
); | ||
}); | ||
|
||
it('should get assigned units of alert group', async () => { | ||
const orgId = 'orgId'; | ||
const alertGroupId = 'alertGroupId'; | ||
const query = new GetAlertGroupAssignedUnitsQuery(orgId, alertGroupId); | ||
await handler.execute(query); | ||
|
||
expect( | ||
mockUnitAssignmentRepository.getUnitsOfAlertGroup, | ||
).toHaveBeenCalledWith(orgId, alertGroupId); | ||
}); | ||
}); |
Oops, something went wrong.