customFields.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. CustomFields = new Mongo.Collection('customFields');
  2. /**
  3. * A custom field on a card in the board
  4. */
  5. CustomFields.attachSchema(
  6. new SimpleSchema({
  7. boardIds: {
  8. /**
  9. * the ID of the board
  10. */
  11. type: [String],
  12. },
  13. name: {
  14. /**
  15. * name of the custom field
  16. */
  17. type: String,
  18. },
  19. type: {
  20. /**
  21. * type of the custom field
  22. */
  23. type: String,
  24. allowedValues: ['text', 'number', 'date', 'dropdown'],
  25. },
  26. settings: {
  27. /**
  28. * settings of the custom field
  29. */
  30. type: Object,
  31. },
  32. 'settings.dropdownItems': {
  33. /**
  34. * list of drop down items objects
  35. */
  36. type: [Object],
  37. optional: true,
  38. },
  39. 'settings.dropdownItems.$': {
  40. type: new SimpleSchema({
  41. _id: {
  42. /**
  43. * ID of the drop down item
  44. */
  45. type: String,
  46. },
  47. name: {
  48. /**
  49. * name of the drop down item
  50. */
  51. type: String,
  52. },
  53. }),
  54. },
  55. showOnCard: {
  56. /**
  57. * should we show on the cards this custom field
  58. */
  59. type: Boolean,
  60. },
  61. automaticallyOnCard: {
  62. /**
  63. * should the custom fields automatically be added on cards?
  64. */
  65. type: Boolean,
  66. },
  67. showLabelOnMiniCard: {
  68. /**
  69. * should the label of the custom field be shown on minicards?
  70. */
  71. type: Boolean,
  72. },
  73. createdAt: {
  74. type: Date,
  75. optional: true,
  76. // eslint-disable-next-line consistent-return
  77. autoValue() {
  78. if (this.isInsert) {
  79. return new Date();
  80. } else {
  81. this.unset();
  82. }
  83. },
  84. },
  85. modifiedAt: {
  86. type: Date,
  87. denyUpdate: false,
  88. // eslint-disable-next-line consistent-return
  89. autoValue() {
  90. if (this.isInsert || this.isUpsert || this.isUpdate) {
  91. return new Date();
  92. } else {
  93. this.unset();
  94. }
  95. },
  96. },
  97. })
  98. );
  99. CustomFields.mutations({
  100. addBoard(boardId) {
  101. if (boardId) {
  102. return {
  103. $push: {
  104. boardIds: boardId,
  105. },
  106. };
  107. } else {
  108. return null;
  109. }
  110. },
  111. });
  112. CustomFields.allow({
  113. insert(userId, doc) {
  114. return allowIsAnyBoardMember(
  115. userId,
  116. Boards.find({
  117. _id: { $in: doc.boardIds },
  118. }).fetch()
  119. );
  120. },
  121. update(userId, doc) {
  122. return allowIsAnyBoardMember(
  123. userId,
  124. Boards.find({
  125. _id: { $in: doc.boardIds },
  126. }).fetch()
  127. );
  128. },
  129. remove(userId, doc) {
  130. return allowIsAnyBoardMember(
  131. userId,
  132. Boards.find({
  133. _id: { $in: doc.boardIds },
  134. }).fetch()
  135. );
  136. },
  137. fetch: ['userId', 'boardIds'],
  138. });
  139. // not sure if we need this?
  140. //CustomFields.hookOptions.after.update = { fetchPrevious: false };
  141. function customFieldCreation(userId, doc) {
  142. Activities.insert({
  143. userId,
  144. activityType: 'createCustomField',
  145. boardId: doc.boardIds[0], // We are creating a customField, it has only one boardId
  146. customFieldId: doc._id,
  147. });
  148. }
  149. function customFieldDeletion(userId, doc){
  150. Activities.insert({
  151. userId,
  152. activityType: 'deleteCustomField',
  153. boardId: doc.boardIds[0], // We are creating a customField, it has only one boardId
  154. customFieldId: doc._id,
  155. });
  156. }
  157. // This has some bug, it does not show edited customField value at Outgoing Webhook,
  158. // instead it shows undefined, and no listId and swimlaneId.
  159. function customFieldEdit(userId, doc){
  160. const card = Cards.findOne(doc.cardId);
  161. Activities.insert({
  162. userId,
  163. activityType: 'editCustomField',
  164. boardId: doc.boardIds[0], // We are creating a customField, it has only one boardId
  165. customFieldId: doc._id,
  166. listId: card.listId,
  167. swimlaneId: card.swimlaneId,
  168. });
  169. }
  170. if (Meteor.isServer) {
  171. Meteor.startup(() => {
  172. CustomFields._collection._ensureIndex({ modifiedAt: -1 });
  173. CustomFields._collection._ensureIndex({ boardIds: 1 });
  174. });
  175. CustomFields.after.insert((userId, doc) => {
  176. customFieldCreation(userId, doc);
  177. });
  178. CustomFields.before.update((userId, doc, fieldNames, modifier, options) => {
  179. modifier.$set = modifier.$set || {};
  180. modifier.$set.modifiedAt = Date.now();
  181. });
  182. CustomFields.before.update((userId, doc, fieldNames, modifier) => {
  183. if (_.contains(fieldNames, 'boardIds') && modifier.$pull) {
  184. Cards.update(
  185. { boardId: modifier.$pull.boardIds, 'customFields._id': doc._id },
  186. { $pull: { customFields: { _id: doc._id } } },
  187. { multi: true }
  188. );
  189. customFieldEdit(userId, doc);
  190. Activities.remove({
  191. customFieldId: doc._id,
  192. boardId: modifier.$pull.boardIds,
  193. listId: card.listId,
  194. swimlaneId: card.swimlaneId,
  195. });
  196. } else if (_.contains(fieldNames, 'boardIds') && modifier.$push) {
  197. Activities.insert({
  198. userId,
  199. activityType: 'createCustomField',
  200. boardId: modifier.$push.boardIds,
  201. customFieldId: doc._id,
  202. });
  203. }
  204. });
  205. CustomFields.before.remove((userId, doc) => {
  206. customFieldDeletion(userId, doc);
  207. Activities.remove({
  208. customFieldId: doc._id,
  209. });
  210. Cards.update(
  211. { boardId: { $in: doc.boardIds }, 'customFields._id': doc._id },
  212. { $pull: { customFields: { _id: doc._id } } },
  213. { multi: true }
  214. );
  215. });
  216. }
  217. //CUSTOM FIELD REST API
  218. if (Meteor.isServer) {
  219. /**
  220. * @operation get_all_custom_fields
  221. * @summary Get the list of Custom Fields attached to a board
  222. *
  223. * @param {string} boardID the ID of the board
  224. * @return_type [{_id: string,
  225. * name: string,
  226. * type: string}]
  227. */
  228. JsonRoutes.add('GET', '/api/boards/:boardId/custom-fields', function(
  229. req,
  230. res
  231. ) {
  232. Authentication.checkUserId(req.userId);
  233. const paramBoardId = req.params.boardId;
  234. JsonRoutes.sendResult(res, {
  235. code: 200,
  236. data: CustomFields.find({ boardIds: { $in: [paramBoardId] } }).map(
  237. function(cf) {
  238. return {
  239. _id: cf._id,
  240. name: cf.name,
  241. type: cf.type,
  242. };
  243. }
  244. ),
  245. });
  246. });
  247. /**
  248. * @operation get_custom_field
  249. * @summary Get a Custom Fields attached to a board
  250. *
  251. * @param {string} boardID the ID of the board
  252. * @param {string} customFieldId the ID of the custom field
  253. * @return_type CustomFields
  254. */
  255. JsonRoutes.add(
  256. 'GET',
  257. '/api/boards/:boardId/custom-fields/:customFieldId',
  258. function(req, res) {
  259. Authentication.checkUserId(req.userId);
  260. const paramBoardId = req.params.boardId;
  261. const paramCustomFieldId = req.params.customFieldId;
  262. JsonRoutes.sendResult(res, {
  263. code: 200,
  264. data: CustomFields.findOne({
  265. _id: paramCustomFieldId,
  266. boardIds: { $in: [paramBoardId] },
  267. }),
  268. });
  269. }
  270. );
  271. /**
  272. * @operation new_custom_field
  273. * @summary Create a Custom Field
  274. *
  275. * @param {string} boardID the ID of the board
  276. * @param {string} name the name of the custom field
  277. * @param {string} type the type of the custom field
  278. * @param {string} settings the settings object of the custom field
  279. * @param {boolean} showOnCard should we show the custom field on cards?
  280. * @param {boolean} automaticallyOnCard should the custom fields automatically be added on cards?
  281. * @param {boolean} showLabelOnMiniCard should the label of the custom field be shown on minicards?
  282. * @return_type {_id: string}
  283. */
  284. JsonRoutes.add('POST', '/api/boards/:boardId/custom-fields', function(
  285. req,
  286. res
  287. ) {
  288. Authentication.checkUserId(req.userId);
  289. const paramBoardId = req.params.boardId;
  290. const id = CustomFields.direct.insert({
  291. name: req.body.name,
  292. type: req.body.type,
  293. settings: req.body.settings,
  294. showOnCard: req.body.showOnCard,
  295. automaticallyOnCard: req.body.automaticallyOnCard,
  296. showLabelOnMiniCard: req.body.showLabelOnMiniCard,
  297. boardIds: { $in: [paramBoardId] },
  298. });
  299. const customField = CustomFields.findOne({
  300. _id: id,
  301. boardIds: { $in: [paramBoardId] },
  302. });
  303. customFieldCreation(req.body.authorId, customField);
  304. JsonRoutes.sendResult(res, {
  305. code: 200,
  306. data: {
  307. _id: id,
  308. },
  309. });
  310. });
  311. /**
  312. * @operation delete_custom_field
  313. * @summary Delete a Custom Fields attached to a board
  314. *
  315. * @description The Custom Field can't be retrieved after this operation
  316. *
  317. * @param {string} boardID the ID of the board
  318. * @param {string} customFieldId the ID of the custom field
  319. * @return_type {_id: string}
  320. */
  321. JsonRoutes.add(
  322. 'DELETE',
  323. '/api/boards/:boardId/custom-fields/:customFieldId',
  324. function(req, res) {
  325. Authentication.checkUserId(req.userId);
  326. const paramBoardId = req.params.boardId;
  327. const id = req.params.customFieldId;
  328. CustomFields.remove({ _id: id, boardIds: { $in: [paramBoardId] } });
  329. JsonRoutes.sendResult(res, {
  330. code: 200,
  331. data: {
  332. _id: id,
  333. },
  334. });
  335. }
  336. );
  337. }
  338. export default CustomFields;