customFields.js 9.7 KB

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