2
0

customFields.js 16 KB

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