Skip to content

Commit b5dd5b2

Browse files
committed
refactor(inventory): break out inventory types
Breaks out inventory types out of the inventory index.js.
1 parent 836cd3d commit b5dd5b2

File tree

6 files changed

+80
-134
lines changed

6 files changed

+80
-134
lines changed

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/config/routes.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ const employeeReports = require('../controllers/payroll/reports');
7474
const inventory = require('../controllers/inventory');
7575
const inventoryUnits = require('../controllers/inventory/units');
7676
const inventoryGroups = require('../controllers/inventory/groups');
77+
const inventoryTypes = require('../controllers/inventory/types');
7778

7879
const depots = require('../controllers/inventory/depots');
7980
const inventoryReports = require('../controllers/inventory/reports');
@@ -371,14 +372,7 @@ exports.configure = function configure(app) {
371372

372373
/** Inventory Group API endpoints */
373374
app.use('/inventory/groups', inventoryGroups);
374-
375-
/** Inventory Type API endpoints */
376-
app.post('/inventory/types', inventory.createInventoryTypes);
377-
app.get('/inventory/types', inventory.listInventoryTypes);
378-
app.get('/inventory/types/:id', inventory.detailsInventoryTypes);
379-
app.put('/inventory/types/:id', inventory.updateInventoryTypes);
380-
app.delete('/inventory/types/:id', inventory.deleteInventoryTypes);
381-
375+
app.use('/inventory/types', inventoryTypes);
382376
app.use('/inventory/units', inventoryUnits);
383377

384378
/** Inventory Import API endpoints */

server/controllers/inventory/index.js

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// const debug = require('debug')('bhima:inventory:index');
1414
const _ = require('lodash');
1515
const core = require('./inventory/core');
16-
const types = require('./inventory/types');
1716
const importing = require('./import');
1817
const util = require('../../lib/util');
1918
const db = require('../../lib/db');
@@ -31,13 +30,6 @@ exports.updateInventoryItems = updateInventoryItems;
3130
exports.getInventoryItems = getInventoryItems;
3231
exports.getInventoryItemsById = getInventoryItemsById;
3332

34-
// expose inventory types methods
35-
exports.createInventoryTypes = createInventoryTypes;
36-
exports.updateInventoryTypes = updateInventoryTypes;
37-
exports.listInventoryTypes = listInventoryTypes;
38-
exports.detailsInventoryTypes = detailsInventoryTypes;
39-
exports.deleteInventoryTypes = deleteInventoryTypes;
40-
4133
exports.deleteInventory = deleteInventory;
4234

4335
exports.getInventoryUnitCosts = getInventoryUnitCosts;
@@ -276,53 +268,3 @@ async function deleteInventory(req, res) {
276268
await core.remove(req.params.uuid);
277269
res.sendStatus(204);
278270
}
279-
280-
// ======================= inventory type =============================
281-
/**
282-
* POST /inventory/types
283-
* Create a new inventory types
284-
*/
285-
async function createInventoryTypes(req, res) {
286-
const id = await types.create(req.body);
287-
res.status(201).json({ id });
288-
289-
}
290-
291-
/**
292-
* PUT /inventory/types/:id
293-
* Create a new inventory types
294-
*/
295-
async function updateInventoryTypes(req, res) {
296-
const rows = await types.update(req.body, req.params.id);
297-
res.status(201).json(rows);
298-
299-
}
300-
301-
/**
302-
* GET /inventory/types
303-
* get the list of inventory types
304-
*/
305-
async function listInventoryTypes(req, res) {
306-
const rows = await types.list();
307-
res.status(200).json(rows);
308-
309-
}
310-
311-
/**
312-
* GET /inventory/types/:id
313-
* get the list of inventory types
314-
*/
315-
async function detailsInventoryTypes(req, res) {
316-
const rows = await types.details(req.params.id);
317-
res.status(200).json(rows);
318-
319-
}
320-
321-
/**
322-
* DELETE /inventory/types/:id
323-
* delete an inventory types
324-
*/
325-
async function deleteInventoryTypes(req, res) {
326-
await types.remove(req.params.id);
327-
res.sendStatus(204);
328-
}

server/controllers/inventory/inventory/groups.js

Whitespace-only changes.

server/controllers/inventory/inventory/types.js

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const router = require('express').Router();
2+
const db = require('../../../lib/db');
3+
4+
/**
5+
* POST /inventory/types
6+
*
7+
* @description
8+
* Create a new inventory type
9+
*/
10+
router.post('/', async (req, res) => {
11+
const sql = 'INSERT INTO inventory_type (text, description) VALUES (?, ?);';
12+
const row = await db.exec(sql, [req.body.text, req.body.description]);
13+
res.status(201).json({ id : row.insertId });
14+
});
15+
16+
/**
17+
* GET /inventory/types
18+
*
19+
* @description
20+
* Get the list of inventory types
21+
*/
22+
router.get('/', async (req, res) => {
23+
const rows = await getTypes();
24+
res.status(200).json(rows);
25+
});
26+
27+
/**
28+
* GET /inventory/types/:id
29+
*
30+
* @description
31+
* Get the specific type.
32+
*/
33+
router.get('/:id', async (req, res) => {
34+
const rows = await getTypes(req.params.id);
35+
res.status(200).json(rows);
36+
});
37+
38+
/**
39+
* PUT /inventory/types/:id
40+
*
41+
* @description
42+
* Update the specific inventory type identified by id.
43+
*/
44+
router.put('/:id', async (req, res) => {
45+
const sql = 'UPDATE inventory_type SET ? WHERE id = ?;';
46+
await db.exec(sql, [req.body, req.params.id]);
47+
const rows = await getTypes(req.params.id);
48+
res.status(200).json(rows);
49+
});
50+
51+
/**
52+
* DELETE /inventory/types/:id
53+
*
54+
* @description
55+
* Delete an inventory type.
56+
*/
57+
router.delete('/:id', async (req, res) => {
58+
const sql = 'DELETE FROM inventory_type WHERE id = ?;';
59+
await db.exec(sql, [req.params.id]);
60+
res.sendStatus(204);
61+
});
62+
63+
/**
64+
* Get list of inventory types
65+
* @param {string} id the type id is optional
66+
*/
67+
function getTypes(id) {
68+
const sql = `SELECT id, text, description, is_predefined FROM inventory_type ${id ? ' WHERE id = ?' : ''};`;
69+
return db.exec(sql, [id]);
70+
}
71+
72+
module.exports = router;

0 commit comments

Comments
 (0)