nantum.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. 'use strict';
  2. const { Ven } = require('../db');
  3. async function fetchEvent(venId) {
  4. return {
  5. event_identifier: 'aa2233eca8d4e829ff5c0f0c8e68710',
  6. client_id: venId,
  7. test_event: false,
  8. event_mod_number: 0,
  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-05-14T00:00:00.000Z',
  16. start_time: '2020-05-15T14:00:00.000Z',
  17. end_time: '2020-05-15T15: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 fetchReport(venId) {
  38. const venRecord = await Ven.findOne({
  39. where: { ven_id: venId },
  40. });
  41. if (venRecord && venRecord.data.report) return venRecord.data.report;
  42. return {};
  43. }
  44. async function fetchOpted(venId) {
  45. const venRecord = await Ven.findOne({
  46. where: { ven_id: venId },
  47. });
  48. if (venRecord && venRecord.data.opted) return venRecord.data.opted;
  49. return [];
  50. }
  51. async function updateRegistration(registration) {
  52. if (registration.ven_id == null) {
  53. throw new Error('Registration is missing ven_id');
  54. }
  55. if (registration.common_name == null) {
  56. throw new Error('Registration is missing common_name');
  57. }
  58. let venRecord = await Ven.findOne({
  59. where: { ven_id: registration.ven_id },
  60. });
  61. if (venRecord) {
  62. const newData = venRecord.data || {};
  63. newData.registration = registration;
  64. venRecord.set('data', newData); // setting `data` directly on object doesn't trigger change detection
  65. } else {
  66. venRecord = new Ven();
  67. venRecord.ven_id = registration.ven_id;
  68. venRecord.common_name = registration.common_name;
  69. const newData = { registration: registration };
  70. venRecord.set('data', newData);
  71. }
  72. await venRecord.save();
  73. }
  74. async function updateOpted(venId, opted) {
  75. let venRecord = await Ven.findOne({
  76. where: { ven_id: venId },
  77. });
  78. if (venRecord) {
  79. const newData = venRecord.data || {};
  80. newData.opted = opted;
  81. venRecord.set('data', newData); // setting `data` directly on object doesn't trigger change detection
  82. } else {
  83. throw new Error(`Ven ${venId} must be registered`);
  84. }
  85. await venRecord.save();
  86. }
  87. async function updateReport(venId, report) {
  88. let venRecord = await Ven.findOne({
  89. where: { ven_id: venId },
  90. });
  91. if (venRecord) {
  92. const newData = venRecord.data || {};
  93. newData.report = report;
  94. venRecord.set('data', newData); // setting `data` directly on object doesn't trigger change detection
  95. } else {
  96. throw new Error(`Ven ${venId} must be registered`);
  97. }
  98. await venRecord.save();
  99. }
  100. module.exports = {
  101. fetchEvent,
  102. fetchOpted,
  103. fetchRegistration,
  104. fetchReport,
  105. updateRegistration,
  106. updateReport,
  107. updateOpted,
  108. };