team.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. Team = new Mongo.Collection('team');
  3. /**
  4. * A Team in Wekan. Organization in Trello.
  5. */
  6. Team.attachSchema(
  7. new SimpleSchema({
  8. teamDisplayName: {
  9. /**
  10. * the name to display for the team
  11. */
  12. type: String,
  13. optional: true,
  14. },
  15. teamDesc: {
  16. /**
  17. * the description the team
  18. */
  19. type: String,
  20. optional: true,
  21. max: 190,
  22. },
  23. teamShortName: {
  24. /**
  25. * short name of the team
  26. */
  27. type: String,
  28. optional: true,
  29. max: 255,
  30. },
  31. teamWebsite: {
  32. /**
  33. * website of the team
  34. */
  35. type: String,
  36. optional: true,
  37. max: 255,
  38. },
  39. teamIsActive: {
  40. /**
  41. * status of the team
  42. */
  43. type: Boolean,
  44. optional: true,
  45. },
  46. createdAt: {
  47. /**
  48. * creation date of the team
  49. */
  50. type: Date,
  51. // eslint-disable-next-line consistent-return
  52. autoValue() {
  53. if (this.isInsert) {
  54. return new Date();
  55. } else if (this.isUpsert) {
  56. return { $setOnInsert: new Date() };
  57. } else {
  58. this.unset();
  59. }
  60. },
  61. },
  62. modifiedAt: {
  63. type: Date,
  64. denyUpdate: false,
  65. // eslint-disable-next-line consistent-return
  66. autoValue() {
  67. if (this.isInsert || this.isUpsert || this.isUpdate) {
  68. return new Date();
  69. } else {
  70. this.unset();
  71. }
  72. },
  73. },
  74. }),
  75. );
  76. if (Meteor.isServer) {
  77. Team.allow({
  78. insert(userId, doc) {
  79. const user = ReactiveCache.getUser(userId) || ReactiveCache.getCurrentUser();
  80. if (user?.isAdmin)
  81. return true;
  82. if (!user) {
  83. return false;
  84. }
  85. return doc._id === userId;
  86. },
  87. update(userId, doc) {
  88. const user = ReactiveCache.getUser(userId) || ReactiveCache.getCurrentUser();
  89. if (user?.isAdmin)
  90. return true;
  91. if (!user) {
  92. return false;
  93. }
  94. return doc._id === userId;
  95. },
  96. remove(userId, doc) {
  97. const user = ReactiveCache.getUser(userId) || ReactiveCache.getCurrentUser();
  98. if (user?.isAdmin)
  99. return true;
  100. if (!user) {
  101. return false;
  102. }
  103. return doc._id === userId;
  104. },
  105. fetch: [],
  106. });
  107. Meteor.methods({
  108. setCreateTeam(
  109. teamDisplayName,
  110. teamDesc,
  111. teamShortName,
  112. teamWebsite,
  113. teamIsActive,
  114. ) {
  115. if (ReactiveCache.getCurrentUser()?.isAdmin) {
  116. check(teamDisplayName, String);
  117. check(teamDesc, String);
  118. check(teamShortName, String);
  119. check(teamWebsite, String);
  120. check(teamIsActive, Boolean);
  121. const nTeamNames = ReactiveCache.getTeams({ teamShortName }).length;
  122. if (nTeamNames > 0) {
  123. throw new Meteor.Error('teamname-already-taken');
  124. } else {
  125. Team.insert({
  126. teamDisplayName,
  127. teamDesc,
  128. teamShortName,
  129. teamWebsite,
  130. teamIsActive,
  131. });
  132. }
  133. }
  134. },
  135. setCreateTeamFromOidc(
  136. teamDisplayName,
  137. teamDesc,
  138. teamShortName,
  139. teamWebsite,
  140. teamIsActive,
  141. ) {
  142. check(teamDisplayName, String);
  143. check(teamDesc, String);
  144. check(teamShortName, String);
  145. check(teamWebsite, String);
  146. check(teamIsActive, Boolean);
  147. const nTeamNames = ReactiveCache.getTeams({ teamShortName }).length;
  148. if (nTeamNames > 0) {
  149. throw new Meteor.Error('teamname-already-taken');
  150. } else {
  151. Team.insert({
  152. teamDisplayName,
  153. teamDesc,
  154. teamShortName,
  155. teamWebsite,
  156. teamIsActive,
  157. });
  158. }
  159. },
  160. setTeamDisplayName(team, teamDisplayName) {
  161. if (ReactiveCache.getCurrentUser()?.isAdmin) {
  162. check(team, Object);
  163. check(teamDisplayName, String);
  164. Team.update(team, {
  165. $set: { teamDisplayName: teamDisplayName },
  166. });
  167. Meteor.call('setUsersTeamsTeamDisplayName', team._id, teamDisplayName);
  168. }
  169. },
  170. setTeamDesc(team, teamDesc) {
  171. if (ReactiveCache.getCurrentUser()?.isAdmin) {
  172. check(team, Object);
  173. check(teamDesc, String);
  174. Team.update(team, {
  175. $set: { teamDesc: teamDesc },
  176. });
  177. }
  178. },
  179. setTeamShortName(team, teamShortName) {
  180. if (ReactiveCache.getCurrentUser()?.isAdmin) {
  181. check(team, Object);
  182. check(teamShortName, String);
  183. Team.update(team, {
  184. $set: { teamShortName: teamShortName },
  185. });
  186. }
  187. },
  188. setTeamIsActive(team, teamIsActive) {
  189. if (ReactiveCache.getCurrentUser()?.isAdmin) {
  190. check(team, Object);
  191. check(teamIsActive, Boolean);
  192. Team.update(team, {
  193. $set: { teamIsActive: teamIsActive },
  194. });
  195. }
  196. },
  197. setTeamAllFieldsFromOidc(
  198. team,
  199. teamDisplayName,
  200. teamDesc,
  201. teamShortName,
  202. teamWebsite,
  203. teamIsActive,
  204. ) {
  205. check(team, Object);
  206. check(teamDisplayName, String);
  207. check(teamDesc, String);
  208. check(teamShortName, String);
  209. check(teamWebsite, String);
  210. check(teamIsActive, Boolean);
  211. Team.update(team, {
  212. $set: {
  213. teamDisplayName: teamDisplayName,
  214. teamDesc: teamDesc,
  215. teamShortName: teamShortName,
  216. teamWebsite: teamWebsite,
  217. teamIsActive: teamIsActive,
  218. },
  219. });
  220. Meteor.call('setUsersTeamsTeamDisplayName', team._id, teamDisplayName);
  221. },
  222. setTeamAllFields(
  223. team,
  224. teamDisplayName,
  225. teamDesc,
  226. teamShortName,
  227. teamWebsite,
  228. teamIsActive,
  229. ) {
  230. if (ReactiveCache.getCurrentUser()?.isAdmin) {
  231. check(team, Object);
  232. check(teamDisplayName, String);
  233. check(teamDesc, String);
  234. check(teamShortName, String);
  235. check(teamWebsite, String);
  236. check(teamIsActive, Boolean);
  237. Team.update(team, {
  238. $set: {
  239. teamDisplayName: teamDisplayName,
  240. teamDesc: teamDesc,
  241. teamShortName: teamShortName,
  242. teamWebsite: teamWebsite,
  243. teamIsActive: teamIsActive,
  244. },
  245. });
  246. Meteor.call('setUsersTeamsTeamDisplayName', team._id, teamDisplayName);
  247. }
  248. },
  249. });
  250. }
  251. if (Meteor.isServer) {
  252. // Index for Team name.
  253. Meteor.startup(() => {
  254. Team._collection.createIndex({ teamDisplayName: 1 });
  255. });
  256. }
  257. export default Team;