'use strict'; const Nantum = require('@hw/edge-sdks'); const { API } = Nantum; const logger = require('../logger'); const { company, nantumUrl } = require('../config'); const { request } = API({ company, logger }); async function getVen(clientCertificateFingerprint) { const results = await request({ uri: `${nantumUrl}/oadr_vens`, query: { client_certificate_fingerprint: clientCertificateFingerprint, }, }); return results[0]; } async function createVen(ven) { await request({ method: 'POST', uri: `${nantumUrl}/oadr_vens`, body: ven, }); } async function updateVen(id, newProperties) { await request({ method: 'PUT', uri: `${nantumUrl}/oadr_vens/${id}`, body: newProperties, }); } async function getEventResponse(ven, eventId, modificationNumber) { const results = await request({ uri: `${nantumUrl}/oadr_event_responses`, query: { ven_id: ven._id, event_id: eventId, modification_number: modificationNumber, }, }); return results[0]; } async function createEventResponse(eventResponse) { await request({ method: 'POST', uri: `${nantumUrl}/oadr_event_responses`, body: eventResponse, }); } async function updateEventResponse(id, newProperties) { await request({ method: 'PUT', uri: `${nantumUrl}/oadr_event_responses/${id}`, body: newProperties, }); } async function markEventAsSeen(ven, eventId, modificationNumber) { //TODO: potentially racy. Consider breaking out `seen_events` into its own document. const existing = ven.seen_events || []; await request({ method: 'PUT', uri: `${nantumUrl}/oadr_vens/${ven._id}`, body: { seen_events: [ ...existing, { event_id: eventId, modification_number: modificationNumber, }, ], }, }); } async function getEvents() { return await request({ uri: `${nantumUrl}/oadr_events`, query: {}, }); } async function sendReportReadings(ven, readings) { logger.info( 'received report readings', ven._id, JSON.stringify(readings, null, 2), ); } module.exports = { getEvents, getEventResponse, createEventResponse, updateEventResponse, markEventAsSeen, getVen, updateVen, createVen, sendReportReadings, };