Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. import { Schema, model } from "mongoose";
  2.  
  3. const eventSchema = new Schema({
  4. callTimes: { type: Array, of: Date, required: true },
  5. eventDate: { type: Date, required: true },
  6. employeeResponses: [
  7. {
  8. _id: {
  9. type: Schema.Types.ObjectId,
  10. ref: "User",
  11. required: true,
  12. },
  13. response: { type: String, required: true },
  14. notes: String,
  15. },
  16. ],
  17. scheduledEmployees: [
  18. {
  19. _id: { type: Schema.Types.ObjectId, ref: "User" },
  20. },
  21. ],
  22. });
  23.  
  24. export default model("Event", eventSchema);
  25.  
  26. [
  27.  
  28. {
  29. "_id": "5d5daaafcf11d95c0a75a023",
  30. "callTimes": [
  31. "2019-08-21T10:30:41-07:00",
  32. "2019-08-21T11:00:41-07:00",
  33. "2019-08-21T11:30:41-07:00"
  34. ],
  35. "eventDate": "2019-08-20T02:30:36.000Z",
  36. "employeeResponses": [],
  37. "scheduledEmployees": []
  38. },
  39.  
  40. {
  41. "_id": "5d5b5ee857a6d20abf49db19",
  42. "callTimes": [
  43. "2019-08-19T17:15:43-07:00",
  44. "2019-08-19T17:45:43-07:00",
  45. "2019-08-19T18:15:43-07:00",
  46. "2019-08-19T19:00:43-07:00"
  47. ],
  48. "eventDate": "2019-08-21T02:30:36.000Z",
  49. "employeeResponses": [
  50. {
  51. "_id": "5d5b5e952871780ef474807b",
  52. "response": "Available to work.",
  53. "notes": "I can work all day."
  54. },
  55. {
  56. "_id": "5d5b5e952871780ef474807c",
  57. "response": "Not available to work.",
  58. "notes": "I have school during that time."
  59. }
  60. ],
  61. "scheduledEmployees": []
  62. }
  63.  
  64. ...etc
  65.  
  66. ]
  67.  
  68. const updateFormAp = async (req, res) => {
  69. try {
  70. const { _id, responses, notes } = req.body;
  71.  
  72. if (!_id || !responses) throw "Missing required update event parameters. You must include an event id and response.";
  73.  
  74. const formExists = await Form.findOne({ _id });
  75. if (!formExists) throw "Unable to locate that event form.";
  76.  
  77. // iterate over responses and update the Events accordingly...
  78. await Event.bulkWrite(
  79. responses.map(response => {
  80. try {
  81. const { id: eventId, value, updateEvent } = response;
  82. const { id: userId } = req.session.user;
  83.  
  84. // if the employee response exists...
  85. const filter = updateEvent
  86. ? {
  87. // set the filter to event id + employee id
  88. _id: eventId,
  89. "employeeResponses._id": userId,
  90. }
  91. : {
  92. // else set the filter to event id only
  93. _id: eventId,
  94. };
  95.  
  96. // if the employee response exists...
  97. const update = updateEvent
  98. ? {
  99. // update the sub document in place
  100. $set: {
  101. "employeeResponses.$.response": value,
  102. "employeeResponses.$.notes": notes,
  103. },
  104. }
  105. : {
  106. // else add a new sub document
  107. $push: {
  108. employeeResponses: {
  109. _id: userId,
  110. response: value,
  111. notes,
  112. },
  113. },
  114. };
  115.  
  116. return {
  117. updateOne: {
  118. filter,
  119. update,
  120. },
  121. };
  122. } catch (error) {
  123. throw error;
  124. }
  125. }),
  126. );
  127.  
  128. res
  129. .status(201)
  130. .json({ message: "Successfully added your responses to the A/P form!" });
  131. } catch (err) {
  132. res
  133. .status(400)
  134. .json({ error: err.toString() });
  135. }
  136. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement