UNPKG

navidev-abap-gw-api

Version:

API para la extracción de datos de servicios SAP GW

208 lines (207 loc) 9.97 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const date_and_time_1 = __importDefault(require("date-and-time")); const metadataOdataTypes_1 = require("../metadata/metadataOdataTypes"); const commonConstants_1 = require("../shared/constants/commonConstants"); const draftConstants_1 = require("../shared/constants/draftConstants"); class BaseUtils { constructor(entityInfo, odataVersion) { this.entityInfo = entityInfo; this.odataVersion = odataVersion; } /** * Convierte valor para pasarlo por url para método como READ o PATCH * @param value * @param fieldName * @returns */ convertQueryValue(value, fieldName) { const fieldInfo = this.entityInfo.fields.find((row) => row.name == fieldName); if (!fieldInfo) return value; if (fieldInfo.type === metadataOdataTypes_1.ODATA_TYPES.DATETIME) { return `datetime'${this.convertJSON2SAPValue(value, fieldName)}'`; } return value; } /** * Convierte el valor de un campo al formato de SAP. * Nota importante: Para los método de READ y QUERY hay un determinados campos que se deben englobar * en un tipo para que funcione. Por ejemplo, el campo de fecha que hay que poner datetime'<valor de la fecha>' para * que funcione. Pero el formato de datetime'' no es válido para el método de POST/PUT/PATCH. * @param value * @param fieldName * @returns */ convertJSON2SAPValue(value, fieldName) { const fieldInfo = this.entityInfo.fields.find((row) => row.name == fieldName); if (!fieldInfo) return value; if (fieldInfo.type === metadataOdataTypes_1.ODATA_TYPES.DATETIME) return [date_and_time_1.default.format(value, "YYYY-MM-DD"), "00:00:00"].join("T"); if (fieldInfo.type == metadataOdataTypes_1.ODATA_TYPES.DATE) return date_and_time_1.default.format(value, "YYYY-MM-DD"); // Si el tipo de campo es decimal y el valor es un número, para odata V2 se convierte a string.Ya que SAP los numeros los espera como string. // Si es V4 se pasa como numero tal if (fieldInfo.type === metadataOdataTypes_1.ODATA_TYPES.DECIMAL && typeof value === "number") return this.odataVersion == commonConstants_1.VERSION_ODATA.V2 ? value.toString() : value; return value; } /** * Convierte una fecha en formato SAP a Date en formato javascript. * @param value */ convertSAPDate2Date(value) { const match = value.match(/\/Date\((\d+)\)\//); if (match && match[1]) { const milisegundos = parseInt(match[1]); return new Date(milisegundos); } return null; } /** * Convierte la fecha de SAP con offset a Date en formato javascript. * @param value */ convertSAPDateOffset2Date(value) { const match = value.match(/\/Date\((\d+)([+-]\d{4})?\)\//); if (match && match[1]) { const milisegundos = parseInt(match[1]); const desplazamiento = match[2]; let fecha = new Date(milisegundos); if (desplazamiento) { const horas = parseInt(desplazamiento.slice(0, 3)); const minutos = parseInt(desplazamiento.slice(0, 1) + desplazamiento.slice(3, 5)); fecha = new Date(fecha.getTime() + (horas * 60 + minutos) * 60 * 1000); } return fecha; } return null; } /** * Convierte el valor de un campo de SAP a JSON * @param value * @param fieldName * @returns */ convertSAPValue2JSON(value, fieldName) { const fieldInfo = this.entityInfo.fields.find((row) => row.name == fieldName); if (!fieldInfo) return value; if (!value) return value; // Si es nulo se devuelve el mismo valor if (fieldInfo.type === metadataOdataTypes_1.ODATA_TYPES.DATETIME) return this.convertSAPDate2Date(value); if (fieldInfo.type === metadataOdataTypes_1.ODATA_TYPES.DATE_OFFSET) return this.convertSAPDateOffset2Date(value); return value; } /** * Devuelve si un campo existe en la entidad. * @param fieldName * @returns */ existeFieldEntity(fieldName) { return this.entityInfo.fields.some((field) => field.name === fieldName); } /** * Mapea un JSON a un objeto de salida solo con los campos de la entidad. * @param json * @returns */ mappingSAPJSON2Object(json, selectFields, options) { let jsonOutput = {}; Object.keys(json).forEach((key) => { if (this.existeFieldEntity(key) && (!selectFields || (selectFields && selectFields.includes(key)))) jsonOutput[key] = this.convertSAPValue2JSON(json[key], key); // Camps de borrador para el Odata V4. SAP tiene mucho pero solo mapeo los necesarios if ((this.odataVersion = commonConstants_1.VERSION_ODATA.V4)) this.mappingSAPJSONDraftFields(key, json, jsonOutput); }); return jsonOutput; } /** * Parsea un objeto al formato JSON que espera SAP. * @param values * @returns */ mappingObject2SAPJSON(values, excludeFields) { // Paso los valores a un objeto para poder recorrerlo let valuesObject = values; // Objeto JSON let json = {}; Object.keys(valuesObject).forEach((key) => { if (valuesObject[key] === undefined) return; if (excludeFields && (excludeFields === null || excludeFields === void 0 ? void 0 : excludeFields.some((row) => row == key))) return; const fieldInfo = this.entityInfo.fields.find((field) => field.name === key); if (fieldInfo) { json[key] = this.convertJSON2SAPValue(valuesObject[key], key); } }); // Se convierte el JSON a string return JSON.stringify(json); } /** * Convierte un Array en formato JSON a un Array de objetos con los campos de la entidad. * @param json */ mappingSAPJSON2Array(json, selectFields) { return json.map((row) => this.mappingSAPJSON2Object(row, selectFields)); } /** * Mapeo de los campos de borrador que provienen del JSON * @param key * @param json * @param jsonOutput * @param selectFields */ mappingSAPJSONDraftFields(key, json, jsonOutput, selectFields) { if (key == draftConstants_1.DRAFT_FIELDS.ODATA_ETAG && (!selectFields || (selectFields && selectFields.includes(key)))) jsonOutput[draftConstants_1.DRAFT_INFO_TYPE_FIELDS.ODATA_ETAG] = json[key]; if (key == draftConstants_1.DRAFT_FIELDS.HAS_ACTIVATE_ENTITY && (!selectFields || (selectFields && selectFields.includes(key)))) jsonOutput[draftConstants_1.DRAFT_INFO_TYPE_FIELDS.HAS_ACTIVATE_ENTITY] = json[key]; if (key == draftConstants_1.DRAFT_FIELDS.IS_ACTIVATE_ENTITY && (!selectFields || (selectFields && selectFields.includes(key)))) jsonOutput[draftConstants_1.DRAFT_INFO_TYPE_FIELDS.IS_ACTIVATE_ENTITY] = json[key]; if (key == draftConstants_1.DRAFT_FIELDS.IS_ACTIVATE_ENTITY && (!selectFields || (selectFields && selectFields.includes(key)))) jsonOutput[draftConstants_1.DRAFT_INFO_TYPE_FIELDS.IS_ACTIVATE_ENTITY] = json[key]; // Campos del nodo de datos administrativos del draft if (key == draftConstants_1.DRAFT_FIELDS.DRAFT_ADMINISTRATIVE_DATA) { if (json[draftConstants_1.DRAFT_FIELDS.ADMINISTRATIVE_DATA_SUBFIELDS.IN_PROCESS_BY_USER] && (!selectFields || (selectFields && selectFields.includes(draftConstants_1.DRAFT_FIELDS.ADMINISTRATIVE_DATA_SUBFIELDS.IN_PROCESS_BY_USER)))) jsonOutput[draftConstants_1.DRAFT_INFO_TYPE_FIELDS.IN_PROCESS_BY_USER] = json[draftConstants_1.DRAFT_FIELDS.ADMINISTRATIVE_DATA_SUBFIELDS.IN_PROCESS_BY_USER]; if (json[draftConstants_1.DRAFT_FIELDS.ADMINISTRATIVE_DATA_SUBFIELDS.LAST_CHANGED_BY_USER] && (!selectFields || (selectFields && selectFields.includes(draftConstants_1.DRAFT_FIELDS.ADMINISTRATIVE_DATA_SUBFIELDS.LAST_CHANGED_BY_USER)))) jsonOutput[draftConstants_1.DRAFT_INFO_TYPE_FIELDS.LAST_CHANGED_BY_USER] = json[draftConstants_1.DRAFT_FIELDS.ADMINISTRATIVE_DATA_SUBFIELDS.LAST_CHANGED_BY_USER]; if (json[draftConstants_1.DRAFT_FIELDS.ADMINISTRATIVE_DATA_SUBFIELDS.LAST_CHANGED_BY_USER_DESC] && (!selectFields || (selectFields && selectFields.includes(draftConstants_1.DRAFT_FIELDS.ADMINISTRATIVE_DATA_SUBFIELDS .LAST_CHANGED_BY_USER_DESC)))) jsonOutput[draftConstants_1.DRAFT_INFO_TYPE_FIELDS.LAST_CHANGED_BY_USER_DESC] = json[draftConstants_1.DRAFT_FIELDS.ADMINISTRATIVE_DATA_SUBFIELDS.LAST_CHANGED_BY_USER_DESC]; if (json[draftConstants_1.DRAFT_FIELDS.ADMINISTRATIVE_DATA_SUBFIELDS.IN_PROCESS_BY_USER_DESC] && (!selectFields || (selectFields && selectFields.includes(draftConstants_1.DRAFT_FIELDS.ADMINISTRATIVE_DATA_SUBFIELDS.IN_PROCESS_BY_USER_DESC)))) jsonOutput[draftConstants_1.DRAFT_INFO_TYPE_FIELDS.IN_PROCESS_BY_USER_DESC] = json[draftConstants_1.DRAFT_FIELDS.ADMINISTRATIVE_DATA_SUBFIELDS.IN_PROCESS_BY_USER_DESC]; } } } exports.default = BaseUtils;