Skip to content
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

[Enhancement] Add Timesheet status filter #8547

Merged
merged 3 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading