Skip to content

Commit

Permalink
feat: update Daily Plan
Browse files Browse the repository at this point in the history
  • Loading branch information
GloireMutaliko21 committed Apr 29, 2024
1 parent c02b317 commit 5f0e700
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 26 deletions.
6 changes: 6 additions & 0 deletions packages/contracts/src/daily-plan.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ export interface IDailyPlanCreateInput extends IBasePerTenantAndOrganizationEnti
status: DailyPlanStatusEnum;
taskId?: ITask['id'];
}

export interface IDailyPlanUpdateInput extends IBasePerTenantAndOrganizationEntityModel {
date?: Date;
workTimePlanned?: number;
status?: DailyPlanStatusEnum;
}
58 changes: 46 additions & 12 deletions packages/core/src/tasks/daily-plan/daily-plan.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
import { Body, Controller, Delete, Get, HttpCode, HttpStatus, Param, Post, Put, Query, UseGuards } from '@nestjs/common';
import {
Body,
Controller,
Delete,
Get,
HttpCode,
HttpStatus,
Param,
Post,
Put,
Query,
UseGuards
} from '@nestjs/common';
import { UpdateResult } from 'typeorm';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { IDailyPlan, IEmployee, ITask, PermissionsEnum } from '@gauzy/contracts';
import { CrudController, PaginationParams } from '../../core/crud';
Expand All @@ -7,7 +20,7 @@ import { UseValidationPipe } from '../../shared/pipes';
import { PermissionGuard, TenantPermissionGuard } from '../../shared/guards';
import { DailyPlan } from './daily-plan.entity';
import { DailyPlanService } from './daily-plan.service';
import { CreateDailyPlanDTO } from './dto';
import { CreateDailyPlanDTO, UpdateDailyPlanDTO } from './dto';

@ApiTags('Daily Plan')
@UseGuards(TenantPermissionGuard, PermissionGuard)
Expand Down Expand Up @@ -37,16 +50,13 @@ export class DailyPlanController extends CrudController<DailyPlan> {
description: 'No Record found'
})
@Get('me')
async getMyPlans(
@Query() params: PaginationParams<DailyPlan>
) {
async getMyPlans(@Query() params: PaginationParams<DailyPlan>) {
return await this.dailyPlanService.getMyPlans(params);
}

/**
* CREATE Daily Plan
* @param entity
* @param params
* @param options
*/

Expand All @@ -66,11 +76,7 @@ export class DailyPlanController extends CrudController<DailyPlan> {
@Body() entity: CreateDailyPlanDTO,
@Query() options: PaginationParams<DailyPlan>
): Promise<IDailyPlan> {
return await this.dailyPlanService.createDailyPlan(
entity,
options,
entity.taskId
);
return await this.dailyPlanService.createDailyPlan(entity, options, entity.taskId);
}

/**
Expand Down Expand Up @@ -121,7 +127,7 @@ export class DailyPlanController extends CrudController<DailyPlan> {
status: HttpStatus.NOT_FOUND,
description: 'No Record found'
})
@Put(':id')
@Put('task/:id')
async removeTaskFromPlan(
@Param('id') planId: IDailyPlan['id'],
@Body('taskId') taskId: ITask['id'],
Expand All @@ -130,6 +136,34 @@ export class DailyPlanController extends CrudController<DailyPlan> {
return await this.dailyPlanService.removeTaskFromPlan(planId, taskId, params);
}

/**
* UPDATE Daily Plan
* @param {UpdateDailyPlanDTO} entity
* @param {IDailyPlan['id']} id
* @return {*} {(Promise<IDailyPlan | UpdateResult>)}
* @memberof DailyPlanController
*/
@ApiOperation({
summary: 'Update daily plan'
})
@ApiResponse({
status: HttpStatus.OK,
description: 'Plan updated',
type: DailyPlan
})
@ApiResponse({
status: HttpStatus.NOT_FOUND,
description: 'No Record found'
})
@Put(':id')
@UseValidationPipe({ transform: true, whitelist: true })
async updateDailyPlan(
@Body() entity: UpdateDailyPlanDTO,
@Query('id') id: IDailyPlan['id']
): Promise<IDailyPlan | UpdateResult> {
return await this.dailyPlanService.updateDailyPlan(id, entity);
}

/**
* DELETE plan
*
Expand Down
49 changes: 40 additions & 9 deletions packages/core/src/tasks/daily-plan/daily-plan.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Brackets, DeepPartial, WhereExpressionBuilder } from 'typeorm';
import { Brackets, DeepPartial, UpdateResult, WhereExpressionBuilder } from 'typeorm';
import { IDailyPlan, IEmployee, ITask } from '@gauzy/contracts';
import { isNotEmpty } from '@gauzy/common';
import { isPostgres } from '@gauzy/config';
Expand Down Expand Up @@ -57,12 +57,12 @@ export class DailyPlanService extends TenantAwareCrudService<DailyPlan> {
query.setFindOptions({
...(isNotEmpty(options) &&
isNotEmpty(options.where) && {
where: options.where
}),
where: options.where
}),
...(isNotEmpty(options) &&
isNotEmpty(options.relations) && {
relations: options.relations
})
relations: options.relations
})
});

query.where(
Expand Down Expand Up @@ -120,12 +120,12 @@ export class DailyPlanService extends TenantAwareCrudService<DailyPlan> {
query.setFindOptions({
...(isNotEmpty(options) &&
isNotEmpty(options.where) && {
where: options.where
}),
where: options.where
}),
...(isNotEmpty(options) &&
isNotEmpty(options.relations) && {
relations: options.relations
})
relations: options.relations
})
});
query.andWhere(
new Brackets((qb: WhereExpressionBuilder) => {
Expand Down Expand Up @@ -208,6 +208,37 @@ export class DailyPlanService extends TenantAwareCrudService<DailyPlan> {
}
}

/**
* @description UPDATE Daily plan
*
* @param {IDailyPlan['id']} id
* @param {DeepPartial<IDailyPlan>} partialEntity
* @returns
* @memberof DailyPlanService
*/
async updateDailyPlan(
id: IDailyPlan['id'],
partialEntity: DeepPartial<IDailyPlan>
): Promise<IDailyPlan | UpdateResult> {
try {
const currentEmployeeId = RequestContext.currentEmployeeId();
const currentTenantId = RequestContext.currentTenantId();

const dailyPlan = await this.findOneByWhereOptions({
id,
employeeId: currentEmployeeId,
tenantId: currentTenantId
});

if (!dailyPlan) {
throw new BadRequestException('Daily plan not found');
}
return await this.update(id, partialEntity);
} catch (error) {
throw new BadRequestException(error);
}
}

/**
* DELETE daily plan
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import { EmployeeFeatureDTO } from '../../../employee/dto';
* Create Daily Plan DTO validation
*/

export class CreateDailyPlanDTO extends IntersectionType(
TenantOrganizationBaseDTO,
EmployeeFeatureDTO
) implements IDailyPlanCreateInput {

export class CreateDailyPlanDTO
extends IntersectionType(TenantOrganizationBaseDTO, EmployeeFeatureDTO)
implements IDailyPlanCreateInput
{
@ApiProperty({ type: () => Date })
@Type(() => Date)
@IsNotEmpty()
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/tasks/daily-plan/dto/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './create-daily-plan.dto';
export * from './update-daily-plan.dto';
27 changes: 27 additions & 0 deletions packages/core/src/tasks/daily-plan/dto/update-daily-plan.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { IsDate, IsEnum, IsNumber, IsOptional } from 'class-validator';
import { DailyPlanStatusEnum, IDailyPlanUpdateInput } from '@gauzy/contracts';
import { TenantBaseDTO } from '../../../core/dto';

/**
* Update Daily Plan DTO validation
*/

export class UpdateDailyPlanDTO extends TenantBaseDTO implements IDailyPlanUpdateInput {
@ApiProperty({ type: () => Date })
@Type(() => Date)
@IsOptional()
@IsDate()
readonly date?: Date;

@ApiProperty({ type: () => Number })
@IsOptional()
@IsNumber()
readonly workTimePlanned?: number;

@ApiProperty({ type: () => String, enum: DailyPlanStatusEnum })
@IsOptional()
@IsEnum(DailyPlanStatusEnum, { message: 'status `$value` must be a valid enum value' })
readonly status?: DailyPlanStatusEnum;
}

0 comments on commit 5f0e700

Please sign in to comment.