canceled-party-registration.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. _type: 'oadrCanceledPartyRegistration',
  27. responseCode: code,
  28. responseRequestId: requestId,
  29. };
  30. if (description != null) {
  31. result.responseDescription = description;
  32. }
  33. if (code < 200 || code >= 300) {
  34. return result;
  35. }
  36. const registrationId = childAttr(o, 'registrationID');
  37. if (registrationId != null) result.registrationId = registrationId;
  38. const venId = childAttr(o, 'venID');
  39. if (venId != null) result.venId = venId;
  40. return result;
  41. }
  42. function serializeEiResponse(code, description, requestId) {
  43. const descriptionFrag =
  44. description != null
  45. ? fragment()
  46. .ele(energyInteropNs, 'ei:responseDescription')
  47. .txt(description)
  48. : fragment();
  49. return fragment()
  50. .ele(energyInteropNs, 'ei:responseCode')
  51. .txt(code)
  52. .up()
  53. .import(descriptionFrag)
  54. .ele(energyInteropPayloadsNs, 'pyld:requestID')
  55. .txt(requestId)
  56. .up();
  57. }
  58. function validate(obj) {
  59. if (!obj.responseCode) {
  60. throw new Error('Missing responseCode');
  61. }
  62. if (!obj.responseRequestId) {
  63. throw new Error('Missing responseRequestId');
  64. }
  65. }
  66. function serialize(obj) {
  67. validate(obj);
  68. const registrationId =
  69. obj.registrationId != null
  70. ? fragment()
  71. .ele(energyInteropNs, 'ei:registrationID')
  72. .txt(obj.registrationId)
  73. : fragment();
  74. const venId =
  75. obj.venId != null
  76. ? fragment()
  77. .ele(energyInteropNs, 'ei:venID')
  78. .txt(obj.venId)
  79. : fragment();
  80. const doc = create({
  81. namespaceAlias: {
  82. ns: oadrPayloadNs,
  83. oadr2b: oadrNs,
  84. ei: energyInteropNs,
  85. pyld: energyInteropPayloadsNs,
  86. },
  87. })
  88. .ele('@oadr2b', 'oadr2b:oadrPayload')
  89. .ele('oadr2b:oadrSignedObject')
  90. .ele('oadr2b:oadrCanceledPartyRegistration')
  91. .att('@ei', 'ei:schemaVersion', '2.0b')
  92. .ele('@ei', 'ei:eiResponse')
  93. .import(
  94. serializeEiResponse(
  95. obj.responseCode,
  96. obj.responseDescription,
  97. obj.responseRequestId,
  98. ),
  99. )
  100. .up()
  101. .import(registrationId)
  102. .import(venId)
  103. .doc();
  104. return doc.end({ headless: true, prettyPrint: false });
  105. }
  106. async function canParse(input) {
  107. const json = await parseXML(input);
  108. const o = json['oadrPayload']['$$']['oadrSignedObject'][0]['$$'];
  109. return o['oadrCanceledPartyRegistration'] != null;
  110. }
  111. module.exports = {
  112. parse,
  113. serialize,
  114. canParse,
  115. };