team.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. }
  148. },
  149. setTeamDesc(team, teamDesc) {
  150. if (Meteor.user() && Meteor.user().isAdmin) {
  151. check(team, Object);
  152. check(teamDesc, String);
  153. Team.update(team, {
  154. $set: { teamDesc: teamDesc },
  155. });
  156. }
  157. },
  158. setTeamShortName(team, teamShortName) {
  159. if (Meteor.user() && Meteor.user().isAdmin) {
  160. check(team, Object);
  161. check(teamShortName, String);
  162. Team.update(team, {
  163. $set: { teamShortName: teamShortName },
  164. });
  165. }
  166. },
  167. setTeamIsActive(team, teamIsActive) {
  168. if (Meteor.user() && Meteor.user().isAdmin) {
  169. check(team, Object);
  170. check(teamIsActive, Boolean);
  171. Team.update(team, {
  172. $set: { teamIsActive: teamIsActive },
  173. });
  174. }
  175. },
  176. setTeamAllFields(
  177. team,
  178. teamDisplayName,
  179. teamDesc,
  180. teamShortName,
  181. teamWebsite,
  182. teamIsActive,
  183. ) {
  184. if (Meteor.user() && Meteor.user().isAdmin) {
  185. check(team, Object);
  186. check(teamDisplayName, String);
  187. check(teamDesc, String);
  188. check(teamShortName, String);
  189. check(teamWebsite, String);
  190. check(teamIsActive, Boolean);
  191. Team.update(team, {
  192. $set: {
  193. teamDisplayName: teamDisplayName,
  194. teamDesc: teamDesc,
  195. teamShortName: teamShortName,
  196. teamWebsite: teamWebsite,
  197. teamIsActive: teamIsActive,
  198. },
  199. });
  200. }
  201. },
  202. });
  203. }
  204. if (Meteor.isServer) {
  205. // Index for Team name.
  206. Meteor.startup(() => {
  207. Team._collection._ensureIndex({ teamDisplayName: -1 });
  208. });
  209. }
  210. export default Team;