customFields.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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. Authentication.checkUserId(req.userId);
  283. const paramBoardId = req.params.boardId;
  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. Authentication.checkUserId(req.userId);
  311. const paramBoardId = req.params.boardId;
  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. Authentication.checkUserId(req.userId);
  340. const paramBoardId = req.params.boardId;
  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. Authentication.checkUserId(req.userId);
  380. const paramFieldId = req.params.customFieldId;
  381. if (req.body.hasOwnProperty('name')) {
  382. CustomFields.direct.update(
  383. { _id: paramFieldId },
  384. { $set: { name: req.body.name } },
  385. );
  386. }
  387. if (req.body.hasOwnProperty('type')) {
  388. CustomFields.direct.update(
  389. { _id: paramFieldId },
  390. { $set: { type: req.body.type } },
  391. );
  392. }
  393. if (req.body.hasOwnProperty('settings')) {
  394. CustomFields.direct.update(
  395. { _id: paramFieldId },
  396. { $set: { settings: req.body.settings } },
  397. );
  398. }
  399. if (req.body.hasOwnProperty('showOnCard')) {
  400. CustomFields.direct.update(
  401. { _id: paramFieldId },
  402. { $set: { showOnCard: req.body.showOnCard } },
  403. );
  404. }
  405. if (req.body.hasOwnProperty('automaticallyOnCard')) {
  406. CustomFields.direct.update(
  407. { _id: paramFieldId },
  408. { $set: { automaticallyOnCard: req.body.automaticallyOnCard } },
  409. );
  410. }
  411. if (req.body.hasOwnProperty('alwaysOnCard')) {
  412. CustomFields.direct.update(
  413. { _id: paramFieldId },
  414. { $set: { alwaysOnCard: req.body.alwaysOnCard } },
  415. );
  416. }
  417. if (req.body.hasOwnProperty('showLabelOnMiniCard')) {
  418. CustomFields.direct.update(
  419. { _id: paramFieldId },
  420. { $set: { showLabelOnMiniCard: req.body.showLabelOnMiniCard } },
  421. );
  422. }
  423. JsonRoutes.sendResult(res, {
  424. code: 200,
  425. data: { _id: paramFieldId },
  426. });
  427. },
  428. );
  429. /**
  430. * @operation add_custom_field_dropdown_items
  431. * @summary Update a Custom Field's dropdown items
  432. *
  433. * @param {string} [items] names of the custom field
  434. * @return_type {_id: string}
  435. */
  436. JsonRoutes.add(
  437. 'POST',
  438. '/api/boards/:boardId/custom-fields/:customFieldId/dropdown-items',
  439. (req, res) => {
  440. Authentication.checkUserId(req.userId);
  441. const paramCustomFieldId = req.params.customFieldId;
  442. const paramItems = req.body.items;
  443. if (req.body.hasOwnProperty('items')) {
  444. if (Array.isArray(paramItems)) {
  445. CustomFields.direct.update(
  446. { _id: paramCustomFieldId },
  447. {
  448. $push: {
  449. 'settings.dropdownItems': {
  450. $each: paramItems
  451. .filter(name => typeof name === 'string')
  452. .map(name => ({
  453. _id: Random.id(6),
  454. name,
  455. })),
  456. },
  457. },
  458. },
  459. );
  460. }
  461. }
  462. JsonRoutes.sendResult(res, {
  463. code: 200,
  464. data: { _id: paramCustomFieldId },
  465. });
  466. },
  467. );
  468. /**
  469. * @operation edit_custom_field_dropdown_item
  470. * @summary Update a Custom Field's dropdown item
  471. *
  472. * @param {string} name names of the custom field
  473. * @return_type {_id: string}
  474. */
  475. JsonRoutes.add(
  476. 'PUT',
  477. '/api/boards/:boardId/custom-fields/:customFieldId/dropdown-items/:dropdownItemId',
  478. (req, res) => {
  479. Authentication.checkUserId(req.userId);
  480. const paramDropdownItemId = req.params.dropdownItemId;
  481. const paramCustomFieldId = req.params.customFieldId;
  482. const paramName = req.body.name;
  483. if (req.body.hasOwnProperty('name')) {
  484. CustomFields.direct.update(
  485. {
  486. _id: paramCustomFieldId,
  487. 'settings.dropdownItems._id': paramDropdownItemId,
  488. },
  489. {
  490. $set: {
  491. 'settings.dropdownItems.$': {
  492. _id: paramDropdownItemId,
  493. name: paramName,
  494. },
  495. },
  496. },
  497. );
  498. }
  499. JsonRoutes.sendResult(res, {
  500. code: 200,
  501. data: { _id: customFieldId },
  502. });
  503. },
  504. );
  505. /**
  506. * @operation delete_custom_field_dropdown_item
  507. * @summary Update a Custom Field's dropdown items
  508. *
  509. * @param {string} itemId ID of the dropdown item
  510. * @return_type {_id: string}
  511. */
  512. JsonRoutes.add(
  513. 'DELETE',
  514. '/api/boards/:boardId/custom-fields/:customFieldId/dropdown-items/:dropdownItemId',
  515. (req, res) => {
  516. Authentication.checkUserId(req.userId);
  517. paramCustomFieldId = req.params.customFieldId;
  518. paramDropdownItemId = req.params.dropdownItemId;
  519. CustomFields.direct.update(
  520. { _id: paramCustomFieldId },
  521. {
  522. $pull: {
  523. 'settings.dropdownItems': { _id: paramDropdownItemId },
  524. },
  525. },
  526. );
  527. JsonRoutes.sendResult(res, {
  528. code: 200,
  529. data: { _id: paramCustomFieldId },
  530. });
  531. },
  532. );
  533. /**
  534. * @operation delete_custom_field
  535. * @summary Delete a Custom Fields attached to a board
  536. *
  537. * @description The Custom Field can't be retrieved after this operation
  538. *
  539. * @param {string} boardID the ID of the board
  540. * @param {string} customFieldId the ID of the custom field
  541. * @return_type {_id: string}
  542. */
  543. JsonRoutes.add(
  544. 'DELETE',
  545. '/api/boards/:boardId/custom-fields/:customFieldId',
  546. function(req, res) {
  547. Authentication.checkUserId(req.userId);
  548. const paramBoardId = req.params.boardId;
  549. const id = req.params.customFieldId;
  550. CustomFields.remove({ _id: id, boardIds: { $in: [paramBoardId] } });
  551. JsonRoutes.sendResult(res, {
  552. code: 200,
  553. data: {
  554. _id: id,
  555. },
  556. });
  557. },
  558. );
  559. }
  560. export default CustomFields;