Skip to content

Commit

Permalink
feat: individual and group closing dates (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarchois authored May 30, 2021
1 parent c760687 commit 3b6dad8
Show file tree
Hide file tree
Showing 27 changed files with 134 additions and 65 deletions.
18 changes: 18 additions & 0 deletions api/migrations/1622383660612-ShootingDates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {MigrationInterface, QueryRunner} from "typeorm";

export class ShootingDates1622383660612 implements MigrationInterface {
name = 'ShootingDates1622383660612'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "shooting" DROP COLUMN "closingDate"`);
await queryRunner.query(`ALTER TABLE "shooting" ADD "groupClosingDate" date NOT NULL`);
await queryRunner.query(`ALTER TABLE "shooting" ADD "individualClosingDate" date NOT NULL`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "shooting" DROP COLUMN "individualClosingDate"`);
await queryRunner.query(`ALTER TABLE "shooting" DROP COLUMN "groupClosingDate"`);
await queryRunner.query(`ALTER TABLE "shooting" ADD "closingDate" date NOT NULL`);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export class CreateShootingCommand implements ICommand {
constructor(
public readonly name: string,
public readonly shootingDate: Date,
public readonly closingDate: Date,
public readonly groupClosingDate: Date,
public readonly individualClosingDate: Date,
public readonly schoolId: string,
public readonly notice: string,
) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('CreateShootingCommandHandler', () => {
'Prise de vue début année',
new Date('2021-04-18'),
new Date('2021-09-01'),
new Date('2021-12-01'),
'553e2b3c-eb11-42b1-8f76-903add071ca7',
'Message notice'
);
Expand Down Expand Up @@ -47,6 +48,7 @@ describe('CreateShootingCommandHandler', () => {
'Prise de vue début année',
new Date('2021-04-18'),
new Date('2021-09-01'),
new Date('2021-12-01'),
ShootingStatus.DISABLED,
instance(school),
'Message notice'
Expand All @@ -66,6 +68,7 @@ describe('CreateShootingCommandHandler', () => {
'Prise de vue début année',
new Date('2021-04-18'),
new Date('2021-09-01'),
new Date('2021-12-01'),
ShootingStatus.DISABLED,
instance(school),
'Message notice'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class CreateShootingCommandHandler {
) {}

public async execute(command: CreateShootingCommand): Promise<string> {
const { name, closingDate, schoolId, shootingDate, notice } = command;
const { name, groupClosingDate, individualClosingDate, schoolId, shootingDate, notice } = command;

const school = await this.schoolRepository.findOneById(schoolId);
if (!school) {
Expand All @@ -27,7 +27,8 @@ export class CreateShootingCommandHandler {
new Shooting(
name,
shootingDate,
closingDate,
groupClosingDate,
individualClosingDate,
ShootingStatus.DISABLED,
school,
notice
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export class UpdateShootingCommand implements ICommand {
public readonly id: string,
public readonly name: string,
public readonly shootingDate: Date,
public readonly closingDate: Date,
public readonly groupClosingDate: Date,
public readonly individualClosingDate: Date,
public readonly notice: string,
) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('UpdateShootingCommandHandler', () => {
'Prise de vue début année',
new Date('2021-04-18'),
new Date('2021-09-01'),
new Date('2021-12-01'),
'Message notice'
);

Expand Down Expand Up @@ -49,6 +50,7 @@ describe('UpdateShootingCommandHandler', () => {
'Prise de vue début année',
deepEqual(new Date('2021-04-18')),
deepEqual(new Date('2021-09-01')),
deepEqual(new Date('2021-12-01')),
'Message notice'
)).once();
verify(shootingRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad')).once();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ export class UpdateShootingCommandHandler {
) { }

public async execute(command: UpdateShootingCommand): Promise<string> {
const { name, closingDate, shootingDate, notice, id } = command;
const { name, groupClosingDate, individualClosingDate, shootingDate, notice, id } = command;

const shooting = await this.shootingRepository.findOneById(id);
if (!shooting) {
throw new ShootingNotFoundException();
}

shooting.update(name, shootingDate, closingDate, notice);
shooting.update(name, shootingDate, groupClosingDate, individualClosingDate, notice);
await this.shootingRepository.save(shooting);

return shooting.getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ describe('GetShootingByIdQueryHandler', () => {
ShootingStatus.DISABLED,
new Date('2021-04-18'),
new Date('2021-09-01'),
new Date('2021-12-01'),
'Notice'
);

const shooting = mock(Shooting);
when(shooting.getId()).thenReturn('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2');
when(shooting.getName()).thenReturn('Prise de vue fin année');
when(shooting.getClosingDate()).thenReturn(new Date('2021-09-01'));
when(shooting.getGroupClosingDate()).thenReturn(new Date('2021-09-01'));
when(shooting.getIndividualClosingDate()).thenReturn(new Date('2021-12-01'));
when(shooting.getShootingDate()).thenReturn(new Date('2021-04-18'));
when(shooting.getNotice()).thenReturn('Notice');
when(shooting.getStatus()).thenReturn(ShootingStatus.DISABLED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export class GetShootingByIdQueryHandler {
shooting.getName(),
shooting.getStatus(),
shooting.getShootingDate(),
shooting.getClosingDate(),
shooting.getGroupClosingDate(),
shooting.getIndividualClosingDate(),
shooting.getNotice()
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { mock, instance, when, verify } from 'ts-mockito';
import { Shooting, ShootingStatus } from 'src/Domain/School/Shooting.entity';
import { ShootingView } from '../../View/ShootingView';
import { GetShootingsBySchoolQuery } from './GetShootingsBySchoolQuery';
import { GetShootingsBySchoolQueryHandler } from './GetShootingsBySchoolQueryHandler';
import { ShootingRepository } from 'src/Infrastructure/School/Repository/ShootingRepository';
import { School } from 'src/Domain/School/School.entity';
import { ShootingSummaryView } from '../../View/ShootingSummaryView';

describe('GetShootingBySchoolQueryHandler', () => {
it('testGetShootings', async () => {
Expand All @@ -16,8 +16,7 @@ describe('GetShootingBySchoolQueryHandler', () => {
'4de2ffc4-e835-44c8-95b7-17c171c09873'
);
when(shooting.getName()).thenReturn('Prise de vue fin année');
when(shooting.getClosingDate()).thenReturn(new Date('2021-09-01'));
when(shooting.getShootingDate()).thenReturn(new Date('2021-04-18'));
when(shooting.getShootingDate()).thenReturn(new Date('2021-09-01'));
when(shooting.getSchool()).thenReturn(instance(school));
when(shooting.getStatus()).thenReturn(ShootingStatus.DISABLED);

Expand All @@ -32,11 +31,10 @@ describe('GetShootingBySchoolQueryHandler', () => {
);

const expectedResult = [
new ShootingView(
new ShootingSummaryView(
'4de2ffc4-e835-44c8-95b7-17c171c09873',
'Prise de vue fin année',
ShootingStatus.DISABLED,
new Date('2021-04-18'),
new Date('2021-09-01')
)
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { QueryHandler } from '@nestjs/cqrs';
import { Inject } from '@nestjs/common';
import { GetShootingsBySchoolQuery } from './GetShootingsBySchoolQuery';
import { IShootingRepository } from 'src/Domain/School/Repository/IShootingRepository';
import { ShootingView } from '../../View/ShootingView';
import { ShootingSummaryView } from '../../View/ShootingSummaryView';

@QueryHandler(GetShootingsBySchoolQuery)
export class GetShootingsBySchoolQueryHandler {
Expand All @@ -11,20 +11,19 @@ export class GetShootingsBySchoolQueryHandler {
private readonly shootingRepository: IShootingRepository
) {}

public async execute({ schoolId }: GetShootingsBySchoolQuery): Promise<ShootingView[]> {
const shootingViews: ShootingView[] = [];
public async execute({ schoolId }: GetShootingsBySchoolQuery): Promise<ShootingSummaryView[]> {
const shootingViews: ShootingSummaryView[] = [];
const shootings = await this.shootingRepository.findBySchool(
schoolId
);

for (const shooting of shootings) {
shootingViews.push(
new ShootingView(
new ShootingSummaryView(
shooting.getId(),
shooting.getName(),
shooting.getStatus(),
shooting.getShootingDate(),
shooting.getClosingDate(),
shooting.getShootingDate()
)
);
}
Expand Down
10 changes: 10 additions & 0 deletions api/src/Application/School/View/ShootingSummaryView.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ShootingStatus } from 'src/Domain/School/Shooting.entity';

export class ShootingSummaryView {
constructor(
public readonly id: string,
public readonly name: string,
public readonly status: ShootingStatus,
public readonly shootingDate: Date
) {}
}
3 changes: 2 additions & 1 deletion api/src/Application/School/View/ShootingView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export class ShootingView {
public readonly name: string,
public readonly status: ShootingStatus,
public readonly shootingDate: Date,
public readonly closingDate: Date,
public readonly groupClosingDate: Date,
public readonly individualClosingDate: Date,
public readonly notice?: string,
) {}
}
10 changes: 7 additions & 3 deletions api/src/Domain/School/Shooting.entity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ describe('Shooting', () => {
'Prise de vue début année',
new Date('2021-04-17'),
new Date('2021-09-01'),
new Date('2021-12-01'),
ShootingStatus.DISABLED,
instance(school),
'Fin des commandes le 10'
);
expect(shooting.getId()).toBeUndefined();
expect(shooting.getName()).toBe('Prise de vue début année');
expect(shooting.getShootingDate()).toMatchObject(new Date('2021-04-17T00:00:00.000Z'));
expect(shooting.getClosingDate()).toMatchObject(new Date('2021-09-01T00:00:00.000Z'));
expect(shooting.getGroupClosingDate()).toMatchObject(new Date('2021-09-01T00:00:00.000Z'));
expect(shooting.getIndividualClosingDate()).toMatchObject(new Date('2021-12-01T00:00:00.000Z'));
expect(shooting.getStatus()).toBe(ShootingStatus.DISABLED);
expect(shooting.getSchool()).toBe(instance(school));
expect(shooting.getNotice()).toBe('Fin des commandes le 10');
Expand All @@ -28,6 +30,7 @@ describe('Shooting', () => {
'Prise de vue début année',
new Date('2021-04-17'),
new Date('2021-09-01'),
new Date('2021-12-01'),
ShootingStatus.DISABLED,
instance(school)
);
Expand All @@ -36,14 +39,15 @@ describe('Shooting', () => {
'Prise de vue fin année',
new Date('2022-04-17'),
new Date('2022-09-01'),
new Date('2022-12-01'),
'Message notice'
);


expect(shooting.getId()).toBeUndefined();
expect(shooting.getName()).toBe('Prise de vue fin année');
expect(shooting.getShootingDate()).toMatchObject(new Date('2022-04-17T00:00:00.000Z'));
expect(shooting.getClosingDate()).toMatchObject(new Date('2022-09-01T00:00:00.000Z'));
expect(shooting.getGroupClosingDate()).toMatchObject(new Date('2022-09-01T00:00:00.000Z'));
expect(shooting.getIndividualClosingDate()).toMatchObject(new Date('2022-12-01T00:00:00.000Z'));
expect(shooting.getStatus()).toBe(ShootingStatus.DISABLED);
expect(shooting.getSchool()).toBe(instance(school));
expect(shooting.getNotice()).toBe('Message notice');
Expand Down
25 changes: 18 additions & 7 deletions api/src/Domain/School/Shooting.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export class Shooting {
private shootingDate: Date;

@Column({ type: 'date', nullable: false })
private closingDate: Date;
private groupClosingDate: Date;

@Column({ type: 'date', nullable: false })
private individualClosingDate: Date;

@Column({ type: 'varchar', nullable: true })
private notice: string;
Expand All @@ -32,14 +35,16 @@ export class Shooting {
constructor(
name: string,
shootingDate: Date,
closingDate: Date,
groupClosingDate: Date,
individualClosingDate: Date,
status: ShootingStatus,
school: School,
notice?: string
) {
this.name = name;
this.shootingDate = shootingDate;
this.closingDate = closingDate;
this.groupClosingDate = groupClosingDate;
this.individualClosingDate = individualClosingDate;
this.status = status;
this.school = school;
this.notice = notice;
Expand All @@ -57,8 +62,12 @@ export class Shooting {
return this.shootingDate;
}

public getClosingDate(): Date {
return this.closingDate;
public getGroupClosingDate(): Date {
return this.groupClosingDate;
}

public getIndividualClosingDate(): Date {
return this.individualClosingDate;
}

public getNotice(): string {
Expand All @@ -76,12 +85,14 @@ export class Shooting {
public update(
name: string,
shootingDate: Date,
closingDate: Date,
groupClosingDate: Date,
individualClosingDate: Date,
notice?: string,
): void {
this.name = name;
this.shootingDate = shootingDate;
this.closingDate = closingDate;
this.groupClosingDate = groupClosingDate;
this.individualClosingDate = individualClosingDate;
this.notice = notice;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ export class CreateShootingAction {
@Roles(UserRole.PHOTOGRAPHER)
@ApiOperation({ summary: 'Create a shooting' })
public async index(@Param() idDto: IdDTO, @Body() dto: ShootingDTO) {
const { name, shootingDate, closingDate, notice } = dto;
const { name, shootingDate, groupClosingDate, individualClosingDate, notice } = dto;

try {
const id = await this.commandBus.execute(
new CreateShootingCommand(
name,
new Date(shootingDate),
new Date(closingDate),
new Date(groupClosingDate),
new Date(individualClosingDate),
idDto.id,
notice
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { IdDTO } from 'src/Infrastructure/Common/DTO/IdDTO';
import { Roles } from 'src/Infrastructure/User/Decorator/Roles';
import { RolesGuard } from 'src/Infrastructure/User/Security/RolesGuard';
import { UserRole } from 'src/Domain/User/User.entity';
import { ShootingView } from 'src/Application/School/View/ShootingView';
import { GetShootingsBySchoolQuery } from 'src/Application/School/Query/Shooting/GetShootingsBySchoolQuery';
import { ShootingSummaryView } from 'src/Application/School/View/ShootingSummaryView';

@Controller('schools')
@ApiTags('School shooting')
Expand All @@ -22,7 +22,7 @@ export class GetSchoolShootingsAction {
@Get(':id/shootings')
@Roles(UserRole.PHOTOGRAPHER)
@ApiOperation({ summary: 'Get school shootings' })
public async index(@Param() dto: IdDTO): Promise<ShootingView[]> {
public async index(@Param() dto: IdDTO): Promise<ShootingSummaryView[]> {
try {
return await this.queryBus.execute(new GetShootingsBySchoolQuery(dto.id));
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ export class UpdateShootingAction {
@ApiOperation({ summary: 'Update shooting' })
public async index(@Param() idDto: IdDTO, @Body() dto: ShootingDTO) {
try {
const { name, closingDate, shootingDate, notice } = dto;
const { name, groupClosingDate, shootingDate, individualClosingDate, notice } = dto;
const id = await this.commandBus.execute(
new UpdateShootingCommand(
idDto.id,
name,
new Date(shootingDate),
new Date(closingDate),
new Date(groupClosingDate),
new Date(individualClosingDate),
notice
)
);
Expand Down
Loading

0 comments on commit 3b6dad8

Please sign in to comment.