'use strict'; const { v4 } = require('uuid'); const _ = require('lodash'); class FakeNantumModule { constructor({ events, vens } = {}) { this.vens = vens || []; this.events = events || []; this.eventResponses = []; this.reportReadings = []; } async getVen(clientCertificateFingerprint) { return _.find(this.vens, { client_certificate_fingerprint: clientCertificateFingerprint, }); } async createVen(ven) { const venId = v4(); const newVen = { ...ven, _id: venId }; this.vens.push(newVen); } async updateVen(venId, newProperties) { const venIndex = _.findIndex(this.vens, { _id: venId }); if (venIndex == null) throw new Error(`Could not find ven with _id: ${venId}`); this.vens[venIndex] = { ...this.vens[venIndex], ...newProperties }; } async getEventResponse(ven, eventId, modificationNumber) { return _.find(this.eventResponses, { ven_id: ven._id, event_id: eventId, modification_number: modificationNumber, })[0]; } async createEventResponse(eventResponse) { const eventResponseId = v4(); const newEventResponse = { ...eventResponse, _id: eventResponseId }; this.eventResponses.push(newEventResponse); } async updateEventResponse(id, newProperties) { const eventResponseIndex = _.findIndex(this.eventResponses, { _id: id }); if (eventResponseIndex == null) throw new Error(`Could not find eventResponse with _id: ${id}`); this.eventResponses[eventResponseIndex] = { ...this.eventResponses[eventResponseIndex], ...newProperties, }; } async markEventAsSeen(ven, eventId, modificationNumber) { const existing = ven.seen_events || []; const newVen = { ...ven, seen_events: [ ...existing, { event_id: eventId, modification_number: modificationNumber, }, ], }; await this.updateVen(ven._id, newVen); } async getEvents() { return [...this.events]; } async sendReportReadings(ven, readings) { this.reportReadings.push({ ven: ven._id, readings }); } } module.exports = FakeNantumModule;