fake-nantum-module.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. 'use strict';
  2. const { v4 } = require('uuid');
  3. const _ = require('lodash');
  4. class FakeNantumModule {
  5. constructor({ vens, events, venRegistrations, venReports, seenEvents } = {}) {
  6. this.vens = vens || [];
  7. this.venRegistrations = venRegistrations || [];
  8. this.venReports = venReports || [];
  9. this.events = events || [];
  10. this.seenEvents = seenEvents || [];
  11. this.eventResponses = [];
  12. this.reportReadings = [];
  13. }
  14. async getVenRegistration(clientCertificateFingerprint) {
  15. return _.find(this.venRegistrations, {
  16. client_certificate_fingerprint: clientCertificateFingerprint,
  17. });
  18. }
  19. async createVenRegistration(ven) {
  20. const venId = v4();
  21. const newVen = { ...ven, _id: venId };
  22. this.venRegistrations.push(newVen);
  23. }
  24. async deleteVenRegistration(venId) {
  25. const venIndex = _.findIndex(this.venRegistrations, { _id: venId });
  26. if (venIndex == null)
  27. throw new Error(`Could not find ven with _id: ${venId}`);
  28. this.venRegistrations = this.venRegistrations.filter(ven => ven._id !== venId);
  29. }
  30. async getEventResponse(ven, eventId, modificationNumber) {
  31. return _.find(this.eventResponses, {
  32. oadr_utility_ven_id: ven._id,
  33. oadr_utility_event_id: eventId,
  34. modification_number: modificationNumber,
  35. })[0];
  36. }
  37. async createEventResponse(eventResponse) {
  38. const eventResponseId = v4();
  39. const newEventResponse = { ...eventResponse, _id: eventResponseId };
  40. this.eventResponses.push(newEventResponse);
  41. }
  42. async updateEventResponse(id, newProperties) {
  43. const eventResponseIndex = _.findIndex(this.eventResponses, { _id: id });
  44. if (eventResponseIndex == null)
  45. throw new Error(`Could not find eventResponse with _id: ${id}`);
  46. this.eventResponses[eventResponseIndex] = {
  47. ...this.eventResponses[eventResponseIndex],
  48. ...newProperties,
  49. };
  50. }
  51. markEventAsSeen(oadrUtilityVenRegistrationId, eventId, modificationNumber) {
  52. const newSeenEvent = {
  53. _id: v4(),
  54. oadr_utility_ven_registration_id: oadrUtilityVenRegistrationId,
  55. oadr_utility_event_id: eventId,
  56. modification_number: modificationNumber
  57. };
  58. this.seenEvents.push(newSeenEvent);
  59. }
  60. async getSeenEvents(venRegistrationId) {
  61. return _.filter(this.seenEvents, {
  62. oadr_utility_ven_registration_id: venRegistrationId,
  63. });
  64. }
  65. async getVen(venRegistrationId) {
  66. return _.filter(this.seenEvents, {
  67. oadr_utility_ven_registration_id: venRegistrationId,
  68. })[0];
  69. }
  70. async getEvents() {
  71. return [...this.events];
  72. }
  73. async sendReportReadings(ven, readings) {
  74. this.reportReadings.push({ ven: ven._id, readings });
  75. }
  76. async getVenReports(venRegistrationId) {
  77. return _.find(this.venReports, {
  78. oadr_utility_ven_registration_id: venRegistrationId,
  79. });
  80. }
  81. async updateVenReports(venReportId, newReportList) {
  82. const venReportIndex = _.findIndex(this.venReports, { _id: venReportId });
  83. if (venReportIndex == null)
  84. throw new Error(`Could not find venReport with _id: ${venReportId}`);
  85. this.venReports[venReportIndex].reports = newReportList;
  86. }
  87. async createVenReports(venReports) {
  88. const venReportsId = v4();
  89. const newVenReport = { ...venReports, _id: venReportsId };
  90. this.venReports.push(newVenReport);
  91. }
  92. }
  93. module.exports = FakeNantumModule;