Skip to content

Commit

Permalink
refactor: updated GET bills by agreementId
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarobernal2412 committed Dec 16, 2024
1 parent e986bdb commit 451c950
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 112 deletions.
2 changes: 1 addition & 1 deletion configurations/config.ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ server:
httpOptionsOK: true
servePackageInfo: true
listenOnHttps: false
apiVersion: 7
apiVersion: 6
statusBouncer: false
streaming: true
2 changes: 1 addition & 1 deletion configurations/config.development.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ server:
httpOptionsOK: true
servePackageInfo: true
listenOnHttps: false
apiVersion: 7
apiVersion: 6
statusBouncer: false
streaming: true
1 change: 1 addition & 0 deletions configurations/config.production.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ server:
httpOptionsOK: true
servePackageInfo: true
listenOnHttps: false
apiVersion: 6
statusBouncer: false
streaming: true
2 changes: 1 addition & 1 deletion configurations/config.testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ server:
httpOptionsOK: true
servePackageInfo: true
listenOnHttps: false
apiVersion: 7
apiVersion: 6
statusBouncer: false
streaming: true
5 changes: 5 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ services:
container_name: registry
environment:
- PORT=5400
- NODE_ENV=development
- API_PREFIX=%{API_PREFIX}
- MONGOPASS=${MONGOPASS}
- MONGOADMIN=${MONGOADMIN}
- HTTPS_SERVER=${HTTPS_SERVER}
depends_on:
- mongodb
networks:
Expand Down
22 changes: 20 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ function _deploy(configurations, expressMiddlewares, callback) {
const CURRENT_API_VERSION = config.server.apiVersion;
const swaggerUtils = require('./src/utils').swagger;

const API_PREFIX = process.env.API_PREFIX || '/api/v6';
const agreementRegistry = require('./src/controllers/v6/AgreementRegistry.js');
const setUpAccountableRegistry = require('./src/controllers/v6/AccountableRegistry.js');
const billRegistry = require('./src/controllers/v6/BillRegistry.js');
const stateRegistry = require('./src/controllers/v6/StateRegistry.js');
const templateRegistry = require('./src/controllers/v6/TemplateRegistry.js');

// Serve static files
app.use(express.static(frontendPath));

Expand Down Expand Up @@ -112,7 +119,7 @@ function _deploy(configurations, expressMiddlewares, callback) {
});
}

app.use('/api/v6/states/:agreement', middlewares.stateInProgress);
// app.use(`${API_PREFIX}/states/:agreement`, middlewares.stateInProgress);

// Redirects
app.get('/api/latest/docs', (req, res) => {
Expand Down Expand Up @@ -166,6 +173,17 @@ function _deploy(configurations, expressMiddlewares, callback) {
}

logger.info('Initializing app after db connection');

// app.use(`${API_PREFIX}/agreements`, agreementRegistry);

// app.use(`${API_PREFIX}/setUpAccountableRegistry`, setUpAccountableRegistry);

app.use(`${API_PREFIX}/bills`, billRegistry);

// // app.use(`${API_PREFIX}/states`, middlewares.stateInProgress, stateRegistry);
// app.use(`${API_PREFIX}/states`, stateRegistry);

// app.use(`${API_PREFIX}/templates`, templateRegistry);

// Serve Swagger UI
const swaggerDocument = swaggerUtils.getSwaggerDoc(CURRENT_API_VERSION);
Expand Down Expand Up @@ -199,7 +217,7 @@ function _deploy(configurations, expressMiddlewares, callback) {

const server = createServer().listen(serverOptions, () => {
logger.info(`Server listening on port ${serverPort} (${config.server.listenOnHttps ? 'https' : 'http'}://localhost:${serverPort})`);
logger.info('Swagger UI and API docs served in /api/v6/docs and /api/v6/api-docs');
logger.info(`Swagger UI and API docs served in ${API_PREFIX}/docs and ${API_PREFIX}/api-docs`);
if (callback) callback(server);
});
});
Expand Down
6 changes: 4 additions & 2 deletions src/api/swaggerV7.yaml → src/api/swaggerV6.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ openapi: 3.0.0
info:
title: Governify Registry
description: Governify Registry API
version: '7'
version: '6'
termsOfService: www.isa.us.es
contact:
name: ISA Research Group
Expand All @@ -13,7 +13,7 @@ info:
url: 'http://opensource.org/licenses/MIT'

servers:
- url: /api/v7
- url: /api/v6
description: Development server
- url: https://registry.governify.io/api/v6
description: Production server
Expand Down Expand Up @@ -403,10 +403,12 @@ paths:
type: string
- name: from
in: query
required: true
schema:
type: string
- name: to
in: query
required: true
schema:
type: string
responses:
Expand Down
52 changes: 6 additions & 46 deletions src/controllers/v6/BillRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,52 +25,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

'use strict';

const express = require('express');
const router = express.Router();
const bills = require('./bills/bills.js');

/**
* Registry override module.
* @module BillRegistry
* @see module:BillRegistryService
* @see module:overrides
* @requires BillRegistryService
* */
module.exports = {
router.get('/:agreementId', bills.billsGET);
router.put('/:agreementId/:id', bills.billsPUT);
router.delete('/:agreementId', bills.billsDELETE);

billsGET: _billsGET,
billsPUT: _billsPUT,
billsDELETE: _billsDELETE

};

/**
* billsGET.
* @param {Object} req request
* @param {Object} res response
* @param {Object} next next function
* @alias module:BillRegistry.billsGET
* */
function _billsGET (req, res, next) {
bills.billsGET(req, res, next);
}

/**
* billsDELETE.
* @param {Object} req request
* @param {Object} res response
* @param {Object} next next function
* @alias module:BillRegistry.billsDELETE
* */
function _billsDELETE (req, res, next) {
bills.billsDELETE(req, res, next);
}

/**
* billsPUT.
* @param {Object} req request
* @param {Object} res response
* @param {Object} next next function
* @alias module:BillRegistry.billsPUT
* */
function _billsPUT (req, res, next) {
bills.billsPUT(req.swagger.params, res, next);
}
module.exports = router;
1 change: 1 addition & 0 deletions src/controllers/v6/StateRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
'use strict';

const states = require('./states/states.js');
// const overrideRegistry = require('../OverrideRegistry.js');

/**
* Registry states module.
Expand Down
106 changes: 47 additions & 59 deletions src/controllers/v6/bills/bills.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,70 +101,58 @@ function _billsPUT (args, res) {
* @param {Object} next next function
* @alias module:bills.billsGET
* */
function _billsGET (req, res) {
/**
* parameters expected in the args:
* namespace (String)
**/
async function _billsGET(req, res) {
const { agreementId } = req.params;
const { from, to } = req.query;

const args = req.swagger.params;
const from = args.from.value;
const to = args.to.value;
logger.info('New request to GET bills bills/bills.js');
const BillsModel = db.models.BillsModel;
const AgreementModel = db.models.AgreementModel;
logger.info('New request to GET bills from bills/bills.js');

AgreementModel.findOne({ id: args.agreementId.value }, function (err, agreement) {
if (err) {
logger.error(err.toString());
res.status(500).json(new ErrorModel(500, err));
try {
const AgreementModel = db.models.AgreementModel;
const BillsModel = db.models.BillsModel;

// Find the agreement based on the agreementId
const agreement = await AgreementModel.findOne({ id: agreementId });

if (!agreement) {
return res.status(404).send('Agreement not found');
}

// Find all bills for the given agreementId
const bills = await BillsModel.find({ agreementId });

let periods;
if (from && to) {
const window = { from, end: to };
periods = utils.time.getPeriods(agreement, window);
} else {
if (agreement) {
BillsModel.find({ agreementId: args.agreementId.value }, function (err, bills) {
if (err) {
logger.error(err.toString());
res.status(500).json(new ErrorModel(500, err));
} else {
let periods;
if (from && to) {
const window = {
from: from,
end: to
};
periods = utils.time.getPeriods(agreement, window);
} else {
periods = utils.time.getPeriods(agreement);
}

const billsDates = [];
const orderedBills = [];
for (const x in bills) {
const bill = bills[x];
billsDates.push(moment(bill.period.from).unix());
}
for (const i in periods) {
const period = periods[i];
if (!billsDates.includes(moment(period.from).unix())) {
const standardBill = {
agreementId: args.agreementId.value,
billId: moment(period.from).unix() + '',
state: 'open',
period: period
};
orderedBills.push(standardBill);
} else {
orderedBills.push(bills[billsDates.indexOf(moment(period.from).unix())]);
}
}
logger.info('Bills returned returned');
res.status(200).json(orderedBills);
}
});
periods = utils.time.getPeriods(agreement);
}

const billsDates = bills.map(bill => moment(bill.period.from).unix());
const orderedBills = periods.map(period => {
const periodUnix = moment(period.from).unix();
const existingBillIndex = billsDates.indexOf(periodUnix);

if (existingBillIndex === -1) {
return {
agreementId,
billId: periodUnix.toString(),
state: 'open',
period,
};
} else {
res.status(404).send('Agreement not found');
return bills[existingBillIndex];
}
}
});
});

logger.info('Bills returned successfully');
return res.status(200).json(orderedBills);

} catch (err) {
logger.error('Error occurred:', err);
return res.status(500).json(new ErrorModel(500, err));
}
}

/**
Expand Down

0 comments on commit 451c950

Please sign in to comment.