You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
3.8 KiB

11 months ago
  1. const { safeJsonParse } = require("../utils/http");
  2. const prisma = require("../utils/prisma");
  3. const Invite = {
  4. makeCode: () => {
  5. const uuidAPIKey = require("uuid-apikey");
  6. return uuidAPIKey.create().apiKey;
  7. },
  8. create: async function ({ createdByUserId = 0, workspaceIds = [] }) {
  9. try {
  10. const invite = await prisma.invites.create({
  11. data: {
  12. code: this.makeCode(),
  13. createdBy: createdByUserId,
  14. workspaceIds: JSON.stringify(workspaceIds),
  15. },
  16. });
  17. return { invite, error: null };
  18. } catch (error) {
  19. console.error("FAILED TO CREATE INVITE.", error.message);
  20. return { invite: null, error: error.message };
  21. }
  22. },
  23. deactivate: async function (inviteId = null) {
  24. try {
  25. await prisma.invites.update({
  26. where: { id: Number(inviteId) },
  27. data: { status: "disabled" },
  28. });
  29. return { success: true, error: null };
  30. } catch (error) {
  31. console.error(error.message);
  32. return { success: false, error: error.message };
  33. }
  34. },
  35. markClaimed: async function (inviteId = null, user) {
  36. try {
  37. const invite = await prisma.invites.update({
  38. where: { id: Number(inviteId) },
  39. data: { status: "claimed", claimedBy: user.id },
  40. });
  41. try {
  42. if (!!invite?.workspaceIds) {
  43. const { Workspace } = require("./workspace");
  44. const { WorkspaceUser } = require("./workspaceUsers");
  45. const workspaceIds = (await Workspace.where({})).map(
  46. (workspace) => workspace.id
  47. );
  48. const ids = safeJsonParse(invite.workspaceIds)
  49. .map((id) => Number(id))
  50. .filter((id) => workspaceIds.includes(id));
  51. if (ids.length !== 0) await WorkspaceUser.createMany(user.id, ids);
  52. }
  53. } catch (e) {
  54. console.error(
  55. "Could not add user to workspaces automatically",
  56. e.message
  57. );
  58. }
  59. return { success: true, error: null };
  60. } catch (error) {
  61. console.error(error.message);
  62. return { success: false, error: error.message };
  63. }
  64. },
  65. get: async function (clause = {}) {
  66. try {
  67. const invite = await prisma.invites.findFirst({ where: clause });
  68. return invite || null;
  69. } catch (error) {
  70. console.error(error.message);
  71. return null;
  72. }
  73. },
  74. count: async function (clause = {}) {
  75. try {
  76. const count = await prisma.invites.count({ where: clause });
  77. return count;
  78. } catch (error) {
  79. console.error(error.message);
  80. return 0;
  81. }
  82. },
  83. delete: async function (clause = {}) {
  84. try {
  85. await prisma.invites.deleteMany({ where: clause });
  86. return true;
  87. } catch (error) {
  88. console.error(error.message);
  89. return false;
  90. }
  91. },
  92. where: async function (clause = {}, limit) {
  93. try {
  94. const invites = await prisma.invites.findMany({
  95. where: clause,
  96. take: limit || undefined,
  97. });
  98. return invites;
  99. } catch (error) {
  100. console.error(error.message);
  101. return [];
  102. }
  103. },
  104. whereWithUsers: async function (clause = {}, limit) {
  105. const { User } = require("./user");
  106. try {
  107. const invites = await this.where(clause, limit);
  108. for (const invite of invites) {
  109. if (invite.claimedBy) {
  110. const acceptedUser = await User.get({ id: invite.claimedBy });
  111. invite.claimedBy = {
  112. id: acceptedUser?.id,
  113. username: acceptedUser?.username,
  114. };
  115. }
  116. if (invite.createdBy) {
  117. const createdUser = await User.get({ id: invite.createdBy });
  118. invite.createdBy = {
  119. id: createdUser?.id,
  120. username: createdUser?.username,
  121. };
  122. }
  123. }
  124. return invites;
  125. } catch (error) {
  126. console.error(error.message);
  127. return [];
  128. }
  129. },
  130. };
  131. module.exports = { Invite };