customFields.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 }).map(function (cf) {
  82. return {
  83. _id: cf._id,
  84. name: cf.name,
  85. type: cf.type,
  86. };
  87. }),
  88. });
  89. });
  90. JsonRoutes.add('GET', '/api/boards/:boardId/custom-fields/:customFieldId', function (req, res) {
  91. Authentication.checkUserId( req.userId);
  92. const paramBoardId = req.params.boardId;
  93. const paramCustomFieldId = req.params.customFieldId;
  94. JsonRoutes.sendResult(res, {
  95. code: 200,
  96. data: CustomFields.findOne({ _id: paramCustomFieldId, boardId: paramBoardId }),
  97. });
  98. });
  99. JsonRoutes.add('POST', '/api/boards/:boardId/custom-fields', function (req, res) {
  100. Authentication.checkUserId( req.userId);
  101. const paramBoardId = req.params.boardId;
  102. const id = CustomFields.direct.insert({
  103. name: req.body.name,
  104. type: req.body.type,
  105. settings: req.body.settings,
  106. showOnCard: req.body.showOnCard,
  107. boardId: paramBoardId,
  108. });
  109. const customField = CustomFields.findOne({_id: id, boardId: paramBoardId });
  110. customFieldCreation(req.body.authorId, customField);
  111. JsonRoutes.sendResult(res, {
  112. code: 200,
  113. data: {
  114. _id: id,
  115. },
  116. });
  117. });
  118. JsonRoutes.add('DELETE', '/api/boards/:boardId/custom-fields/:customFieldId', function (req, res) {
  119. Authentication.checkUserId( req.userId);
  120. const paramBoardId = req.params.boardId;
  121. const id = req.params.customFieldId;
  122. CustomFields.remove({ _id: id, boardId: paramBoardId });
  123. JsonRoutes.sendResult(res, {
  124. code: 200,
  125. data: {
  126. _id: id,
  127. },
  128. });
  129. });
  130. }