Skip to content

Commit

Permalink
feat(code): added code middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1peshcoff committed Apr 28, 2019
1 parent a7dd4e2 commit 508a460
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
90 changes: 89 additions & 1 deletion lib/integrations.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const { Integration } = require('../models');
const moment = require('moment');

const { Integration, Code } = require('../models');
const { Sequelize } = require('./sequelize');
const errors = require('./errors');
const helpers = require('./helpers');

Expand Down Expand Up @@ -66,3 +69,88 @@ exports.deleteIntegration = async (req, res) => {
data: req.integration
});
};

exports.addCodesToIntegration = async (req, res) => {
if (!req.permissions.manage_discounts) {
return errors.makeForbiddenError(res, 'You are not allowed to populate codes.');
}

if (!Array.isArray(req.body)) {
return errors.makeBadRequestError(res, 'The body should be an array.');
}

if (req.body.length === 0) {
return errors.makeBadRequestError(res, 'No codes are provided.');
}

const arrayToCreate = req.body.map(code => ({
integration_id: req.integration.id,
value: code
}));

await Code.bulkCreate(arrayToCreate);

return res.json({
success: true,
message: 'Codes are populated.'
});
};

exports.claimCode = async (req, res) => {
// Checking if a user has already claimed more codes than available.
const startPeriod = moment().startOf(req.integration.quota_period).toDate();
const endPeriod = moment().endOf(req.integration.quota_period).toDate();

const existingCodes = await Code.count({
where: {
claimed_by: req.user.id,
integration_id: req.integration.id,
updated_at: {
[Sequelize.Op.gte]: startPeriod,
[Sequelize.Op.lte]: endPeriod
}
}
});

if (existingCodes >= req.integration.quota_amount) {
return errors.makeForbiddenError(res, 'Your quota is exceeded for this integration.');
}

// Trying to get a random code from DB.
const codeToClaim = await Code.findOne({
where: {
claimed_by: null,
integration_id: req.integration.id
}
});

// There can be cases when there are no free codes anymore.
if (!codeToClaim) {
return errors.makeForbiddenError(res, 'There are no codes left. Wait for CD to add them.');
}

await codeToClaim.update({ claimed_by: req.user.id });

// TODO: Send mail to user.
return res.json({
success: true,
data: codeToClaim
});
};

exports.getMyCodes = async (req, res) => {
const myCodes = await Code.findAll({
where: {
claimed_by: req.user.id
},
include: [Integration],
order: [
['updated_at', 'DESC'],
]
});

return res.json({
success: true,
data: myCodes
});
};
5 changes: 5 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ process.on('unhandledRejection', (err) => {
GeneralRouter.use(middlewares.authenticateUser);
GeneralRouter.get('/integrations', integrations.listAllIntegrations);
GeneralRouter.post('/integrations', integrations.createIntegration);
GeneralRouter.post('/integrations/:integration_id/codes', integrations.findIntegration, integrations.addCodesToIntegration);
GeneralRouter.post('/integrations/:integration_id/claim', integrations.findIntegration, integrations.claimCode);
GeneralRouter.put('/integrations/:integration_id', integrations.findIntegration, integrations.updateIntegration);
GeneralRouter.delete('/integrations/:integration_id', integrations.findIntegration, integrations.deleteIntegration);

GeneralRouter.post('/codes/mine', integrations.getMyCodes);


server.use('/', GeneralRouter);
server.use(middlewares.notFound);
server.use(middlewares.errorHandler);
Expand Down

0 comments on commit 508a460

Please sign in to comment.