Skip to content

Commit

Permalink
Finished product edit route. Added weight to fields when returning al…
Browse files Browse the repository at this point in the history
…l products
  • Loading branch information
alehuo committed May 9, 2018
1 parent 1aaa902 commit 6097305
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/db/productStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ module.exports.findAll = () => {
'RVITEM.itemid',
'RVITEM.descr',
'RVITEM.pgrpid',
'RVITEM.weight',
'PRICE.barcode',
'PRICE.buyprice',
'PRICE.sellprice'
Expand Down
27 changes: 25 additions & 2 deletions src/routes/admin/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ router.put('/product/:productId(\\d+)', async (req, res) => {
product.descr = req.body.descr ? req.body.descr : product.descr;
product.weight = req.body.weight ? req.body.weight : product.weight;

product.buyprice = req.body.buyprice
? req.body.buyprice
: product.buyprice;

product.sellprice = req.body.sellprice
? req.body.sellprice
: product.sellprice;

product.count = req.body.quantity ? req.body.quantity : product.count;

// Validate weight
if (req.body.weight && req.body.weight < 0) {
errors.push('Weight can\'t be negative');
Expand All @@ -59,24 +69,36 @@ router.put('/product/:productId(\\d+)', async (req, res) => {
return res.status(400).json({ errors });
}

// Basic product info
const result = await productStore.updateProduct({
id: product.itemid,
id: req.params.productId,
name: product.descr,
group: product.pgrpid,
weight: product.weight,
userid: req.rvuser.userid
});

// sellprice, buyprice, quantity
const result2 = await productStore.changeProductStock(
product.itemid,
product.buyprice,
product.sellprice,
product.count,
req.rvuser.userid
);

const newProd = await productStore.findById(product.itemid);
return res.status(200).json(prodFilter(newProd));
} catch (error) {
logger.error('Error at %s: %s', req.baseUrl + req.path, error.stack);
logger.error('Error at ' + req.baseUrl + req.path + ': ' + error.stack);
return res.status(500).json({ error: 'Internal server error' });
}
});

router.get('/', async (req, res) => {
try {
var products = await productStore.findAll();

const prods = products.map(product => {
return {
product_id: product.itemid,
Expand All @@ -85,6 +107,7 @@ router.get('/', async (req, res) => {
product_group: product.pgrpid,
buyprice: product.buyprice,
sellprice: product.sellprice,
product_weight: parseInt(product.weight || 0),
quantity: parseInt(product.quantity || 0)
};
});
Expand Down
1 change: 1 addition & 0 deletions src/routes/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ router.get('/', async (req, res) => {
product_group: product.pgrpid,
buyprice: product.buyprice,
sellprice: product.sellprice,
weight: product.weight,
quantity: parseInt(product.quantity || 0)
};
});
Expand Down
30 changes: 29 additions & 1 deletion test/admin/routes.adminproducts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('routes: admin products', () => {
.request(server)
.get('/api/v1/admin/products/product/1816')
.set('Authorization', 'Bearer ' + token)
.end((err,res) => {
.end((err, res) => {
should.exist(res.body.itemid);
should.exist(res.body.pgrpid);
should.exist(res.body.descr);
Expand All @@ -80,6 +80,34 @@ describe('routes: admin products', () => {
});
});

it('admins should be able to get a product that exists', done => {
chai
.request(server)
.put('/api/v1/admin/products/product/1816')
.set('Authorization', 'Bearer ' + token)
.send({
pgrpid: 3,
quantity: 450,
buyprice: 120,
sellprice: 200,
weight: 555
})
.end((err, res) => {
should.exist(res.body.itemid);
should.exist(res.body.pgrpid);
should.exist(res.body.count);
should.exist(res.body.sellprice);
should.exist(res.body.buyprice);
should.exist(res.body.weight);
res.body.pgrpid.should.equal(3);
res.body.buyprice.should.equal(120);
res.body.sellprice.should.equal(200);
res.body.count.should.equal(450);
res.body.weight.should.equal(555);
done();
});
});

it('Requesting product with existing barcode', async () => {
return chai
.request(server)
Expand Down

0 comments on commit 6097305

Please sign in to comment.