'use strict'; const { Ven } = require('../db'); async function fetchEvent(venId) { return { event_identifier: '112233eca8d4e829ff5c0f0c8e68710', client_id: venId, test_event: false, event_mod_number: 2, offLine: false, dr_mode_data: { operation_mode_value: 'NORMAL', // currentTime: 'xxxxx', //TODO: find reasonable value }, dr_event_data: { notification_time: '2020-04-22T00:00:00.000Z', start_time: '2020-04-22T06:00:00.000Z', end_time: '2020-04-22T06:55:00.000Z', event_instance: [ { event_type_id: 'LOAD_DISPATCH', event_info_values: [ { value: 41, timeOffset: 0 }, { value: 42, timeOffset: 5 * 60 }, { value: 43, timeOffset: 10 * 60 }, ], }, ], }, }; } async function fetchRegistration(venId) { const venRecord = await Ven.findOne({ where: { ven_id: venId }, }); if (venRecord) return venRecord.data.registration; } async function updateRegistration(registration) { if (registration.ven_id == null) { throw new Error('Registration is missing ven_id'); } if (registration.common_name == null) { throw new Error('Registration is missing common_name'); } let venRecord = await Ven.findOne({ where: { ven_id: registration.ven_id }, }); if (venRecord) { const newData = venRecord.data || {}; newData.registration = registration; venRecord.set('data', newData); // setting `data` directly on object doesn't trigger change detection } else { venRecord = new Ven(); venRecord.ven_id = registration.ven_id; venRecord.common_name = registration.common_name; const newData = { registration: registration }; venRecord.set('data', newData); } await venRecord.save(); } module.exports = { fetchEvent, fetchRegistration, updateRegistration, };