const express = require('express');
const PublishService = require('../services/publish');
const wrapAsync = require('../helpers').wrapAsync;
const { checkCrudPermissions } = require('../middlewares/check-permissions');

const router = express.Router();

router.use(checkCrudPermissions('publish_events'));

const publishHandler = wrapAsync(async (req, res) => {
  const { projectId, title, description } = req.body;
  const result = await PublishService.publishToProduction(projectId, req.currentUser, title, description);
  res.status(200).send(result);
});

router.post('/', publishHandler);
router.post('/publish', publishHandler);

router.use('/', require('../helpers').commonErrorHandler);

module.exports = router;
