team.service.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // api/team.service.js
  2. import ApplicationError from "../../libs/errors/application.error.js";
  3. import BaseService from "../../services/base.service.js";
  4. import userService from "../users/user.service.js";
  5. import Team from "./team.model.js";
  6. class TeamsService extends BaseService {
  7. getModel() {
  8. return Team;
  9. }
  10. /*async create(data, accountId) {
  11. try {
  12. let team = await this.oneBy({
  13. code: data.code,
  14. accountId: accountId,
  15. });
  16. if (team) {
  17. return new ApplicationError(
  18. "code is invalid or already taken",
  19. {},
  20. 500
  21. );
  22. }
  23. const newTeam = new Team(data);
  24. await newTeam.save();
  25. return newTeam.toObject();
  26. } catch (e) {
  27. return new ApplicationError(e.message, {}, 500);
  28. }
  29. }*/
  30. // In team.service.js
  31. async create(data, accountId) {
  32. try {
  33. let team = await this.oneBy({
  34. code: data.code,
  35. accountId: accountId,
  36. });
  37. if (team) {
  38. return new ApplicationError(
  39. "code is invalid or already taken",
  40. {},
  41. 500
  42. );
  43. }
  44. const newTeam = new Team({
  45. ...data,
  46. accountId: accountId // Add accountId to the team data
  47. });
  48. await newTeam.save();
  49. return newTeam.toObject();
  50. } catch (e) {
  51. return new ApplicationError(e.message, {}, 500);
  52. }
  53. }
  54. async update(id, accountId, data) {
  55. return this.getModel().findOneAndUpdate(
  56. { _id: id, accountId: accountId },
  57. data,
  58. { new: true }
  59. );
  60. }
  61. async delete(id, accountId) {
  62. let team = await this.oneBy({
  63. _id: id,
  64. accountId: accountId,
  65. });
  66. if (!team) {
  67. throw new ApplicationError("Team is not present", {}, 500);
  68. }
  69. for (const user of team.users) {
  70. await this.removeUser(id, accountId, user._id);
  71. }
  72. return this.getModel().findOneAndDelete({ _id: id, accountId: accountId });
  73. }
  74. async addUser(id, accountId, userId) {
  75. let team = await this.oneBy({
  76. _id: id,
  77. accountId: accountId,
  78. });
  79. if (!team) {
  80. throw new ApplicationError("Team is not present", {}, 500);
  81. }
  82. let users = team.users;
  83. let user = await userService.findById(userId);
  84. if (!user) {
  85. throw new ApplicationError("User is not present", {}, 500);
  86. }
  87. users.push({
  88. _id: user._id,
  89. email: user.email,
  90. });
  91. const uniqueUsers = users.reduce((accumulator, user) => {
  92. if (!accumulator.some((u) => u._id.equals(user._id))) {
  93. accumulator.push(user);
  94. }
  95. return accumulator;
  96. }, []);
  97. let updatedTeam = await this.update(id, accountId, {
  98. users: uniqueUsers,
  99. });
  100. let teams = user.teams;
  101. teams.push({
  102. _id: team._id,
  103. name: team.name,
  104. code: team.code,
  105. });
  106. const uniqueTeams = teams.reduce((accumulator, user) => {
  107. if (!accumulator.some((u) => u._id.equals(user._id))) {
  108. accumulator.push(user);
  109. }
  110. return accumulator;
  111. }, []);
  112. await userService.update(user._id, accountId, {
  113. teams: uniqueTeams,
  114. });
  115. return updatedTeam.toObject();
  116. }
  117. async removeUser(id, accountId, userId) {
  118. let team = await this.oneBy({
  119. _id: id,
  120. accountId: accountId,
  121. });
  122. if (!team) {
  123. throw new ApplicationError("Team is not present", {}, 500);
  124. }
  125. let user = await userService.findById(userId);
  126. if (!user) {
  127. throw new ApplicationError("User is not present", {}, 500);
  128. }
  129. let filteredUsers = team.users.filter((user) => {
  130. return user._id != userId;
  131. });
  132. let updatedTeam = await this.update(id, accountId, {
  133. users: filteredUsers,
  134. });
  135. let filteredTeams = user.teams.filter((team) => {
  136. return team._id != id;
  137. });
  138. await userService.update(user._id, accountId, {
  139. teams: filteredTeams,
  140. });
  141. return updatedTeam.toObject();
  142. }
  143. }
  144. export default new TeamsService();