Skip to content

Commit

Permalink
feat(timeclock): add pagination and improve query filter (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
nandinboldn committed Jan 9, 2023
1 parent 378ef04 commit 8affeea
Show file tree
Hide file tree
Showing 12 changed files with 436 additions and 404 deletions.
292 changes: 32 additions & 260 deletions packages/plugin-timeclock-api/src/graphql/resolvers/queries.ts
Expand Up @@ -7,116 +7,14 @@ import {
IReport,
IUserReport
} from '../../models/definitions/timeclock';
import { generateFilter } from '../../utils';
import { paginate } from '@erxes/api-utils/src';

const timeclockQueries = {
async absences(
_root,
{ startDate, endDate, userIds, branchIds, departmentIds },
{ models, subdomain }: IContext
) {
const totalUserIds: string[] = [];
let commonUser: boolean = false;
let dateGiven: boolean = false;
const absenceSelector: any = { status: { $ne: 'Holiday' } };

if (branchIds) {
for (const branchId of branchIds) {
const branch = await findBranch(subdomain, branchId);
if (userIds) {
commonUser = true;
for (const userId of userIds) {
if (branch.userIds.includes(userId)) {
totalUserIds.push(userId);
}
}
} else {
totalUserIds.push(...branch.userIds);
}
}
}
if (departmentIds) {
for (const deptId of departmentIds) {
const department = await findDepartment(subdomain, deptId);
if (userIds) {
commonUser = true;
for (const userId of userIds) {
if (department.userIds.includes(userId)) {
totalUserIds.push(userId);
}
}
} else {
totalUserIds.push(...department.userIds);
}
}
}

if (!commonUser && userIds) {
totalUserIds.push(...userIds);
}

const timeFields = [
{
startTime:
startDate && endDate
? {
$gte: fixDate(startDate),
$lte: fixDate(endDate)
}
: startDate
? {
$gte: fixDate(startDate)
}
: { $lte: fixDate(endDate) }
},
{
endTime:
startDate && endDate
? {
$gte: fixDate(startDate),
$lte: fixDate(endDate)
}
: startDate
? {
$gte: fixDate(startDate)
}
: { $lte: fixDate(endDate) }
}
];

if (startDate || endDate) {
dateGiven = true;
}

let returnModel: any = [];

for (const userId of totalUserIds) {
returnModel.push(
...(dateGiven
? await models.Absences.find({
$and: [...timeFields, { userId: `${userId}` }, absenceSelector]
})
: await models.Absences.find({
userId: `${userId}`,
...absenceSelector
}))
);
}

if (!departmentIds && !branchIds && !userIds) {
if (dateGiven) {
returnModel.push(
...(await models.Absences.find({
$and: [...timeFields, absenceSelector]
}))
);
}
// if no filter is given, return everything
else {
returnModel = models.Absences.find(absenceSelector);
}
}

return returnModel;
async absences(_root, queryParams, { models, subdomain }: IContext) {
return models.Absences.find(
await generateFilter(queryParams, subdomain, 'absence')
);
},

absenceTypes(_root, {}, { models }: IContext) {
Expand All @@ -127,166 +25,40 @@ const timeclockQueries = {
return models.Absences.find({ status: 'Holiday' });
},

async timeclocks(
_root,
{ startDate, endDate, userIds, branchIds, departmentIds },
{ subdomain, models }: IContext
) {
const totalUserIds: string[] = [];
let commonUser: boolean = false;
let dateGiven: boolean = false;

if (branchIds) {
for (const branchId of branchIds) {
const branch = await findBranch(subdomain, branchId);
if (userIds) {
commonUser = true;
for (const userId of userIds) {
if (branch.userIds.includes(userId)) {
totalUserIds.push(userId);
}
}
} else {
totalUserIds.push(...branch.userIds);
}
}
}
if (departmentIds) {
for (const deptId of departmentIds) {
const department = await findDepartment(subdomain, deptId);
if (userIds) {
commonUser = true;
for (const userId of userIds) {
if (department.userIds.includes(userId)) {
totalUserIds.push(userId);
}
}
} else {
totalUserIds.push(...department.userIds);
}
}
}

if (!commonUser && userIds) {
totalUserIds.push(...userIds);
}
async timeclocksMain(_root, queryParams, { subdomain, models }: IContext) {
const selector = await generateFilter(queryParams, subdomain, 'timeclock');
const queryList = models.Timeclocks.find(selector);

const timeFields = [
{
shiftStart:
startDate && endDate
? {
$gte: fixDate(startDate),
$lte: fixDate(endDate)
}
: startDate
? {
$gte: fixDate(startDate)
}
: { $lte: fixDate(endDate) }
},
{
shiftEnd:
startDate && endDate
? {
$gte: fixDate(startDate),
$lte: fixDate(endDate)
}
: startDate
? {
$gte: fixDate(startDate)
}
: { $lte: fixDate(endDate) }
}
];

if (startDate || endDate) {
dateGiven = true;
}

let returnModel: any = [];

for (const userId of totalUserIds) {
returnModel.push(
...(dateGiven
? await models.Timeclocks.find({
$and: [...timeFields, { userId: `${userId}` }]
})
: await models.Timeclocks.find({ userId: `${userId}` }))
);
}

if (!departmentIds && !branchIds && !userIds) {
if (dateGiven) {
returnModel.push(
...(await models.Timeclocks.find({ $or: [...timeFields] }))
);
}
// if no filter is given, return everything
else {
returnModel = models.Timeclocks.find();
}
}
const list = paginate(models.Timeclocks.find(selector), {
perPage: queryParams.perPage,
page: queryParams.page
});

return returnModel;
const totalCount = queryList.countDocuments();
return { list, totalCount };
},
async schedulesMain(_root, queryParams, { models, subdomain }: IContext) {
const selector = await generateFilter(queryParams, subdomain, 'schedule');
const totalCount = models.Schedules.find(selector).countDocuments();

async schedules(
_root,
{ userIds, departmentIds, branchIds },
{ models, subdomain }: IContext
) {
const totalUserIds: string[] = [];
let commonUser: boolean = false;

if (branchIds) {
for (const branchId of branchIds) {
const branch = await findBranch(subdomain, branchId);
if (userIds) {
commonUser = true;
for (const userId of userIds) {
if (branch.userIds.includes(userId)) {
totalUserIds.push(userId);
}
}
} else {
totalUserIds.push(...branch.userIds);
}
}
}
if (departmentIds) {
for (const deptId of departmentIds) {
const department = await findDepartment(subdomain, deptId);
if (userIds) {
commonUser = true;
for (const userId of userIds) {
if (department.userIds.includes(userId)) {
totalUserIds.push(userId);
}
}
} else {
totalUserIds.push(...department.userIds);
}
}
}

if (!commonUser && userIds) {
totalUserIds.push(...userIds);
}
const list = paginate(models.Schedules.find(selector), {
perPage: queryParams.perPage,
page: queryParams.page
});

let returnModel: any = [];
return { list, totalCount };
},

for (const userId of totalUserIds) {
returnModel.push(
...(await models.Schedules.find({ userId: `${userId}` }))
);
}
async requestsMain(_root, queryParams, { models, subdomain }: IContext) {
const selector = await generateFilter(queryParams, subdomain, 'absence');
const totalCount = models.Absences.find(selector).countDocuments();

if (!departmentIds && !branchIds && !userIds) {
returnModel = models.Schedules.find();
}
const list = paginate(models.Absences.find(selector), {
perPage: queryParams.perPage,
page: queryParams.page
});

return returnModel;
return { list, totalCount };
},

payDates(_root, {}, { models }: IContext) {
Expand Down
54 changes: 40 additions & 14 deletions packages/plugin-timeclock-api/src/graphql/schema.ts
Expand Up @@ -107,20 +107,21 @@ export const types = `
payDates: [Int]
}
`;

export const queries = `
timeclocks(startDate: Date, endDate: Date, userIds: [String], branchIds: [String], departmentIds: [String]): [Timeclock]
absences(startDate: Date, endDate: Date, userIds: [String], branchIds: [String], departmentIds: [String]): [Absence]
schedules(startDate: Date, endDate: Date, userIds: [String], branchIds: [String], departmentIds: [String]): [Schedule]
absenceTypes:[AbsenceType]
timeclockReports(departmentIds: [String], branchIds: [String], userIds: [String]): [Report]
timeclockReportByUser(selectedUser: String): UserReport
timeclockDetail(_id: String!): Timeclock
absenceDetail(_id: String!): Absence
scheduleDetail(_id: String!): Schedule
payDates: [PayDate]
holidays: [Absence]
type TimeClocksListResponse {
list: [Timeclock]
totalCount: Float
}
type SchedulesListResponse {
list: [Schedule]
totalCount: Float
}
type RequestsListResponse {
list: [Absence]
totalCount: Float
}
`;

const params = `
Expand All @@ -131,6 +132,16 @@ const params = `
deviceType: String
`;

const queryParams = `
page: Int
perPage: Int
startDate: Date
endDate: Date
userIds: [String]
branchIds: [String]
departmentIds: [String]
`;

const absence_params = `
userId: String
startTime: Date
Expand All @@ -151,6 +162,21 @@ const schedule_params = `
shifts: [ShiftsRequestInput]
`;

export const queries = `
timeclocksMain(${queryParams}): TimeClocksListResponse
schedulesMain(${queryParams}): SchedulesListResponse
requestsMain(${queryParams}): RequestsListResponse
absenceTypes:[AbsenceType]
timeclockReports(departmentIds: [String], branchIds: [String], userIds: [String]): [Report]
timeclockReportByUser(selectedUser: String): UserReport
timeclockDetail(_id: String!): Timeclock
absenceDetail(_id: String!): Absence
scheduleDetail(_id: String!): Schedule
payDates: [PayDate]
holidays: [Absence]
`;

export const mutations = `
timeclockStart(${params}): Timeclock
timeclockStop(${params}): Timeclock
Expand Down

0 comments on commit 8affeea

Please sign in to comment.