nantum.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 fetchOpted(venId) {
  38. const venRecord = await Ven.findOne({
  39. where: { ven_id: venId },
  40. });
  41. if (venRecord) return venRecord.data.opted || [];
  42. return [];
  43. }
  44. async function updateRegistration(registration) {
  45. if (registration.ven_id == null) {
  46. throw new Error('Registration is missing ven_id');
  47. }
  48. if (registration.common_name == null) {
  49. throw new Error('Registration is missing common_name');
  50. }
  51. let venRecord = await Ven.findOne({
  52. where: { ven_id: registration.ven_id },
  53. });
  54. if (venRecord) {
  55. const newData = venRecord.data || {};
  56. newData.registration = registration;
  57. venRecord.set('data', newData); // setting `data` directly on object doesn't trigger change detection
  58. } else {
  59. venRecord = new Ven();
  60. venRecord.ven_id = registration.ven_id;
  61. venRecord.common_name = registration.common_name;
  62. const newData = { registration: registration };
  63. venRecord.set('data', newData);
  64. }
  65. await venRecord.save();
  66. }
  67. async function updateOpted(venId, opted) {
  68. let venRecord = await Ven.findOne({
  69. where: { ven_id: venId },
  70. });
  71. if (venRecord) {
  72. const newData = venRecord.data || {};
  73. newData.opted = opted;
  74. venRecord.set('data', newData); // setting `data` directly on object doesn't trigger change detection
  75. } else {
  76. throw new Error(`Ven ${venId} must be registered`);
  77. }
  78. await venRecord.save();
  79. }
  80. module.exports = {
  81. fetchEvent,
  82. fetchOpted,
  83. fetchRegistration,
  84. updateRegistration,
  85. updateOpted,
  86. };