customFields.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. });
  68. }
  69. //CUSTOM FIELD REST API
  70. if (Meteor.isServer) {
  71. JsonRoutes.add('GET', '/api/boards/:boardId/custom-fields', function (req, res) {
  72. Authentication.checkUserId( req.userId);
  73. const paramBoardId = req.params.boardId;
  74. JsonRoutes.sendResult(res, {
  75. code: 200,
  76. data: CustomFields.find({ boardId: paramBoardId }),
  77. });
  78. });
  79. JsonRoutes.add('GET', '/api/boards/:boardId/custom-fields/:customFieldId', function (req, res) {
  80. Authentication.checkUserId( req.userId);
  81. const paramBoardId = req.params.boardId;
  82. const paramCustomFieldId = req.params.customFieldId;
  83. JsonRoutes.sendResult(res, {
  84. code: 200,
  85. data: CustomFields.findOne({ _id: paramCustomFieldId, boardId: paramBoardId }),
  86. });
  87. });
  88. JsonRoutes.add('POST', '/api/boards/:boardId/custom-fields', function (req, res) {
  89. Authentication.checkUserId( req.userId);
  90. const paramBoardId = req.params.boardId;
  91. const id = CustomFields.direct.insert({
  92. name: req.body.name,
  93. type: req.body.type,
  94. settings: req.body.settings,
  95. showOnCard: req.body.showOnCard,
  96. boardId: paramBoardId,
  97. });
  98. const customField = CustomFields.findOne({_id: id, boardId: paramBoardId });
  99. customFieldCreation(req.body.authorId, customField);
  100. JsonRoutes.sendResult(res, {
  101. code: 200,
  102. data: {
  103. _id: id,
  104. },
  105. });
  106. });
  107. JsonRoutes.add('DELETE', '/api/boards/:boardId/custom-fields/:customFieldId', function (req, res) {
  108. Authentication.checkUserId( req.userId);
  109. const paramBoardId = req.params.boardId;
  110. const id = req.params.customFieldId;
  111. CustomFields.remove({ _id: id, boardId: paramBoardId });
  112. JsonRoutes.sendResult(res, {
  113. code: 200,
  114. data: {
  115. _id: id,
  116. },
  117. });
  118. });
  119. }