customFields.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. CustomFields = new Mongo.Collection('customFields');
  2. CustomFields.attachSchema(new SimpleSchema({
  3. boardId: {
  4. type: String,
  5. },
  6. name: {
  7. type: String,
  8. },
  9. type: {
  10. type: String,
  11. allowedValues: ['text', 'number', 'date', 'dropdown'],
  12. },
  13. settings: {
  14. type: Object,
  15. },
  16. 'settings.dropdownItems': {
  17. type: [Object],
  18. optional: true,
  19. },
  20. 'settings.dropdownItems.$': {
  21. type: new SimpleSchema({
  22. _id: {
  23. type: String,
  24. },
  25. name: {
  26. type: String,
  27. },
  28. }),
  29. },
  30. showOnCard: {
  31. type: Boolean,
  32. },
  33. }));
  34. CustomFields.allow({
  35. insert(userId, doc) {
  36. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  37. },
  38. update(userId, doc) {
  39. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  40. },
  41. remove(userId, doc) {
  42. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  43. },
  44. fetch: ['userId', 'boardId'],
  45. });
  46. // not sure if we need this?
  47. //CustomFields.hookOptions.after.update = { fetchPrevious: false };
  48. function customFieldCreation(userId, doc){
  49. Activities.insert({
  50. userId,
  51. activityType: 'createCustomField',
  52. boardId: doc.boardId,
  53. customFieldId: doc._id,
  54. });
  55. }
  56. if (Meteor.isServer) {
  57. /*Meteor.startup(() => {
  58. CustomFields._collection._ensureIndex({ boardId: 1});
  59. });*/
  60. CustomFields.after.insert((userId, doc) => {
  61. customFieldCreation(userId, doc);
  62. });
  63. CustomFields.after.remove((userId, doc) => {
  64. Activities.remove({
  65. customFieldId: doc._id,
  66. });
  67. Cards.update(
  68. {'boardId': doc.boardId, 'customFields._id': doc._id},
  69. {$pull: {'customFields': {'_id': doc._id}}},
  70. {multi: true}
  71. );
  72. });
  73. }
  74. //CUSTOM FIELD REST API
  75. if (Meteor.isServer) {
  76. JsonRoutes.add('GET', '/api/boards/:boardId/custom-fields', function (req, res) {
  77. Authentication.checkUserId( req.userId);
  78. const paramBoardId = req.params.boardId;
  79. JsonRoutes.sendResult(res, {
  80. code: 200,
  81. data: CustomFields.find({ boardId: paramBoardId }),
  82. });
  83. });
  84. JsonRoutes.add('GET', '/api/boards/:boardId/custom-fields/:customFieldId', function (req, res) {
  85. Authentication.checkUserId( req.userId);
  86. const paramBoardId = req.params.boardId;
  87. const paramCustomFieldId = req.params.customFieldId;
  88. JsonRoutes.sendResult(res, {
  89. code: 200,
  90. data: CustomFields.findOne({ _id: paramCustomFieldId, boardId: paramBoardId }),
  91. });
  92. });
  93. JsonRoutes.add('POST', '/api/boards/:boardId/custom-fields', function (req, res) {
  94. Authentication.checkUserId( req.userId);
  95. const paramBoardId = req.params.boardId;
  96. const id = CustomFields.direct.insert({
  97. name: req.body.name,
  98. type: req.body.type,
  99. settings: req.body.settings,
  100. showOnCard: req.body.showOnCard,
  101. boardId: paramBoardId,
  102. });
  103. const customField = CustomFields.findOne({_id: id, boardId: paramBoardId });
  104. customFieldCreation(req.body.authorId, customField);
  105. JsonRoutes.sendResult(res, {
  106. code: 200,
  107. data: {
  108. _id: id,
  109. },
  110. });
  111. });
  112. JsonRoutes.add('DELETE', '/api/boards/:boardId/custom-fields/:customFieldId', function (req, res) {
  113. Authentication.checkUserId( req.userId);
  114. const paramBoardId = req.params.boardId;
  115. const id = req.params.customFieldId;
  116. CustomFields.remove({ _id: id, boardId: paramBoardId });
  117. JsonRoutes.sendResult(res, {
  118. code: 200,
  119. data: {
  120. _id: id,
  121. },
  122. });
  123. });
  124. }