phx-react
Version:
PHX REACT
166 lines • 7.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.dateStrToDate = exports.triggerMessage = exports.formatDateMMDDYY = exports.sortClassroomByName = exports.processBorrowBook = exports.fomartBookCode = exports.getProfile = exports.getRoleName = exports.getCurrentDateTime = exports.getValidBookCodeLength = void 0;
exports.getDateOffsetByDays = getDateOffsetByDays;
const __1 = require("../../../..");
const eventBus_1 = require("../../utils/eventBus");
const constants_1 = require("./constants");
const getValidBookCodeLength = (prefixCode) => prefixCode.length + constants_1.VALID_BOOK_CODE_LENGTH;
exports.getValidBookCodeLength = getValidBookCodeLength;
const getCurrentDateTime = (date = new Date()) => {
const day = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate();
const month = date.getMonth() + 1 < 10 ? `0${date.getMonth() + 1}` : date.getMonth() + 1;
const year = date.getFullYear();
const hour = date.getHours();
const minute = date.getMinutes() > 9 ? date.getMinutes() : `0${date.getMinutes()}`;
const second = date.getSeconds() > 9 ? date.getSeconds() : `0${date.getSeconds()}`;
return `${day}/${month}/${year} ${hour}:${minute}:${second}`;
};
exports.getCurrentDateTime = getCurrentDateTime;
const getRoleName = (userRoles) => {
var _a, _b;
try {
if (!userRoles)
return __1.EMPTY_VALUE;
// Nếu có role học sinh thì trả luôn là Học sinh
const studentRoleName = (_b = (_a = userRoles === null || userRoles === void 0 ? void 0 : userRoles.filter((item) => (item === null || item === void 0 ? void 0 : item.role.role_code) === constants_1.KEY_HOC_SINH_CODE)) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.role.name;
if (studentRoleName)
return studentRoleName;
const StaffAndTeacherRoleNameArray = [];
userRoles === null || userRoles === void 0 ? void 0 : userRoles.forEach((item) => {
const roleCode = item.role.role_code;
if (roleCode === constants_1.KEY_NHAN_VIEN_CODE || roleCode === constants_1.KEY_GIAO_VIEN_CODE) {
const roleName = item.role.name;
StaffAndTeacherRoleNameArray.push(roleName);
}
});
return StaffAndTeacherRoleNameArray.join(', ') || __1.EMPTY_VALUE;
}
catch (e) {
console.log(e);
return __1.EMPTY_VALUE;
}
};
exports.getRoleName = getRoleName;
const getProfile = (user) => {
const validProfile = ['profile_staff', 'profile_student', 'profile_teacher']
.map((key) => user === null || user === void 0 ? void 0 : user[key])
.find((profile) => profile && (profile === null || profile === void 0 ? void 0 : profile.user_code) !== null);
return validProfile;
};
exports.getProfile = getProfile;
const fomartBookCode = (code, qrPrefix) => {
if (!qrPrefix || !code)
return '';
if ((code === null || code === void 0 ? void 0 : code.length) > (0, exports.getValidBookCodeLength)(qrPrefix))
return code;
let formatedCode = code.toUpperCase().replace(/P{2,}/g, '');
const splitCode = code.split('-');
if (splitCode.length < 2)
return formatedCode;
formatedCode = `${qrPrefix}-${splitCode[1]}`;
return qrPrefix ? formatedCode.slice(0, (0, exports.getValidBookCodeLength)(qrPrefix)) : '';
};
exports.fomartBookCode = fomartBookCode;
const processBorrowBook = (arrBook, listBookState) => {
const inBookshelfBookArray = [];
const inWarehouseBookArray = [];
const returnBorrowBook = [];
const returnAllTicketIdArray = [];
try {
// nếu tổng số sách đã quét của phiếu bằng tổng số sách mượn của phiếu thì phiếu đó sẽ được trả hết
// => chuyển trạng thái phiếu sang đã trả, push ticketId vào mảng returnAllTicketIdArray
// lưu sách mượn vào mảng returnBookArray để trả từng quyển
arrBook.forEach((item) => {
const { listborrowBook, ticketId, totalBorrowed, totalReturned } = item;
// nếu tổng số sách được quét của 1 vé mượn = tổng số sách mượn - tổng sách đã trả thì phiếu đó sẽ được chuyển sang đã trả
if (listborrowBook.length === totalBorrowed - totalReturned) {
returnAllTicketIdArray.push(ticketId);
}
listborrowBook.forEach((borrowBook) => {
var _a, _b;
const { bookBorrowId, bookId, preStateId } = borrowBook;
if (preStateId === ((_a = listBookState.find((state) => state.code === constants_1.IN_WAREHOUSE_STATE_CODE)) === null || _a === void 0 ? void 0 : _a.id)) {
inWarehouseBookArray.push(bookId);
}
else if (preStateId === ((_b = listBookState.find((state) => state.code === constants_1.IN_BOOKSHELF_STATE_CODE)) === null || _b === void 0 ? void 0 : _b.id)) {
inBookshelfBookArray.push(bookId);
}
else {
inBookshelfBookArray.push(bookId);
}
returnBorrowBook.push(bookBorrowId);
});
});
return {
inBookshelfBookArray,
inWarehouseBookArray,
returnBorrowBook,
returnAllTicketIdArray,
};
}
catch (e) {
console.log(e);
return {
inBookshelfBookArray,
inWarehouseBookArray,
returnBorrowBook,
returnAllTicketIdArray,
};
}
};
exports.processBorrowBook = processBorrowBook;
// hàm này trả ra mm/dd/yyyy cách ngày hiện tại daysOffset ngày
function getDateOffsetByDays(daysOffset, type = 'default') {
const currentDate = new Date();
currentDate.setDate(currentDate.getDate() + daysOffset);
const day = currentDate.getDate();
const month = currentDate.getMonth() + 1;
const year = currentDate.getFullYear();
if (type === 'datePicker') {
return String(day).padStart(2, '0') + '-' + String(month).padStart(2, '0') + '-' + year;
}
return `${month}/${day}/${year}`;
}
const sortClassroomByName = (arrayClass) => {
const newArrayClass = structuredClone(arrayClass);
newArrayClass === null || newArrayClass === void 0 ? void 0 : newArrayClass.sort((a, b) => {
var _a, _b;
// Tách số lớp và tên lớp
const matchA = (_a = a.name) === null || _a === void 0 ? void 0 : _a.match(/(\d+)(\D+)/);
const matchB = (_b = b.name) === null || _b === void 0 ? void 0 : _b.match(/(\d+)(\D+)/);
if (!matchA || !matchB)
return false;
// Lấy số lớp và tên lớp
const numberA = parseInt(matchA[1]);
const numberB = parseInt(matchB[1]);
const letterA = matchA[2];
const letterB = matchB[2];
// So sánh số lớp trước
if (numberA !== numberB) {
return numberA - numberB;
}
// Nếu số lớp bằng nhau thì so sánh tên lớp
return letterA.localeCompare(letterB);
});
return newArrayClass;
};
exports.sortClassroomByName = sortClassroomByName;
// chuyển data từ datepicker: từ dd/mm/yyy sang mm/dd/yyyy
const formatDateMMDDYY = (date) => {
const arr = date.split('-');
return `${arr[1]}/${arr[0]}/${arr[2]}`;
};
exports.formatDateMMDDYY = formatDateMMDDYY;
const triggerMessage = ({ message, type = 'custom', isFailure = false, }) => {
eventBus_1.eventBus.emit(constants_1.KEY_ALERT_LIBRARY, {
message,
show: true,
type,
isFailure,
});
};
exports.triggerMessage = triggerMessage;
const dateStrToDate = (date, char = '/') => date ? date.slice(8, 10) + char + date.slice(5, 7) + char + date.slice(0, 4) : '';
exports.dateStrToDate = dateStrToDate;
//# sourceMappingURL=helper.js.map