UNPKG

@wbg-mde/repository

Version:

Managing all common method for file system CRUD operations.

186 lines (185 loc) 9.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const export_master_config_1 = require("../configs/export.master.config"); const app_repo_utility_1 = require("../app.repo.utility"); const path = require("path"); class ExportHelpers { formatFundAgencies(action, datum, schema, exportKey, outData, key) { try { switch (action) { case export_master_config_1.Export_Master_Config.actions.export: { if (datum && datum instanceof Array) { datum = datum.filter((x) => { return (x.name !== undefined); }); if (datum.length > 0) { let grantNos = [...new Set(datum.map(x => x.grant))]; grantNos = grantNos.filter((n) => { if (n !== null) { return n; } }); const fundAg = datum.map((x) => { var obj = {}; obj["#"] = x.name; if (x.abbr || x.role) { obj["@"] = {}; if (x.abbr) { obj["@"]["abbr"] = x.abbr; } if (x.role) { obj["@"]["role"] = x.role; } } return obj; }); const [fundAgencyKey, grantNoKey] = exportKey.split(','); app_repo_utility_1.App_Repository_Utility.setNestedProperty(outData, fundAgencyKey, fundAg); if (grantNos.length === 1) { app_repo_utility_1.App_Repository_Utility.setNestedProperty(outData, grantNoKey, grantNos[0]); } else if (grantNos.length > 1) { const grand_nos = datum .filter((x) => x.grant !== undefined) .map((x) => { var obj = {}; obj["#"] = x.grant; obj["@"] = {}; obj["@"]["agency"] = x.abbr || x.name; if (x.role) { obj["@"]["role"] = x.role; } return obj; }); app_repo_utility_1.App_Repository_Utility.setNestedProperty(outData, grantNoKey, grand_nos); } } } } break; case export_master_config_1.Export_Master_Config.actions.import: { const [fundAgencyKey, grantNoKey] = exportKey.split(','); let __data = app_repo_utility_1.App_Repository_Utility.getNestedProperty(datum, fundAgencyKey); const grant_nos = app_repo_utility_1.App_Repository_Utility.getNestedProperty(datum, grantNoKey); if (__data && __data instanceof Array) { const fundAgencies = __data.reduce((periods, val) => { let agency = Object.assign({}, { "name": val["#"] || val }); const attrValues = val["@"]; if (attrValues) { agency = Object.assign(agency, attrValues); } if (grant_nos) { if (grant_nos.length === 1 && typeof grant_nos[0] === 'string') { agency["grant"] = grant_nos[0]; } else if (grant_nos.length >= 1) { const grant_number = grant_nos.find((x) => { return (x['@'] && (x['@']['agency'] === (agency['abbr'] || agency['name'])) && (x['@']['role'] === agency['role'])); }); if (grant_number) { agency = Object.assign(agency, { 'grant': grant_number['#'] }); } } } periods.push(agency); return periods; }, []); app_repo_utility_1.App_Repository_Utility.setNestedProperty(outData, key, fundAgencies); } } break; } } catch (e) { console.log(e); } } formatPeriods(action, datum, schema, exportKey, outData, key) { try { switch (action) { case export_master_config_1.Export_Master_Config.actions.export: { if (datum && datum instanceof Array) { const periods = datum.reduce((periods, val) => { if (val.start) { let item = { date: val.start, event: "start" }; if (val.cycle) { item['cycle'] = val.cycle; } periods.push({ "@": item }); } if (val.end) { let item = { date: val.end, event: "end" }; if (val.cycle) { item['cycle'] = val.cycle; } periods.push({ "@": item }); } return periods; }, []); app_repo_utility_1.App_Repository_Utility.setNestedProperty(outData, exportKey, periods); } else { app_repo_utility_1.App_Repository_Utility.setNestedProperty(outData, exportKey, datum); } } case export_master_config_1.Export_Master_Config.actions.import: { let __data = app_repo_utility_1.App_Repository_Utility.getNestedProperty(datum, exportKey); let curSchema = schema[0]; if (__data && __data instanceof Array) { const periods = __data.reduce((periods, val) => { val = val["@"] || val; if (val.event === "start") { periods.push({ start: val.date, cycle: val.cycle }); } else { if (periods.length > 0) { periods[periods.length - 1]["end"] = val.date; } else { periods.push({ end: val.date, cycle: val.cycle }); } } return periods; }, []); app_repo_utility_1.App_Repository_Utility.setNestedProperty(outData, key, periods); } } } } catch (e) { } } formatPath(action, datum, schema, exportKey, outData, key) { try { switch (action) { case export_master_config_1.Export_Master_Config.actions.export: { if (!this.isWebURL(datum)) { app_repo_utility_1.App_Repository_Utility.setNestedProperty(outData, exportKey, path.basename(datum)); } else { app_repo_utility_1.App_Repository_Utility.setNestedProperty(outData, exportKey, datum); } } break; case export_master_config_1.Export_Master_Config.actions.import: { app_repo_utility_1.App_Repository_Utility.setNestedProperty(outData, key, datum); } break; } } catch (e) { } } isWebURL(url) { let regUrl = /^(((ht|f){1}((tp:|tps:)[/][/]){1})|((www.){1}))[-a-zA-Z0-9@:%_\+.~#?&//=]+$/; if (regUrl.test(url) === true) { return true; } else { return false; } } } exports.ExportHelpers = ExportHelpers;