customFields.js 9.0 KB

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