customFields.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. CustomFields = new Mongo.Collection('customFields');
  2. /**
  3. * A custom field on a card in the board
  4. */
  5. CustomFields.attachSchema(new SimpleSchema({
  6. boardId: {
  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.allow({
  74. insert(userId, doc) {
  75. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  76. },
  77. update(userId, doc) {
  78. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  79. },
  80. remove(userId, doc) {
  81. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  82. },
  83. fetch: ['userId', 'boardId'],
  84. });
  85. // not sure if we need this?
  86. //CustomFields.hookOptions.after.update = { fetchPrevious: false };
  87. function customFieldCreation(userId, doc){
  88. Activities.insert({
  89. userId,
  90. activityType: 'createCustomField',
  91. boardId: doc.boardId,
  92. customFieldId: doc._id,
  93. });
  94. }
  95. if (Meteor.isServer) {
  96. Meteor.startup(() => {
  97. CustomFields._collection._ensureIndex({ boardId: 1 });
  98. });
  99. CustomFields.after.insert((userId, doc) => {
  100. customFieldCreation(userId, doc);
  101. });
  102. CustomFields.after.remove((userId, doc) => {
  103. Activities.remove({
  104. customFieldId: doc._id,
  105. });
  106. Cards.update(
  107. {'boardId': doc.boardId, 'customFields._id': doc._id},
  108. {$pull: {'customFields': {'_id': doc._id}}},
  109. {multi: true}
  110. );
  111. });
  112. }
  113. //CUSTOM FIELD REST API
  114. if (Meteor.isServer) {
  115. /**
  116. * @operation get_all_custom_fields
  117. * @summary Get the list of Custom Fields attached to a board
  118. *
  119. * @param {string} boardID the ID of the board
  120. * @return_type [{_id: string,
  121. * name: string,
  122. * type: string}]
  123. */
  124. JsonRoutes.add('GET', '/api/boards/:boardId/custom-fields', function (req, res) {
  125. Authentication.checkUserId( req.userId);
  126. const paramBoardId = req.params.boardId;
  127. JsonRoutes.sendResult(res, {
  128. code: 200,
  129. data: CustomFields.find({ boardId: paramBoardId }).map(function (cf) {
  130. return {
  131. _id: cf._id,
  132. name: cf.name,
  133. type: cf.type,
  134. };
  135. }),
  136. });
  137. });
  138. /**
  139. * @operation get_custom_field
  140. * @summary Get a Custom Fields attached to a board
  141. *
  142. * @param {string} boardID the ID of the board
  143. * @param {string} customFieldId the ID of the custom field
  144. * @return_type CustomFields
  145. */
  146. JsonRoutes.add('GET', '/api/boards/:boardId/custom-fields/:customFieldId', function (req, res) {
  147. Authentication.checkUserId( req.userId);
  148. const paramBoardId = req.params.boardId;
  149. const paramCustomFieldId = req.params.customFieldId;
  150. JsonRoutes.sendResult(res, {
  151. code: 200,
  152. data: CustomFields.findOne({ _id: paramCustomFieldId, boardId: paramBoardId }),
  153. });
  154. });
  155. /**
  156. * @operation new_custom_field
  157. * @summary Create a Custom Field
  158. *
  159. * @param {string} boardID the ID of the board
  160. * @param {string} name the name of the custom field
  161. * @param {string} type the type of the custom field
  162. * @param {string} settings the settings object of the custom field
  163. * @param {boolean} showOnCard should we show the custom field on cards?
  164. * @param {boolean} automaticallyOnCard should the custom fields automatically be added on cards?
  165. * @param {boolean} showLabelOnMiniCard should the label of the custom field be shown on minicards?
  166. * @return_type {_id: string}
  167. */
  168. JsonRoutes.add('POST', '/api/boards/:boardId/custom-fields', function (req, res) {
  169. Authentication.checkUserId( req.userId);
  170. const paramBoardId = req.params.boardId;
  171. const id = CustomFields.direct.insert({
  172. name: req.body.name,
  173. type: req.body.type,
  174. settings: req.body.settings,
  175. showOnCard: req.body.showOnCard,
  176. automaticallyOnCard: req.body.automaticallyOnCard,
  177. showLabelOnMiniCard: req.body.showLabelOnMiniCard,
  178. boardId: paramBoardId,
  179. });
  180. const customField = CustomFields.findOne({_id: id, boardId: paramBoardId });
  181. customFieldCreation(req.body.authorId, customField);
  182. JsonRoutes.sendResult(res, {
  183. code: 200,
  184. data: {
  185. _id: id,
  186. },
  187. });
  188. });
  189. /**
  190. * @operation delete_custom_field
  191. * @summary Delete a Custom Fields attached to a board
  192. *
  193. * @description The Custom Field can't be retrieved after this operation
  194. *
  195. * @param {string} boardID the ID of the board
  196. * @param {string} customFieldId the ID of the custom field
  197. * @return_type {_id: string}
  198. */
  199. JsonRoutes.add('DELETE', '/api/boards/:boardId/custom-fields/:customFieldId', function (req, res) {
  200. Authentication.checkUserId( req.userId);
  201. const paramBoardId = req.params.boardId;
  202. const id = req.params.customFieldId;
  203. CustomFields.remove({ _id: id, boardId: paramBoardId });
  204. JsonRoutes.sendResult(res, {
  205. code: 200,
  206. data: {
  207. _id: id,
  208. },
  209. });
  210. });
  211. }