customFields.js 16 KB

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