Skip to content

Commit

Permalink
feat(timeclock): improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
nandinboldn committed Dec 22, 2022
1 parent 8688dec commit a9fa3bb
Show file tree
Hide file tree
Showing 37 changed files with 1,479 additions and 844 deletions.
9 changes: 8 additions & 1 deletion packages/plugin-timeclock-api/.env.sample
Expand Up @@ -11,4 +11,11 @@ RABBITMQ_HOST=amqp://localhost
# Redis
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=
REDIS_PASSWORD=

# MySQL
MYSQL_HOST=
MYSQL_DB=
MYSQL_USERNAME=
MYSQL_PASSWORD=
MYSQL_TABLE=
129 changes: 129 additions & 0 deletions packages/plugin-timeclock-api/cronjobs/timelock.ts
@@ -0,0 +1,129 @@
import { Sequelize, DataTypes, Op } from 'sequelize';
import { generateModels, models } from '../src/connectionResolver';

/**
* Connects to mysql server and extracts
*/

export interface ITimeClock {
userId?: string;
employeeId?: string;
shiftStart: Date;
shiftEnd?: Date;
shiftActive?: boolean;
branchName?: number;
latitude?: number;
}

export const connectToMysql = async (req, res) => {
const sequelize = new Sequelize('testt', 'nandi', 'password', {
host: 'localhost',
dialect: 'mysql',
define: { freezeTableName: true }
});

sequelize
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(error => {
console.error('Unable to connect to the database: ', error);
});

const Timeclock = sequelize.define(
'`dbo.attLog`',
{
ID: {
type: DataTypes.STRING
},
authDateTime: {
type: DataTypes.DATE
},
authDate: {
type: DataTypes.DATEONLY
},
authTime: {
type: DataTypes.TIME
},
direction: {
type: DataTypes.STRING
},
deviceName: {
type: DataTypes.STRING
},
deviceSerialNo: {
type: DataTypes.STRING
},
employeeName: {
type: DataTypes.STRING
},
cardNo: {
type: DataTypes.STRING
}
},
{ timestamps: false }
);

const data = extractAllData(Timeclock);
// console.log('111111', data);

// for (const timeRow of data) {

// }

// extractNewData(Timeclock);
res.json('success');
};

const extractAllData = (db: any) => {
let data;
const timeclockData: ITimeClock[] = [];

db.findAll({
raw: true,
order: [
['ID', 'ASC'],
['authDateTime', 'ASC']
],
limit: 10
})
.then(response => {
data = JSON.parse(JSON.stringify(response));
for (const row of data) {
// console.log('heheh', row);
// const empId = row._ID;
// const branch = row.deviceName;
// const empName = row.employeeName;
// const getEarliestTime = row.authDateTime;
// const getLatestTime = row.authDateTime;
// console.log('hjehhehehhe', empId, branch, empName);
// const time = models?.Timeclocks.createTimeClock({
// shiftStart: getEarliestTime,
// shiftEnd: getLatestTime,
// shiftActive: false
// });
}
})
.catch(error => {
console.error('Failed to retrieve data : ', error);
});

return data;
};

const extractNewData = (db: any) => {
// find all time clock of today
const time = new Date('2022-12-22');

const arr = db
.findAll({
raw: true,
authDateTime: {
where: { [Op.gte]: time }
}
})
.then(res => {
// console.log('22222222222222', res);
});
};
4 changes: 4 additions & 0 deletions packages/plugin-timeclock-api/package.json
Expand Up @@ -6,5 +6,9 @@
"dev": "cd .erxes && yarn dev",
"build": "cd .erxes && yarn build",
"start": "cd .erxes/dist/plugin-timeclock-api/.erxes && node src"
},
"dependencies": {
"mysql2": "^2.3.3",
"sequelize": "^6.27.0"
}
}
5 changes: 5 additions & 0 deletions packages/plugin-timeclock-api/src/configs.ts
Expand Up @@ -4,6 +4,8 @@ import resolvers from './graphql/resolvers';
import { initBroker } from './messageBroker';
import { getSubdomain } from '@erxes/api-utils/src/core';
import { generateModels } from './connectionResolver';
import { connectToMysql } from '../cronjobs/timelock';
import * as cors from 'cors';

export let mainDb;
export let debug;
Expand Down Expand Up @@ -33,6 +35,9 @@ export default {

onServerInit: async options => {
mainDb = options.db;
const app = options.app;

app.get('/mysql', connectToMysql);

initBroker(options.messageBrokerClient);

Expand Down
62 changes: 42 additions & 20 deletions packages/plugin-timeclock-api/src/graphql/resolvers/mutations.ts
Expand Up @@ -8,7 +8,6 @@ import {
IAbsenceType
} from '../../models/definitions/timeclock';
import { findBranches } from './utils';
import { loadShiftClass } from '../../models/Timeclock';

interface ITimeClockEdit extends ITimeClock {
_id: string;
Expand Down Expand Up @@ -201,7 +200,7 @@ const timeclockMutations = {
async solveAbsenceRequest(
_root,
{ _id, status, ...doc }: IAbsenceEdit,
{ models, user }: IContext
{ models }: IContext
) {
const absence = models.Absences.getAbsence(_id);
let updated = models.Absences.updateAbsence(_id, {
Expand All @@ -217,27 +216,33 @@ const timeclockMutations = {
shiftRequest.reason &&
shiftRequest.reason.toLocaleLowerCase() === 'shift request'
) {
updated = models.Absences.updateAbsence(_id, { status: 'Shift', ...doc });
const newSchedule = await models.Schedules.createSchedule({
userId: user._id,
solved: true,
status: 'Approved'
updated = models.Absences.updateAbsence(_id, {
status: `Shift / ${status}`,
...doc
});
// if shift request is approved
if (status === 'Approved') {
const newSchedule = await models.Schedules.createSchedule({
userId: shiftRequest.userId,
solved: true,
status: 'Approved'
});

await models.Shifts.createShift({
scheduleId: newSchedule._id,
shiftStart: shiftRequest.startTime,
shiftEnd: shiftRequest.endTime,
solved: true,
status: 'Approved'
});
await models.Shifts.createShift({
scheduleId: newSchedule._id,
shiftStart: shiftRequest.startTime,
shiftEnd: shiftRequest.endTime,
solved: true,
status: 'Approved'
});

await models.Timeclocks.createTimeClock({
userId: user._id,
shiftStart: shiftRequest.startTime,
shiftEnd: shiftRequest.endTime,
shiftActive: false
});
await models.Timeclocks.createTimeClock({
userId: shiftRequest.userId,
shiftStart: shiftRequest.startTime,
shiftEnd: shiftRequest.endTime,
shiftActive: false
});
}
}

return updated;
Expand Down Expand Up @@ -335,6 +340,23 @@ const timeclockMutations = {
return schedule;
},

scheduleRemove(_root, { _id }, { models }: IContext) {
models.Schedules.removeSchedule(_id);
models.Shifts.remove({ scheduleId: _id });
return;
},
async scheduleShiftRemove(_root, { _id }, { models }: IContext) {
const getShift = await models.Shifts.getShift(_id);
const getShiftsCount = await models.Shifts.count({
scheduleId: getShift.scheduleId
});
// if it's the only one shift in schedule, remove schedule
if (getShiftsCount === 1 && getShift.scheduleId) {
await models.Schedules.removeSchedule(getShift.scheduleId);
}

return models.Shifts.removeShift(_id);
},
payDateAdd(_root, { dateNums }, { models }: IContext) {
return models.PayDates.createPayDate({ payDates: dateNums });
},
Expand Down

0 comments on commit a9fa3bb

Please sign in to comment.