Source: lib/db/writes.js

  1. /** @module lib/db/writes */
  2. import { ObjectId } from "bson";
  3. import {
  4. getApiKeysCollection,
  5. getApprovedCompletionsCollection,
  6. getCompletionsPendingReviewCollection,
  7. getRejectedCompletionsCollection,
  8. } from "../mongodb";
  9. /**
  10. * Approves a completion.
  11. *
  12. * @param data The completion data to approve.
  13. */
  14. async function approveCompletion(data) {
  15. const [collection, approvedCollection] = await Promise.all([
  16. getCompletionsPendingReviewCollection(),
  17. getApprovedCompletionsCollection(),
  18. ]);
  19. const id = data._id;
  20. delete data._id;
  21. const approvedResult = await approvedCollection.insertOne(data);
  22. await collection.deleteOne({ _id: new ObjectId(id) });
  23. return approvedResult;
  24. }
  25. /**
  26. * Rejects a completion.
  27. *
  28. * @param data The completion data to reject.
  29. */
  30. async function rejectCompletion(data) {
  31. const [collection, rejectedCollection] = await Promise.all([
  32. getCompletionsPendingReviewCollection(),
  33. getRejectedCompletionsCollection(),
  34. ]);
  35. const id = data._id;
  36. delete data._id;
  37. const rejectedResult = await rejectedCollection.insertOne(data);
  38. await collection.deleteOne({ _id: new ObjectId(id) });
  39. return rejectedResult;
  40. }
  41. async function approve2Reject(data) {
  42. const [collection, rejectedCollection] = await Promise.all([
  43. getApprovedCompletionsCollection(),
  44. getRejectedCompletionsCollection(),
  45. ]);
  46. const id = data._id;
  47. delete data._id;
  48. const rejectedResult = await rejectedCollection.insertOne(data);
  49. await collection.deleteOne({ _id: new ObjectId(id) });
  50. return rejectedResult;
  51. }
  52. async function reject2Approve(data) {
  53. const [collection, approvedCollection] = await Promise.all([
  54. getRejectedCompletionsCollection(),
  55. getApprovedCompletionsCollection(),
  56. ]);
  57. const id = data._id;
  58. delete data._id;
  59. const approvedResult = await approvedCollection.insertOne(data);
  60. await collection.deleteOne({ _id: new ObjectId(id) });
  61. return approvedResult;
  62. }
  63. /**
  64. * Starts a review pipeline for a completion.
  65. *
  66. * @param data The completion data to review
  67. * @param {"pending2approve" | "pending2reject" | "approve2reject" | "reject2Approve"} direction Direction of the review
  68. */
  69. export async function reviewCompletion(data, direction) {
  70. // TODO: Does approve2pending; reject2pending make sense?
  71. switch (direction) {
  72. case "pending2approve":
  73. return approveCompletion(data);
  74. case "pending2reject":
  75. return rejectCompletion(data);
  76. // TODO: Support these use cases on the front-end, eventually
  77. case "approve2reject":
  78. return approve2Reject(data);
  79. case "reject2approve":
  80. return reject2Approve(data);
  81. default:
  82. throw new Error("Invalid direction");
  83. }
  84. }
  85. /**
  86. * Saves a completion to the database for review.
  87. *
  88. * @param completionData Completion data to save
  89. */
  90. export async function saveCompletion(completionData) {
  91. const collection = await getCompletionsPendingReviewCollection();
  92. const result = await collection.insertOne(completionData);
  93. return result;
  94. }
  95. /**
  96. * Inserts an API key created from the API Management page into the database.
  97. *
  98. * @param data The API key data to save.
  99. */
  100. export async function saveApiKeys(data) {
  101. const collection = await getApiKeysCollection();
  102. const result = await collection.insertOne({
  103. ...data,
  104. createdAt: new Date().toISOString(),
  105. });
  106. return result;
  107. }