-
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
62 changed files
with
1,146 additions
and
276 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import {MigrationInterface, QueryRunner} from "typeorm"; | ||
|
||
export class Voucher1617438352492 implements MigrationInterface { | ||
name = 'Voucher1617438352492' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`CREATE TABLE "voucher" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "code" character varying NOT NULL, "email" character varying NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, "schoolId" uuid NOT NULL, CONSTRAINT "PK_677ae75f380e81c2f103a57ffaf" PRIMARY KEY ("id"))`); | ||
await queryRunner.query(`ALTER TABLE "voucher" ADD CONSTRAINT "FK_c837a392c774db4d54bc8d8484c" FOREIGN KEY ("schoolId") REFERENCES "school"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "voucher" DROP CONSTRAINT "FK_c837a392c774db4d54bc8d8484c"`); | ||
await queryRunner.query(`DROP TABLE "voucher"`); | ||
} | ||
|
||
} |
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 interface ICodeGenerator { | ||
generate(): string; | ||
} |
9 changes: 9 additions & 0 deletions
9
api/src/Application/School/Command/User/AddUserToSchoolCommand.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,9 @@ | ||
import { ICommand } from 'src/Application/ICommand'; | ||
import { User } from 'src/Domain/User/User.entity'; | ||
|
||
export class AddUserToSchoolCommand implements ICommand { | ||
constructor( | ||
public readonly user: User, | ||
public readonly schoolId: string | ||
) {} | ||
} |
93 changes: 93 additions & 0 deletions
93
api/src/Application/School/Command/User/AddUserToSchoolCommandHandler.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,93 @@ | ||
import { mock, instance, when, verify, anything, deepEqual } from 'ts-mockito'; | ||
import { School } from 'src/Domain/School/School.entity'; | ||
import { AddUserToSchoolCommandHandler } from 'src/Application/School/Command/User/AddUserToSchoolCommandHandler'; | ||
import { AddUserToSchoolCommand } from 'src/Application/School/Command/User/AddUserToSchoolCommand'; | ||
import { SchoolRepository } from 'src/Infrastructure/School/Repository/SchoolRepository'; | ||
import { User, UserRole } from 'src/Domain/User/User.entity'; | ||
import { SchoolNotFoundException } from 'src/Domain/School/Exception/SchoolNotFoundException'; | ||
import { SchoolUserRepository } from 'src/Infrastructure/School/Repository/SchoolUserRepository'; | ||
import { SchoolUser } from 'src/Domain/School/SchoolUser.entity'; | ||
import { IsUserAlreadyAddedToSchool } from 'src/Domain/User/Specification/IsUserAlreadyAddedToSchool'; | ||
import { UserAlreadyAddedToSchoolException } from 'src/Domain/User/Exception/UserAlreadyAddedToSchoolException'; | ||
|
||
describe('AddUserToSchoolCommandHandler', () => { | ||
let schoolRepository: SchoolRepository; | ||
let schoolUserRepository: SchoolUserRepository; | ||
let isUserAlreadyAddedToSchool: IsUserAlreadyAddedToSchool; | ||
let school: School; | ||
let handler: AddUserToSchoolCommandHandler; | ||
|
||
const user = mock(User); | ||
const createdSchoolUser = mock(SchoolUser); | ||
|
||
const command = new AddUserToSchoolCommand( | ||
instance(user), | ||
'fcf9a99f-0c7b-45ca-b68a-bfd79d73a49f', | ||
); | ||
|
||
beforeEach(() => { | ||
schoolRepository = mock(SchoolRepository); | ||
schoolUserRepository = mock(SchoolUserRepository); | ||
isUserAlreadyAddedToSchool = mock(IsUserAlreadyAddedToSchool); | ||
school = mock(School); | ||
|
||
handler = new AddUserToSchoolCommandHandler( | ||
instance(schoolRepository), | ||
instance(schoolUserRepository), | ||
instance(isUserAlreadyAddedToSchool), | ||
); | ||
}); | ||
|
||
it('testDirectorSuccessfullyAdded', async () => { | ||
when(createdSchoolUser.getId()).thenReturn('0b1d9435-4258-42f1-882d-4f314f8fb57d'); | ||
when(user.getRole()).thenReturn(UserRole.DIRECTOR); | ||
when(schoolRepository.findOneById('fcf9a99f-0c7b-45ca-b68a-bfd79d73a49f')) | ||
.thenResolve(instance(school)); | ||
when(isUserAlreadyAddedToSchool.isSatisfiedBy(instance(school), instance(user))) | ||
.thenResolve(false); | ||
when(schoolUserRepository.save( | ||
deepEqual(new SchoolUser(instance(school), instance(user))) | ||
)).thenResolve(instance(createdSchoolUser)); | ||
|
||
expect(await handler.execute(command)).toBe( | ||
'0b1d9435-4258-42f1-882d-4f314f8fb57d' | ||
); | ||
|
||
verify(schoolRepository.findOneById('fcf9a99f-0c7b-45ca-b68a-bfd79d73a49f')).once(); | ||
verify(isUserAlreadyAddedToSchool.isSatisfiedBy(instance(school), instance(user))).once(); | ||
verify(schoolUserRepository.save( | ||
deepEqual(new SchoolUser(instance(school), instance(user))) | ||
)).once(); | ||
}); | ||
|
||
it('testSchoolNotFound', async () => { | ||
when(schoolRepository.findOneById('fcf9a99f-0c7b-45ca-b68a-bfd79d73a49f')) | ||
.thenResolve(null); | ||
try { | ||
expect(await handler.execute(command)).toBeUndefined(); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(SchoolNotFoundException); | ||
expect(e.message).toBe('schools.errors.not_found'); | ||
verify(schoolRepository.findOneById('fcf9a99f-0c7b-45ca-b68a-bfd79d73a49f')).once(); | ||
verify(schoolUserRepository.save(anything())).never(); | ||
verify(isUserAlreadyAddedToSchool.isSatisfiedBy(anything(), anything())).never(); | ||
} | ||
}); | ||
|
||
it('testUserAlreadyAdded', async () => { | ||
when(schoolRepository.findOneById('fcf9a99f-0c7b-45ca-b68a-bfd79d73a49f')) | ||
.thenResolve(instance(school)); | ||
when(isUserAlreadyAddedToSchool.isSatisfiedBy(instance(school), instance(user))) | ||
.thenResolve(true); | ||
|
||
try { | ||
expect(await handler.execute(command)).toBeUndefined(); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(UserAlreadyAddedToSchoolException); | ||
expect(e.message).toBe('users.errors.already_assigned_to_school'); | ||
verify(schoolRepository.findOneById('fcf9a99f-0c7b-45ca-b68a-bfd79d73a49f')).once(); | ||
verify(schoolUserRepository.save(anything())).never(); | ||
verify(isUserAlreadyAddedToSchool.isSatisfiedBy(instance(school), instance(user))).once(); | ||
} | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
api/src/Application/School/Command/User/AddUserToSchoolCommandHandler.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 { CommandHandler } from '@nestjs/cqrs'; | ||
import { SchoolNotFoundException } from 'src/Domain/School/Exception/SchoolNotFoundException'; | ||
import { ISchoolRepository } from 'src/Domain/School/Repository/ISchoolRepository'; | ||
import { ISchoolUserRepository } from 'src/Domain/School/Repository/ISchoolUserRepository'; | ||
import { SchoolUser } from 'src/Domain/School/SchoolUser.entity'; | ||
import { UserAlreadyAddedToSchoolException } from 'src/Domain/User/Exception/UserAlreadyAddedToSchoolException'; | ||
import { IsUserAlreadyAddedToSchool } from 'src/Domain/User/Specification/IsUserAlreadyAddedToSchool'; | ||
import { AddUserToSchoolCommand } from './AddUserToSchoolCommand'; | ||
|
||
@CommandHandler(AddUserToSchoolCommand) | ||
export class AddUserToSchoolCommandHandler { | ||
constructor( | ||
@Inject('ISchoolRepository') | ||
private readonly schoolRepository: ISchoolRepository, | ||
@Inject('ISchoolUserRepository') | ||
private readonly schoolUserRepository: ISchoolUserRepository, | ||
private readonly isUserAlreadyAddedToSchool: IsUserAlreadyAddedToSchool, | ||
) {} | ||
|
||
public async execute(command: AddUserToSchoolCommand): Promise<string> { | ||
const { schoolId, user } = command; | ||
|
||
const school = await this.schoolRepository.findOneById(schoolId); | ||
if (!school) { | ||
throw new SchoolNotFoundException(); | ||
} | ||
|
||
if (true === (await this.isUserAlreadyAddedToSchool.isSatisfiedBy(school, user))) { | ||
throw new UserAlreadyAddedToSchoolException(); | ||
} | ||
|
||
const schoolUser = await this.schoolUserRepository.save( | ||
new SchoolUser(school, user) | ||
); | ||
|
||
// @todo : send email | ||
|
||
return schoolUser.getId(); | ||
} | ||
} |
8 changes: 0 additions & 8 deletions
8
api/src/Application/School/Command/User/AssignUserToSchoolCommand.ts
This file was deleted.
Oops, something went wrong.
124 changes: 0 additions & 124 deletions
124
api/src/Application/School/Command/User/AssignUserToSchoolCommandHandler.spec.ts
This file was deleted.
Oops, something went wrong.
48 changes: 0 additions & 48 deletions
48
api/src/Application/School/Command/User/AssignUserToSchoolCommandHandler.ts
This file was deleted.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
api/src/Application/School/Command/User/RemoveSchoolUserCommand.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 { ICommand } from 'src/Application/ICommand'; | ||
|
||
export class RemoveSchoolUserCommand implements ICommand { | ||
constructor(public readonly id: string) {} | ||
} |
Oops, something went wrong.