fake-nantum-module.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. 'use strict';
  2. const { v4 } = require('uuid');
  3. const _ = require('lodash');
  4. class FakeNantumModule {
  5. constructor({ events, vens } = {}) {
  6. this.vens = vens || [];
  7. this.events = events || [];
  8. this.eventResponses = [];
  9. this.reportReadings = [];
  10. }
  11. async getVen(clientCertificateFingerprint) {
  12. return _.find(this.vens, {
  13. client_certificate_fingerprint: clientCertificateFingerprint,
  14. });
  15. }
  16. async createVen(ven) {
  17. const venId = v4();
  18. const newVen = { ...ven, _id: venId };
  19. this.vens.push(newVen);
  20. }
  21. async updateVen(venId, newProperties) {
  22. const venIndex = _.findIndex(this.vens, { _id: venId });
  23. if (venIndex == null)
  24. throw new Error(`Could not find ven with _id: ${venId}`);
  25. this.vens[venIndex] = { ...this.vens[venIndex], ...newProperties };
  26. }
  27. async getEventResponse(ven, eventId, modificationNumber) {
  28. return _.find(this.eventResponses, {
  29. ven_id: ven._id,
  30. event_id: eventId,
  31. modification_number: modificationNumber,
  32. })[0];
  33. }
  34. async createEventResponse(eventResponse) {
  35. const eventResponseId = v4();
  36. const newEventResponse = { ...eventResponse, _id: eventResponseId };
  37. this.eventResponses.push(newEventResponse);
  38. }
  39. async updateEventResponse(id, newProperties) {
  40. const eventResponseIndex = _.findIndex(this.eventResponses, { _id: id });
  41. if (eventResponseIndex == null)
  42. throw new Error(`Could not find eventResponse with _id: ${id}`);
  43. this.eventResponses[eventResponseIndex] = {
  44. ...this.eventResponses[eventResponseIndex],
  45. ...newProperties,
  46. };
  47. }
  48. async markEventAsSeen(ven, eventId, modificationNumber) {
  49. const existing = ven.seen_events || [];
  50. const newVen = {
  51. ...ven,
  52. seen_events: [
  53. ...existing,
  54. {
  55. event_id: eventId,
  56. modification_number: modificationNumber,
  57. },
  58. ],
  59. };
  60. await this.updateVen(ven._id, newVen);
  61. }
  62. async getEvents() {
  63. return [...this.events];
  64. }
  65. async sendReportReadings(ven, readings) {
  66. this.reportReadings.push({ ven: ven._id, readings });
  67. }
  68. }
  69. module.exports = FakeNantumModule;