canceled-party-registration.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. 'use strict';
  2. const { parseXML, childAttr } = require('../parser');
  3. const { fragment } = require('xmlbuilder2');
  4. const {
  5. createDoc,
  6. energyInteropNs,
  7. parseEiResponse,
  8. serializeEiResponse,
  9. } = require('../shared');
  10. async function parse(input) {
  11. const json = await parseXML(input);
  12. const o =
  13. json['oadrPayload']['$$']['oadrSignedObject'][0]['$$'][
  14. 'oadrCanceledPartyRegistration'
  15. ][0]['$$'];
  16. const { code, description, requestId } = parseEiResponse(
  17. o['eiResponse'][0]['$$'],
  18. );
  19. const result = {
  20. _type: 'oadrCanceledPartyRegistration',
  21. responseCode: code,
  22. responseRequestId: requestId,
  23. };
  24. if (description != null) {
  25. result.responseDescription = description;
  26. }
  27. if (code < 200 || code >= 300) {
  28. return result;
  29. }
  30. const registrationId = childAttr(o, 'registrationID');
  31. if (registrationId != null) result.registrationId = registrationId;
  32. const venId = childAttr(o, 'venID');
  33. if (venId != null) result.venId = venId;
  34. return result;
  35. }
  36. function validate(obj) {
  37. if (!obj.responseCode) {
  38. throw new Error('Missing responseCode');
  39. }
  40. if (!obj.responseRequestId) {
  41. throw new Error('Missing responseRequestId');
  42. }
  43. }
  44. function serialize(obj) {
  45. validate(obj);
  46. const registrationId =
  47. obj.registrationId != null
  48. ? fragment()
  49. .ele(energyInteropNs, 'ei:registrationID')
  50. .txt(obj.registrationId)
  51. : fragment();
  52. const venId =
  53. obj.venId != null
  54. ? fragment()
  55. .ele(energyInteropNs, 'ei:venID')
  56. .txt(obj.venId)
  57. : fragment();
  58. const doc = createDoc()
  59. .ele('@oadr2b', 'oadr2b:oadrPayload')
  60. .ele('oadr2b:oadrSignedObject')
  61. .ele('oadr2b:oadrCanceledPartyRegistration')
  62. .att('@ei', 'ei:schemaVersion', '2.0b')
  63. .import(serializeEiResponse(obj))
  64. .import(registrationId)
  65. .import(venId)
  66. .doc();
  67. return doc.end({ headless: true, prettyPrint: false });
  68. }
  69. async function canParse(input) {
  70. const json = await parseXML(input);
  71. const o = json['oadrPayload']['$$']['oadrSignedObject'][0]['$$'];
  72. return o['oadrCanceledPartyRegistration'] != null;
  73. }
  74. function canSerialize(message) {
  75. return message._type === 'oadrCanceledPartyRegistration';
  76. }
  77. module.exports = {
  78. parse,
  79. serialize,
  80. canParse,
  81. canSerialize,
  82. };