-
Notifications
You must be signed in to change notification settings - Fork 1
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
16 changed files
with
351 additions
and
9 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
15 changes: 15 additions & 0 deletions
15
src/modules/authentication/api/person-time-limit-info.reponse.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,15 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { TimeLimitOccasion } from '../../person/domain/time-limit-occasion.enums.js'; | ||
|
||
export class PersonTimeLimitInfoResponse { | ||
@ApiProperty() | ||
public occasion: TimeLimitOccasion; | ||
|
||
@ApiProperty() | ||
public deadline: string; | ||
|
||
public constructor(occasion: TimeLimitOccasion, deadline: Date) { | ||
this.occasion = occasion; | ||
this.deadline = deadline.toISOString(); | ||
} | ||
} |
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
120 changes: 120 additions & 0 deletions
120
src/modules/person/domain/person-time-limit-info.service.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,120 @@ | ||
import { DeepMocked, createMock } from '@golevelup/ts-jest'; | ||
import { TestingModule, Test } from '@nestjs/testing'; | ||
import { PersonRepository } from '../../person/persistence/person.repository.js'; | ||
import PersonTimeLimitService from './person-time-limit-info.service.js'; | ||
import { DBiamPersonenkontextService } from '../../personenkontext/domain/dbiam-personenkontext.service.js'; | ||
import { Person } from '../../person/domain/person.js'; | ||
import { DoFactory } from '../../../../test/utils/do-factory.js'; | ||
import { TimeLimitOccasion } from '../domain/time-limit-occasion.enums.js'; | ||
import { Personenkontext } from '../../personenkontext/domain/personenkontext.js'; | ||
import { PersonTimeLimitInfo } from './person-time-limit-info.js'; | ||
import { KOPERS_DEADLINE_IN_DAYS } from './person-time-limit.js'; | ||
|
||
describe('PersonTimeLimitService', () => { | ||
let module: TestingModule; | ||
let sut: PersonTimeLimitService; | ||
let personRepoMock: DeepMocked<PersonRepository>; | ||
let dBiamPersonenkontextServiceMock: DeepMocked<DBiamPersonenkontextService>; | ||
|
||
beforeAll(async () => { | ||
module = await Test.createTestingModule({ | ||
providers: [ | ||
PersonTimeLimitService, | ||
{ | ||
provide: PersonRepository, | ||
useValue: createMock<PersonRepository>(), | ||
}, | ||
{ | ||
provide: DBiamPersonenkontextService, | ||
useValue: createMock<DBiamPersonenkontextService>(), | ||
}, | ||
], | ||
}).compile(); | ||
sut = module.get(PersonTimeLimitService); | ||
personRepoMock = module.get(PersonRepository); | ||
dBiamPersonenkontextServiceMock = module.get(DBiamPersonenkontextService); | ||
}); | ||
|
||
afterAll(async () => { | ||
await module.close(); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(sut).toBeDefined(); | ||
}); | ||
|
||
describe('getPersonTimeLimitInfo', () => { | ||
it('should return PersonTimeLimitInfo array', async () => { | ||
const person: Person<true> = DoFactory.createPerson(true); | ||
person.personalnummer = undefined; | ||
personRepoMock.findById.mockResolvedValue(person); | ||
|
||
const pesonenkontext: Personenkontext<true> = DoFactory.createPersonenkontext(true); | ||
dBiamPersonenkontextServiceMock.getKopersPersonenkontexte.mockResolvedValue([pesonenkontext]); | ||
|
||
const result: PersonTimeLimitInfo[] = await sut.getPersonTimeLimitInfo(person.id); | ||
|
||
const expectedDeadline: Date = new Date(pesonenkontext.createdAt); | ||
expectedDeadline.setDate(expectedDeadline.getDate() + KOPERS_DEADLINE_IN_DAYS); | ||
|
||
expect(result).toEqual<PersonTimeLimitInfo[]>([ | ||
{ | ||
occasion: TimeLimitOccasion.KOPERS, | ||
deadline: expectedDeadline, | ||
}, | ||
]); | ||
}); | ||
|
||
it.each([ | ||
{ | ||
personenkontextDates: ['2021-01-02', '2021-01-01'], | ||
expectedDate: '2021-01-01', | ||
}, | ||
{ | ||
personenkontextDates: ['2021-01-01', '2021-01-02'], | ||
expectedDate: '2021-01-01', | ||
}, | ||
])( | ||
'should return PersonTimeLimitInfo array with earliest Koperslock', | ||
async ({ | ||
personenkontextDates, | ||
expectedDate, | ||
}: { | ||
personenkontextDates: string[]; | ||
expectedDate: string; | ||
}) => { | ||
const person: Person<true> = DoFactory.createPerson(true); | ||
person.personalnummer = undefined; | ||
personRepoMock.findById.mockResolvedValue(person); | ||
|
||
const personenkontexte: Personenkontext<true>[] = personenkontextDates.map((date: string) => | ||
DoFactory.createPersonenkontext(true, { createdAt: new Date(date) }), | ||
); | ||
dBiamPersonenkontextServiceMock.getKopersPersonenkontexte.mockResolvedValue(personenkontexte); | ||
|
||
const result: PersonTimeLimitInfo[] = await sut.getPersonTimeLimitInfo(person.id); | ||
|
||
const expectedDeadline: Date = new Date(expectedDate); | ||
expectedDeadline.setDate(expectedDeadline.getDate() + KOPERS_DEADLINE_IN_DAYS); | ||
|
||
expect(result).toEqual<PersonTimeLimitInfo[]>([ | ||
{ | ||
occasion: TimeLimitOccasion.KOPERS, | ||
deadline: expectedDeadline, | ||
}, | ||
]); | ||
}, | ||
); | ||
|
||
it('should return empty array when person isnt found ', async () => { | ||
personRepoMock.findById.mockResolvedValue(null); | ||
const result: PersonTimeLimitInfo[] = await sut.getPersonTimeLimitInfo(''); | ||
|
||
expect(result).toEqual<PersonTimeLimitInfo[]>([]); | ||
}); | ||
}); | ||
}); |
39 changes: 39 additions & 0 deletions
39
src/modules/person/domain/person-time-limit-info.service.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 { Injectable } from '@nestjs/common'; | ||
import { Personenkontext } from '../../personenkontext/domain/personenkontext.js'; | ||
import { DBiamPersonenkontextService } from '../../personenkontext/domain/dbiam-personenkontext.service.js'; | ||
import { PersonRepository } from '../../person/persistence/person.repository.js'; | ||
import { Person } from '../../person/domain/person.js'; | ||
import { PersonTimeLimitInfo } from './person-time-limit-info.js'; | ||
import { TimeLimitOccasion } from './time-limit-occasion.enums.js'; | ||
import { KOPERS_DEADLINE_IN_DAYS } from './person-time-limit.js'; | ||
|
||
@Injectable() | ||
export default class PersonTimeLimitService { | ||
public constructor( | ||
private readonly personRepository: PersonRepository, | ||
private readonly dBiamPersonenkontextService: DBiamPersonenkontextService, | ||
) {} | ||
|
||
public async getPersonTimeLimitInfo(personId: string): Promise<PersonTimeLimitInfo[]> { | ||
const person: Option<Person<true>> = await this.personRepository.findById(personId); | ||
if (!person) { | ||
return []; | ||
} | ||
const lockInfos: PersonTimeLimitInfo[] = []; | ||
if (!person.personalnummer) { | ||
const kopersKontexte: Personenkontext<true>[] = | ||
await this.dBiamPersonenkontextService.getKopersPersonenkontexte(person.id); | ||
if (kopersKontexte.length > 0) { | ||
const earliestKopersKontext: Personenkontext<true> = kopersKontexte.reduce( | ||
(prev: Personenkontext<true>, current: Personenkontext<true>) => | ||
prev.createdAt < current.createdAt ? prev : current, | ||
kopersKontexte[0]!, | ||
); | ||
const kopersdeadline: Date = new Date(earliestKopersKontext.createdAt); | ||
kopersdeadline.setDate(kopersdeadline.getDate() + KOPERS_DEADLINE_IN_DAYS); | ||
lockInfos.push(new PersonTimeLimitInfo(TimeLimitOccasion.KOPERS, kopersdeadline)); | ||
} | ||
} | ||
return lockInfos; | ||
} | ||
} |
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 { TimeLimitOccasion } from './time-limit-occasion.enums.js'; | ||
|
||
export class PersonTimeLimitInfo { | ||
public constructor( | ||
public readonly occasion: TimeLimitOccasion, | ||
public readonly deadline: Date, | ||
) {} | ||
} |
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 @@ | ||
export const KOPERS_DEADLINE_IN_DAYS: number = 56; |
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 enum TimeLimitOccasion { | ||
KOPERS = 'KOPERS', | ||
} |
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.