Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Recruitment Session): controller, service, entity #12

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions api/src/recruitment-session/create-recruitment-session.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { RecruitmentSession, RecruitmentSessionState } from "@hkrecruitment/shared/recruitment-session";
import { ApiProperty } from '@nestjs/swagger';

export class CreateRecruitmentSessionDto implements RecruitmentSession {
@ApiProperty()
state: RecruitmentSessionState;

@ApiProperty()
slotDuration: number;

@ApiProperty()
interviewStart: Date;

@ApiProperty()
interviewEnd: Date;

@ApiProperty()
days: [Date];

@ApiProperty()
createdAt: Date;

@ApiProperty()
lastModified: Date;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { RecruitmentSession, RecruitmentSessionState } from '@hkrecruitment/shared/recruitment-session';
import { Exclude, Expose } from 'class-transformer';

@Exclude()
export class RecruitmentSessionResponseDto implements Partial<RecruitmentSession> {
@Expose() id: number;
@Expose() createdAt: Date;
}

102 changes: 102 additions & 0 deletions api/src/recruitment-session/recruitment-session.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import {
Body,
Controller,
BadRequestException,
NotFoundException,
ConflictException,
Param,
Post,
Delete,
Req,
Patch,
ForbiddenException,
} from '@nestjs/common';
import { RecruitmentSessionService } from './recruitment-session.service';
import { createRecruitmentSessionSchema, RecruitmentSession, RecruitmentSessionState, updateRecruitmentSessionSchema } from '@hkrecruitment/shared/recruitment-session';
import { Action, AppAbility, checkAbility } from "@hkrecruitment/shared"
import { JoiValidate } from '../joi-validation/joi-validate.decorator';
import {
ApiBadRequestResponse,
ApiBearerAuth,
ApiForbiddenResponse,
ApiNotFoundResponse,
ApiCreatedResponse,
ApiOkResponse,
ApiTags,
ApiConflictResponse,
ApiNoContentResponse,
} from '@nestjs/swagger';
import { CheckPolicies } from 'src/authorization/check-policies.decorator';
import { CreateRecruitmentSessionDto } from './create-recruitment-session.dto';
import { UpdateRecruitmentSessionDto } from './update-recruitment-session.dto';
import * as Joi from 'joi';
import { Ability } from 'src/authorization/ability.decorator';
import { AuthenticatedRequest } from 'src/authorization/authenticated-request.types';
import { plainToClass } from 'class-transformer';
import { RecruitmentSessionResponseDto } from './recruitment-session-response.dto';

@ApiBearerAuth()
@ApiTags('recruitment-session')
@Controller('recruitment-session')
export class RecruitmentSessionController {
constructor(private readonly recruitmentSessionService: RecruitmentSessionService) {}

// CREATE NEW RECRUITMENT SESSION
@ApiBadRequestResponse()
@ApiForbiddenResponse()
@ApiConflictResponse({
description: 'The recruitment session cannot be created', //
})
@ApiCreatedResponse()
@JoiValidate({
body: createRecruitmentSessionSchema,
})
@CheckPolicies((ability) => ability.can(Action.Create, 'RecruitmentSession'))
@Post()
async createRecruitmentSession(@Body() rSess: CreateRecruitmentSessionDto): Promise<RecruitmentSession> {
return this.recruitmentSessionService.createRecruitmentSession({...rSess});
}

// UPDATE A RECRUITMENT SESSION
@Patch(':session_id')
@ApiBadRequestResponse()
@ApiForbiddenResponse()
@ApiOkResponse()
@JoiValidate({
param: Joi.number().positive().integer().required().label('session_id'),
body: updateRecruitmentSessionSchema,
})
async updateRecruitmentSession(
@Param('session_id') sessionId: number,
@Body() updateRecruitmentSession: UpdateRecruitmentSessionDto,
@Ability() ability: AppAbility,
@Req() req: AuthenticatedRequest,
): Promise<RecruitmentSessionResponseDto> {
const session = await this.recruitmentSessionService.findRecruitmentSessionById(sessionId);

if (session === null) throw new NotFoundException();

const sessionToCheck = {
...updateRecruitmentSession,
sessionId: session.id,
};
if (
!checkAbility(ability, Action.Update, sessionToCheck, 'RecruitmentSession', [
'applicantId',
])
)
throw new ForbiddenException();

const updatedRecruitmentSession = await this.recruitmentSessionService.updateRecruitmentSession(
{
...session,
...updateRecruitmentSession,
lastModified: new Date(),
},
);

return plainToClass(RecruitmentSessionResponseDto, updatedRecruitmentSession);
}


}
30 changes: 30 additions & 0 deletions api/src/recruitment-session/recruitment-session.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
import { RecruitmentSession as RecruitmentSessionInterface, RecruitmentSessionState } from '@hkrecruitment/shared/src/recruitment-session'


@Entity()
export class RecruitmentSession implements RecruitmentSessionInterface {
@PrimaryGeneratedColumn('increment')
id: number;

@Column()
state: RecruitmentSessionState;

@Column()
slotDuration: number;

@Column()
interviewStart: Date;

@Column()
interviewEnd: Date;

@Column()
days: [Date];

@Column()
createdAt: Date;

@Column()
lastModified: Date;
}
34 changes: 34 additions & 0 deletions api/src/recruitment-session/recruitment-session.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { RecruitmentSession } from "./recruitment-session.entity";
import { CreateRecruitmentSessionDto } from "./create-recruitment-session.dto";


@Injectable()
export class RecruitmentSessionService {
constructor(
@InjectRepository(RecruitmentSession)
private readonly recruitmentSessionRepository: Repository<RecruitmentSession>
) {}

async createRecruitmentSession(rSess: CreateRecruitmentSessionDto): Promise<RecruitmentSession> {
return await this.recruitmentSessionRepository.save(rSess);
}

async findAllRecruitmentSessions(): Promise<RecruitmentSession[]> {
return await this.recruitmentSessionRepository.find();
}

async findRecruitmentSessionById(id: number): Promise<RecruitmentSession> {
return await this.recruitmentSessionRepository.findOne({where: {id} });
}

async deletRecruitmentSession(rSess: RecruitmentSession): Promise<RecruitmentSession> {
return await this.recruitmentSessionRepository.remove(rSess);
}

async updateRecruitmentSession(rSess: RecruitmentSession): Promise<RecruitmentSession> {
return await this.recruitmentSessionRepository.save(rSess);
}
}
23 changes: 23 additions & 0 deletions api/src/recruitment-session/update-recruitment-session.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { RecruitmentSession, RecruitmentSessionState } from '@hkrecruitment/shared/recruitment-session';
import { ApiProperty } from '@nestjs/swagger';


export class UpdateRecruitmentSessionDto implements Partial<RecruitmentSession> {
@ApiProperty({ required: false })
state?: RecruitmentSessionState;

@ApiProperty({ required: false })
slotDuration?: number;

@ApiProperty({ required: false })
interviewStart?: Date;

@ApiProperty({ required: false })
interviewEnd?: Date;

@ApiProperty({ required: false })
days?: [Date];

@ApiProperty({ required: false })
lastModified?: Date;
}
6 changes: 4 additions & 2 deletions shared/src/abilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { applyAbilitiesForPerson, Person, Role } from "./person";
import { Application, applyAbilitiesOnApplication } from "./application";
import { applyAbilitiesOnAvailability, Availability } from "./availability";
import { TimeSlot } from "./timeslot";
import { RecruitmentSession } from "recruitment-session";

export interface UserAuth {
sub: string;
Expand All @@ -26,8 +27,9 @@ type SubjectsTypes =
| Partial<Person>
| Partial<Application>
| Partial<Availability>
| Partial<TimeSlot>;
type SubjectNames = "Person" | "Application" | "Availability" | "TimeSlot";
| Partial<TimeSlot>
| Partial<RecruitmentSession>;
type SubjectNames = "Person" | "Application" | "Availability" | "TimeSlot" | "RecruitmentSession";
export type Subjects = SubjectsTypes | SubjectNames;

export type AppAbility = PureAbility<[Action, Subjects]>;
Expand Down
82 changes: 82 additions & 0 deletions shared/src/recruitment-session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { AvailabilityState } from "availability";
import { Action, ApplyAbilities } from "./abilities";
import { Role } from "./person";
import DateExtension from "@joi/date";
import * as Joi from "joi";

const JoiDate = Joi.extend(DateExtension);


export enum RecruitmentSessionState {
Active = "active",
Concluded = "concluded",
}


// import BaseJoi from "joi";

export interface RecruitmentSession {
state: RecruitmentSessionState;
slotDuration: number;
interviewStart: Date;
interviewEnd: Date;
days: [Date];
createdAt: Date;
lastModified: Date;
}

/* Validation schemas */

export const createRecruitmentSessionSchema = Joi.object<RecruitmentSession> ({
state: Joi.string()
.valid("active", "concluded")
.required(),
slotDuration: Joi.number()
.integer(),
interviewStart: JoiDate.date().format("YYYY-MM-DD HH:mm").required(),
interviewEnd: JoiDate.date().format("YYYY-MM-DD HH:mm").required(),
// days:
lastModified: JoiDate.date().format("YYYY-MM-DD HH:mm").required()
}).options({
stripUnknown: true,
abortEarly: false,
presence: "required",
});

export const updateRecruitmentSessionSchema = Joi.object<RecruitmentSession> ({
state: Joi.string()
.valid("active", "concluded")
.optional(),
slotDuration: Joi.number()
.integer(),
interviewStart: JoiDate.date().format("YYYY-MM-DD HH:mm").optional(),
interviewEnd: JoiDate.date().format("YYYY-MM-DD HH:mm").optional(),
// days:
// .optional(),
createdAt: JoiDate.date().format("YYYY-MM-DD HH:mm").optional(),
lastModified: JoiDate.date().format("YYYY-MM-DD HH:mm").optional()
});



/* Abilities */

export const applyAbilitiesOnRecruitmentSession: ApplyAbilities = (
user,
{ can, cannot }
) => {
can(Action.Manage, "RecruitmentSession");
switch (user.role) {
case Role.Admin:
case Role.Supervisor:
can(Action.Manage, "RecruitmentSession");
break;
case Role.Clerk: // puo o non puo ??????
case Role.Member:
case Role.Applicant:
can(Action.Read, "RecruitmentSession");
break;
default:
cannot(Action.Manage, "RecruitmentSession");
}
};
Loading