Skip to content

Commit

Permalink
Merge pull request #8547 from ever-co/feat/add-timesheet-status-filter
Browse files Browse the repository at this point in the history
[Enhancement] Add Timesheet status filter
  • Loading branch information
rahul-rocket authored Nov 19, 2024
2 parents 1489cf2 + 9622255 commit 82a2e42
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
3 changes: 3 additions & 0 deletions packages/contracts/src/timesheet.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ export interface IGetTimesheetInput extends IBasePerTenantAndOrganizationEntityM
projectIds?: ID[];
clientId?: ID[];
employeeIds?: ID[];
status?: TimesheetStatus[];
taskIds?: ID[];
}

export interface IDateRange {
Expand Down Expand Up @@ -191,6 +193,7 @@ export interface ITimeLogFilters extends IBasePerTenantAndOrganizationEntityMode
categoryId?: ID;
timeZone?: string;
timeFormat?: TimeFormatEnum;
status?: TimesheetStatus[];
}

export interface ITimeLogTodayFilters extends IBasePerTenantAndOrganizationEntityModel {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import { IGetTimesheetInput } from "@gauzy/contracts";
import { IntersectionType } from "@nestjs/swagger";
import { RelationsQueryDTO, SelectorsQueryDTO } from "./../../../../shared/dto";
import { IGetTimesheetInput } from '@gauzy/contracts';
import { IntersectionType } from '@nestjs/swagger';
import { ApiPropertyOptional } from '@nestjs/swagger';
import { IsOptional, IsArray } from 'class-validator';
import { TimesheetStatus } from '@gauzy/contracts';
import { RelationsQueryDTO, SelectorsQueryDTO } from './../../../../shared/dto';

/**
* Get timesheet request DTO validation
*/
export class TimesheetQueryDTO extends IntersectionType(
RelationsQueryDTO,
SelectorsQueryDTO
) implements IGetTimesheetInput {}
export class TimesheetQueryDTO
extends IntersectionType(RelationsQueryDTO, SelectorsQueryDTO)
implements IGetTimesheetInput
{
/**
* An array of status to filter the time logs by specific status.
* If not provided, no filtering by status will be applied.
*/
@ApiPropertyOptional({ type: () => Array, isArray: true })
@IsOptional()
@IsArray()
status: TimesheetStatus[];
}
19 changes: 17 additions & 2 deletions packages/core/src/time-tracking/timesheet/timesheet.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { Between, In, SelectQueryBuilder, Brackets, WhereExpressionBuilder } from 'typeorm';
import * as moment from 'moment';
import { IGetTimesheetInput, PermissionsEnum, ITimesheet } from '@gauzy/contracts';
import { IGetTimesheetInput, PermissionsEnum, ITimesheet, TimesheetStatus } from '@gauzy/contracts';
import { RequestContext } from './../../core/context';
import { TenantAwareCrudService } from './../../core/crud';
import { getDateRangeFormat } from './../../core/utils';
Expand Down Expand Up @@ -84,7 +84,15 @@ export class TimeSheetService extends TenantAwareCrudService<Timesheet> {
* @returns
*/
async getFilterTimesheetQuery(qb: SelectQueryBuilder<Timesheet>, request: IGetTimesheetInput) {
let { organizationId, startDate, endDate, onlyMe: isOnlyMeSelected, employeeIds = [] } = request;
let {
organizationId,
startDate,
endDate,
onlyMe: isOnlyMeSelected,
employeeIds = [],
status = [],
taskIds = []
} = request;

const tenantId = RequestContext.currentTenantId() ?? request.tenantId; // Retrieve the tenant ID from the request
const user = RequestContext.currentUser(); // Retrieve the current user
Expand All @@ -108,6 +116,12 @@ export class TimeSheetService extends TenantAwareCrudService<Timesheet> {
new Brackets((qb: WhereExpressionBuilder) => {
qb.where({
startedAt: Between(start, end),
...(status.length > 0
? {
status: In(status.filter((s) => Object.values(TimesheetStatus).includes(s)))
}
: {}),
...(taskIds.length > 0 ? { taskId: In(taskIds) } : {}),
...(employeeIds.length > 0 ? { employeeId: In(employeeIds) } : {})
});
})
Expand All @@ -116,6 +130,7 @@ export class TimeSheetService extends TenantAwareCrudService<Timesheet> {
// Additional conditions for filtering by tenantId and organizationId
qb.andWhere(p(`"${qb.alias}"."tenantId" = :tenantId`), { tenantId });
qb.andWhere(p(`"${qb.alias}"."organizationId" = :organizationId`), { organizationId });

return qb;
}
}

0 comments on commit 82a2e42

Please sign in to comment.