Skip to content

Commit

Permalink
feat(timeclock): extract data improvement (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
nandinboldn committed Jan 17, 2023
1 parent 4b59e68 commit c03c0f2
Show file tree
Hide file tree
Showing 23 changed files with 699 additions and 596 deletions.
20 changes: 6 additions & 14 deletions packages/plugin-timeclock-api/src/cronjobs/timelock.ts
@@ -1,25 +1,17 @@
import * as dayjs from 'dayjs';
import { getEnv } from '@erxes/api-utils/src';
import { connectAndQueryFromMySql } from '../utils';

const connectAndImportFromMysql = async (subdomain: string) => {
const MYSQL_TABLE = getEnv({ name: 'MYSQL_TABLE' });

const connectAndImportFromMysql = (subdomain: string) => {
// get time data from yesterday till now
const format = 'YYYY-MM-DD HH:mm:ss';
const NOW = dayjs(Date.now());
const YESTERDAY = NOW.add(-1, 'day');

const query =
'SELECT * FROM ' +
MYSQL_TABLE +
" WHERE authDateTime >= '" +
YESTERDAY.format(format) +
"' AND authDateTime < '" +
NOW.format(format) +
"' ORDER by ID, authDateTime";

return await connectAndQueryFromMySql(subdomain, query);
return connectAndQueryFromMySql(
subdomain,
YESTERDAY.format(format),
NOW.format(format)
);
};

export default {
Expand Down
26 changes: 16 additions & 10 deletions packages/plugin-timeclock-api/src/graphql/resolvers/mutations.ts
Expand Up @@ -8,13 +8,13 @@ import {
IAbsenceType
} from '../../models/definitions/timeclock';
import {
connectAndImportFromMysql,
createScheduleShiftsByUserIds,
findBranch,
findBranches,
findDepartment
} from './utils';
import dayjs = require('dayjs');
import { connectAndQueryFromMySql } from '../../utils';

interface ITimeClockEdit extends ITimeClock {
_id: string;
Expand Down Expand Up @@ -315,10 +315,8 @@ const timeclockMutations = {
return updated;
},

async sendScheduleRequest(_root, { userId, shifts }, { models }: IContext) {
const schedule = await models.Schedules.createSchedule({
userId: `${userId}`
});
async sendScheduleRequest(_root, { shifts, ...doc }, { models }: IContext) {
const schedule = await models.Schedules.createSchedule(doc);

shifts.map(shift => {
models.Shifts.createShift({
Expand All @@ -333,11 +331,16 @@ const timeclockMutations = {

async submitSchedule(
_root,
{ branchIds, departmentIds, userIds, shifts },
{ branchIds, departmentIds, userIds, shifts, scheduleConfigId },
{ subdomain, models }: IContext
) {
if (userIds.length) {
return createScheduleShiftsByUserIds(userIds, shifts, models);
return createScheduleShiftsByUserIds(
userIds,
shifts,
models,
scheduleConfigId
);
}

const concatBranchDept: string[] = [];
Expand Down Expand Up @@ -502,9 +505,12 @@ const timeclockMutations = {
return newScheduleConfig;
},

async extractAllDataFromMySQL(_root, {}, { subdomain }: IContext) {
const ret = await connectAndImportFromMysql(subdomain);
return ret;
async extractAllDataFromMySQL(
_root,
{ startDate, endDate },
{ subdomain }: IContext
) {
return await connectAndQueryFromMySql(subdomain, startDate, endDate);
}
};

Expand Down

0 comments on commit c03c0f2

Please sign in to comment.