UNPKG

phx-node

Version:

PHX NODE

136 lines 8.23 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PHXAllowExplanation = void 0; const common_1 = require("@nestjs/common"); const grpc_client_system_service_1 = require("../grpc-client/grpc-client-system.service"); const query_1 = require("../../query/explanation/query"); const constant_1 = require("../../lib/constant"); let PHXAllowExplanation = class PHXAllowExplanation { constructor(grpcClientSystemService) { this.grpcClientSystemService = grpcClientSystemService; } getDurationTime(timeCheckin, timeCheckout, lunchBreakStart, lunchBreakEnd) { if (!timeCheckin || !timeCheckout) return 0; const baseDate = new Date(); baseDate.setHours(0, 0, 0, 0); const parseTimeToDate = (timeStr) => { const [h, m, s] = timeStr.split(":").map(Number); const d = new Date(baseDate); d.setHours(h, m, s || 0, 0); return d; }; const start = parseTimeToDate(timeCheckin); const end = parseTimeToDate(timeCheckout); let totalMinutes = Math.floor((end.getTime() - start.getTime()) / (1000 * 60)); if (lunchBreakStart && lunchBreakEnd) { const breakStart = parseTimeToDate(lunchBreakStart); const breakEnd = parseTimeToDate(lunchBreakEnd); const overlapStart = new Date(Math.max(start.getTime(), breakStart.getTime())); const overlapEnd = new Date(Math.min(end.getTime(), breakEnd.getTime())); const breakDurationMinutes = Math.max(0, (overlapEnd.getTime() - overlapStart.getTime()) / (1000 * 60)); totalMinutes -= breakDurationMinutes; } const totalMinuteCeils = Math.ceil(totalMinutes); const totalHours = totalMinuteCeils / 60; return totalHours; } get(payload) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; const { user_id, hostname, request_code, work_date, start_at, end_at, attendance_code_id, } = payload; try { const isCheckValidateTimeInput = Boolean(attendance_code_id && start_at && end_at); const { hrm_checkin_hrm_checkin_shift_config, hrm_checkin_explanation_config, users, } = yield this.grpcClientSystemService.query({ query: query_1.queryShiftAndExplanationConfig, variables: { user_id }, hostname, }); const isHasData = (hrm_checkin_hrm_checkin_shift_config === null || hrm_checkin_hrm_checkin_shift_config === void 0 ? void 0 : hrm_checkin_hrm_checkin_shift_config.length) > 0 && (hrm_checkin_explanation_config === null || hrm_checkin_explanation_config === void 0 ? void 0 : hrm_checkin_explanation_config.length) > 0 && (users === null || users === void 0 ? void 0 : users.length) > 0; if (!isHasData) return false; const staffInfo = (_a = users[0]) !== null && _a !== void 0 ? _a : {}; const staffDepartmentData = staffInfo.department_users || []; const staffDepartmentId = ((_b = staffDepartmentData[0]) === null || _b === void 0 ? void 0 : _b.department_id) || 0; const limitTimeCreate = (_c = hrm_checkin_explanation_config.find((item) => { var _a; return ((_a = item.approval_type) === null || _a === void 0 ? void 0 : _a.code) === request_code || item.request_type_id === null; })) === null || _c === void 0 ? void 0 : _c.limit_time_create; const dataShiftConfig = hrm_checkin_hrm_checkin_shift_config.find((configData) => configData.school_id === staffInfo.school_id && (configData.department_id === staffDepartmentId || configData.department_id === null)) || {}; if (isCheckValidateTimeInput) { const { hrm_checkin_attendance_code } = yield this.grpcClientSystemService.query({ query: query_1.queryAttendanceCode, variables: { id: attendance_code_id }, hostname, }); const workTimeExplanation = Number(((_d = hrm_checkin_attendance_code === null || hrm_checkin_attendance_code === void 0 ? void 0 : hrm_checkin_attendance_code[0]) === null || _d === void 0 ? void 0 : _d.work_time) || 0); if (!workTimeExplanation) return true; const { break_start, break_end, allow_break_time } = dataShiftConfig; const isHasBreakTime = Boolean(allow_break_time && break_start && break_end); const totalWorkingReal = isHasBreakTime ? this.getDurationTime(start_at, end_at, break_start, break_end) : this.getDurationTime(start_at, end_at); return totalWorkingReal >= workTimeExplanation; } else { if (!limitTimeCreate) return true; const { applicable_days = [] } = dataShiftConfig; const parseDate = (dateStr) => { const [y, m, d] = dateStr.split("-").map(Number); return new Date(y, m - 1, d); }; const getDayName = (date) => constant_1.LIST_DAY_OF_WEEK[date.getDay()]; const workDate = parseDate(work_date); const today = new Date(); const currentDate = new Date(today.getFullYear(), today.getMonth(), today.getDate()); if (workDate > currentDate) return false; let workingDaysPassed = 0; const d = new Date(workDate); while (d < currentDate) { d.setDate(d.getDate() + 1); if (applicable_days.includes(getDayName(d))) { workingDaysPassed++; } } return workingDaysPassed <= Number(limitTimeCreate); } } catch (error) { console.log(error, "error"); return false; } }); } }; exports.PHXAllowExplanation = PHXAllowExplanation; exports.PHXAllowExplanation = PHXAllowExplanation = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [grpc_client_system_service_1.PHXGrpcClientSystemService]) ], PHXAllowExplanation); //# sourceMappingURL=check-allow-explanation.service.js.map