customFields.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. function customFieldDeletion(userId, doc){
  115. Activities.insert({
  116. userId,
  117. activityType: 'deleteCustomField',
  118. boardId: doc.boardIds[0], // We are creating a customField, it has only one boardId
  119. customFieldId: doc._id,
  120. });
  121. }
  122. // This has some bug, it does not show edited customField value at Outgoing Webhook,
  123. // instead it shows undefined, and no listId and swimlaneId.
  124. function customFieldEdit(userId, doc){
  125. const card = Cards.findOne(doc.cardId);
  126. Activities.insert({
  127. userId,
  128. activityType: 'editCustomField',
  129. boardId: doc.boardIds[0], // We are creating a customField, it has only one boardId
  130. customFieldId: doc._id,
  131. listId: card.listId,
  132. swimlaneId: card.swimlaneId,
  133. });
  134. }
  135. if (Meteor.isServer) {
  136. Meteor.startup(() => {
  137. CustomFields._collection._ensureIndex({ boardIds: 1 });
  138. });
  139. CustomFields.after.insert((userId, doc) => {
  140. customFieldCreation(userId, doc);
  141. });
  142. CustomFields.before.update((userId, doc, fieldNames, modifier) => {
  143. if (_.contains(fieldNames, 'boardIds') && modifier.$pull) {
  144. Cards.update(
  145. {boardId: modifier.$pull.boardIds, 'customFields._id': doc._id},
  146. {$pull: {'customFields': {'_id': doc._id}}},
  147. {multi: true}
  148. );
  149. customFieldEdit(userId, doc);
  150. Activities.remove({
  151. customFieldId: doc._id,
  152. boardId: modifier.$pull.boardIds,
  153. listId: card.listId,
  154. swimlaneId: card.swimlaneId,
  155. });
  156. } else if (_.contains(fieldNames, 'boardIds') && modifier.$push) {
  157. Activities.insert({
  158. userId,
  159. activityType: 'createCustomField',
  160. boardId: modifier.$push.boardIds,
  161. customFieldId: doc._id,
  162. });
  163. }
  164. });
  165. CustomFields.before.remove((userId, doc) => {
  166. customFieldDeletion(userId, doc);
  167. Activities.remove({
  168. customFieldId: doc._id,
  169. });
  170. Cards.update(
  171. {boardId: {$in: doc.boardIds}, 'customFields._id': doc._id},
  172. {$pull: {'customFields': {'_id': doc._id}}},
  173. {multi: true}
  174. );
  175. });
  176. }
  177. //CUSTOM FIELD REST API
  178. if (Meteor.isServer) {
  179. /**
  180. * @operation get_all_custom_fields
  181. * @summary Get the list of Custom Fields attached to a board
  182. *
  183. * @param {string} boardID the ID of the board
  184. * @return_type [{_id: string,
  185. * name: string,
  186. * type: string}]
  187. */
  188. JsonRoutes.add('GET', '/api/boards/:boardId/custom-fields', function (req, res) {
  189. Authentication.checkUserId( req.userId);
  190. const paramBoardId = req.params.boardId;
  191. JsonRoutes.sendResult(res, {
  192. code: 200,
  193. data: CustomFields.find({ boardIds: {$in: [paramBoardId]} }).map(function (cf) {
  194. return {
  195. _id: cf._id,
  196. name: cf.name,
  197. type: cf.type,
  198. };
  199. }),
  200. });
  201. });
  202. /**
  203. * @operation get_custom_field
  204. * @summary Get a Custom Fields attached to a board
  205. *
  206. * @param {string} boardID the ID of the board
  207. * @param {string} customFieldId the ID of the custom field
  208. * @return_type CustomFields
  209. */
  210. JsonRoutes.add('GET', '/api/boards/:boardId/custom-fields/:customFieldId', function (req, res) {
  211. Authentication.checkUserId( req.userId);
  212. const paramBoardId = req.params.boardId;
  213. const paramCustomFieldId = req.params.customFieldId;
  214. JsonRoutes.sendResult(res, {
  215. code: 200,
  216. data: CustomFields.findOne({ _id: paramCustomFieldId, boardIds: {$in: [paramBoardId]} }),
  217. });
  218. });
  219. /**
  220. * @operation new_custom_field
  221. * @summary Create a Custom Field
  222. *
  223. * @param {string} boardID the ID of the board
  224. * @param {string} name the name of the custom field
  225. * @param {string} type the type of the custom field
  226. * @param {string} settings the settings object of the custom field
  227. * @param {boolean} showOnCard should we show the custom field on cards?
  228. * @param {boolean} automaticallyOnCard should the custom fields automatically be added on cards?
  229. * @param {boolean} showLabelOnMiniCard should the label of the custom field be shown on minicards?
  230. * @return_type {_id: string}
  231. */
  232. JsonRoutes.add('POST', '/api/boards/:boardId/custom-fields', function (req, res) {
  233. Authentication.checkUserId( req.userId);
  234. const paramBoardId = req.params.boardId;
  235. const id = CustomFields.direct.insert({
  236. name: req.body.name,
  237. type: req.body.type,
  238. settings: req.body.settings,
  239. showOnCard: req.body.showOnCard,
  240. automaticallyOnCard: req.body.automaticallyOnCard,
  241. showLabelOnMiniCard: req.body.showLabelOnMiniCard,
  242. boardIds: {$in: [paramBoardId]},
  243. });
  244. const customField = CustomFields.findOne({_id: id, boardIds: {$in: [paramBoardId]} });
  245. customFieldCreation(req.body.authorId, customField);
  246. JsonRoutes.sendResult(res, {
  247. code: 200,
  248. data: {
  249. _id: id,
  250. },
  251. });
  252. });
  253. /**
  254. * @operation delete_custom_field
  255. * @summary Delete a Custom Fields attached to a board
  256. *
  257. * @description The Custom Field can't be retrieved after this operation
  258. *
  259. * @param {string} boardID the ID of the board
  260. * @param {string} customFieldId the ID of the custom field
  261. * @return_type {_id: string}
  262. */
  263. JsonRoutes.add('DELETE', '/api/boards/:boardId/custom-fields/:customFieldId', function (req, res) {
  264. Authentication.checkUserId( req.userId);
  265. const paramBoardId = req.params.boardId;
  266. const id = req.params.customFieldId;
  267. CustomFields.remove({ _id: id, boardIds: {$in: [paramBoardId]} });
  268. JsonRoutes.sendResult(res, {
  269. code: 200,
  270. data: {
  271. _id: id,
  272. },
  273. });
  274. });
  275. }