customFields.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. CustomFields = new Mongo.Collection('customFields');
  2. /**
  3. * A custom field on a card in the board
  4. */
  5. CustomFields.attachSchema(new SimpleSchema({
  6. boardIds: {
  7. /**
  8. * the ID of the board
  9. */
  10. type: [String],
  11. },
  12. name: {
  13. /**
  14. * name of the custom field
  15. */
  16. type: String,
  17. },
  18. type: {
  19. /**
  20. * type of the custom field
  21. */
  22. type: String,
  23. allowedValues: ['text', 'number', 'date', 'dropdown'],
  24. },
  25. settings: {
  26. /**
  27. * settings of the custom field
  28. */
  29. type: Object,
  30. },
  31. 'settings.dropdownItems': {
  32. /**
  33. * list of drop down items objects
  34. */
  35. type: [Object],
  36. optional: true,
  37. },
  38. 'settings.dropdownItems.$': {
  39. type: new SimpleSchema({
  40. _id: {
  41. /**
  42. * ID of the drop down item
  43. */
  44. type: String,
  45. },
  46. name: {
  47. /**
  48. * name of the drop down item
  49. */
  50. type: String,
  51. },
  52. }),
  53. },
  54. showOnCard: {
  55. /**
  56. * should we show on the cards this custom field
  57. */
  58. type: Boolean,
  59. },
  60. automaticallyOnCard: {
  61. /**
  62. * should the custom fields automatically be added on cards?
  63. */
  64. type: Boolean,
  65. },
  66. showLabelOnMiniCard: {
  67. /**
  68. * should the label of the custom field be shown on minicards?
  69. */
  70. type: Boolean,
  71. },
  72. }));
  73. CustomFields.mutations({
  74. addBoard(boardId) {
  75. if (boardId) {
  76. return {
  77. $push: {
  78. boardIds: boardId,
  79. },
  80. };
  81. } else {
  82. return null;
  83. }
  84. },
  85. });
  86. CustomFields.allow({
  87. insert(userId, doc) {
  88. return allowIsAnyBoardMember(userId, Boards.find({
  89. _id: {$in: doc.boardIds},
  90. }).fetch());
  91. },
  92. update(userId, doc) {
  93. return allowIsAnyBoardMember(userId, Boards.find({
  94. _id: {$in: doc.boardIds},
  95. }).fetch());
  96. },
  97. remove(userId, doc) {
  98. return allowIsAnyBoardMember(userId, Boards.find({
  99. _id: {$in: doc.boardIds},
  100. }).fetch());
  101. },
  102. fetch: ['userId', 'boardIds'],
  103. });
  104. // not sure if we need this?
  105. //CustomFields.hookOptions.after.update = { fetchPrevious: false };
  106. function customFieldCreation(userId, doc){
  107. Activities.insert({
  108. userId,
  109. activityType: 'createCustomField',
  110. boardId: doc.boardIds[0], // We are creating a customField, it has only one boardId
  111. customFieldId: doc._id,
  112. });
  113. }
  114. if (Meteor.isServer) {
  115. Meteor.startup(() => {
  116. CustomFields._collection._ensureIndex({ boardIds: 1 });
  117. });
  118. CustomFields.after.insert((userId, doc) => {
  119. customFieldCreation(userId, doc);
  120. });
  121. CustomFields.before.update((userId, doc, fieldNames, modifier) => {
  122. if (_.contains(fieldNames, 'boardIds') && modifier.$pull) {
  123. Cards.update(
  124. {boardId: modifier.$pull.boardIds, 'customFields._id': doc._id},
  125. {$pull: {'customFields': {'_id': doc._id}}},
  126. {multi: true}
  127. );
  128. Activities.remove({
  129. customFieldId: doc._id,
  130. boardId: modifier.$pull.boardIds,
  131. });
  132. } else if (_.contains(fieldNames, 'boardIds') && modifier.$push) {
  133. Activities.insert({
  134. userId,
  135. activityType: 'createCustomField',
  136. boardId: modifier.$push.boardIds,
  137. customFieldId: doc._id,
  138. });
  139. }
  140. });
  141. CustomFields.before.remove((userId, doc) => {
  142. Activities.remove({
  143. customFieldId: doc._id,
  144. });
  145. Cards.update(
  146. {boardId: {$in: doc.boardIds}, 'customFields._id': doc._id},
  147. {$pull: {'customFields': {'_id': doc._id}}},
  148. {multi: true}
  149. );
  150. });
  151. }
  152. //CUSTOM FIELD REST API
  153. if (Meteor.isServer) {
  154. /**
  155. * @operation get_all_custom_fields
  156. * @summary Get the list of Custom Fields attached to a board
  157. *
  158. * @param {string} boardID the ID of the board
  159. * @return_type [{_id: string,
  160. * name: string,
  161. * type: string}]
  162. */
  163. JsonRoutes.add('GET', '/api/boards/:boardId/custom-fields', function (req, res) {
  164. Authentication.checkUserId( req.userId);
  165. const paramBoardId = req.params.boardId;
  166. JsonRoutes.sendResult(res, {
  167. code: 200,
  168. data: CustomFields.find({ boardIds: {$in: [paramBoardId]} }).map(function (cf) {
  169. return {
  170. _id: cf._id,
  171. name: cf.name,
  172. type: cf.type,
  173. };
  174. }),
  175. });
  176. });
  177. /**
  178. * @operation get_custom_field
  179. * @summary Get a Custom Fields attached to a board
  180. *
  181. * @param {string} boardID the ID of the board
  182. * @param {string} customFieldId the ID of the custom field
  183. * @return_type CustomFields
  184. */
  185. JsonRoutes.add('GET', '/api/boards/:boardId/custom-fields/:customFieldId', function (req, res) {
  186. Authentication.checkUserId( req.userId);
  187. const paramBoardId = req.params.boardId;
  188. const paramCustomFieldId = req.params.customFieldId;
  189. JsonRoutes.sendResult(res, {
  190. code: 200,
  191. data: CustomFields.findOne({ _id: paramCustomFieldId, boardIds: {$in: [paramBoardId]} }),
  192. });
  193. });
  194. /**
  195. * @operation new_custom_field
  196. * @summary Create a Custom Field
  197. *
  198. * @param {string} boardID the ID of the board
  199. * @param {string} name the name of the custom field
  200. * @param {string} type the type of the custom field
  201. * @param {string} settings the settings object of the custom field
  202. * @param {boolean} showOnCard should we show the custom field on cards?
  203. * @param {boolean} automaticallyOnCard should the custom fields automatically be added on cards?
  204. * @param {boolean} showLabelOnMiniCard should the label of the custom field be shown on minicards?
  205. * @return_type {_id: string}
  206. */
  207. JsonRoutes.add('POST', '/api/boards/:boardId/custom-fields', function (req, res) {
  208. Authentication.checkUserId( req.userId);
  209. const paramBoardId = req.params.boardId;
  210. const id = CustomFields.direct.insert({
  211. name: req.body.name,
  212. type: req.body.type,
  213. settings: req.body.settings,
  214. showOnCard: req.body.showOnCard,
  215. automaticallyOnCard: req.body.automaticallyOnCard,
  216. showLabelOnMiniCard: req.body.showLabelOnMiniCard,
  217. boardIds: {$in: [paramBoardId]},
  218. });
  219. const customField = CustomFields.findOne({_id: id, boardIds: {$in: [paramBoardId]} });
  220. customFieldCreation(req.body.authorId, customField);
  221. JsonRoutes.sendResult(res, {
  222. code: 200,
  223. data: {
  224. _id: id,
  225. },
  226. });
  227. });
  228. /**
  229. * @operation delete_custom_field
  230. * @summary Delete a Custom Fields attached to a board
  231. *
  232. * @description The Custom Field can't be retrieved after this operation
  233. *
  234. * @param {string} boardID the ID of the board
  235. * @param {string} customFieldId the ID of the custom field
  236. * @return_type {_id: string}
  237. */
  238. JsonRoutes.add('DELETE', '/api/boards/:boardId/custom-fields/:customFieldId', function (req, res) {
  239. Authentication.checkUserId( req.userId);
  240. const paramBoardId = req.params.boardId;
  241. const id = req.params.customFieldId;
  242. CustomFields.remove({ _id: id, boardIds: {$in: [paramBoardId]} });
  243. JsonRoutes.sendResult(res, {
  244. code: 200,
  245. data: {
  246. _id: id,
  247. },
  248. });
  249. });
  250. }