customFields.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. defaultValue: false,
  72. },
  73. automaticallyOnCard: {
  74. /**
  75. * should the custom fields automatically be added on cards?
  76. */
  77. type: Boolean,
  78. defaultValue: false,
  79. },
  80. alwaysOnCard: {
  81. /**
  82. * should the custom field be automatically added to all cards?
  83. */
  84. type: Boolean,
  85. defaultValue: false,
  86. },
  87. showLabelOnMiniCard: {
  88. /**
  89. * should the label of the custom field be shown on minicards?
  90. */
  91. type: Boolean,
  92. defaultValue: false,
  93. },
  94. createdAt: {
  95. type: Date,
  96. optional: true,
  97. // eslint-disable-next-line consistent-return
  98. autoValue() {
  99. if (this.isInsert) {
  100. return new Date();
  101. } else if (this.isUpsert) {
  102. return { $setOnInsert: new Date() };
  103. } else {
  104. this.unset();
  105. }
  106. },
  107. },
  108. modifiedAt: {
  109. type: Date,
  110. denyUpdate: false,
  111. // eslint-disable-next-line consistent-return
  112. autoValue() {
  113. if (this.isInsert || this.isUpsert || this.isUpdate) {
  114. return new Date();
  115. } else {
  116. this.unset();
  117. }
  118. },
  119. },
  120. }),
  121. );
  122. CustomFields.addToAllCards = cf => {
  123. Cards.update(
  124. {
  125. boardId: { $in: cf.boardIds },
  126. customFields: { $not: { $elemMatch: { _id: cf._id } } },
  127. },
  128. {
  129. $push: { customFields: { _id: cf._id, value: null } },
  130. },
  131. { multi: true },
  132. );
  133. };
  134. CustomFields.mutations({
  135. addBoard(boardId) {
  136. if (boardId) {
  137. return {
  138. $push: {
  139. boardIds: boardId,
  140. },
  141. };
  142. } else {
  143. return null;
  144. }
  145. },
  146. });
  147. CustomFields.allow({
  148. insert(userId, doc) {
  149. return allowIsAnyBoardMember(
  150. userId,
  151. Boards.find({
  152. _id: { $in: doc.boardIds },
  153. }).fetch(),
  154. );
  155. },
  156. update(userId, doc) {
  157. return allowIsAnyBoardMember(
  158. userId,
  159. Boards.find({
  160. _id: { $in: doc.boardIds },
  161. }).fetch(),
  162. );
  163. },
  164. remove(userId, doc) {
  165. return allowIsAnyBoardMember(
  166. userId,
  167. Boards.find({
  168. _id: { $in: doc.boardIds },
  169. }).fetch(),
  170. );
  171. },
  172. fetch: ['userId', 'boardIds'],
  173. });
  174. // not sure if we need this?
  175. //CustomFields.hookOptions.after.update = { fetchPrevious: false };
  176. function customFieldCreation(userId, doc) {
  177. Activities.insert({
  178. userId,
  179. activityType: 'createCustomField',
  180. boardId: doc.boardIds[0], // We are creating a customField, it has only one boardId
  181. customFieldId: doc._id,
  182. });
  183. }
  184. function customFieldDeletion(userId, doc) {
  185. Activities.insert({
  186. userId,
  187. activityType: 'deleteCustomField',
  188. boardId: doc.boardIds[0], // We are creating a customField, it has only one boardId
  189. customFieldId: doc._id,
  190. });
  191. }
  192. // This has some bug, it does not show edited customField value at Outgoing Webhook,
  193. // instead it shows undefined, and no listId and swimlaneId.
  194. function customFieldEdit(userId, doc) {
  195. const card = Cards.findOne(doc.cardId);
  196. const customFieldValue = Activities.findOne({ customFieldId: doc._id }).value;
  197. Activities.insert({
  198. userId,
  199. activityType: 'setCustomField',
  200. boardId: doc.boardIds[0], // We are creating a customField, it has only one boardId
  201. customFieldId: doc._id,
  202. customFieldValue,
  203. listId: doc.listId,
  204. swimlaneId: doc.swimlaneId,
  205. });
  206. }
  207. if (Meteor.isServer) {
  208. Meteor.startup(() => {
  209. CustomFields._collection._ensureIndex({ modifiedAt: -1 });
  210. CustomFields._collection._ensureIndex({ boardIds: 1 });
  211. });
  212. CustomFields.after.insert((userId, doc) => {
  213. customFieldCreation(userId, doc);
  214. if (doc.alwaysOnCard) {
  215. CustomFields.addToAllCards(doc);
  216. }
  217. });
  218. CustomFields.before.update((userId, doc, fieldNames, modifier) => {
  219. if (_.contains(fieldNames, 'boardIds') && modifier.$pull) {
  220. Cards.update(
  221. { boardId: modifier.$pull.boardIds, 'customFields._id': doc._id },
  222. { $pull: { customFields: { _id: doc._id } } },
  223. { multi: true },
  224. );
  225. customFieldEdit(userId, doc);
  226. Activities.remove({
  227. customFieldId: doc._id,
  228. boardId: modifier.$pull.boardIds,
  229. listId: doc.listId,
  230. swimlaneId: doc.swimlaneId,
  231. });
  232. } else if (_.contains(fieldNames, 'boardIds') && modifier.$push) {
  233. Activities.insert({
  234. userId,
  235. activityType: 'createCustomField',
  236. boardId: modifier.$push.boardIds,
  237. customFieldId: doc._id,
  238. });
  239. }
  240. });
  241. CustomFields.after.update((userId, doc) => {
  242. if (doc.alwaysOnCard) {
  243. CustomFields.addToAllCards(doc);
  244. }
  245. });
  246. CustomFields.before.remove((userId, doc) => {
  247. customFieldDeletion(userId, doc);
  248. Activities.remove({
  249. customFieldId: doc._id,
  250. });
  251. Cards.update(
  252. { boardId: { $in: doc.boardIds }, 'customFields._id': doc._id },
  253. { $pull: { customFields: { _id: doc._id } } },
  254. { multi: true },
  255. );
  256. });
  257. }
  258. //CUSTOM FIELD REST API
  259. if (Meteor.isServer) {
  260. /**
  261. * @operation get_all_custom_fields
  262. * @summary Get the list of Custom Fields attached to a board
  263. *
  264. * @param {string} boardID the ID of the board
  265. * @return_type [{_id: string,
  266. * name: string,
  267. * type: string}]
  268. */
  269. JsonRoutes.add('GET', '/api/boards/:boardId/custom-fields', function(
  270. req,
  271. res,
  272. ) {
  273. Authentication.checkUserId(req.userId);
  274. const paramBoardId = req.params.boardId;
  275. JsonRoutes.sendResult(res, {
  276. code: 200,
  277. data: CustomFields.find({ boardIds: { $in: [paramBoardId] } }).map(
  278. function(cf) {
  279. return {
  280. _id: cf._id,
  281. name: cf.name,
  282. type: cf.type,
  283. };
  284. },
  285. ),
  286. });
  287. });
  288. /**
  289. * @operation get_custom_field
  290. * @summary Get a Custom Fields attached to a board
  291. *
  292. * @param {string} boardID the ID of the board
  293. * @param {string} customFieldId the ID of the custom field
  294. * @return_type CustomFields
  295. */
  296. JsonRoutes.add(
  297. 'GET',
  298. '/api/boards/:boardId/custom-fields/:customFieldId',
  299. function(req, res) {
  300. Authentication.checkUserId(req.userId);
  301. const paramBoardId = req.params.boardId;
  302. const paramCustomFieldId = req.params.customFieldId;
  303. JsonRoutes.sendResult(res, {
  304. code: 200,
  305. data: CustomFields.findOne({
  306. _id: paramCustomFieldId,
  307. boardIds: { $in: [paramBoardId] },
  308. }),
  309. });
  310. },
  311. );
  312. /**
  313. * @operation new_custom_field
  314. * @summary Create a Custom Field
  315. *
  316. * @param {string} boardID the ID of the board
  317. * @param {string} name the name of the custom field
  318. * @param {string} type the type of the custom field
  319. * @param {string} settings the settings object of the custom field
  320. * @param {boolean} showOnCard should we show the custom field on cards?
  321. * @param {boolean} automaticallyOnCard should the custom fields automatically be added on cards?
  322. * @param {boolean} showLabelOnMiniCard should the label of the custom field be shown on minicards?
  323. * @return_type {_id: string}
  324. */
  325. JsonRoutes.add('POST', '/api/boards/:boardId/custom-fields', function(
  326. req,
  327. res,
  328. ) {
  329. Authentication.checkUserId(req.userId);
  330. const paramBoardId = req.params.boardId;
  331. const board = Boards.findOne({ _id: paramBoardId });
  332. const id = CustomFields.direct.insert({
  333. name: req.body.name,
  334. type: req.body.type,
  335. settings: req.body.settings,
  336. showOnCard: req.body.showOnCard,
  337. automaticallyOnCard: req.body.automaticallyOnCard,
  338. showLabelOnMiniCard: req.body.showLabelOnMiniCard,
  339. boardIds: [board._id],
  340. });
  341. const customField = CustomFields.findOne({
  342. _id: id,
  343. boardIds: { $in: [paramBoardId] },
  344. });
  345. customFieldCreation(req.body.authorId, customField);
  346. JsonRoutes.sendResult(res, {
  347. code: 200,
  348. data: {
  349. _id: id,
  350. },
  351. });
  352. });
  353. /**
  354. * @operation edit_custom_field
  355. * @summary Update a Custom Field
  356. *
  357. * @param {string} name the name of the custom field
  358. * @param {string} type the type of the custom field
  359. * @param {string} settings the settings object of the custom field
  360. * @param {boolean} showOnCard should we show the custom field on cards?
  361. * @param {boolean} automaticallyOnCard should the custom fields automatically be added on cards?
  362. * @param {boolean} showLabelOnMiniCard should the label of the custom field be shown on minicards?
  363. * @return_type {_id: string}
  364. */
  365. JsonRoutes.add(
  366. 'PUT',
  367. '/api/boards/:boardId/custom-fields/:customFieldId',
  368. (req, res) => {
  369. Authentication.checkUserId(req.userId);
  370. const paramFieldId = req.params.customFieldId;
  371. if (req.body.hasOwnProperty('name')) {
  372. CustomFields.direct.update(
  373. { _id: paramFieldId },
  374. { $set: { name: req.body.name } },
  375. );
  376. }
  377. if (req.body.hasOwnProperty('type')) {
  378. CustomFields.direct.update(
  379. { _id: paramFieldId },
  380. { $set: { type: req.body.type } },
  381. );
  382. }
  383. if (req.body.hasOwnProperty('settings')) {
  384. CustomFields.direct.update(
  385. { _id: paramFieldId },
  386. { $set: { settings: req.body.settings } },
  387. );
  388. }
  389. if (req.body.hasOwnProperty('showOnCard')) {
  390. CustomFields.direct.update(
  391. { _id: paramFieldId },
  392. { $set: { showOnCard: req.body.showOnCard } },
  393. );
  394. }
  395. if (req.body.hasOwnProperty('automaticallyOnCard')) {
  396. CustomFields.direct.update(
  397. { _id: paramFieldId },
  398. { $set: { automaticallyOnCard: req.body.automaticallyOnCard } },
  399. );
  400. }
  401. if (req.body.hasOwnProperty('alwaysOnCard')) {
  402. CustomFields.direct.update(
  403. { _id: paramFieldId },
  404. { $set: { alwaysOnCard: req.body.alwaysOnCard } },
  405. );
  406. }
  407. if (req.body.hasOwnProperty('showLabelOnMiniCard')) {
  408. CustomFields.direct.update(
  409. { _id: paramFieldId },
  410. { $set: { showLabelOnMiniCard: req.body.showLabelOnMiniCard } },
  411. );
  412. }
  413. JsonRoutes.sendResult(res, {
  414. code: 200,
  415. data: { _id: paramFieldId },
  416. });
  417. },
  418. );
  419. /**
  420. * @operation add_custom_field_dropdown_items
  421. * @summary Update a Custom Field's dropdown items
  422. *
  423. * @param {string[]} items names of the custom field
  424. * @return_type {_id: string}
  425. */
  426. JsonRoutes.add(
  427. 'POST',
  428. '/api/boards/:boardId/custom-fields/:customFieldId/dropdown-items',
  429. (req, res) => {
  430. Authentication.checkUserId(req.userId);
  431. if (req.body.hasOwnProperty('items') && Array.isArray(req.body.items)) {
  432. CustomFields.direct.update(
  433. { _id: req.params.customFieldId },
  434. {
  435. $push: {
  436. 'settings.dropdownItems': {
  437. $each: req.body.items
  438. .filter(name => typeof name === 'string')
  439. .map(name => ({
  440. _id: Random.id(6),
  441. name,
  442. })),
  443. },
  444. },
  445. },
  446. );
  447. }
  448. JsonRoutes.sendResult(res, {
  449. code: 200,
  450. data: { _id: req.params.customFieldId },
  451. });
  452. },
  453. );
  454. /**
  455. * @operation edit_custom_field_dropdown_item
  456. * @summary Update a Custom Field's dropdown item
  457. *
  458. * @param {string} name names of the custom field
  459. * @return_type {_id: string}
  460. */
  461. JsonRoutes.add(
  462. 'PUT',
  463. '/api/boards/:boardId/custom-fields/:customFieldId/dropdown-items/:dropdownItemId',
  464. (req, res) => {
  465. Authentication.checkUserId(req.userId);
  466. if (req.body.hasOwnProperty('name')) {
  467. CustomFields.direct.update(
  468. {
  469. _id: req.params.customFieldId,
  470. 'settings.dropdownItems._id': req.params.dropdownItemId,
  471. },
  472. {
  473. $set: {
  474. 'settings.dropdownItems.$': {
  475. _id: req.params.dropdownItemId,
  476. name: req.body.name,
  477. },
  478. },
  479. },
  480. );
  481. }
  482. JsonRoutes.sendResult(res, {
  483. code: 200,
  484. data: { _id: req.params.customFieldId },
  485. });
  486. },
  487. );
  488. /**
  489. * @operation delete_custom_field_dropdown_item
  490. * @summary Update a Custom Field's dropdown items
  491. *
  492. * @param {string} itemId ID of the dropdown item
  493. * @return_type {_id: string}
  494. */
  495. JsonRoutes.add(
  496. 'DELETE',
  497. '/api/boards/:boardId/custom-fields/:customFieldId/dropdown-items/:dropdownItemId',
  498. (req, res) => {
  499. Authentication.checkUserId(req.userId);
  500. CustomFields.direct.update(
  501. { _id: req.params.customFieldId },
  502. {
  503. $pull: {
  504. 'settings.dropdownItems': { _id: req.params.dropdownItemId },
  505. },
  506. },
  507. );
  508. JsonRoutes.sendResult(res, {
  509. code: 200,
  510. data: { _id: req.params.customFieldId },
  511. });
  512. },
  513. );
  514. /**
  515. * @operation delete_custom_field
  516. * @summary Delete a Custom Fields attached to a board
  517. *
  518. * @description The Custom Field can't be retrieved after this operation
  519. *
  520. * @param {string} boardID the ID of the board
  521. * @param {string} customFieldId the ID of the custom field
  522. * @return_type {_id: string}
  523. */
  524. JsonRoutes.add(
  525. 'DELETE',
  526. '/api/boards/:boardId/custom-fields/:customFieldId',
  527. function(req, res) {
  528. Authentication.checkUserId(req.userId);
  529. const paramBoardId = req.params.boardId;
  530. const id = req.params.customFieldId;
  531. CustomFields.remove({ _id: id, boardIds: { $in: [paramBoardId] } });
  532. JsonRoutes.sendResult(res, {
  533. code: 200,
  534. data: {
  535. _id: id,
  536. },
  537. });
  538. },
  539. );
  540. }
  541. export default CustomFields;