Skip to content

Commit

Permalink
feat: add get all plans service
Browse files Browse the repository at this point in the history
  • Loading branch information
GloireMutaliko21 committed May 3, 2024
1 parent 7ba70f3 commit 9aa6b25
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 16 deletions.
32 changes: 28 additions & 4 deletions packages/core/src/tasks/daily-plan/daily-plan.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import { UpdateResult } from 'typeorm';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { IDailyPlan, IEmployee, IPagination, ITask, PermissionsEnum } from '@gauzy/contracts';
import { CrudController, PaginationParams } from '../../core/crud';
import { Permissions } from './../../shared/decorators';
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, UpdateDailyPlanDTO } from './dto';
import { Permissions } from './../../shared/decorators';
import { PermissionGuard, TenantPermissionGuard } from './../../shared/guards';

@ApiTags('Daily Plan')
@UseGuards(TenantPermissionGuard, PermissionGuard)
Expand All @@ -31,6 +31,30 @@ export class DailyPlanController extends CrudController<DailyPlan> {
super(dailyPlanService);
}

/**
* GET daily plans
*
* @param options
* @returns
*/
@ApiOperation({
summary: 'Find daily plans.'
})
@ApiResponse({
status: HttpStatus.OK,
description: 'Found plans',
type: DailyPlan
})
@ApiResponse({
status: HttpStatus.NOT_FOUND,
description: 'No Record found'
})
@Get()
@UseValidationPipe()
async get(@Query() params: PaginationParams<DailyPlan>) {
return await this.dailyPlanService.getAllPlans(params);
}

/**
* GET my daily plans
*
Expand Down Expand Up @@ -79,7 +103,7 @@ export class DailyPlanController extends CrudController<DailyPlan> {
@Param('id') employeeId: IEmployee['id'],
@Query() params: PaginationParams<DailyPlan>
): Promise<IPagination<IDailyPlan>> {
return await this.dailyPlanService.getDailyPlansByEmployee(employeeId, params);
return await this.dailyPlanService.getDailyPlansByEmployee(params, employeeId);
}

/**
Expand Down Expand Up @@ -157,7 +181,7 @@ export class DailyPlanController extends CrudController<DailyPlan> {
@UseValidationPipe({ transform: true, whitelist: true })
async update(
@Query('id') id: IDailyPlan['id'],
@Body() entity: UpdateDailyPlanDTO,
@Body() entity: UpdateDailyPlanDTO
): Promise<IDailyPlan | UpdateResult> {
return await this.dailyPlanService.updateDailyPlan(id, entity);
}
Expand Down
44 changes: 32 additions & 12 deletions packages/core/src/tasks/daily-plan/daily-plan.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,21 @@ export class DailyPlanService extends TenantAwareCrudService<DailyPlan> {
}

/**
* Retrieves daily plans for a specific employee with pagination and additional query options.
* Retrieves daily plans with pagination and additional query options.
*
* @param employeeId - The ID of the employee for whom to retrieve daily plans.
* @param options - Pagination and additional query options for filtering and retrieving daily plans.
* @returns A promise that resolves to an object containing the list of daily plans and the total count.
* @throws BadRequestException - If there's an error during the query.
*/
async getDailyPlansByEmployee(
employeeId: IEmployee['id'],
options: PaginationParams<DailyPlan>

async getAllPlans(
options: PaginationParams<DailyPlan>,
employeeId?: IEmployee['id']
): Promise<IPagination<IDailyPlan>> {
try {
const tenantId = RequestContext.currentTenantId();
const { where } = options;
const { organizationId, tenantId } = where;

// Create the initial query
const query = this.typeOrmRepository.createQueryBuilder(this.tableName);

Expand All @@ -114,17 +116,14 @@ export class DailyPlanService extends TenantAwareCrudService<DailyPlan> {

// Apply optional find options if provided
query.setFindOptions({
...(isNotEmpty(options) &&
isNotEmpty(options.where) && {
where: options.where
}),
...(isNotEmpty(options) &&
isNotEmpty(options.relations) && {
relations: options.relations
})
});

query.andWhere(p(`"${query.alias}".tenantId = :tenantId`), { tenantId });
query.andWhere(p(`"${query.alias}"."tenantId" = :tenantId`), { tenantId });
query.andWhere(p(`"${query.alias}"."organizationId" = :organizationId`), { organizationId });

if (employeeId) {
query.andWhere(p(`"${query.alias}"."employeeId" = :employeeId`), { employeeId });
Expand All @@ -140,6 +139,27 @@ export class DailyPlanService extends TenantAwareCrudService<DailyPlan> {
}
}

/**
* Retrieves daily plans for a specific employee with pagination and additional query options.
*
* @param employeeId - The ID of the employee for whom to retrieve daily plans.
* @param options - Pagination and additional query options for filtering and retrieving daily plans.
* @returns A promise that resolves to an object containing the list of daily plans and the total count.
* @throws BadRequestException - If there's an error during the query.
*/

async getDailyPlansByEmployee(
options: PaginationParams,
employeeId?: IEmployee['id']
): Promise<IPagination<IDailyPlan>> {
try {
// Fetch all daily plans
return await this.getAllPlans(options, employeeId);
} catch (error) {
console.log('Error fetching all daily plans');
}
}

/**
* Retrieves daily plans for the current employee based on given pagination options.
*
Expand All @@ -152,7 +172,7 @@ export class DailyPlanService extends TenantAwareCrudService<DailyPlan> {
const currentEmployeeId = RequestContext.currentEmployeeId();

// Fetch daily plans for the current employee
return await this.getDailyPlansByEmployee(currentEmployeeId, options);
return await this.getAllPlans(options, currentEmployeeId);
} catch (error) {
console.error('Error fetching daily plans for me:', error); // Log the error for debugging
}
Expand Down

0 comments on commit 9aa6b25

Please sign in to comment.