customFields.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. Activities.insert({
  169. userId,
  170. activityType: 'setCustomField',
  171. boardId: doc.boardIds[0], // We are creating a customField, it has only one boardId
  172. customFieldId: doc._id,
  173. customFieldValue,
  174. listId: doc.listId,
  175. swimlaneId: doc.swimlaneId,
  176. });
  177. }
  178. if (Meteor.isServer) {
  179. Meteor.startup(() => {
  180. CustomFields._collection._ensureIndex({ modifiedAt: -1 });
  181. CustomFields._collection._ensureIndex({ boardIds: 1 });
  182. });
  183. CustomFields.after.insert((userId, doc) => {
  184. customFieldCreation(userId, doc);
  185. });
  186. CustomFields.before.update((userId, doc, fieldNames, modifier) => {
  187. if (_.contains(fieldNames, 'boardIds') && modifier.$pull) {
  188. Cards.update(
  189. { boardId: modifier.$pull.boardIds, 'customFields._id': doc._id },
  190. { $pull: { customFields: { _id: doc._id } } },
  191. { multi: true },
  192. );
  193. customFieldEdit(userId, doc);
  194. Activities.remove({
  195. customFieldId: doc._id,
  196. boardId: modifier.$pull.boardIds,
  197. listId: doc.listId,
  198. swimlaneId: doc.swimlaneId,
  199. });
  200. } else if (_.contains(fieldNames, 'boardIds') && modifier.$push) {
  201. Activities.insert({
  202. userId,
  203. activityType: 'createCustomField',
  204. boardId: modifier.$push.boardIds,
  205. customFieldId: doc._id,
  206. });
  207. }
  208. });
  209. CustomFields.before.remove((userId, doc) => {
  210. customFieldDeletion(userId, doc);
  211. Activities.remove({
  212. customFieldId: doc._id,
  213. });
  214. Cards.update(
  215. { boardId: { $in: doc.boardIds }, 'customFields._id': doc._id },
  216. { $pull: { customFields: { _id: doc._id } } },
  217. { multi: true },
  218. );
  219. });
  220. }
  221. //CUSTOM FIELD REST API
  222. if (Meteor.isServer) {
  223. /**
  224. * @operation get_all_custom_fields
  225. * @summary Get the list of Custom Fields attached to a board
  226. *
  227. * @param {string} boardID the ID of the board
  228. * @return_type [{_id: string,
  229. * name: string,
  230. * type: string}]
  231. */
  232. JsonRoutes.add('GET', '/api/boards/:boardId/custom-fields', function(
  233. req,
  234. res,
  235. ) {
  236. Authentication.checkUserId(req.userId);
  237. const paramBoardId = req.params.boardId;
  238. JsonRoutes.sendResult(res, {
  239. code: 200,
  240. data: CustomFields.find({ boardIds: { $in: [paramBoardId] } }).map(
  241. function(cf) {
  242. return {
  243. _id: cf._id,
  244. name: cf.name,
  245. type: cf.type,
  246. };
  247. },
  248. ),
  249. });
  250. });
  251. /**
  252. * @operation get_custom_field
  253. * @summary Get a Custom Fields attached to a board
  254. *
  255. * @param {string} boardID the ID of the board
  256. * @param {string} customFieldId the ID of the custom field
  257. * @return_type CustomFields
  258. */
  259. JsonRoutes.add(
  260. 'GET',
  261. '/api/boards/:boardId/custom-fields/:customFieldId',
  262. function(req, res) {
  263. Authentication.checkUserId(req.userId);
  264. const paramBoardId = req.params.boardId;
  265. const paramCustomFieldId = req.params.customFieldId;
  266. JsonRoutes.sendResult(res, {
  267. code: 200,
  268. data: CustomFields.findOne({
  269. _id: paramCustomFieldId,
  270. boardIds: { $in: [paramBoardId] },
  271. }),
  272. });
  273. },
  274. );
  275. /**
  276. * @operation new_custom_field
  277. * @summary Create a Custom Field
  278. *
  279. * @param {string} boardID the ID of the board
  280. * @param {string} name the name of the custom field
  281. * @param {string} type the type of the custom field
  282. * @param {string} settings the settings object of the custom field
  283. * @param {boolean} showOnCard should we show the custom field on cards?
  284. * @param {boolean} automaticallyOnCard should the custom fields automatically be added on cards?
  285. * @param {boolean} showLabelOnMiniCard should the label of the custom field be shown on minicards?
  286. * @return_type {_id: string}
  287. */
  288. JsonRoutes.add('POST', '/api/boards/:boardId/custom-fields', function(
  289. req,
  290. res,
  291. ) {
  292. Authentication.checkUserId(req.userId);
  293. const paramBoardId = req.params.boardId;
  294. const board = Boards.findOne({ _id: paramBoardId });
  295. const id = CustomFields.direct.insert({
  296. name: req.body.name,
  297. type: req.body.type,
  298. settings: req.body.settings,
  299. showOnCard: req.body.showOnCard,
  300. automaticallyOnCard: req.body.automaticallyOnCard,
  301. showLabelOnMiniCard: req.body.showLabelOnMiniCard,
  302. boardIds: [board._id],
  303. });
  304. const customField = CustomFields.findOne({
  305. _id: id,
  306. boardIds: { $in: [paramBoardId] },
  307. });
  308. customFieldCreation(req.body.authorId, customField);
  309. JsonRoutes.sendResult(res, {
  310. code: 200,
  311. data: {
  312. _id: id,
  313. },
  314. });
  315. });
  316. /**
  317. * @operation delete_custom_field
  318. * @summary Delete a Custom Fields attached to a board
  319. *
  320. * @description The Custom Field can't be retrieved after this operation
  321. *
  322. * @param {string} boardID the ID of the board
  323. * @param {string} customFieldId the ID of the custom field
  324. * @return_type {_id: string}
  325. */
  326. JsonRoutes.add(
  327. 'DELETE',
  328. '/api/boards/:boardId/custom-fields/:customFieldId',
  329. function(req, res) {
  330. Authentication.checkUserId(req.userId);
  331. const paramBoardId = req.params.boardId;
  332. const id = req.params.customFieldId;
  333. CustomFields.remove({ _id: id, boardIds: { $in: [paramBoardId] } });
  334. JsonRoutes.sendResult(res, {
  335. code: 200,
  336. data: {
  337. _id: id,
  338. },
  339. });
  340. },
  341. );
  342. }
  343. export default CustomFields;