nantum.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use strict';
  2. const { Ven } = require('../db');
  3. async function fetchEvent(venId) {
  4. return {
  5. event_identifier: '112233eca8d4e829ff5c0f0c8e68710',
  6. client_id: venId,
  7. test_event: false,
  8. event_mod_number: 2,
  9. offLine: false,
  10. dr_mode_data: {
  11. operation_mode_value: 'NORMAL',
  12. // currentTime: 'xxxxx', //TODO: find reasonable value
  13. },
  14. dr_event_data: {
  15. notification_time: '2020-04-22T00:00:00.000Z',
  16. start_time: '2020-04-22T06:00:00.000Z',
  17. end_time: '2020-04-22T06:55:00.000Z',
  18. event_instance: [
  19. {
  20. event_type_id: 'LOAD_DISPATCH',
  21. event_info_values: [
  22. { value: 41, timeOffset: 0 },
  23. { value: 42, timeOffset: 5 * 60 },
  24. { value: 43, timeOffset: 10 * 60 },
  25. ],
  26. },
  27. ],
  28. },
  29. };
  30. }
  31. async function fetchRegistration(venId) {
  32. const venRecord = await Ven.findOne({
  33. where: { ven_id: venId },
  34. });
  35. if (venRecord) return venRecord.data.registration;
  36. }
  37. async function updateRegistration(registration) {
  38. if (registration.ven_id == null) {
  39. throw new Error('Registration is missing ven_id');
  40. }
  41. if (registration.common_name == null) {
  42. throw new Error('Registration is missing common_name');
  43. }
  44. let venRecord = await Ven.findOne({
  45. where: { ven_id: registration.ven_id },
  46. });
  47. if (venRecord) {
  48. const newData = venRecord.data || {};
  49. newData.registration = registration;
  50. venRecord.set('data', newData); // setting `data` directly on object doesn't trigger change detection
  51. } else {
  52. venRecord = new Ven();
  53. venRecord.ven_id = registration.ven_id;
  54. venRecord.common_name = registration.common_name;
  55. const newData = { registration: registration };
  56. venRecord.set('data', newData);
  57. }
  58. await venRecord.save();
  59. }
  60. module.exports = {
  61. fetchEvent,
  62. fetchRegistration,
  63. updateRegistration,
  64. };