'use strict'; const { v4 } = require('uuid'); const _ = require('lodash'); class FakeNantumModule { constructor({ vens, events, venRegistrations, venReports, seenEvents } = {}) { this.vens = vens || []; this.venRegistrations = venRegistrations || []; this.venReports = venReports || []; this.events = events || []; this.seenEvents = seenEvents || []; this.eventResponses = []; this.reportReadings = []; } async getVenRegistration(clientCertificateFingerprint) { return _.find(this.venRegistrations, { client_certificate_fingerprint: clientCertificateFingerprint, }); } async createVenRegistration(ven) { const venId = v4(); const newVen = { ...ven, _id: venId }; this.venRegistrations.push(newVen); } async deleteVenRegistration(venId) { const venIndex = _.findIndex(this.venRegistrations, { _id: venId }); if (venIndex == null) throw new Error(`Could not find ven with _id: ${venId}`); this.venRegistrations = this.venRegistrations.filter(ven => ven._id !== venId); } async getEventResponse(ven, eventId, modificationNumber) { return _.find(this.eventResponses, { oadr_ven_id: ven._id, oadr_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, }; } markEventAsSeen(oadrVenRegistrationId, eventId, modificationNumber) { const newSeenEvent = { _id: v4(), oadr_ven_registration_id: oadrVenRegistrationId, oadr_event_id: eventId, modification_number: modificationNumber }; this.seenEvents.push(newSeenEvent); } async getSeenEvents(venRegistrationId) { return _.filter(this.seenEvents, { oadr_ven_registration_id: venRegistrationId, }); } async getVen(venRegistrationId) { return _.filter(this.seenEvents, { oadr_ven_registration_id: venRegistrationId, })[0]; } async getEvents() { return [...this.events]; } async sendReportReadings(ven, readings) { this.reportReadings.push({ ven: ven._id, readings }); } async getVenReports(venRegistrationId) { return _.find(this.venReports, { oadr_ven_registration_id: venRegistrationId, }); } async updateVenReports(venReportId, newReportList) { const venReportIndex = _.findIndex(this.venReports, { _id: venReportId }); if (venReportIndex == null) throw new Error(`Could not find venReport with _id: ${venReportId}`); this.venReports[venReportIndex].reports = newReportList; } async createVenReports(venReports) { const venReportsId = v4(); const newVenReport = { ...venReports, _id: venReportsId }; this.venReports.push(newVenReport); } } module.exports = FakeNantumModule;