| 1 |
import { parseXML } from "@web/core/utils/xml"; |
| 2 |
import { GLORY_RESULT } from "./constants"; |
| 3 |
|
| 4 |
/** |
| 5 |
* @param {Blob} xmlBlob |
| 6 |
*/ |
| 7 |
export async function parseGloryXml(xmlBlob) { |
| 8 |
const text = await xmlBlob.text(); |
| 9 |
// Remove control characters from Glory response string |
| 10 |
const xmlString = text.replace(/[\cD\0]/g, ""); |
| 11 |
return parseXML(xmlString); |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* @param {import("models").GloryXmlElement} gloryElement |
| 16 |
*/ |
| 17 |
function gloryElementToDomElement(gloryElement) { |
| 18 |
if (typeof gloryElement === "string") { |
| 19 |
return document.createTextNode(gloryElement); |
| 20 |
} |
| 21 |
const element = document.createElementNS(null, gloryElement.name); |
| 22 |
for (const [key, value] of Object.entries(gloryElement.attributes ?? {})) { |
| 23 |
element.setAttribute(key, value); |
| 24 |
} |
| 25 |
for (const child of gloryElement.children ?? []) { |
| 26 |
element.appendChild(gloryElementToDomElement(child)); |
| 27 |
} |
| 28 |
return element; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* @param {import("models").GloryXmlElement} gloryElement |
| 33 |
*/ |
| 34 |
export function serializeGloryXml(gloryElement) { |
| 35 |
const domElement = gloryElementToDomElement(gloryElement); |
| 36 |
const xmlString = new XMLSerializer().serializeToString(domElement); |
| 37 |
return xmlString + "\0"; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @param {number} sequenceNumber |
| 42 |
* @param {string} sessionId |
| 43 |
* @returns {import("models").GloryXmlElement[]} |
| 44 |
*/ |
| 45 |
export const makeGloryHeader = (sequenceNumber, sessionId) => { |
| 46 |
const sequenceNumberString = sequenceNumber.toString(10).padStart(11, "0"); |
| 47 |
const header = [ |
| 48 |
{ |
| 49 |
name: "Id", |
| 50 |
children: ["OdooPos"], |
| 51 |
}, |
| 52 |
{ |
| 53 |
name: "SeqNo", |
| 54 |
children: [sequenceNumberString], |
| 55 |
}, |
| 56 |
]; |
| 57 |
if (sessionId) { |
| 58 |
header.push({ |
| 59 |
name: "SessionID", |
| 60 |
children: [sessionId], |
| 61 |
}); |
| 62 |
} |
| 63 |
return header; |
| 64 |
}; |
| 65 |
|
| 66 |
/** |
| 67 |
* Takes an XML response and returns a status |
| 68 |
* string e.g. "SUCCESS" or "CHANGE_SHORTAGE" |
| 69 |
* |
| 70 |
* @param {Element} xmlResponse |
| 71 |
* @returns {string} |
| 72 |
*/ |
| 73 |
export function parseGloryResult(xmlResponse) { |
| 74 |
const resultCode = xmlResponse.getAttribute("result"); |
| 75 |
const resultString = GLORY_RESULT[resultCode]; |
| 76 |
if (!resultString) { |
| 77 |
return `UNKNOWN_STATUS_${resultCode}`; |
| 78 |
} |
| 79 |
|
| 80 |
return resultString; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Takes an XML response containing the verification status |
| 85 |
* of the Glory machine, and returns a number from 0-3 |
| 86 |
* corresponding to the required verification action: |
| 87 |
* |
| 88 |
* `0`: No verification needed |
| 89 |
* |
| 90 |
* `1`: Notes and coins need verification |
| 91 |
* |
| 92 |
* `2`: Notes need verification |
| 93 |
* |
| 94 |
* `3`: Coins need verification |
| 95 |
* |
| 96 |
* This result can be passed directly into a `CollectRequest` |
| 97 |
* to trigger the verification process. |
| 98 |
* |
| 99 |
* @param {Element} xmlResponse |
| 100 |
* @returns {0 | 1 | 2 | 3} |
| 101 |
*/ |
| 102 |
export function parseVerificationInfo(xmlResponse) { |
| 103 |
const denominationInfos = Array.from( |
| 104 |
xmlResponse.getElementsByTagName("RequireVerifyDenomination") |
| 105 |
); |
| 106 |
const collectionContainerInfos = Array.from( |
| 107 |
xmlResponse.getElementsByTagName("RequireVerifyCollectionContainer") |
| 108 |
); |
| 109 |
const mixStackerInfos = Array.from(xmlResponse.getElementsByTagName("RequireVerifyMixStacker")); |
| 110 |
const allInfos = [...denominationInfos, ...collectionContainerInfos, ...mixStackerInfos]; |
| 111 |
|
| 112 |
const notesRequireVerify = allInfos.some( |
| 113 |
(info) => info.getAttribute("devid") === "1" && info.getAttribute("val") === "1" |
| 114 |
); |
| 115 |
const coinsRequireVerify = allInfos.some( |
| 116 |
(info) => info.getAttribute("devid") === "2" && info.getAttribute("val") === "1" |
| 117 |
); |
| 118 |
|
| 119 |
if (notesRequireVerify && coinsRequireVerify) { |
| 120 |
return 1; |
| 121 |
} |
| 122 |
if (notesRequireVerify) { |
| 123 |
return 2; |
| 124 |
} |
| 125 |
if (coinsRequireVerify) { |
| 126 |
return 3; |
| 127 |
} |
| 128 |
|
| 129 |
return 0; |
| 130 |
} |