2
0

customFields.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 CustomFields
  295. */
  296. JsonRoutes.add(
  297. 'GET',
  298. '/api/boards/:boardId/custom-fields/:customFieldId',
  299. function(req, res) {
  300. Authentication.checkUserId(req.userId);
  301. const paramBoardId = req.params.boardId;
  302. const paramCustomFieldId = req.params.customFieldId;
  303. JsonRoutes.sendResult(res, {
  304. code: 200,
  305. data: CustomFields.findOne({
  306. _id: paramCustomFieldId,
  307. boardIds: { $in: [paramBoardId] },
  308. }),
  309. });
  310. },
  311. );
  312. /**
  313. * @operation new_custom_field
  314. * @summary Create a Custom Field
  315. *
  316. * @param {string} boardID the ID of the board
  317. * @param {string} name the name of the custom field
  318. * @param {string} type the type of the custom field
  319. * @param {string} settings the settings object of the custom field
  320. * @param {boolean} showOnCard should we show the custom field on cards?
  321. * @param {boolean} automaticallyOnCard should the custom fields automatically be added on cards?
  322. * @param {boolean} showLabelOnMiniCard should the label of the custom field be shown on minicards?
  323. * @return_type {_id: string}
  324. */
  325. JsonRoutes.add('POST', '/api/boards/:boardId/custom-fields', function(
  326. req,
  327. res,
  328. ) {
  329. Authentication.checkUserId(req.userId);
  330. const paramBoardId = req.params.boardId;
  331. const board = Boards.findOne({ _id: paramBoardId });
  332. const id = CustomFields.direct.insert({
  333. name: req.body.name,
  334. type: req.body.type,
  335. settings: req.body.settings,
  336. showOnCard: req.body.showOnCard,
  337. automaticallyOnCard: req.body.automaticallyOnCard,
  338. showLabelOnMiniCard: req.body.showLabelOnMiniCard,
  339. boardIds: [board._id],
  340. });
  341. const customField = CustomFields.findOne({
  342. _id: id,
  343. boardIds: { $in: [paramBoardId] },
  344. });
  345. customFieldCreation(req.body.authorId, customField);
  346. JsonRoutes.sendResult(res, {
  347. code: 200,
  348. data: {
  349. _id: id,
  350. },
  351. });
  352. });
  353. /**
  354. * @operation delete_custom_field
  355. * @summary Delete a Custom Fields attached to a board
  356. *
  357. * @description The Custom Field can't be retrieved after this operation
  358. *
  359. * @param {string} boardID the ID of the board
  360. * @param {string} customFieldId the ID of the custom field
  361. * @return_type {_id: string}
  362. */
  363. JsonRoutes.add(
  364. 'DELETE',
  365. '/api/boards/:boardId/custom-fields/:customFieldId',
  366. function(req, res) {
  367. Authentication.checkUserId(req.userId);
  368. const paramBoardId = req.params.boardId;
  369. const id = req.params.customFieldId;
  370. CustomFields.remove({ _id: id, boardIds: { $in: [paramBoardId] } });
  371. JsonRoutes.sendResult(res, {
  372. code: 200,
  373. data: {
  374. _id: id,
  375. },
  376. });
  377. },
  378. );
  379. }
  380. export default CustomFields;