customFields.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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: ['text', 'number', 'date', 'dropdown', 'currency'],
  25. },
  26. settings: {
  27. /**
  28. * settings of the custom field
  29. */
  30. type: Object,
  31. },
  32. 'settings.currencyCode': {
  33. type: String,
  34. optional: true,
  35. },
  36. 'settings.dropdownItems': {
  37. /**
  38. * list of drop down items objects
  39. */
  40. type: [Object],
  41. optional: true,
  42. },
  43. 'settings.dropdownItems.$': {
  44. type: new SimpleSchema({
  45. _id: {
  46. /**
  47. * ID of the drop down item
  48. */
  49. type: String,
  50. },
  51. name: {
  52. /**
  53. * name of the drop down item
  54. */
  55. type: String,
  56. },
  57. }),
  58. },
  59. showOnCard: {
  60. /**
  61. * should we show on the cards this custom field
  62. */
  63. type: Boolean,
  64. },
  65. automaticallyOnCard: {
  66. /**
  67. * should the custom fields automatically be added on cards?
  68. */
  69. type: Boolean,
  70. },
  71. showLabelOnMiniCard: {
  72. /**
  73. * should the label of the custom field be shown on minicards?
  74. */
  75. type: Boolean,
  76. },
  77. createdAt: {
  78. type: Date,
  79. optional: true,
  80. // eslint-disable-next-line consistent-return
  81. autoValue() {
  82. if (this.isInsert) {
  83. return new Date();
  84. } else if (this.isUpsert) {
  85. return { $setOnInsert: new Date() };
  86. } else {
  87. this.unset();
  88. }
  89. },
  90. },
  91. modifiedAt: {
  92. type: Date,
  93. denyUpdate: false,
  94. // eslint-disable-next-line consistent-return
  95. autoValue() {
  96. if (this.isInsert || this.isUpsert || this.isUpdate) {
  97. return new Date();
  98. } else {
  99. this.unset();
  100. }
  101. },
  102. },
  103. }),
  104. );
  105. CustomFields.mutations({
  106. addBoard(boardId) {
  107. if (boardId) {
  108. return {
  109. $push: {
  110. boardIds: boardId,
  111. },
  112. };
  113. } else {
  114. return null;
  115. }
  116. },
  117. });
  118. CustomFields.allow({
  119. insert(userId, doc) {
  120. return allowIsAnyBoardMember(
  121. userId,
  122. Boards.find({
  123. _id: { $in: doc.boardIds },
  124. }).fetch(),
  125. );
  126. },
  127. update(userId, doc) {
  128. return allowIsAnyBoardMember(
  129. userId,
  130. Boards.find({
  131. _id: { $in: doc.boardIds },
  132. }).fetch(),
  133. );
  134. },
  135. remove(userId, doc) {
  136. return allowIsAnyBoardMember(
  137. userId,
  138. Boards.find({
  139. _id: { $in: doc.boardIds },
  140. }).fetch(),
  141. );
  142. },
  143. fetch: ['userId', 'boardIds'],
  144. });
  145. // not sure if we need this?
  146. //CustomFields.hookOptions.after.update = { fetchPrevious: false };
  147. function customFieldCreation(userId, doc) {
  148. Activities.insert({
  149. userId,
  150. activityType: 'createCustomField',
  151. boardId: doc.boardIds[0], // We are creating a customField, it has only one boardId
  152. customFieldId: doc._id,
  153. });
  154. }
  155. function customFieldDeletion(userId, doc) {
  156. Activities.insert({
  157. userId,
  158. activityType: 'deleteCustomField',
  159. boardId: doc.boardIds[0], // We are creating a customField, it has only one boardId
  160. customFieldId: doc._id,
  161. });
  162. }
  163. // This has some bug, it does not show edited customField value at Outgoing Webhook,
  164. // instead it shows undefined, and no listId and swimlaneId.
  165. function customFieldEdit(userId, doc) {
  166. const card = Cards.findOne(doc.cardId);
  167. const customFieldValue = Activities.findOne({ customFieldId: doc._id }).value;
  168. const boardId = card.boardId;
  169. //boardId: doc.boardIds[0], // We are creating a customField, it has only one boardId
  170. Activities.insert({
  171. userId,
  172. activityType: 'setCustomField',
  173. boardId,
  174. customFieldId: doc._id,
  175. customFieldValue,
  176. listId: card.listId,
  177. swimlaneId: card.swimlaneId,
  178. });
  179. }
  180. if (Meteor.isServer) {
  181. Meteor.startup(() => {
  182. CustomFields._collection._ensureIndex({ modifiedAt: -1 });
  183. CustomFields._collection._ensureIndex({ boardIds: 1 });
  184. });
  185. CustomFields.after.insert((userId, doc) => {
  186. customFieldCreation(userId, doc);
  187. });
  188. CustomFields.before.update((userId, doc, fieldNames, modifier) => {
  189. if (_.contains(fieldNames, 'boardIds') && modifier.$pull) {
  190. Cards.update(
  191. { boardId: modifier.$pull.boardIds, 'customFields._id': doc._id },
  192. { $pull: { customFields: { _id: doc._id } } },
  193. { multi: true },
  194. );
  195. customFieldEdit(userId, doc);
  196. Activities.remove({
  197. customFieldId: doc._id,
  198. boardId: modifier.$pull.boardIds,
  199. listId: card.listId,
  200. swimlaneId: card.swimlaneId,
  201. });
  202. } else if (_.contains(fieldNames, 'boardIds') && modifier.$push) {
  203. Activities.insert({
  204. userId,
  205. activityType: 'createCustomField',
  206. boardId: modifier.$push.boardIds,
  207. customFieldId: doc._id,
  208. });
  209. }
  210. });
  211. CustomFields.before.remove((userId, doc) => {
  212. customFieldDeletion(userId, doc);
  213. Activities.remove({
  214. customFieldId: doc._id,
  215. });
  216. Cards.update(
  217. { boardId: { $in: doc.boardIds }, 'customFields._id': doc._id },
  218. { $pull: { customFields: { _id: doc._id } } },
  219. { multi: true },
  220. );
  221. });
  222. }
  223. //CUSTOM FIELD REST API
  224. if (Meteor.isServer) {
  225. /**
  226. * @operation get_all_custom_fields
  227. * @summary Get the list of Custom Fields attached to a board
  228. *
  229. * @param {string} boardID the ID of the board
  230. * @return_type [{_id: string,
  231. * name: string,
  232. * type: string}]
  233. */
  234. JsonRoutes.add('GET', '/api/boards/:boardId/custom-fields', function(
  235. req,
  236. res,
  237. ) {
  238. Authentication.checkUserId(req.userId);
  239. const paramBoardId = req.params.boardId;
  240. JsonRoutes.sendResult(res, {
  241. code: 200,
  242. data: CustomFields.find({ boardIds: { $in: [paramBoardId] } }).map(
  243. function(cf) {
  244. return {
  245. _id: cf._id,
  246. name: cf.name,
  247. type: cf.type,
  248. };
  249. },
  250. ),
  251. });
  252. });
  253. /**
  254. * @operation get_custom_field
  255. * @summary Get a Custom Fields attached to a board
  256. *
  257. * @param {string} boardID the ID of the board
  258. * @param {string} customFieldId the ID of the custom field
  259. * @return_type CustomFields
  260. */
  261. JsonRoutes.add(
  262. 'GET',
  263. '/api/boards/:boardId/custom-fields/:customFieldId',
  264. function(req, res) {
  265. Authentication.checkUserId(req.userId);
  266. const paramBoardId = req.params.boardId;
  267. const paramCustomFieldId = req.params.customFieldId;
  268. JsonRoutes.sendResult(res, {
  269. code: 200,
  270. data: CustomFields.findOne({
  271. _id: paramCustomFieldId,
  272. boardIds: { $in: [paramBoardId] },
  273. }),
  274. });
  275. },
  276. );
  277. /**
  278. * @operation new_custom_field
  279. * @summary Create a Custom Field
  280. *
  281. * @param {string} boardID the ID of the board
  282. * @param {string} name the name of the custom field
  283. * @param {string} type the type of the custom field
  284. * @param {string} settings the settings object of the custom field
  285. * @param {boolean} showOnCard should we show the custom field on cards?
  286. * @param {boolean} automaticallyOnCard should the custom fields automatically be added on cards?
  287. * @param {boolean} showLabelOnMiniCard should the label of the custom field be shown on minicards?
  288. * @return_type {_id: string}
  289. */
  290. JsonRoutes.add('POST', '/api/boards/:boardId/custom-fields', function(
  291. req,
  292. res,
  293. ) {
  294. Authentication.checkUserId(req.userId);
  295. const paramBoardId = req.params.boardId;
  296. const board = Boards.findOne({ _id: paramBoardId });
  297. const id = CustomFields.direct.insert({
  298. name: req.body.name,
  299. type: req.body.type,
  300. settings: req.body.settings,
  301. showOnCard: req.body.showOnCard,
  302. automaticallyOnCard: req.body.automaticallyOnCard,
  303. showLabelOnMiniCard: req.body.showLabelOnMiniCard,
  304. boardIds: [board._id],
  305. });
  306. const customField = CustomFields.findOne({
  307. _id: id,
  308. boardIds: { $in: [paramBoardId] },
  309. });
  310. customFieldCreation(req.body.authorId, customField);
  311. JsonRoutes.sendResult(res, {
  312. code: 200,
  313. data: {
  314. _id: id,
  315. },
  316. });
  317. });
  318. /**
  319. * @operation delete_custom_field
  320. * @summary Delete a Custom Fields attached to a board
  321. *
  322. * @description The Custom Field can't be retrieved after this operation
  323. *
  324. * @param {string} boardID the ID of the board
  325. * @param {string} customFieldId the ID of the custom field
  326. * @return_type {_id: string}
  327. */
  328. JsonRoutes.add(
  329. 'DELETE',
  330. '/api/boards/:boardId/custom-fields/:customFieldId',
  331. function(req, res) {
  332. Authentication.checkUserId(req.userId);
  333. const paramBoardId = req.params.boardId;
  334. const id = req.params.customFieldId;
  335. CustomFields.remove({ _id: id, boardIds: { $in: [paramBoardId] } });
  336. JsonRoutes.sendResult(res, {
  337. code: 200,
  338. data: {
  339. _id: id,
  340. },
  341. });
  342. },
  343. );
  344. }
  345. export default CustomFields;