UNPKG

phx-node

Version:

PHX NODE

131 lines 6.92 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.GetActualWorkingHoursService = 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/hrm-checkin/query"); let GetActualWorkingHoursService = class GetActualWorkingHoursService { constructor(grpcClientSystemService) { this.grpcClientSystemService = grpcClientSystemService; } getDurationTime(timeCheckin, timeCheckout, lunchBreakStart, lunchBreakEnd) { if (!timeCheckin || !timeCheckout) return 0; const start = new Date(timeCheckin); const end = new Date(timeCheckout); let totalMs = end.getTime() - start.getTime(); if (lunchBreakStart && lunchBreakEnd) { const parseTimeToDate = (base, timeStr) => { const [h, m, s] = timeStr.split(":").map(Number); const d = new Date(base); d.setHours(h, m, s || 0, 0); return d; }; const breakStart = parseTimeToDate(start, lunchBreakStart); const breakEnd = parseTimeToDate(start, lunchBreakEnd); const overlapStart = Math.max(start.getTime(), breakStart.getTime()); const overlapEnd = Math.min(end.getTime(), breakEnd.getTime()); const breakMs = Math.max(0, overlapEnd - overlapStart); totalMs -= breakMs; } const totalMinutes = Math.ceil(totalMs / 60000); const totalHours = totalMinutes / 60; const hoursView = Math.floor(totalHours); const minutes = totalMinutes % 60; return { totalMinutes, totalHours: Math.round(totalHours * 100) / 100, view: `${hoursView}h${minutes.toString().padStart(2, "0")}`, }; } getTimeCheckInOut(listCheckins) { var _a, _b; const lengthOfList = listCheckins.length; if (lengthOfList < 2) return ""; const timeCheckIn = (_a = listCheckins[0]) === null || _a === void 0 ? void 0 : _a.created_at; return { checkIn: timeCheckIn, checkOut: (_b = listCheckins[lengthOfList - 1]) === null || _b === void 0 ? void 0 : _b.created_at, }; } getTotalActualWorkingHours(payload) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; const { user_id, hostname, working_day } = payload; const isHasFullData = Boolean(user_id && hostname && working_day); if (!isHasFullData) return 0; const res = yield this.grpcClientSystemService.query({ query: query_1.queryProfileStaff, variables: { work_date: working_day, user_id, }, hostname, }); const isHasStaffData = res && ((_a = res.users) === null || _a === void 0 ? void 0 : _a.length) > 0; if (!isHasStaffData) return 0; const profileStaff = res.users[0]; const { school, attendance_raws = [], department_users = [], } = profileStaff; const attendance_raws_by_workdate = attendance_raws.filter((item) => item === null || item === void 0 ? void 0 : item.created_at.startsWith(working_day)); const dataCheckInOut = this.getTimeCheckInOut(attendance_raws_by_workdate || []); if (!dataCheckInOut) return 0; const staffDepartmentData = department_users || []; const staffDepartmentId = ((_b = staffDepartmentData[0]) === null || _b === void 0 ? void 0 : _b.department_id) || 0; const listAttendanceConfigs = (school === null || school === void 0 ? void 0 : school.hrm_checkin_shift_configs) || []; const dataAttendanceConfig = listAttendanceConfigs.find((configData) => configData.department_id === staffDepartmentId || configData.department_id === null) || {}; const { break_start, break_end, allow_break_time } = dataAttendanceConfig; const isHasBreakTime = Boolean(allow_break_time && break_start && break_end); return isHasBreakTime ? this.getDurationTime(dataCheckInOut.checkIn, dataCheckInOut.checkOut, break_start, break_end) : this.getDurationTime(dataCheckInOut.checkIn, dataCheckInOut.checkOut); }); } get(payload) { return __awaiter(this, void 0, void 0, function* () { try { const res = yield this.getTotalActualWorkingHours(payload); if (res) { return res; } return { totalMinutes: 0, totalHours: 0, view: "", }; } catch (error) { console.log(`Không tìm được giờ làm thực tế: ${new Date()}-${error}`); return ""; } }); } }; exports.GetActualWorkingHoursService = GetActualWorkingHoursService; exports.GetActualWorkingHoursService = GetActualWorkingHoursService = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [grpc_client_system_service_1.PHXGrpcClientSystemService]) ], GetActualWorkingHoursService); //# sourceMappingURL=get-actual-working-hours.service.js.map