Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | 1x 1x 86x 1x 6x 12x 5x 5x 5x 5x 5x 5x 5x 3x 3x 3x 3x 3x 3x 3x 3x 5x 3x 5x 3x 5x 18x 18x 18x 14x 14x 18x 18x 5x 14x 5x 3x 12x 12x 16x 12x 3x 3x 3x 3x 3x 3x 3x 1x | 'use strict';
const _ = require('lodash');
const moment = require('moment-timezone');
class SyncConcurrentSystemStaffDailySchedules {
static get key() {
return 'SyncConcurrentSystemStaffDailySchedules-job';
}
static get concurrency() {
return 2;
}
static buildJobId(staffId, time) {
return `${staffId}-${time}`;
}
static buildJobDataRedisKey(staffId, time) {
return `${SyncConcurrentSystemStaffDailySchedules.key}data:${staffId}:${time}`;
}
static async removeJobDataOnRedis(job) {
const Logger = use('Logger');
const Redis = use('Redis');
// Get job data by staffId & time
const { data } = job;
const { staffId, time } = data;
const jobDataKey = SyncConcurrentSystemStaffDailySchedules.buildJobDataRedisKey(staffId, time);
try {
await Redis.del(jobDataKey);
} catch (e) {
Logger.error(
'SyncConcurrentSystemStaffDailySchedules.removeJobDataOnRedis-Redis.del failed-[%s]: %s',
jobDataKey,
JSON.stringify({
error: {
message: e.message,
stack: e.stack,
},
}),
);
}
}
async handle(job) {
const Config = use('Config');
const Logger = use('Logger');
const Redis = use('Redis');
// Get job data by staffId & time
const { data } = job;
const { staffId, time } = data;
const jobDataKey = SyncConcurrentSystemStaffDailySchedules.buildJobDataRedisKey(staffId, time);
const arrStrJsonSchedules = await Redis.lrange(jobDataKey, 0, -1);
// parse to json object and sort by time (trigger time)
const arrSchedules = _.sortBy(
arrStrJsonSchedules.map((str) => {
return JSON.parse(str);
}),
['time'],
);
// build schedules data to send to target system
const { timezone, dailySchedulesMap } = arrSchedules.reduce(
(obj, { timezone, schedules: newSchedules }) => {
// newSchedules: { dayOfWeek, startAt, endAt }
if (!obj.timezone) {
obj.timezone = timezone;
}
// group new schedules by date
const newSchedulesMap = newSchedules.reduce((map, s) => {
const dayStr = moment(s.startAt).tz(obj.timezone).format('YYYY-MM-DD');
let schedules = map[dayStr];
if (!schedules) {
schedules = [];
map[dayStr] = schedules;
}
schedules.push(s);
return map;
}, {});
// overwrite daily schedules with the newest into dailySchedulesMap
for (const k in newSchedulesMap) {
obj.dailySchedulesMap[k] = newSchedulesMap[k];
}
return obj;
},
{
timezone: undefined,
dailySchedulesMap: {},
},
);
const schedulesData = {
staffId,
body: {
timezone,
schedules: Object.values(dailySchedulesMap).reduce((arr, schedules) => {
schedules = _.sortBy(schedules, ['startAt']);
schedules.forEach((s) => {
arr.push(s);
});
return arr;
}, []),
},
};
Logger.info(
'SyncConcurrentSystemStaffDailySchedules.handle-[%s]: %s',
jobDataKey,
JSON.stringify({
inputSchedules: arrSchedules,
schedulesDataForSystem: schedulesData,
}),
);
const staffConfigs = Config.get('modules.staff.general');
const namespace = staffConfigs.namespace;
const targetSystemService = make(`${namespace}/Services/TargetSystemService`);
await targetSystemService.updateStaffScheduleDaily(schedulesData).then(async () => {
try {
await Redis.del(jobDataKey);
} catch (e) {
Logger.error(
'SyncConcurrentSystemStaffDailySchedules.handle-Redis.del failed-[%s]: %s',
jobDataKey,
JSON.stringify({
error: {
message: e.message,
stack: e.stack,
},
}),
);
}
});
}
}
module.exports = SyncConcurrentSystemStaffDailySchedules;
|