team.js 5.1 KB

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