customFields.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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 [{_id: string,
  295. * boardIds: string}]
  296. */
  297. JsonRoutes.add(
  298. 'GET',
  299. '/api/boards/:boardId/custom-fields/:customFieldId',
  300. function(req, res) {
  301. Authentication.checkUserId(req.userId);
  302. const paramBoardId = req.params.boardId;
  303. const paramCustomFieldId = req.params.customFieldId;
  304. JsonRoutes.sendResult(res, {
  305. code: 200,
  306. data: CustomFields.findOne({
  307. _id: paramCustomFieldId,
  308. boardIds: { $in: [paramBoardId] },
  309. }),
  310. });
  311. },
  312. );
  313. /**
  314. * @operation new_custom_field
  315. * @summary Create a Custom Field
  316. *
  317. * @param {string} boardID the ID of the board
  318. * @param {string} name the name of the custom field
  319. * @param {string} type the type of the custom field
  320. * @param {string} settings the settings object of the custom field
  321. * @param {boolean} showOnCard should we show the custom field on cards?
  322. * @param {boolean} automaticallyOnCard should the custom fields automatically be added on cards?
  323. * @param {boolean} showLabelOnMiniCard should the label of the custom field be shown on minicards?
  324. * @return_type {_id: string}
  325. */
  326. JsonRoutes.add('POST', '/api/boards/:boardId/custom-fields', function(
  327. req,
  328. res,
  329. ) {
  330. Authentication.checkUserId(req.userId);
  331. const paramBoardId = req.params.boardId;
  332. const board = Boards.findOne({ _id: paramBoardId });
  333. const id = CustomFields.direct.insert({
  334. name: req.body.name,
  335. type: req.body.type,
  336. settings: req.body.settings,
  337. showOnCard: req.body.showOnCard,
  338. automaticallyOnCard: req.body.automaticallyOnCard,
  339. showLabelOnMiniCard: req.body.showLabelOnMiniCard,
  340. boardIds: [board._id],
  341. });
  342. const customField = CustomFields.findOne({
  343. _id: id,
  344. boardIds: { $in: [paramBoardId] },
  345. });
  346. customFieldCreation(req.body.authorId, customField);
  347. JsonRoutes.sendResult(res, {
  348. code: 200,
  349. data: {
  350. _id: id,
  351. },
  352. });
  353. });
  354. /**
  355. * @operation edit_custom_field
  356. * @summary Update a Custom Field
  357. *
  358. * @param {string} name the name of the custom field
  359. * @param {string} type the type of the custom field
  360. * @param {string} settings the settings object of the custom field
  361. * @param {boolean} showOnCard should we show the custom field on cards
  362. * @param {boolean} automaticallyOnCard should the custom fields automatically be added on cards
  363. * @param {boolean} showLabelOnMiniCard should the label of the custom field be shown on minicards
  364. * @return_type {_id: string}
  365. */
  366. JsonRoutes.add(
  367. 'PUT',
  368. '/api/boards/:boardId/custom-fields/:customFieldId',
  369. (req, res) => {
  370. Authentication.checkUserId(req.userId);
  371. const paramFieldId = req.params.customFieldId;
  372. if (req.body.hasOwnProperty('name')) {
  373. CustomFields.direct.update(
  374. { _id: paramFieldId },
  375. { $set: { name: req.body.name } },
  376. );
  377. }
  378. if (req.body.hasOwnProperty('type')) {
  379. CustomFields.direct.update(
  380. { _id: paramFieldId },
  381. { $set: { type: req.body.type } },
  382. );
  383. }
  384. if (req.body.hasOwnProperty('settings')) {
  385. CustomFields.direct.update(
  386. { _id: paramFieldId },
  387. { $set: { settings: req.body.settings } },
  388. );
  389. }
  390. if (req.body.hasOwnProperty('showOnCard')) {
  391. CustomFields.direct.update(
  392. { _id: paramFieldId },
  393. { $set: { showOnCard: req.body.showOnCard } },
  394. );
  395. }
  396. if (req.body.hasOwnProperty('automaticallyOnCard')) {
  397. CustomFields.direct.update(
  398. { _id: paramFieldId },
  399. { $set: { automaticallyOnCard: req.body.automaticallyOnCard } },
  400. );
  401. }
  402. if (req.body.hasOwnProperty('alwaysOnCard')) {
  403. CustomFields.direct.update(
  404. { _id: paramFieldId },
  405. { $set: { alwaysOnCard: req.body.alwaysOnCard } },
  406. );
  407. }
  408. if (req.body.hasOwnProperty('showLabelOnMiniCard')) {
  409. CustomFields.direct.update(
  410. { _id: paramFieldId },
  411. { $set: { showLabelOnMiniCard: req.body.showLabelOnMiniCard } },
  412. );
  413. }
  414. JsonRoutes.sendResult(res, {
  415. code: 200,
  416. data: { _id: paramFieldId },
  417. });
  418. },
  419. );
  420. /**
  421. * @operation add_custom_field_dropdown_items
  422. * @summary Update a Custom Field's dropdown items
  423. *
  424. * @param {string} [items] names of the custom field
  425. * @return_type {_id: string}
  426. */
  427. JsonRoutes.add(
  428. 'POST',
  429. '/api/boards/:boardId/custom-fields/:customFieldId/dropdown-items',
  430. (req, res) => {
  431. Authentication.checkUserId(req.userId);
  432. const paramCustomFieldId = req.params.customFieldId;
  433. const paramItems = req.body.items;
  434. if (req.body.hasOwnProperty('items')) {
  435. if (Array.isArray(paramItems)) {
  436. CustomFields.direct.update(
  437. { _id: paramCustomFieldId },
  438. {
  439. $push: {
  440. 'settings.dropdownItems': {
  441. $each: paramItems
  442. .filter(name => typeof name === 'string')
  443. .map(name => ({
  444. _id: Random.id(6),
  445. name,
  446. })),
  447. },
  448. },
  449. },
  450. );
  451. }
  452. }
  453. JsonRoutes.sendResult(res, {
  454. code: 200,
  455. data: { _id: paramCustomFieldId },
  456. });
  457. },
  458. );
  459. /**
  460. * @operation edit_custom_field_dropdown_item
  461. * @summary Update a Custom Field's dropdown item
  462. *
  463. * @param {string} name names of the custom field
  464. * @return_type {_id: string}
  465. */
  466. JsonRoutes.add(
  467. 'PUT',
  468. '/api/boards/:boardId/custom-fields/:customFieldId/dropdown-items/:dropdownItemId',
  469. (req, res) => {
  470. Authentication.checkUserId(req.userId);
  471. const paramDropdownItemId = req.params.dropdownItemId;
  472. const paramCustomFieldId = req.params.customFieldId;
  473. const paramName = req.body.name;
  474. if (req.body.hasOwnProperty('name')) {
  475. CustomFields.direct.update(
  476. {
  477. _id: paramCustomFieldId,
  478. 'settings.dropdownItems._id': paramDropdownItemId,
  479. },
  480. {
  481. $set: {
  482. 'settings.dropdownItems.$': {
  483. _id: paramDropdownItemId,
  484. name: paramName,
  485. },
  486. },
  487. },
  488. );
  489. }
  490. JsonRoutes.sendResult(res, {
  491. code: 200,
  492. data: { _id: customFieldId },
  493. });
  494. },
  495. );
  496. /**
  497. * @operation delete_custom_field_dropdown_item
  498. * @summary Update a Custom Field's dropdown items
  499. *
  500. * @param {string} itemId ID of the dropdown item
  501. * @return_type {_id: string}
  502. */
  503. JsonRoutes.add(
  504. 'DELETE',
  505. '/api/boards/:boardId/custom-fields/:customFieldId/dropdown-items/:dropdownItemId',
  506. (req, res) => {
  507. Authentication.checkUserId(req.userId);
  508. paramCustomFieldId = req.params.customFieldId;
  509. paramDropdownItemId = req.params.dropdownItemId;
  510. CustomFields.direct.update(
  511. { _id: paramCustomFieldId },
  512. {
  513. $pull: {
  514. 'settings.dropdownItems': { _id: paramDropdownItemId },
  515. },
  516. },
  517. );
  518. JsonRoutes.sendResult(res, {
  519. code: 200,
  520. data: { _id: paramCustomFieldId },
  521. });
  522. },
  523. );
  524. /**
  525. * @operation delete_custom_field
  526. * @summary Delete a Custom Fields attached to a board
  527. *
  528. * @description The Custom Field can't be retrieved after this operation
  529. *
  530. * @param {string} boardID the ID of the board
  531. * @param {string} customFieldId the ID of the custom field
  532. * @return_type {_id: string}
  533. */
  534. JsonRoutes.add(
  535. 'DELETE',
  536. '/api/boards/:boardId/custom-fields/:customFieldId',
  537. function(req, res) {
  538. Authentication.checkUserId(req.userId);
  539. const paramBoardId = req.params.boardId;
  540. const id = req.params.customFieldId;
  541. CustomFields.remove({ _id: id, boardIds: { $in: [paramBoardId] } });
  542. JsonRoutes.sendResult(res, {
  543. code: 200,
  544. data: {
  545. _id: id,
  546. },
  547. });
  548. },
  549. );
  550. }
  551. export default CustomFields;