UNPKG

will-core

Version:
112 lines (111 loc) 4.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TheUtility = void 0; const will_util_1 = require("will-util"); const AssureAlias_1 = require("../models/AssureAlias"); const EnvironmentVariable_1 = require("./EnvironmentVariable"); class TheUtility { static createMapEntities(rs, setting) { let map = new Map(); for (let row of rs.rows) { let valid = true; let type = ""; if (setting.groupNames) { for (let g of setting.groupNames) { type += String(row[g]); } } if (setting.groupId) { valid = setting.groupId == type; } if (valid) { let key = row[setting.keyName]; let values = ""; for (let v of setting.valueNames) { values += String(row[v]); } map.set(key, values); } } return map; } static createDataEntity(records) { let result = {}; for (let r of records) { let map = this.createMapEntities(r.resultset, r.setting); result[r.setting.categoryName || r.tablename] = Object.fromEntries(map); } return result; } static getDefaultLanguage(context) { return context?.params?.language || context?.meta?.headers?.language || EnvironmentVariable_1.DEFAULT_LANGUAGE; } static isEnglish(context) { return this.isLanguage(context, "EN"); } static isThai(context) { return this.isLanguage(context, "TH"); } static isLanguage(context, language) { return language == this.getDefaultLanguage(context); } static isReservedParameter(parameter) { return AssureAlias_1.RESERVED_PARAMETERS.includes(parameter); } static transformData(rs, fields, formatter) { let result = {}; let found = false; if (fields) { for (let key in fields) { let dbf = fields[key]; let selected = typeof dbf.selected === "undefined" || dbf.selected; if (selected) { let value = null; if (rs.hasOwnProperty(key)) { value = rs[key]; } found = true; let info = { name: key, value: value, rs: rs, field: fields[key] }; result[key] = formatter ? formatter.call(this, info) : this.formatData(info); } } } if (!found) { for (let key in rs) { let info = { name: key, value: rs[key], rs: rs }; result[key] = formatter ? formatter.call(this, info) : this.formatData(info); } } return result; } static formatData(info) { if (info.value instanceof Date) { if (info.field?.type == "DATE") { info.value = will_util_1.Utilities.formatDate(info.value); } else if (info.field?.type == "TIME") { info.value = will_util_1.Utilities.formatTime(info.value); } else if (info.field?.type == "DATETIME") { info.value = will_util_1.Utilities.formatDateTime(info.value); } else { info.value = will_util_1.Utilities.formatDate(info.value); } } else if (typeof info.value === "number") { if (info.field?.type == "INTEGER" || info.field?.type == "BIGINT") { let maxdigit = info.field?.options?.maxdigit || 0; let mindigit = info.field?.options?.mindigit || 0; info.value = info.value.toLocaleString("en-US", { maximumFractionDigits: maxdigit, minimumFractionDigits: mindigit }); } else { let maxdigit = info.field?.options?.maxdigit || 2; let mindigit = info.field?.options?.mindigit || 2; info.value = info.value.toLocaleString("en-US", { maximumFractionDigits: maxdigit, minimumFractionDigits: mindigit }); } } return info.value; } } exports.TheUtility = TheUtility;