customFields.js 15 KB

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