canceled-party-registration.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 'use strict';
  2. const { parseXML, childAttr, required } = require('../parser');
  3. const { create, fragment } = require('xmlbuilder2');
  4. const oadrPayloadNs = 'http://www.w3.org/2000/09/xmldsig#';
  5. const oadrNs = 'http://openadr.org/oadr-2.0b/2012/07';
  6. const energyInteropNs = 'http://docs.oasis-open.org/ns/energyinterop/201110';
  7. const energyInteropPayloadsNs =
  8. 'http://docs.oasis-open.org/ns/energyinterop/201110/payloads';
  9. function parseEiResponse(response) {
  10. return {
  11. code: required(childAttr(response, 'responseCode'), 'responseCode'),
  12. description: childAttr(response, 'responseDescription'),
  13. requestId: required(childAttr(response, 'requestID'), 'requestID'),
  14. };
  15. }
  16. async function parse(input) {
  17. const json = await parseXML(input);
  18. const o =
  19. json['oadrPayload']['$$']['oadrSignedObject'][0]['$$'][
  20. 'oadrCanceledPartyRegistration'
  21. ][0]['$$'];
  22. const { code, description, requestId } = parseEiResponse(
  23. o['eiResponse'][0]['$$'],
  24. );
  25. const result = {
  26. responseCode: code,
  27. responseDescription: description,
  28. responseRequestId: requestId,
  29. };
  30. if (code < 200 || code >= 300) {
  31. return result;
  32. }
  33. const registrationId = childAttr(o, 'registrationID');
  34. if (registrationId != null) result.registrationId = registrationId;
  35. const venId = childAttr(o, 'venID');
  36. if (venId != null) result.venId = venId;
  37. return result;
  38. }
  39. function serializeEiResponse(code, description, requestId) {
  40. const descriptionFrag =
  41. description != null
  42. ? fragment()
  43. .ele(energyInteropNs, 'ei:responseDescription')
  44. .txt(description)
  45. : fragment();
  46. return fragment()
  47. .ele(energyInteropNs, 'ei:responseCode')
  48. .txt(code)
  49. .up()
  50. .import(descriptionFrag)
  51. .ele(energyInteropPayloadsNs, 'pyld:requestID')
  52. .txt(requestId)
  53. .up();
  54. }
  55. function validate(obj) {
  56. if (!obj.responseCode) {
  57. throw new Error('Missing responseCode');
  58. }
  59. if (!obj.responseRequestId) {
  60. throw new Error('Missing responseRequestId');
  61. }
  62. }
  63. function serialize(obj) {
  64. validate(obj);
  65. const registrationId =
  66. obj.registrationId != null
  67. ? fragment()
  68. .ele(energyInteropNs, 'ei:registrationID')
  69. .txt(obj.registrationId)
  70. : fragment();
  71. const venId =
  72. obj.venId != null
  73. ? fragment()
  74. .ele(energyInteropNs, 'ei:venID')
  75. .txt(obj.venId)
  76. : fragment();
  77. const doc = create({
  78. namespaceAlias: {
  79. ns: oadrPayloadNs,
  80. oadr2b: oadrNs,
  81. ei: energyInteropNs,
  82. pyld: energyInteropPayloadsNs,
  83. },
  84. })
  85. .ele('@oadr2b', 'oadr2b:oadrPayload')
  86. .ele('oadr2b:oadrSignedObject')
  87. .ele('oadr2b:oadrCanceledPartyRegistration')
  88. .att('@ei', 'ei:schemaVersion', '2.0b')
  89. .ele('@ei', 'ei:eiResponse')
  90. .import(
  91. serializeEiResponse(
  92. obj.responseCode,
  93. obj.responseDescription,
  94. obj.responseRequestId,
  95. ),
  96. )
  97. .up()
  98. .import(registrationId)
  99. .import(venId)
  100. .doc();
  101. return doc.end({ headless: true, prettyPrint: false });
  102. }
  103. async function canParse(input) {
  104. const json = await parseXML(input);
  105. const o = json['oadrPayload']['$$']['oadrSignedObject'][0]['$$'];
  106. return o['oadrCanceledPartyRegistration'] != null;
  107. }
  108. module.exports = {
  109. parse,
  110. serialize,
  111. canParse,
  112. };