org.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. Org = new Mongo.Collection('org');
  2. /**
  3. * A Organization in Wekan. A Enterprise in Trello.
  4. */
  5. Org.attachSchema(
  6. new SimpleSchema({
  7. orgDisplayName: {
  8. /**
  9. * the name to display for the organization
  10. */
  11. type: String,
  12. optional: true,
  13. },
  14. orgDesc: {
  15. /**
  16. * the description the organization
  17. */
  18. type: String,
  19. optional: true,
  20. max: 190,
  21. },
  22. orgShortName: {
  23. /**
  24. * short name of the organization
  25. */
  26. type: String,
  27. optional: true,
  28. max: 255,
  29. },
  30. orgWebsite: {
  31. /**
  32. * website of the organization
  33. */
  34. type: String,
  35. optional: true,
  36. max: 255,
  37. },
  38. orgIsActive: {
  39. /**
  40. * status of the organization
  41. */
  42. type: Boolean,
  43. optional: true,
  44. },
  45. createdAt: {
  46. /**
  47. * creation date of the organization
  48. */
  49. type: Date,
  50. denyUpdate: false,
  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. Org.allow({
  78. insert(userId, doc) {
  79. const user = Users.findOne({
  80. _id: userId,
  81. });
  82. if ((user && user.isAdmin) || (Meteor.user() && Meteor.user().isAdmin))
  83. return true;
  84. if (!user) {
  85. return false;
  86. }
  87. return doc._id === userId;
  88. },
  89. update(userId, doc) {
  90. const user = Users.findOne({
  91. _id: userId,
  92. });
  93. if ((user && user.isAdmin) || (Meteor.user() && Meteor.user().isAdmin))
  94. return true;
  95. if (!user) {
  96. return false;
  97. }
  98. return doc._id === userId;
  99. },
  100. remove(userId, doc) {
  101. const user = Users.findOne({
  102. _id: userId,
  103. });
  104. if ((user && user.isAdmin) || (Meteor.user() && Meteor.user().isAdmin))
  105. return true;
  106. if (!user) {
  107. return false;
  108. }
  109. return doc._id === userId;
  110. },
  111. fetch: [],
  112. });
  113. Meteor.methods({
  114. setCreateOrg(
  115. orgDisplayName,
  116. orgDesc,
  117. orgShortName,
  118. orgWebsite,
  119. orgIsActive,
  120. ) {
  121. if (Meteor.user() && Meteor.user().isAdmin) {
  122. check(orgDisplayName, String);
  123. check(orgDesc, String);
  124. check(orgShortName, String);
  125. check(orgWebsite, String);
  126. check(orgIsActive, Boolean);
  127. const nOrgNames = Org.find({ orgShortName }).count();
  128. if (nOrgNames > 0) {
  129. throw new Meteor.Error('orgname-already-taken');
  130. } else {
  131. Org.insert({
  132. orgDisplayName,
  133. orgDesc,
  134. orgShortName,
  135. orgWebsite,
  136. orgIsActive,
  137. });
  138. }
  139. }
  140. },
  141. setOrgDisplayName(org, orgDisplayName) {
  142. if (Meteor.user() && Meteor.user().isAdmin) {
  143. check(org, Object);
  144. check(orgDisplayName, String);
  145. Org.update(org, {
  146. $set: { orgDisplayName: orgDisplayName },
  147. });
  148. Meteor.call('setUsersOrgsOrgDisplayName', org._id, orgDisplayName);
  149. }
  150. },
  151. setOrgDesc(org, orgDesc) {
  152. if (Meteor.user() && Meteor.user().isAdmin) {
  153. check(org, Object);
  154. check(orgDesc, String);
  155. Org.update(org, {
  156. $set: { orgDesc: orgDesc },
  157. });
  158. }
  159. },
  160. setOrgShortName(org, orgShortName) {
  161. if (Meteor.user() && Meteor.user().isAdmin) {
  162. check(org, Object);
  163. check(orgShortName, String);
  164. Org.update(org, {
  165. $set: { orgShortName: orgShortName },
  166. });
  167. }
  168. },
  169. setOrgIsActive(org, orgIsActive) {
  170. if (Meteor.user() && Meteor.user().isAdmin) {
  171. check(org, Object);
  172. check(orgIsActive, Boolean);
  173. Org.update(org, {
  174. $set: { orgIsActive: orgIsActive },
  175. });
  176. }
  177. },
  178. setOrgAllFields(
  179. org,
  180. orgDisplayName,
  181. orgDesc,
  182. orgShortName,
  183. orgWebsite,
  184. orgIsActive,
  185. ) {
  186. if (Meteor.user() && Meteor.user().isAdmin) {
  187. check(org, Object);
  188. check(orgDisplayName, String);
  189. check(orgDesc, String);
  190. check(orgShortName, String);
  191. check(orgWebsite, String);
  192. check(orgIsActive, Boolean);
  193. Org.update(org, {
  194. $set: {
  195. orgDisplayName: orgDisplayName,
  196. orgDesc: orgDesc,
  197. orgShortName: orgShortName,
  198. orgWebsite: orgWebsite,
  199. orgIsActive: orgIsActive,
  200. },
  201. });
  202. Meteor.call('setUsersOrgsOrgDisplayName', org._id, orgDisplayName);
  203. }
  204. },
  205. });
  206. }
  207. if (Meteor.isServer) {
  208. // Index for Organization name.
  209. Meteor.startup(() => {
  210. // Org._collection.createIndex({ name: -1 });
  211. Org._collection.createIndex({ orgDisplayName: 1 });
  212. });
  213. }
  214. export default Org;