customFields.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. automaticallyOnCard: {
  34. type: Boolean,
  35. },
  36. showLabelOnMiniCard: {
  37. type: Boolean,
  38. },
  39. }));
  40. CustomFields.allow({
  41. insert(userId, doc) {
  42. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  43. },
  44. update(userId, doc) {
  45. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  46. },
  47. remove(userId, doc) {
  48. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  49. },
  50. fetch: ['userId', 'boardId'],
  51. });
  52. // not sure if we need this?
  53. //CustomFields.hookOptions.after.update = { fetchPrevious: false };
  54. function customFieldCreation(userId, doc){
  55. Activities.insert({
  56. userId,
  57. activityType: 'createCustomField',
  58. boardId: doc.boardId,
  59. customFieldId: doc._id,
  60. });
  61. }
  62. if (Meteor.isServer) {
  63. /*Meteor.startup(() => {
  64. CustomFields._collection._ensureIndex({ boardId: 1});
  65. });*/
  66. CustomFields.after.insert((userId, doc) => {
  67. customFieldCreation(userId, doc);
  68. });
  69. CustomFields.after.remove((userId, doc) => {
  70. Activities.remove({
  71. customFieldId: doc._id,
  72. });
  73. Cards.update(
  74. {'boardId': doc.boardId, 'customFields._id': doc._id},
  75. {$pull: {'customFields': {'_id': doc._id}}},
  76. {multi: true}
  77. );
  78. });
  79. }
  80. //CUSTOM FIELD REST API
  81. if (Meteor.isServer) {
  82. JsonRoutes.add('GET', '/api/boards/:boardId/custom-fields', function (req, res) {
  83. Authentication.checkUserId( req.userId);
  84. const paramBoardId = req.params.boardId;
  85. JsonRoutes.sendResult(res, {
  86. code: 200,
  87. data: CustomFields.find({ boardId: paramBoardId }).map(function (cf) {
  88. return {
  89. _id: cf._id,
  90. name: cf.name,
  91. type: cf.type,
  92. };
  93. }),
  94. });
  95. });
  96. JsonRoutes.add('GET', '/api/boards/:boardId/custom-fields/:customFieldId', function (req, res) {
  97. Authentication.checkUserId( req.userId);
  98. const paramBoardId = req.params.boardId;
  99. const paramCustomFieldId = req.params.customFieldId;
  100. JsonRoutes.sendResult(res, {
  101. code: 200,
  102. data: CustomFields.findOne({ _id: paramCustomFieldId, boardId: paramBoardId }),
  103. });
  104. });
  105. JsonRoutes.add('POST', '/api/boards/:boardId/custom-fields', function (req, res) {
  106. Authentication.checkUserId( req.userId);
  107. const paramBoardId = req.params.boardId;
  108. const id = CustomFields.direct.insert({
  109. name: req.body.name,
  110. type: req.body.type,
  111. settings: req.body.settings,
  112. showOnCard: req.body.showOnCard,
  113. automaticallyOnCard: req.body.automaticallyOnCard,
  114. showLabelOnMiniCard: req.body.showLabelOnMiniCard,
  115. boardId: paramBoardId,
  116. });
  117. const customField = CustomFields.findOne({_id: id, boardId: paramBoardId });
  118. customFieldCreation(req.body.authorId, customField);
  119. JsonRoutes.sendResult(res, {
  120. code: 200,
  121. data: {
  122. _id: id,
  123. },
  124. });
  125. });
  126. JsonRoutes.add('DELETE', '/api/boards/:boardId/custom-fields/:customFieldId', function (req, res) {
  127. Authentication.checkUserId( req.userId);
  128. const paramBoardId = req.params.boardId;
  129. const id = req.params.customFieldId;
  130. CustomFields.remove({ _id: id, boardId: paramBoardId });
  131. JsonRoutes.sendResult(res, {
  132. code: 200,
  133. data: {
  134. _id: id,
  135. },
  136. });
  137. });
  138. }