-
Notifications
You must be signed in to change notification settings - Fork 573
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] Employee weekly schedule planning #8741
base: develop
Are you sure you want to change the base?
Changes from 11 commits
e00c602
7c4e4f5
ff2bacb
c369a13
f7eb738
96052b1
6c3ba54
49d09b7
2b926a6
55092e8
a6a6e4c
b1dd009
39e500f
9037f32
413bf4e
043f77f
fdab334
d51f3f7
d8ece61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { IBasePerTenantAndOrganizationEntityModel, ID } from './base-entity.model'; | ||
import { IEmployee } from './employee.model'; | ||
|
||
/** | ||
* Enum representing different availability statuses. | ||
*/ | ||
export enum AvailabilityStatusEnum { | ||
Available = 'Available', | ||
Partial = 'Partial', | ||
Unavailable = 'Unavailable' | ||
} | ||
|
||
/** | ||
* Enum mapping availability statuses to numerical values. | ||
*/ | ||
export enum AvailabilityStatusValue { | ||
Available = 0, | ||
Partial = 1, | ||
Unavailable = 2 | ||
} | ||
|
||
/** | ||
* A mapping object to relate status labels to their respective numerical values. | ||
*/ | ||
export const AvailabilityStatusMap: Record<AvailabilityStatusEnum, AvailabilityStatusValue> = { | ||
[AvailabilityStatusEnum.Available]: AvailabilityStatusValue.Available, | ||
[AvailabilityStatusEnum.Partial]: AvailabilityStatusValue.Partial, | ||
[AvailabilityStatusEnum.Unavailable]: AvailabilityStatusValue.Unavailable | ||
}; | ||
|
||
/** | ||
* Base interface for Employee Availability data. | ||
* Includes common properties shared across different input types. | ||
*/ | ||
interface IBaseEmployeeAvailability extends IBasePerTenantAndOrganizationEntityModel { | ||
employeeId: ID; | ||
startDate: Date; | ||
endDate: Date; | ||
dayOfWeek: number; // 0 = Sunday, 6 = Saturday | ||
availabilityStatus: AvailabilityStatusEnum; | ||
availabilityNotes?: string; | ||
} | ||
|
||
/** | ||
* Represents an Employee Availability record. | ||
*/ | ||
export interface IEmployeeAvailability extends IBaseEmployeeAvailability { | ||
employee: IEmployee; | ||
} | ||
|
||
/** | ||
* Input interface for finding Employee Availability records. | ||
*/ | ||
export interface IEmployeeAvailabilityFindInput extends Partial<IBaseEmployeeAvailability> {} | ||
|
||
/** | ||
* Input interface for creating new Employee Availability records. | ||
*/ | ||
export interface IEmployeeAvailabilityCreateInput extends IBaseEmployeeAvailability {} | ||
|
||
/** | ||
* Input interface for updating Employee Availability records. | ||
*/ | ||
export interface IEmployeeAvailabilityUpdateInput extends Partial<IBaseEmployeeAvailability> {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ICommand } from '@nestjs/cqrs'; | ||
import { IEmployeeAvailabilityCreateInput } from '@gauzy/contracts'; | ||
|
||
export class EmployeeAvailabilityBulkCreateCommand implements ICommand { | ||
static readonly type = '[Employee Availability Bulk] Create'; | ||
|
||
constructor(public readonly input: IEmployeeAvailabilityCreateInput[]) {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ICommand } from '@nestjs/cqrs'; | ||
import { IEmployeeAvailabilityCreateInput } from '@gauzy/contracts'; | ||
|
||
export class EmployeeAvailabilityCreateCommand implements ICommand { | ||
static readonly type = '[EmployeeAvailability] Create'; | ||
|
||
constructor(public readonly input: IEmployeeAvailabilityCreateInput) {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { BadRequestException } from '@nestjs/common'; | ||
import { CommandBus, CommandHandler, ICommandHandler } from '@nestjs/cqrs'; | ||
import { IEmployeeAvailability } from '@gauzy/contracts'; | ||
import { RequestContext } from '../../../core/context'; | ||
import { EmployeeAvailabilityBulkCreateCommand } from '../employee-availability.bulk.create.command'; | ||
import { EmployeeAvailability } from '../../employee-availability.entity'; | ||
import { EmployeeAvailabilityCreateCommand } from '../employee-availability.create.command'; | ||
|
||
@CommandHandler(EmployeeAvailabilityBulkCreateCommand) | ||
export class EmployeeAvailabilityBulkCreateHandler implements ICommandHandler<EmployeeAvailabilityBulkCreateCommand> { | ||
constructor(private readonly commandBus: CommandBus) {} | ||
|
||
public async execute(command: EmployeeAvailabilityBulkCreateCommand): Promise<IEmployeeAvailability[]> { | ||
const { input } = command; | ||
if (!Array.isArray(input) || input.length === 0) { | ||
throw new BadRequestException('Input must be a non-empty array of availability records.'); | ||
} | ||
|
||
const allAvailability: IEmployeeAvailability[] = []; | ||
const tenantId = RequestContext.currentTenantId(); | ||
|
||
for (const item of input) { | ||
let availability = new EmployeeAvailability({ | ||
...item, | ||
tenantId | ||
}); | ||
availability = await this.commandBus.execute(new EmployeeAvailabilityCreateCommand(availability)); | ||
allAvailability.push(availability); | ||
} | ||
samuelmbabhazi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return allAvailability; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,51 @@ | ||||||||||||||||||||
import { BadRequestException } from '@nestjs/common'; | ||||||||||||||||||||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'; | ||||||||||||||||||||
import { IEmployeeAvailability } from '@gauzy/contracts'; | ||||||||||||||||||||
import { RequestContext } from '../../../core/context'; | ||||||||||||||||||||
import { EmployeeAvailabilityService } from '../../employee-availability.service'; | ||||||||||||||||||||
import { EmployeeAvailability } from '../../employee-availability.entity'; | ||||||||||||||||||||
import { EmployeeAvailabilityCreateCommand } from '../employee-availability.create.command'; | ||||||||||||||||||||
|
||||||||||||||||||||
@CommandHandler(EmployeeAvailabilityCreateCommand) | ||||||||||||||||||||
export class EmployeeAvailabilityCreateHandler implements ICommandHandler<EmployeeAvailabilityCreateCommand> { | ||||||||||||||||||||
constructor(private readonly availabilityService: EmployeeAvailabilityService) {} | ||||||||||||||||||||
|
||||||||||||||||||||
/** | ||||||||||||||||||||
* Handles the creation of an employee availability record. | ||||||||||||||||||||
* | ||||||||||||||||||||
* @param {EmployeeAvailabilityCreateCommand} command - The command containing employee availability details. | ||||||||||||||||||||
* @returns {Promise<IEmployeeAvailability>} - The newly created employee availability record. | ||||||||||||||||||||
* @throws {BadRequestException} - If any validation fails (e.g., missing fields, invalid dates). | ||||||||||||||||||||
*/ | ||||||||||||||||||||
public async execute(command: EmployeeAvailabilityCreateCommand): Promise<IEmployeeAvailability> { | ||||||||||||||||||||
const { input } = command; | ||||||||||||||||||||
const { startDate, endDate, employeeId, dayOfWeek, availabilityStatus } = input; | ||||||||||||||||||||
|
||||||||||||||||||||
if (!employeeId) { | ||||||||||||||||||||
throw new BadRequestException('Employee ID is required.'); | ||||||||||||||||||||
} | ||||||||||||||||||||
if (typeof dayOfWeek !== 'number' || dayOfWeek < 0 || dayOfWeek > 6) { | ||||||||||||||||||||
throw new BadRequestException('Day of week must be a number between 0 and 6.'); | ||||||||||||||||||||
} | ||||||||||||||||||||
if (!availabilityStatus) { | ||||||||||||||||||||
throw new BadRequestException('Availability status is required.'); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
if (!startDate || !endDate) { | ||||||||||||||||||||
throw new BadRequestException('Start date and end date are required.'); | ||||||||||||||||||||
} | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add date range validation. Validate that the end date is after the start date to ensure a valid date range. if (!startDate || !endDate) {
throw new BadRequestException('Start date and end date are required.');
}
+if (new Date(endDate) <= new Date(startDate)) {
+ throw new BadRequestException('End date must be after start date.');
+} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||
|
||||||||||||||||||||
if (new Date(endDate) <= new Date(startDate)) { | ||||||||||||||||||||
throw new BadRequestException('End date must be after start date.'); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
const tenantId = RequestContext.currentTenantId(); | ||||||||||||||||||||
|
||||||||||||||||||||
const availability = new EmployeeAvailability({ | ||||||||||||||||||||
...input, | ||||||||||||||||||||
tenantId | ||||||||||||||||||||
}); | ||||||||||||||||||||
|
||||||||||||||||||||
return await this.availabilityService.create(availability); | ||||||||||||||||||||
} | ||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { EmployeeAvailabilityBulkCreateHandler } from './employee-availability.bulk.create.handler'; | ||
import { EmployeeAvailabilityCreateHandler } from './employee-availability.create.handler'; | ||
|
||
/** | ||
* Exports all command handlers for EmployeeAvailability.` | ||
*/ | ||
export const CommandHandlers = [EmployeeAvailabilityBulkCreateHandler, EmployeeAvailabilityCreateHandler]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './employee-availability.bulk.create.command'; | ||
export * from './employee-availability.create.command'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { IEmployeeAvailabilityCreateInput } from '@gauzy/contracts'; | ||
import { EmployeeAvailability } from '../employee-availability.entity'; | ||
import { TenantOrganizationBaseDTO } from '../../core'; | ||
import { IntersectionType, PartialType, PickType } from '@nestjs/swagger'; | ||
|
||
export class CreateEmployeeAvailabilityDTO | ||
extends IntersectionType( | ||
PartialType(TenantOrganizationBaseDTO), | ||
PickType(EmployeeAvailability, [ | ||
'dayOfWeek', | ||
'startDate', | ||
'endDate', | ||
'availabilityNotes', | ||
'availabilityStatus', | ||
'employeeId' | ||
]) | ||
) | ||
implements IEmployeeAvailabilityCreateInput {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { PartialType } from '@nestjs/mapped-types'; | ||
import { IEmployeeAvailabilityUpdateInput } from '@gauzy/contracts'; | ||
import { CreateEmployeeAvailabilityDTO } from './create-employee-availability.dto'; | ||
import { IntersectionType } from '@nestjs/swagger'; | ||
|
||
export class UpdateEmployeeAvailabilityDTO | ||
extends IntersectionType(PartialType(CreateEmployeeAvailabilityDTO)) | ||
implements IEmployeeAvailabilityUpdateInput {} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,113 @@ | ||||||||||||||||||||||||
import { UpdateResult } from 'typeorm'; | ||||||||||||||||||||||||
import { Body, Controller, Get, HttpCode, HttpStatus, Param, Post, Put, Query, UseGuards } from '@nestjs/common'; | ||||||||||||||||||||||||
import { CommandBus } from '@nestjs/cqrs'; | ||||||||||||||||||||||||
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; | ||||||||||||||||||||||||
import { ID, IEmployeeAvailability, IPagination } from '@gauzy/contracts'; | ||||||||||||||||||||||||
import { EmployeeAvailabilityService } from './employee-availability.service'; | ||||||||||||||||||||||||
import { EmployeeAvailability } from './employee-availability.entity'; | ||||||||||||||||||||||||
import { CrudController, PaginationParams } from '../core'; | ||||||||||||||||||||||||
import { PermissionGuard, TenantPermissionGuard, UUIDValidationPipe } from '../shared'; | ||||||||||||||||||||||||
import { EmployeeAvailabilityBulkCreateCommand, EmployeeAvailabilityCreateCommand } from './commands'; | ||||||||||||||||||||||||
import { CreateEmployeeAvailabilityDTO } from './dto/create-employee-availability.dto'; | ||||||||||||||||||||||||
import { UpdateEmployeeAvailabilityDTO } from './dto/update-employee-availability.dto'; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
@ApiTags('EmployeeAvailability') | ||||||||||||||||||||||||
@UseGuards(TenantPermissionGuard, PermissionGuard) | ||||||||||||||||||||||||
@Controller('/employee-availability') | ||||||||||||||||||||||||
export class EmployeeAvailabilityController extends CrudController<EmployeeAvailability> { | ||||||||||||||||||||||||
constructor( | ||||||||||||||||||||||||
private readonly availabilityService: EmployeeAvailabilityService, | ||||||||||||||||||||||||
private readonly commandBus: CommandBus | ||||||||||||||||||||||||
) { | ||||||||||||||||||||||||
super(availabilityService); | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
/** | ||||||||||||||||||||||||
* Create multiple employee availability records in bulk. | ||||||||||||||||||||||||
* | ||||||||||||||||||||||||
* @param entities List of availability records to create | ||||||||||||||||||||||||
* @returns The created availability records | ||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
@ApiOperation({ summary: 'Create multiple availability records' }) | ||||||||||||||||||||||||
@ApiResponse({ | ||||||||||||||||||||||||
status: HttpStatus.CREATED, | ||||||||||||||||||||||||
description: 'The records have been successfully created.' | ||||||||||||||||||||||||
}) | ||||||||||||||||||||||||
@ApiResponse({ | ||||||||||||||||||||||||
status: HttpStatus.BAD_REQUEST, | ||||||||||||||||||||||||
description: 'Invalid input. The response body may contain clues as to what went wrong.' | ||||||||||||||||||||||||
}) | ||||||||||||||||||||||||
@HttpCode(HttpStatus.CREATED) | ||||||||||||||||||||||||
@Post('/bulk') | ||||||||||||||||||||||||
async createBulk(@Body() entities: CreateEmployeeAvailabilityDTO[]): Promise<IEmployeeAvailability[]> { | ||||||||||||||||||||||||
return await this.commandBus.execute(new EmployeeAvailabilityBulkCreateCommand(entities)); | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider adding rate limiting for bulk operations. The bulk creation endpoint could be vulnerable to abuse. Consider adding rate limiting. @HttpCode(HttpStatus.CREATED)
+@UseGuards(ThrottlerGuard)
+@Throttle(10, 60) // 10 requests per minute
@Post('/bulk')
async createBulk(@Body() entities: CreateEmployeeAvailabilityDTO[]): Promise<IEmployeeAvailability[]> { 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
/** | ||||||||||||||||||||||||
* Retrieve all employee availability records. | ||||||||||||||||||||||||
* | ||||||||||||||||||||||||
* @param data Query parameters, including relations and filters | ||||||||||||||||||||||||
* @returns A paginated list of availability records | ||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
@ApiOperation({ summary: 'Retrieve all availability records' }) | ||||||||||||||||||||||||
@ApiResponse({ | ||||||||||||||||||||||||
status: HttpStatus.OK, | ||||||||||||||||||||||||
description: 'Successfully retrieved availability records.' | ||||||||||||||||||||||||
}) | ||||||||||||||||||||||||
@ApiResponse({ | ||||||||||||||||||||||||
status: HttpStatus.NOT_FOUND, | ||||||||||||||||||||||||
description: 'No availability records found.' | ||||||||||||||||||||||||
}) | ||||||||||||||||||||||||
@Get() | ||||||||||||||||||||||||
async findAll( | ||||||||||||||||||||||||
@Query() filter: PaginationParams<EmployeeAvailability> | ||||||||||||||||||||||||
): Promise<IPagination<IEmployeeAvailability>> { | ||||||||||||||||||||||||
return this.availabilityService.findAll(filter); | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
/** | ||||||||||||||||||||||||
* Create a new employee availability record. | ||||||||||||||||||||||||
* | ||||||||||||||||||||||||
* @param entity The data for the new availability record | ||||||||||||||||||||||||
* @returns The created availability record | ||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
@ApiOperation({ summary: 'Create a new availability record' }) | ||||||||||||||||||||||||
@ApiResponse({ | ||||||||||||||||||||||||
status: HttpStatus.CREATED, | ||||||||||||||||||||||||
description: 'The record has been successfully created.' | ||||||||||||||||||||||||
}) | ||||||||||||||||||||||||
@ApiResponse({ | ||||||||||||||||||||||||
status: HttpStatus.BAD_REQUEST, | ||||||||||||||||||||||||
description: 'Invalid input. The response body may contain clues as to what went wrong.' | ||||||||||||||||||||||||
}) | ||||||||||||||||||||||||
@HttpCode(HttpStatus.CREATED) | ||||||||||||||||||||||||
@Post() | ||||||||||||||||||||||||
async create(@Body() entity: CreateEmployeeAvailabilityDTO): Promise<IEmployeeAvailability> { | ||||||||||||||||||||||||
return await this.commandBus.execute(new EmployeeAvailabilityCreateCommand(entity)); | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
/** | ||||||||||||||||||||||||
* Update an existing employee availability record by its ID. | ||||||||||||||||||||||||
* | ||||||||||||||||||||||||
* @param id The ID of the availability record | ||||||||||||||||||||||||
* @param entity The updated data for the record | ||||||||||||||||||||||||
* @returns The updated availability record | ||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
@ApiOperation({ summary: 'Update an existing availability record' }) | ||||||||||||||||||||||||
@ApiResponse({ | ||||||||||||||||||||||||
status: HttpStatus.ACCEPTED, | ||||||||||||||||||||||||
description: 'The record has been successfully updated.' | ||||||||||||||||||||||||
}) | ||||||||||||||||||||||||
@ApiResponse({ | ||||||||||||||||||||||||
status: HttpStatus.BAD_REQUEST, | ||||||||||||||||||||||||
description: 'Invalid input. The response body may contain clues as to what went wrong.' | ||||||||||||||||||||||||
}) | ||||||||||||||||||||||||
@HttpCode(HttpStatus.ACCEPTED) | ||||||||||||||||||||||||
@Put(':id') | ||||||||||||||||||||||||
async update( | ||||||||||||||||||||||||
@Param('id', UUIDValidationPipe) id: ID, | ||||||||||||||||||||||||
@Body() entity: UpdateEmployeeAvailabilityDTO | ||||||||||||||||||||||||
): Promise<IEmployeeAvailability | UpdateResult> { | ||||||||||||||||||||||||
return this.availabilityService.update(id, { ...entity }); | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@samuelmbabhazi Again same, we don't want to insert EmployeeAvailability one by one using EmployeeAvailabilityCreateCommand command.