Skip to content

Commit

Permalink
Fix: stuff asked in pull request
Browse files Browse the repository at this point in the history
  • Loading branch information
markdeluk committed Jan 1, 2024
1 parent 7863c2d commit 9ce1459
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 35 deletions.
91 changes: 58 additions & 33 deletions api/src/availability/availability.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { Body, Controller, Delete, Param, Patch, Post } from '@nestjs/common';
import {
BadRequestException,
Body,
Controller,
Delete,
ForbiddenException,
NotFoundException,
Param,
Patch,
Post,
} from '@nestjs/common';
import { AvailabilityService } from './availability.service';
import {
Action,
checkAbility,
insertAvailabilitySchema,
updateAvailabilitySchema,
} from '@hkrecruitment/shared';
Expand All @@ -28,39 +39,38 @@ import Joi from 'joi';
export class AvailabilityController {
constructor(private readonly availabilityService: AvailabilityService) {}

@ApiBadRequestResponse()
@ApiForbiddenResponse()
@ApiNotFoundResponse()
@ApiOkResponse()
@ApiNoContentResponse()
@ApiForbiddenResponse()
@ApiBadGatewayResponse()
async listAvailabilities(): Promise<Availability[]> {
const matches = await this.availabilityService.listAvailabilities();
if (matches.length == 0) {
throw new NotFoundException();
}
return matches;
}

@ApiBadRequestResponse()
@ApiForbiddenResponse()
@ApiNotFoundResponse()
@ApiOkResponse()
@ApiNoContentResponse()
@ApiBadGatewayResponse()
@ApiBadRequestResponse()
@ApiForbiddenResponse()
@JoiValidate({
param: Joi.number().positive().integer().required(),
})
async findAvailabilityById(id: number): Promise<Availability> {
const matches = await this.availabilityService.findAvailabilityById(id);
return matches;
const match = await this.availabilityService.findAvailabilityById(id);
if (!match) {
throw new NotFoundException();
}
return match;
}

@ApiCreatedResponse()
@ApiBadRequestResponse()
@ApiForbiddenResponse()
@ApiNotFoundResponse()
@ApiOkResponse()
@ApiNoContentResponse()
@ApiBadGatewayResponse()
@ApiConflictResponse()
@ApiCreatedResponse()
@CheckPolicies((ability) => ability.can(Action.Create, 'Availability'))
@Post()
@JoiValidate({
Expand All @@ -70,49 +80,64 @@ export class AvailabilityController {
const res = await this.availabilityService.createAvailability(body);
if (res.identifiers.length > 0) {
return true;
} else {
throw new ForbiddenException();
}
return false;
}

@ApiBadRequestResponse()
@ApiForbiddenResponse()
@ApiNotFoundResponse()
@ApiOkResponse()
@ApiNoContentResponse()
@ApiBadRequestResponse()
@ApiForbiddenResponse()
@ApiBadGatewayResponse()
@ApiConflictResponse()
@ApiCreatedResponse()
@CheckPolicies((ability) => ability.can(Action.Create, 'Availability'))
@Patch()
@JoiValidate({
body: updateAvailabilitySchema,
})
async updateAvailability(@Body() body: Availability): Promise<boolean> {
const res = await this.availabilityService.updateAvailability(body);
if (res.affected != undefined && res.affected != null && res.affected > 0) {
return true;
const test = await this.findAvailabilityById(body.id);
if (test) {
const res = await this.availabilityService.updateAvailability(body);
if (
res.affected != undefined &&
res.affected != null &&
res.affected > 0
) {
return true;
} else {
throw new ForbiddenException();
}
} else {
throw new NotFoundException();
}
return false;
}

@ApiBadRequestResponse()
@ApiForbiddenResponse()
@ApiNotFoundResponse()
@ApiOkResponse()
@ApiNoContentResponse()
@ApiBadRequestResponse()
@ApiForbiddenResponse()
@ApiBadGatewayResponse()
@ApiConflictResponse()
@ApiCreatedResponse()
@CheckPolicies((ability) => ability.can(Action.Create, 'Availability'))
@Delete()
@JoiValidate({
param: Joi.number().positive().integer().required(),
})
async deleteAvailability(@Param() id: number): Promise<boolean> {
const res = await this.availabilityService.deleteAvailability(id);
if (res.affected != undefined && res.affected != null && res.affected > 0) {
return true;
const test = await this.findAvailabilityById(id);
if (test) {
const res = await this.availabilityService.deleteAvailability(id);
if (
res.affected != undefined &&
res.affected != null &&
res.affected > 0
) {
return true;
} else {
throw new ForbiddenException();
}
} else {
throw new NotFoundException();
}
return false;
}
}
2 changes: 1 addition & 1 deletion api/src/availability/availability.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class Availability implements AvailabilityInterface {
id: number;
@Column()
state: AvailabilityState;
@Column()
@Column({ name: 'last_modified' })
lastModified: Date;
@OneToOne(() => TimeSlot)
timeSlot: TimeSlot;
Expand Down
1 change: 0 additions & 1 deletion shared/src/availability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export const insertAvailabilitySchema = Joi.object<Availability>({
state: Joi.string()
.valid(...Object.values(AvailabilityType))
.required(),
lastModified: Joi.date().required(),
//timeSlot: Joi.object(createTimeSlotSchema).required(),
//user: Joi.object(createUserSchema).required(),
}).options({
Expand Down

0 comments on commit 9ce1459

Please sign in to comment.