feat: Exchange rates CRUD.

This commit is contained in:
Ahmed Bouhuolia
2020-04-19 22:09:23 +02:00
parent 8c8ec1534e
commit 4e0d3feebe
7 changed files with 386 additions and 1 deletions

View File

@@ -250,6 +250,14 @@ factory.define('currency', 'currencies', async () => {
};
});
factory.define('exchange_rate', 'exchange_rates', async () => {
return {
date: '2020-02-02',
currency_code: 'USD',
exchange_rate: faker.random.number(),
};
});
factory.define('budget', 'budgets', async () => {
return {
name: faker.lorem.slug(),

View File

@@ -0,0 +1,13 @@
exports.up = function(knex) {
return knex.schema.createTable('exchange_rates', table => {
table.increments();
table.string('currency_code', 4);
table.decimal('exchange_rate');
table.date('date');
});
};
exports.down = function(knex) {
return knex.schema.dropTableIfExists('exchange_rates');
};

View File

@@ -0,0 +1,166 @@
import express from 'express';
import { check, param, query, validationResult } from 'express-validator';
import moment from 'moment';
import asyncMiddleware from '@/http/middleware/asyncMiddleware';
import jwtAuth from '@/http/middleware/jwtAuth';
import ExchangeRate from '@/models/ExchangeRate';
export default {
/**
* Constructor method.
*/
router() {
const router = express.Router();
router.use(jwtAuth);
router.get('/',
this.exchangeRates.validation,
asyncMiddleware(this.exchangeRates.handler));
router.post('/',
this.addExchangeRate.validation,
asyncMiddleware(this.addExchangeRate.handler));
router.post('/:id',
this.editExchangeRate.validation,
asyncMiddleware(this.editExchangeRate.handler));
router.delete('/:id',
this.deleteExchangeRate.validation,
asyncMiddleware(this.deleteExchangeRate.handler));
return router;
},
/**
* Retrieve exchange rates.
*/
exchangeRates: {
validation: [
query('page').optional().isNumeric().toInt(),
query('page_size').optional().isNumeric().toInt(),
],
async handler(req, res) {
const validationErrors = validationResult(req);
if (!validationErrors.isEmpty()) {
return res.boom.badData(null, {
code: 'validation_error', ...validationErrors,
});
}
const filter = {
page: 1,
page_size: 10,
...req.query,
};
const exchangeRates = await ExchangeRate.query()
.pagination(filter.page - 1, filter.page_size);
return res.status(200).send({ exchange_rates: exchangeRates });
}
},
/**
* Adds a new exchange rate on the given date.
*/
addExchangeRate: {
validation: [
check('exchange_rate').exists().isNumeric().toFloat(),
check('currency_code').exists().trim().escape(),
check('date').exists().isISO8601(),
],
async handler(req, res) {
const validationErrors = validationResult(req);
if (!validationErrors.isEmpty()) {
return res.boom.badData(null, {
code: 'validation_error', ...validationErrors,
});
}
const form = { ...req.body };
const foundExchangeRate = await ExchangeRate.query()
.where('currency_code', form.currency_code)
.where('date', form.date);
if (foundExchangeRate.length > 0) {
return res.status(400).send({
errors: [{ type: 'EXCHANGE.RATE.DATE.PERIOD.DEFINED', code: 200 }],
});
}
await ExchangeRate.query().insert({
...form,
date: moment(form.date).format('YYYY-MM-DD'),
});
return res.status(200).send();
},
},
/**
* Edit the given exchange rate.
*/
editExchangeRate: {
validation: [
param('id').exists().isNumeric().toInt(),
check('exchange_rate').exists().isNumeric().toFloat(),
],
async handler(req, res) {
const validationErrors = validationResult(req);
if (!validationErrors.isEmpty()) {
return res.boom.badData(null, {
code: 'validation_error', ...validationErrors,
});
}
const { id } = req.params;
const form = { ...req.body };
const foundExchangeRate = await ExchangeRate.query()
.where('id', id);
if (!foundExchangeRate.length) {
return res.status(400).send({
errors: [{ type: 'EXCHANGE.RATE.NOT.FOUND', code: 200 }],
});
}
await ExchangeRate.query()
.where('id', id)
.update({ ...form });
return res.status(200).send({ id });
},
},
/**
* Delete the given exchange rate from the storage.
*/
deleteExchangeRate: {
validation: [
param('id').isNumeric().toInt(),
],
async handler(req, res) {
const validationErrors = validationResult(req);
if (!validationErrors.isEmpty()) {
return res.boom.badData(null, {
code: 'validation_error', ...validationErrors,
});
}
const { id } = req.params;
const foundExchangeRate = await ExchangeRate.query()
.where('id', id);
if (!foundExchangeRate.length) {
return res.status(404).send({
errors: [{ type: 'EXCHANGE.RATE.NOT.FOUND', code: 200 }],
});
}
await ExchangeRate.query()
.where('id', id).delete();
return res.status(200).send({ id });
}
},
}

View File

@@ -21,6 +21,7 @@ import Suppliers from '@/http/controllers/Suppliers';
import Bills from '@/http/controllers/Bills';
import CurrencyAdjustment from './controllers/CurrencyAdjustment';
import Resources from './controllers/Resources';
import ExchangeRates from '@/http/controllers/ExchangeRates';
// import SalesReports from '@/http/controllers/SalesReports';
// import PurchasesReports from '@/http/controllers/PurchasesReports';
@@ -47,6 +48,7 @@ export default (app) => {
// app.use('/api/bills', Bills.router());
app.use('/api/budget', Budget.router());
app.use('/api/resources', Resources.router());
app.use('/api/exchange_rates', ExchangeRates.router());
// app.use('/api/currency_adjustment', CurrencyAdjustment.router());
// app.use('/api/reports/sales', SalesReports.router());
// app.use('/api/reports/purchases', PurchasesReports.router());

View File

@@ -0,0 +1,10 @@
import BaseModel from '@/models/Model';
export default class ExchangeRate extends BaseModel {
/**
* Table name
*/
static get tableName() {
return 'exchange_rates';
}
}