| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- 'use strict';
- const { Ven } = require('../db');
- async function fetchEvent(venId) {
- return {
- event_identifier: 'aa2233eca8d4e829ff5c0f0c8e68710',
- client_id: venId,
- test_event: false,
- event_mod_number: 0,
- offLine: false,
- dr_mode_data: {
- operation_mode_value: 'NORMAL',
- // currentTime: 'xxxxx', //TODO: find reasonable value
- },
- dr_event_data: {
- notification_time: '2020-05-14T00:00:00.000Z',
- start_time: '2020-05-15T14:00:00.000Z',
- end_time: '2020-05-15T15: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 fetchReport(venId) {
- const venRecord = await Ven.findOne({
- where: { ven_id: venId },
- });
- if (venRecord && venRecord.data.report) return venRecord.data.report;
- return {};
- }
- async function fetchOpted(venId) {
- const venRecord = await Ven.findOne({
- where: { ven_id: venId },
- });
- if (venRecord && venRecord.data.opted) return venRecord.data.opted;
- return [];
- }
- 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();
- }
- async function updateOpted(venId, opted) {
- let venRecord = await Ven.findOne({
- where: { ven_id: venId },
- });
- if (venRecord) {
- const newData = venRecord.data || {};
- newData.opted = opted;
- venRecord.set('data', newData); // setting `data` directly on object doesn't trigger change detection
- } else {
- throw new Error(`Ven ${venId} must be registered`);
- }
- await venRecord.save();
- }
- async function updateReport(venId, report) {
- let venRecord = await Ven.findOne({
- where: { ven_id: venId },
- });
- if (venRecord) {
- const newData = venRecord.data || {};
- newData.report = report;
- venRecord.set('data', newData); // setting `data` directly on object doesn't trigger change detection
- } else {
- throw new Error(`Ven ${venId} must be registered`);
- }
- await venRecord.save();
- }
- module.exports = {
- fetchEvent,
- fetchOpted,
- fetchRegistration,
- fetchReport,
- updateRegistration,
- updateReport,
- updateOpted,
- };
|