Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 | 1x 1x 1x 1x 1x | import { Constants } from '@logchain/configs';
import { AnyObject, RAM_ORDER } from '@logchain/types';
import { DateUtils } from '@logchain/utils';
import { bind, BindingScope } from '@loopback/core';
import { Options } from '@loopback/repository';
import moment from 'moment';
@bind({ scope: BindingScope.TRANSIENT })
export class XMLService {
constructor() {
}
/**
* Get info from XML file to RAM
*
* @param {AnyObject} result - the input result object
* @return {string} the Outgoing_Docid
*/
getRAM_Order(result: AnyObject): RAM_ORDER {
if ( !result['DSET']['tt-edi-ord'][0]['MsgType']
|| !result['DSET']['tt-edi-ord'][0]['Outgoing_Docid']
|| !result['DSET']['tt-edi-ord'][0]['Consignment_num']
|| !result['DSET']['tt-edi-ord'][0]['Unitkey']
) {
return {
'MsgType': result['DSET']['tt-edi-ord'][0]['MsgType']?.[0] ?? '',
'Outgoing_Docid': result['DSET']['tt-edi-ord'][0]['Outgoing_Docid']?.[0].split('|')[0] ?? '',
'Consignment_num': result['DSET']['tt-edi-ord'][0]['Consignment_num']?.[0] ?? '',
'Unitkey': result['DSET']['tt-edi-ord'][0]['Unitkey']?.[0] ?? '',
} as RAM_ORDER;
}
const vesselData: AnyObject = {};
if (result['DSET']['tt-edi-ord'][0]['tt-edi-vessel']) {
result['DSET']['tt-edi-ord'][0]['tt-edi-vessel'].map((vessel: AnyObject) => {
if (!vesselData[vessel['LoadG4code_UN'] + '_' + vessel['UnLoadG4code_UN']]) {
const Vessel_BookingRef: AnyObject = {};
Vessel_BookingRef[result['DSET']['tt-edi-ord'][0]['Unitkey']] = vessel['Vessel_BookingRef']?.[0] ?? '';
vesselData[vessel['LoadG4code_UN'] + '_' + vessel['UnLoadG4code_UN']] = {
'LoadG4code_UN': vessel['LoadG4code_UN']?.[0] ?? '',
'UnLoadG4code_UN': vessel['UnLoadG4code_UN']?.[0] ?? '',
'VesselVoyage': vessel['VesselVoyage']?.[0] ?? '',
'Vessel_BookingRef': Vessel_BookingRef,
'VesselCarrier_Name': vessel['VesselCarrier_Name']?.[0] ?? '',
};
}
});
}
return {
'xml': result,
'MsgType': result['DSET']['tt-edi-ord'][0]['MsgType']?.[0] ?? '',
'Outgoing_Docid': result['DSET']['tt-edi-ord'][0]['Outgoing_Docid']?.[0].split('|')[0] ?? '',
'Jobno': result['DSET']['tt-edi-ord'][0]['Jobno']?.[0].split('|')[0] ?? '',
'Consignment_num': result['DSET']['tt-edi-ord'][0]['Consignment_num']?.[0] ?? '',
'Unitkey': result['DSET']['tt-edi-ord'][0]['Unitkey']?.[0] ?? '',
'POL': result['DSET']['tt-edi-ord'][0]['LdgUNLocation']?.[0] ?? '',
'POD': result['DSET']['tt-edi-ord'][0]['DchUNLocation']?.[0] ?? '',
'ProductId': result['DSET']['tt-edi-ord'][0]['tt-edi-prod'][0]['Product_Chemical']?.[0] ?? '',
'LoadWeight': result['DSET']['tt-edi-ord'][0]['tt-edi-prod'][0]['LoadWeight']?.[0] ?? '',
'VesselData': vesselData
};
}
/**
* Generates an XML document for a PDF file based on the provided data.
*
* @param {AnyObject} data - The data used to populate the XML document.
* @return {string} The generated XML document as a string.
*/
generateXMLForPDFDocument(data: AnyObject): string {
return `<?xml version="1.0"?>
<DSET xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<TransactionId>${data.TransactionId}</TransactionId>
<Consignment_num>${data.Consignment_num}</Consignment_num>
<Unitkey>${data.Unitkey}</Unitkey>
<File>
<FileDate>${data.File.FileDate}</FileDate>
<FileType>${data.File.FileType}</FileType>
<FileTypeName>${data.File.FileTypeName}</FileTypeName>
<FileTypeStatus>${data.File.FileTypeStatus}</FileType>
<FileTypeStatusName>${data.File.FileTypeStatusName}</FileTypeStatus>
<ValidationStatus>${data.File.ValidationStatus}</FileType>
<ValidationStatusName>${data.File.ValidationStatusName}</FileTypeStatus
<FlowStep>${data.File.FlowStep}</flowStep>
<FlowStepName>${data.File.FlowStepName}</FlowStepName>
<DocumentActor>${data.File.DocumentActor}</DocumentActor>
</File>
</DSET>`;
}
/**
* Parses the expected date based on the provided parameters and options.
*
* @param {AnyObject} params - the parameters for parsing the expected date
* @param {Options} options - the options for parsing the expected date
* @return {Date} the parsed expected date
*/
parseExpectedDate(params: { field_name?: string, adjustment?: number, port_of_origin: string, port_of_destination: string },
options: Options): ({ expected_date: Date, expected_date_adjustment: Date} | Boolean) {
// eslint-disable-next-line prefer-const
let { field_name = '', adjustment = 0 } = params;
const { xml = {} } = options;
const origin_field_name = field_name;
if (options?.MsgType === 'Update' && Constants.xml_updated_fields_mapping[field_name]) {
field_name = Constants.xml_updated_fields_mapping[field_name] ?? field_name;
}
let field = [];
if (field_name.includes('.')) {
field = field_name.split('.');
field[0] = field[0].replace('<', '');
field[0] = field[0].replace('>', '');
field[1] = field[1].replace('<', '');
field[1] = field[1].replace('>', '');
} else {
field[0] = field_name;
field[0] = field[0].replace('<', '');
field[0] = field[0].replace('>', '');
}
let expected_date = undefined;
let expected_date_adjustment = undefined;
let tt_edi_ord: AnyObject = {};
let tt_edi_sect_index = -1;
if (xml) {
tt_edi_ord = xml['DSET']['tt-edi-ord'][0];
if (options?.MsgType === 'New' && field.length === 2) {
if (origin_field_name.includes('tt-edi-vessel')) {
for (let index = 0; index < tt_edi_ord['tt-edi-vessel'].length; index++) {
const item = tt_edi_ord['tt-edi-vessel'][index];
if (params.port_of_origin === item['LoadG4code_UN'][0] && params.port_of_destination === item['UnLoadG4code_UN'][0]) {
tt_edi_sect_index = index;
break;
}
}
}
} else if (options?.MsgType === 'Update' && field.length === 2 && tt_edi_ord['tt-edi-sect']) {
if (Constants.xml_updated_fields_using_sect.includes(origin_field_name)) {
for (let index = 0; index < tt_edi_ord['tt-edi-sect'].length; index++) {
const item = tt_edi_ord['tt-edi-sect'][index];
if (params.port_of_origin === item['Frg4Code_UN'][0] && params.port_of_destination === item['ToG4code_UN'][0]) {
tt_edi_sect_index = index;
break;
}
}
} else if (['<FrPlanDchDate>','<ToPlanDchdate>'].includes(origin_field_name)) { // #44219 - [Integration] Updating dates based on Main Header fields
// Find value of tt-edi-sect that have element tag SeqNo with largest value
let largestSeqNo = -1;
for (let index = 0; index < tt_edi_ord['tt-edi-sect'].length; index++) {
const item = tt_edi_ord['tt-edi-sect'][index];
if (item['SeqNo'] && +item['SeqNo'] > largestSeqNo) {
largestSeqNo = +item['SeqNo'];
tt_edi_sect_index = index;
}
}
} else {
// Find value of tt-edi-sect that have element tag SeqNo with largest value
let minestSeqNo = Number.MAX_SAFE_INTEGER;
for (let index = 0; index < tt_edi_ord['tt-edi-sect'].length; index++) {
const item = tt_edi_ord['tt-edi-sect'][index];
if (item['SeqNo'] && +item['SeqNo'] < minestSeqNo) {
minestSeqNo = +item['SeqNo'];
tt_edi_sect_index = index;
}
}
}
}
if (tt_edi_ord?.[field[0]]) {
if (field.length === 2 && tt_edi_ord?.[field[0]][tt_edi_sect_index] && tt_edi_ord?.[field[0]][tt_edi_sect_index][field[1]]) {
expected_date = tt_edi_ord[field[0]][tt_edi_sect_index][field[1]][0] ?? '';
} else if (field.length === 1 && tt_edi_ord?.[field[0]][0]) {
expected_date = tt_edi_ord[field[0]][0] ?? '';
} else {
if (options?.MsgType === 'Update' && Constants.xml_updated_fields.includes(origin_field_name)) {
return true;
}
return false;
}
// Check format of expected date if not YYYY-MM-DD
if (typeof(expected_date) === 'string' && !moment(expected_date, 'YYYY-MM-DD', true).isValid()) {
if (options?.MsgType === 'Update' && Constants.xml_updated_fields.includes(origin_field_name)) {
return true;
}
return false;
}
if (expected_date) {
expected_date = moment(expected_date);
}
} else {
if (options?.MsgType === 'Update' && Constants.xml_updated_fields.includes(origin_field_name)) {
return true;
}
return false;
}
if (adjustment) {
expected_date_adjustment = moment(DateUtils.adjustDays(expected_date.toDate(), adjustment));
} else {
expected_date_adjustment = expected_date;
}
}
return expected_date && expected_date_adjustment ? { expected_date: expected_date.toDate(), expected_date_adjustment: expected_date_adjustment.toDate() } : false;
}
/**
* Creates a success response XML for the given result.
*
* @return {string} A string based on the content of response.
*/
response(params: AnyObject): string {
return `<?xml version="1.0"?>
<DSET xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<TransactionId>${params['TransactionId'] ?? ''}</TransactionId>
<Outgoing_Docid>${params['Outgoing_Docid'] ?? ''}</Outgoing_Docid>
<Status>
<StatusCode>${params['StatusCode'] ?? ''}</StatusCode>
<StatusName>${params['StatusName'] ?? ''}</StatusName>
<StatusMessage>${params['StatusMessage'] ?? ''}</StatusMessage>
</Status>
</DSET>
`;
}
/**
* Creates a success response XML for the given result.
*
* @return {string} A string based on the content of response.
*/
response200(params: AnyObject): string {
return this.response({
...params,
'StatusCode': '200',
'StatusName': 'Success',
'StatusMessage': 'Successful request'
});
}
/**
* Creates a bad request error response XML for the given result.
* Server could not process the XML due to a perceived client error (this can be from anything like wrong XML format, invalid request framing, …)
*
* @return {string} A string based on the content of response.
*/
response400(result: AnyObject, message = 'Bad Request'): string {
return this.response({
...result,
'StatusCode': '400',
'StatusName': 'Bad Request',
'StatusMessage': message
});
}
/**
* Creates a internal server error response XML for the given result.
* When the server encountered an unexpected condition that prevented it to fulfil the request
*
* @return {string} A string based on the content of response.
*/
response500(result: AnyObject): string {
return this.response({
...result,
'StatusCode': '500',
'StatusName': 'Server Error',
'StatusMessage': 'Server error'
});
}
/**
* Creates a Id error response XML for the given result.
* Id already processed
* Returned when the XML's <Outgoing_Docid> has already been processed by the platform
*
* @return {string} A string based on the content of response.
*/
response1001(result: AnyObject): string {
return this.response({
...result,
'StatusCode': '1001',
'StatusName': 'Id Error',
'StatusMessage': 'Id already processed'
});
}
/**
* Creates a Invalid Flow error response XML for the given result.
* Flow record not in the platform or not on any active flow
* Returned when the XML's <Consignement_num> is not on any active flow record.
*
* @return {string} A string based on the content of response.
*/
response1002(result: AnyObject): string {
return this.response({
...result,
'StatusCode': '1002',
'StatusName': 'Invalid Flow',
'StatusMessage': 'Flow record not in the platform or not on any active flow'
});
}
/**
* Creates a Invalid Container No error response XML for the given result.
* Container No does not exist in the flow and cannot be cancelled
* Returned when the XML's <UnitKey> is not on the flow record. Thus, record to cancel is not possible.
*
* @return {string} A string based on the content of response.
*/
response1003(result: AnyObject, message = 'Container No does not exist in the flow and cannot be cancelled'): string {
return this.response({
...result,
'StatusCode': '1003',
'StatusName': 'Invalid Container No',
'StatusMessage': message
});
}
/**
* Invalid Updates error response XML for the given result.
* Updates not relevant to LogChain
* Returned when the XML's values that are needed on LogChain are exactly the same as what is on the system.
*
* @return {string} A string based on the content of response.
*/
response1009(result: AnyObject, message = 'Updates not relevant to LogChain'): string {
return this.response({
...result,
'StatusCode': '1009',
'StatusName': 'Invalid Updates',
'StatusMessage': message
});
}
/**
* Creates a Invalid Trade Lane error response XML for the given result.
* Trade Lane not in the platform
* Returned when the XML's <Consignment_num> has no equivalent active trade lane on the platform. Thus, record creation cannot proceed.
*
* @return {string} A string based on the content of response.
*/
response1011(result: AnyObject): string {
return this.response({
...result,
'StatusCode': '1011',
'StatusName': 'Invalid Trade Lane',
'StatusMessage': 'Trade Lane not in the platform'
});
}
/**
* Creates a Many Trade Lane error response XML for the given result.
* Too many potential active trade lane to match the record
* Returned when the XML's <Consignment_num> has more than 1 potential active trade lane equivalent in the platform. Thus, record creation cannot proceed until it is sure to which trade lane it should match..
*
* @return {string} A string based on the content of response.
*/
response1012(result: AnyObject): string {
return this.response({
...result,
'StatusCode': '1012',
'StatusName': 'Many Trade Lane',
'StatusMessage': 'Too many potential active trade lane to match the record'
});
}
/**
* Creates a Invalid Expected Weight error response XML for the given result.
* Expected weight is not specified or of wrong format
* Returned when the XML's <Consignment_num> has more than 1 potential active trade lane equivalent in the platform. Thus, record creation cannot proceed until it is sure to which trade lane it should match.
*
* @return {string} A string based on the content of response.
*/
response1101(result: AnyObject): string {
return this.response({
...result,
'StatusCode': '1101',
'StatusName': 'Invalid Expected Weight',
'StatusMessage': 'Expected weight is not specified or of wrong format'
});
}
/**
* Creates a Invalid Expected Date error response XML for the given result.
* Expected date not specified, or of wrong format, or a higher date than a next step
* Returned when the XML's date has met 1 of the criteria below
Not included on the XML file
Not a YYYY-MM-DD format
Has a higher date than a next step
Date on XML are those referenced on the trade lane
*
* @return {string} A string based on the content of response.
*/
response1102(result: AnyObject): string {
return this.response({
...result,
'StatusCode': '1102',
'StatusName': 'Invalid Expected Date',
'StatusMessage': 'Expected date not specified, or of wrong format, or a higher date than a next step'
});
}
/**
* Returned based on any of the criteria below
<tt-edi-ord>.<tt-edi-vessel>.<UnLoadG4code_UN> does not exist on the platform
<tt-edi-ord>.<tt-edi-vessel>.<LoadG4code_UN> does not exist on the platform
* @param {AnyObject} result
* @returns
*/
response1103(result: AnyObject, locode: string): string {
return this.response({
...result,
'StatusCode': '1103',
'StatusName': 'Invalid UN Locode',
'StatusMessage': `Provided UN Locode ${locode} does not exist on the LogChain platform`
});
}
/**
* Returned based on any of the criteria below
<tt-edi-vessel>.<UnLoadG4code_UN> exist on the platform
& <tt-edi-vessel>.<LoadG4code_UN> exist on the platform
& <tt-edi-vessel>.<UnLoadG4code_UN> & <tt-edi-vessel>.<LoadG4code_UN> not in the same <tt-edi-vessel>
* @param {AnyObject} result
* @returns
*/
response1104(result: AnyObject, locode: string): string {
return this.response({
...result,
'StatusCode': '1104',
'StatusName': 'Invalid pair of UN Locode',
'StatusMessage': `Pair UN Locode '${locode}' in LogChain platform are not in the same <tt-edi-vessel>`
});
}
/**
* Invalid flow name
* Flow name is not specified or of wrong format
* Returned when at leasr 1 criteria below is met
- <Vessel_BookingRef> not included on the XML file
- <Jobno> not included on the XML file
* @param {AnyObject} result
* @returns
*/
response1105(result: AnyObject): string {
return this.response({
...result,
'StatusCode': '1105',
'StatusName': 'Invalid flow name',
'StatusMessage': `Flow name is not specified`
});
}
}
|