-
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
31 changed files
with
491 additions
and
217 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
import { IQuery } from 'src/Application/IQuery'; | ||
import { UserRole } from 'src/Domain/User/User.entity'; | ||
|
||
export class GetSchoolsQuery implements IQuery { | ||
constructor( | ||
public readonly page: number, | ||
public readonly userId: string, | ||
public readonly userRole: UserRole, | ||
public readonly page: 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
5 changes: 5 additions & 0 deletions
5
api/src/Application/School/Query/Voucher/GetVoucherByCodeQuery.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 GetVoucherByCodeQuery implements IQuery { | ||
constructor(public readonly code: string) {} | ||
} |
53 changes: 53 additions & 0 deletions
53
api/src/Application/School/Query/Voucher/GetVoucherByCodeQueryHandler.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,53 @@ | ||
import { VoucherNotFoundException } from 'src/Domain/School/Exception/VoucherNotFoundException'; | ||
import { Voucher } from 'src/Domain/School/Voucher.entity'; | ||
import { VoucherRepository } from 'src/Infrastructure/School/Repository/VoucherRepository'; | ||
import { mock, instance, when, verify } from 'ts-mockito'; | ||
import { VoucherView } from '../../View/VoucherView'; | ||
import { GetVoucherByCodeQuery } from './GetVoucherByCodeQuery'; | ||
import { GetVoucherByCodeQueryHandler } from './GetVoucherByCodeQueryHandler'; | ||
|
||
describe('GetVoucherByCodeQueryHandler', () => { | ||
const query = new GetVoucherByCodeQuery('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2'); | ||
|
||
it('testGetVoucher', async () => { | ||
const voucherRepository = mock(VoucherRepository); | ||
const queryHandler = new GetVoucherByCodeQueryHandler(instance(voucherRepository)); | ||
const expectedResult = new VoucherView( | ||
'eb9e1d9b-dce2-48a9-B64F-f0872f3157d2', | ||
'xLKJS', | ||
'mathieu@fairness.coop' | ||
); | ||
|
||
const voucher = mock(Voucher); | ||
when(voucher.getId()).thenReturn('eb9e1d9b-dce2-48a9-B64F-f0872f3157d2'); | ||
when(voucher.getCode()).thenReturn('xLKJS'); | ||
when(voucher.getEmail()).thenReturn('mathieu@fairness.coop'); | ||
when( | ||
voucherRepository.findOneByCode('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') | ||
).thenResolve(instance(voucher)); | ||
|
||
expect(await queryHandler.execute(query)).toMatchObject(expectedResult); | ||
|
||
verify( | ||
voucherRepository.findOneByCode('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') | ||
).once(); | ||
}); | ||
|
||
it('testGetVoucherNotFound', async () => { | ||
const voucherRepository = mock(VoucherRepository); | ||
const queryHandler = new GetVoucherByCodeQueryHandler(instance(voucherRepository)); | ||
when( | ||
voucherRepository.findOneByCode('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') | ||
).thenResolve(null); | ||
|
||
try { | ||
expect(await queryHandler.execute(query)).toBeUndefined(); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(VoucherNotFoundException); | ||
expect(e.message).toBe('schools.errors.voucher_not_found'); | ||
verify( | ||
voucherRepository.findOneByCode('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') | ||
).once(); | ||
} | ||
}); | ||
}); |
28 changes: 28 additions & 0 deletions
28
api/src/Application/School/Query/Voucher/GetVoucherByCodeQueryHandler.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,28 @@ | ||
import { QueryHandler } from '@nestjs/cqrs'; | ||
import { Inject } from '@nestjs/common'; | ||
import { IVoucherRepository } from 'src/Domain/School/Repository/IVoucherRepository'; | ||
import { VoucherNotFoundException } from 'src/Domain/School/Exception/VoucherNotFoundException'; | ||
import { GetVoucherByCodeQuery } from './GetVoucherByCodeQuery'; | ||
import { VoucherView } from '../../View/VoucherView'; | ||
|
||
@QueryHandler(GetVoucherByCodeQuery) | ||
export class GetVoucherByCodeQueryHandler { | ||
constructor( | ||
@Inject('IVoucherRepository') | ||
private readonly voucherRepository: IVoucherRepository | ||
) {} | ||
|
||
public async execute({ code }: GetVoucherByCodeQuery): Promise<VoucherView> { | ||
const voucher = await this.voucherRepository.findOneByCode(code); | ||
|
||
if (!voucher) { | ||
throw new VoucherNotFoundException(); | ||
} | ||
|
||
return new VoucherView( | ||
voucher.getId(), | ||
voucher.getCode(), | ||
voucher.getEmail(), | ||
); | ||
} | ||
} |
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,7 @@ | ||
export class VoucherView { | ||
constructor( | ||
public readonly id: string, | ||
public readonly code: string, | ||
public readonly email: 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
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
57 changes: 57 additions & 0 deletions
57
api/src/Infrastructure/School/Action/Voucher/ConsumeVoucherAction.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,57 @@ | ||
import { | ||
Controller, | ||
Inject, | ||
Post, | ||
Body, | ||
BadRequestException, | ||
Param | ||
} from '@nestjs/common'; | ||
import { ApiTags, ApiOperation } from '@nestjs/swagger'; | ||
import { ICommandBus } from 'src/Application/ICommandBus'; | ||
import { CreateUserCommand } from 'src/Application/User/Command/CreateUserCommand'; | ||
import { UserView } from 'src/Application/User/View/UserView'; | ||
import { IQueryBus } from 'src/Application/IQueryBus'; | ||
import { GetVoucherByCodeQuery } from 'src/Application/School/Query/Voucher/GetVoucherByCodeQuery'; | ||
import { VoucherView } from 'src/Application/School/View/VoucherView'; | ||
import { ConsumeVoucherDTO } from '../../DTO/ConsumeVoucherDTO'; | ||
import { UserRole } from 'src/Domain/User/User.entity'; | ||
import { RemoveVoucherCommand } from 'src/Application/School/Command/Voucher/RemoveVoucherCommand'; | ||
|
||
@Controller('vouchers') | ||
@ApiTags('School voucher') | ||
export class ConsumeVoucherAction { | ||
constructor( | ||
@Inject('ICommandBus') | ||
private readonly commandBus: ICommandBus, | ||
@Inject('IQueryBus') | ||
private readonly queryBus: IQueryBus | ||
) {} | ||
|
||
@Post(':code/consume') | ||
@ApiOperation({ summary: 'Consume voucher' }) | ||
public async index( | ||
@Param() code: string, | ||
@Body() { firstName, lastName, password }: ConsumeVoucherDTO | ||
): Promise<UserView> { | ||
try { | ||
const voucher: VoucherView = await this.queryBus.execute(new GetVoucherByCodeQuery(code)); | ||
if (!voucher) { | ||
return; | ||
} | ||
|
||
await this.commandBus.execute( | ||
new CreateUserCommand( | ||
firstName, | ||
lastName, | ||
voucher.email, | ||
password, | ||
UserRole.DIRECTOR | ||
) | ||
); | ||
|
||
await this.commandBus.execute(new RemoveVoucherCommand(voucher.id)); | ||
} catch (e) { | ||
throw new BadRequestException(e.message); | ||
} | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
api/src/Infrastructure/School/DTO/ConsumeVoucherDTO.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,31 @@ | ||
import { ConsumeVoucherDTO } from './ConsumeVoucherDTO'; | ||
import { validate } from 'class-validator'; | ||
|
||
describe('ConsumeVoucherDTO', () => { | ||
it('testValidDTO', async () => { | ||
const dto = new ConsumeVoucherDTO(); | ||
|
||
dto.firstName = 'Mathieu'; | ||
dto.lastName = 'MARCHOIS'; | ||
dto.password = 'password'; | ||
|
||
const validation = await validate(dto); | ||
expect(validation).toHaveLength(0); | ||
}); | ||
|
||
it('testInvalidDTO', async () => { | ||
const dto = new ConsumeVoucherDTO(); | ||
|
||
const validation = await validate(dto); | ||
expect(validation).toHaveLength(3); | ||
expect(validation[0].constraints).toMatchObject({ | ||
isNotEmpty: 'firstName should not be empty' | ||
}); | ||
expect(validation[1].constraints).toMatchObject({ | ||
isNotEmpty: 'lastName should not be empty' | ||
}); | ||
expect(validation[2].constraints).toMatchObject({ | ||
isNotEmpty: 'password should not be empty' | ||
}); | ||
}); | ||
}); |
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,16 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsNotEmpty } from 'class-validator'; | ||
|
||
export class ConsumeVoucherDTO { | ||
@IsNotEmpty() | ||
@ApiProperty() | ||
public firstName: string; | ||
|
||
@IsNotEmpty() | ||
@ApiProperty() | ||
public lastName: string; | ||
|
||
@IsNotEmpty() | ||
@ApiProperty() | ||
public password: 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
Oops, something went wrong.