-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Affiche le nb de congés payés restants
- Loading branch information
1 parent
42b3d96
commit b55d424
Showing
27 changed files
with
426 additions
and
14 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
src/Application/HumanResource/Leave/Query/GetLeaveRequestsOverviewQuery.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,5 @@ | ||
import { IQuery } from 'src/Application/IQuery'; | ||
|
||
export class GetLeaveRequestsOverviewQuery implements IQuery { | ||
constructor(public readonly date: Date, public readonly userId: string) {} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/Application/HumanResource/Leave/Query/GetLeaveRequestsOverviewQueryHandler.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,55 @@ | ||
import { mock, instance, when, verify } from 'ts-mockito'; | ||
import { DateUtilsAdapter } from 'src/Infrastructure/Adapter/DateUtilsAdapter'; | ||
import { LeaveRepository } from 'src/Infrastructure/HumanResource/Leave/Repository/LeaveRepository'; | ||
import { GetLeaveRequestsOverviewQueryHandler } from './GetLeaveRequestsOverviewQueryHandler'; | ||
import { ILeaveRequestsOverview } from 'src/Domain/HumanResource/Leave/ILeaveRequestsOverview'; | ||
import { GetLeaveRequestsOverviewQuery } from './GetLeaveRequestsOverviewQuery'; | ||
|
||
describe('GetLeaveRequestsOverviewQueryHandler', () => { | ||
let leaveRepository: LeaveRepository; | ||
let dateUtils: DateUtilsAdapter; | ||
let queryHandler: GetLeaveRequestsOverviewQueryHandler; | ||
|
||
beforeEach(() => { | ||
leaveRepository = mock(LeaveRepository); | ||
dateUtils = mock(DateUtilsAdapter); | ||
queryHandler = new GetLeaveRequestsOverviewQueryHandler( | ||
instance(dateUtils), | ||
instance(leaveRepository) | ||
); | ||
}); | ||
|
||
it('testGetLeaveRequestsOverview', async () => { | ||
const expectedResult: ILeaveRequestsOverview = { | ||
daysPerYear: 35, | ||
daysTaken: 6, | ||
daysRemaining: 29 | ||
}; | ||
|
||
const now = new Date('2024-07-05'); | ||
const startDate = new Date('2024-06-01'); | ||
const endDate = new Date('2025-05-31'); | ||
const userId = '06691061-b62f-7499-8000-a65d631224f1'; | ||
|
||
when(dateUtils.getLeaveReferencePeriodDays(now)).thenReturn([ | ||
startDate, | ||
endDate | ||
]); | ||
when(dateUtils.getWorkedDaysPerWeek()).thenReturn(5); | ||
when(dateUtils.getNumberOfPaidLeaveWeeks()).thenReturn(7); | ||
|
||
when( | ||
leaveRepository.getSumOfPaidLeaveDurationsBetween( | ||
startDate, | ||
endDate, | ||
userId | ||
) | ||
).thenReturn(new Promise(resolve => resolve(6 * 420))); | ||
|
||
when(dateUtils.getLeaveDurationAsDays(6 * 420)).thenReturn(6); | ||
|
||
expect( | ||
await queryHandler.execute(new GetLeaveRequestsOverviewQuery(now, userId)) | ||
).toMatchObject(expectedResult); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
src/Application/HumanResource/Leave/Query/GetLeaveRequestsOverviewQueryHandler.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,41 @@ | ||
import { Inject } from '@nestjs/common'; | ||
import { QueryHandler } from '@nestjs/cqrs'; | ||
import { GetLeaveRequestsOverviewQuery } from './GetLeaveRequestsOverviewQuery'; | ||
import { ILeaveRequestsOverview } from 'src/Domain/HumanResource/Leave/ILeaveRequestsOverview'; | ||
import { IDateUtils } from 'src/Application/IDateUtils'; | ||
import { ILeaveRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRepository'; | ||
|
||
@QueryHandler(GetLeaveRequestsOverviewQuery) | ||
export class GetLeaveRequestsOverviewQueryHandler { | ||
constructor( | ||
@Inject('IDateUtils') | ||
private readonly dateUtils: IDateUtils, | ||
@Inject('ILeaveRepository') | ||
private readonly leaveRepository: ILeaveRepository | ||
) {} | ||
|
||
public async execute( | ||
query: GetLeaveRequestsOverviewQuery | ||
): Promise<ILeaveRequestsOverview> { | ||
const [startDate, endDate] = this.dateUtils.getLeaveReferencePeriodDays( | ||
query.date | ||
); | ||
|
||
const numDaysPerWeek = this.dateUtils.getWorkedDaysPerWeek(); | ||
const numWeeks = this.dateUtils.getNumberOfPaidLeaveWeeks(); | ||
const daysPerYear = numWeeks * numDaysPerWeek; | ||
const totalDurationTaken = await this.leaveRepository.getSumOfPaidLeaveDurationsBetween( | ||
startDate, | ||
endDate, | ||
query.userId | ||
); | ||
const daysTaken = this.dateUtils.getLeaveDurationAsDays(totalDurationTaken); | ||
const daysRemaining = daysPerYear - daysTaken; | ||
|
||
return { | ||
daysPerYear, | ||
daysTaken, | ||
daysRemaining | ||
}; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface ILeaveRequestsOverview { | ||
daysPerYear: number; | ||
daysTaken: number; | ||
daysRemaining: number; | ||
} |
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
8 changes: 8 additions & 0 deletions
8
src/Infrastructure/HumanResource/Leave/DTO/ListLeaveRequestsControllerDTO.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,8 @@ | ||
import { IsUUID, IsOptional } from 'class-validator'; | ||
import { PaginationDTO } from 'src/Infrastructure/Common/DTO/PaginationDTO'; | ||
|
||
export class ListLeaveRequestsControllerDTO extends PaginationDTO { | ||
@IsUUID() | ||
@IsOptional() | ||
public userId?: string; | ||
} |
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
55 changes: 55 additions & 0 deletions
55
src/Infrastructure/HumanResource/Leave/Table/LeaveRequestOverviewTableFactory.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,55 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { IQueryBus } from '@nestjs/cqrs'; | ||
import { GetUsersQuery } from 'src/Application/HumanResource/User/Query/GetUsersQuery'; | ||
import { UserView } from 'src/Application/HumanResource/User/View/UserView'; | ||
import { ILeaveRequestsOverview } from 'src/Domain/HumanResource/Leave/ILeaveRequestsOverview'; | ||
import { Table } from 'src/Infrastructure/Tables'; | ||
import { HtmlColumn } from 'src/Infrastructure/Tables/HtmlColumn'; | ||
import { RowFactory } from 'src/Infrastructure/Tables/RowFactory'; | ||
import { ITemplates } from 'src/Infrastructure/Templates/ITemplates'; | ||
|
||
@Injectable() | ||
export class LeaveRequestsOverviewTableFactory { | ||
constructor( | ||
@Inject('IQueryBus') | ||
private queryBus: IQueryBus, | ||
private rowFactory: RowFactory, | ||
@Inject('ITemplates') | ||
private templates: ITemplates | ||
) {} | ||
|
||
public async create( | ||
overview: ILeaveRequestsOverview, | ||
userId: string | ||
): Promise<Table> { | ||
const users: UserView[] = await this.queryBus.execute( | ||
new GetUsersQuery(false, true) | ||
); | ||
|
||
const columns = [ | ||
'leaves-user', | ||
new HtmlColumn( | ||
'leaves-overview-daysRemaining', | ||
this.templates.render( | ||
'pages/leave_requests/overview/days_remaining_column_header.njk', | ||
{ | ||
daysPerYear: overview.daysPerYear | ||
} | ||
) | ||
) | ||
]; | ||
|
||
const row = this.rowFactory | ||
.createBuilder() | ||
.template('pages/leave_requests/overview/user_cell.njk', { | ||
users, | ||
userId | ||
}) | ||
.template('pages/leave_requests/overview/days_remaining_cell.njk', { | ||
daysRemaining: overview.daysRemaining | ||
}) | ||
.build(); | ||
|
||
return new Table(columns, [row]); | ||
} | ||
} |
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
Oops, something went wrong.