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 147 148 149 150 151 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 9x 9x 3x 6x 6x 5x 6x 6x 6x 6x 6x 1x 5x 4x 5x 4x 4x 4x 4x 2x 2x 2x 2x 1x | 'use strict';
const moment = require('moment-timezone');
const _ = require('lodash');
const Config = use('Config');
const configs = Config.get('modules.staff.general');
const namespace = configs.namespace;
const { STAFF_SCHEDULE_DAILY_UPDATED } = Config.get('modules.staff.constants');
const Event = use('Event');
const CE = use('C2C/Exceptions');
const { checkSchedule } = use('C2C/Helpers');
class StaffScheduleDailyController {
static get inject() {
return [
`${namespace}/Services/StaffService`,
`${namespace}/Services/TargetSystemService`,
`${namespace}/Services/SalonService`,
'C2C/Services/QueueService',
`C2C/Services/SalonService`,
`${namespace}/Listeners/StaffSchedule`,
];
}
constructor(
staffService,
targetSystemService,
salonService,
queueService,
sharedSalonService,
staffScheduleListener,
) {
this.staffService = staffService;
this.targetSystemService = targetSystemService;
this.salonService = salonService;
this.queue = queueService;
this.sharedSalonService = sharedSalonService;
this.staffScheduleListener = staffScheduleListener;
}
/**
* @swagger
*
* /api/v1/staff/{staffId}/schedules/daily:
* put:
* tags:
* - staff
* operationId: 'updateScheduleDaily'
* summary: 'updateScheduleDaily'
* parameters:
* - $ref: '#/components/parameters/StaffIdParam'
* requestBody:
* description: 'schedule daily of staff'
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/StaffScheduleDailyBody'
* responses:
* 200:
* description: |-
* success
* content:
* application/json; charset=utf-8:
* schema:
* $ref: '#/components/schemas/ApiResponse'
* 401:
* $ref: '#/components/responses/Unauthorized'
* 403:
* $ref: '#/components/responses/Forbidden'
* 404:
* $ref: '#/components/responses/NotFound'
* 422:
* $ref: '#/components/responses/BadData'
* deprecated: false
* security:
* - BearerAuth: []
*/
async update({ request, response, thirdParty }) {
const { schedules } = request.all();
const staffId = request.params.id;
const user = request.user;
const salonId = _.get(user, 'userMetadata.salonId');
const salon = await this.salonService.findOrFail(salonId);
const staff = await this.staffService.findOneOrFail({
id: staffId,
userId: _.get(user, 'userId'),
});
const isLocked = await this.sharedSalonService.checkLockSchedule({ salon, staff, thirdParty });
if (isLocked) {
throw CE.ForbiddenException.raise('errors.scheduleLocked');
}
const timezone = salon.timezone;
await checkSchedule(request.all(), timezone);
const schedulesData = schedules.map((schedule) => {
const startAt = moment(schedule.startAt).tz(timezone);
const endAt = moment(schedule.endAt).tz(timezone);
const weekOfYear = startAt.clone().week();
const isGreaterTime = startAt.isSame(endAt, 'day') && startAt.isSameOrBefore(endAt);
if (!isGreaterTime) {
throw CE.BadRequestException.raise('errors.invalidWorkingTime');
}
return {
staffId: staff.id,
salonId: salon.id,
dayOfWeek: startAt.clone().day(),
weekOfYear: weekOfYear,
startOfDay: startAt.clone().startOf('day'),
endOfDay: endAt.clone().endOf('day'),
startAt: startAt.clone(),
endAt: endAt.clone(),
};
});
// execute job to update
const dataUpdated = {
staffId: staff.id,
salonId: salon.id,
timezone,
schedules: schedulesData.map(({ dayOfWeek, startAt, endAt }) => ({
dayOfWeek,
startAt: startAt.toISOString(),
endAt: endAt.toISOString(),
})),
};
const Job = use(`${namespace}/Jobs/UpdateStaffScheduleDaily`);
try {
const job = await this.queue.add(Job.key, {
conditions: { staffId: staff.id, salonId: salon.id },
schedulesData,
timezone,
});
await job.finished();
await this.staffScheduleListener.syncSystemStaffScheduleDaily(dataUpdated);
} catch ({ message }) {
throw CE.BadRequestException.raise(message);
}
// trigger event
Event.fire(STAFF_SCHEDULE_DAILY_UPDATED, dataUpdated);
return response.success({});
}
}
module.exports = StaffScheduleDailyController;
|