customFields.js 9.1 KB

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