mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 04:40:32 +00:00
feat: Exchange rates CRUD.
This commit is contained in:
@@ -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(),
|
||||
|
||||
@@ -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');
|
||||
};
|
||||
166
server/src/http/controllers/ExchangeRates.js
Normal file
166
server/src/http/controllers/ExchangeRates.js
Normal 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 });
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -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());
|
||||
|
||||
10
server/src/models/ExchangeRate.js
Normal file
10
server/src/models/ExchangeRate.js
Normal file
@@ -0,0 +1,10 @@
|
||||
import BaseModel from '@/models/Model';
|
||||
|
||||
export default class ExchangeRate extends BaseModel {
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'exchange_rates';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user