Skip to content

Commit

Permalink
Fix and save conflict with branch master
Browse files Browse the repository at this point in the history
  • Loading branch information
lomamech committed Apr 6, 2021
1 parent 3234ce4 commit cac2378
Show file tree
Hide file tree
Showing 3 changed files with 259 additions and 16 deletions.
25 changes: 14 additions & 11 deletions server/controllers/stock/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ function createIntegration(req, res, next) {
async function createInventoryAdjustment(req, res, next) {
try {
const movement = req.body;
console.log('MOUVEMEMNTTTTTTTTTTTTTTTTTTTTZ');
console.log(movement);

let filteredInvalidData = [];

const paramsStock = {
Expand Down Expand Up @@ -1100,6 +1103,17 @@ async function createAggregatedConsumption(req, res, next) {
average_consumption_algo : req.session.stock_settings.average_consumption_algo,
};

if (!movement.depot_uuid) {
throw new Error('No defined depot');
}

const checkInvalid = movement.lots
.filter(l => ((l.quantity_consumed + l.quantity_lost) > l.oldQuantity));

if (checkInvalid.length) {
throw new Error('Invalid data! Some lots have consumed or lost more stock than they originally had.');
}

const stockAvailable = await core.getLotsDepot(null, paramsStock);

movement.lots.forEach(lot => {
Expand All @@ -1126,17 +1140,6 @@ async function createAggregatedConsumption(req, res, next) {
);
}

if (!movement.depot_uuid) {
throw new Error('No defined depot');
}

const checkInvalid = movement.lots
.filter(l => ((l.quantity_consumed + l.quantity_lost) > l.oldQuantity));

if (checkInvalid.length) {
throw new Error('Invalid data! Some lots have consumed or lost more stock than they originally had.');
}

// Here we want that the detailed consumption can only concern the periods when there is out of stock
movement.lots.forEach(lot => {
if (movement.stock_out[lot.inventory_uuid] === 0) {
Expand Down
225 changes: 221 additions & 4 deletions test/integration/stock/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ const movementOutPatient = {
lots : [
{
inventory_uuid : helpers.data.QUININE,
uuid : 'ae735e99-8faf-417b-aa63-9b404fca99ac', // QUININE-A
uuid : 'AE735E998FAF417BAA639B404FCA99AC', // QUININE-A
quantity : 20,
unit_cost : 1.5,
}, {
inventory_uuid : helpers.data.MULTIVITAMINE,
uuid : '064ab1d9-5246-4402-ae8a-958fcdb07b35', // VITAMINE-A
uuid : '064AB1D952464402AE8A958FCDB07B35', // VITAMINE-A
quantity : 10,
unit_cost : 2,
},
Expand All @@ -134,12 +134,12 @@ const movementDepot = {
lots : [
{
inventory_uuid : helpers.data.QUININE,
uuid : '6f80748b-1d94-4247-804e-d4be99e827d2', // QUININE-B
uuid : '6F80748B1D944247804ED4BE99E827D2', // QUININE-B
quantity : 50,
unit_cost : 1.5,
}, {
inventory_uuid : helpers.data.MULTIVITAMINE,
uuid : '064ab1d9-5246-4402-ae8a-958fcdb07b35', // VITAMINE-A
uuid : '064AB1D952464402AE8A958FCDB07B35', // VITAMINE-A
quantity : 10,
unit_cost : 2,
},
Expand Down Expand Up @@ -456,6 +456,160 @@ const movementComplexeAggregate = {
],
};

const movementOverConsumptionAggregate = {
depot_uuid : 'BD4B14524742E4FAA128246814140877',
date : getLastDaysComplexe,
description : 'Aggregated consumption for December',
is_exit : 0,
flux_id : 16,
user_id : 1,
stock_out : {
'43F3DECBFCE9426E940AB2150E62186C' : 0,
'43F3DECBFCE9940A426EE62186BC2150' : 20,
},
fiscal_id : 6,
period_id : `${getMovementYearComplexe}${formatedMonth}`,
lots : [
{
inventory_uuid : '43F3DECBFCE9426E940AB2150E62186C',
code : 'DDIS_IODP1S2_0',
label : 'lot 1',
quantity : 750,
unit_cost : 1.46,
expiration_date : '2023-02-07T23:00:00.000Z',
lots : [],
isValid : null,
id : 0,
uuid : 'E36AFF4F99C244A897B770E34A21E658',
text : 'Polyvidone iodée, 10%, 200ml, flacon, Unité',
_initialised : true,
old_quantity : 750,
quantity_consumed : 500,
quantity_lost : 250,
detailed : [
{
start_date : moment(new Date(getLastDaysComplexe), 'YYYY-MM-DD').subtract(30, 'days'),
end_date : moment(new Date(getLastDaysComplexe), 'YYYY-MM-DD').subtract(28, 'days'),
unit_cost : null,
quantity_consumed : 0,
quantity_lost : 95,
isInvalid : false,
isValid : true,
identifier : '733aae8a-9d50-4876-a595-7c4aa83717dd',
_error : null,
},
{
start_date : moment(new Date(getLastDaysComplexe), 'YYYY-MM-DD').subtract(22, 'days'),
end_date : moment(new Date(getLastDaysComplexe), 'YYYY-MM-DD').subtract(20, 'days'),
unit_cost : null,
quantity_consumed : 0,
quantity_lost : 155,
isInvalid : false,
isValid : true,
identifier : '4afb3aac-f711-418b-9550-453dc0b966d7',
_error : null,
},
{
start_date : moment(new Date(getLastDaysComplexe), 'YYYY-MM-DD').subtract(16, 'days'),
end_date : moment(new Date(getLastDaysComplexe), 'YYYY-MM-DD').subtract(14, 'days'),
unit_cost : null,
quantity_consumed : 225,
quantity_lost : 0,
isInvalid : false,
isValid : true,
identifier : '9e87fa50-dc9c-4caa-8f3a-e3ad8032f9b3',
_error : null,
},
{
start_date : moment(new Date(getLastDaysComplexe), 'YYYY-MM-DD').subtract(7, 'days'),
end_date : moment(new Date(getLastDaysComplexe), 'YYYY-MM-DD').subtract(5, 'days'),
unit_cost : null,
quantity_consumed : 125,
quantity_lost : 0,
isInvalid : false,
isValid : true,
identifier : '5671481b-dbba-4001-877e-58b20fd664da',
_error : null,
},
{
start_date : moment(new Date(getLastDaysComplexe), 'YYYY-MM-DD').subtract(4, 'days'),
end_date : moment(new Date(getLastDaysComplexe), 'YYYY-MM-DD').subtract(2, 'days'),
unit_cost : null,
quantity_consumed : 150,
quantity_lost : 0,
isInvalid : false,
isValid : true,
identifier : 'd3ad7fc4-b0d5-40ad-8182-02d306700c79',
_error : null,
},
],
oldQuantity : 750,
},
{
inventory_uuid : '43F3DECBFCE9426E940AB2150E62186C',
code : 'DDIS_IODP1S2_0',
label : 'lot 2',
quantity : 750,
unit_cost : 1.46,
expiration_date : '2023-02-07T23:00:00.000Z',
lots : [],
isValid : null,
id : 1,
uuid : 'ACAA9876EF834D9F84E1BB7C2AF22777',
text : 'Polyvidone iodée, 10%, 200ml, flacon, Unité',
_initialised : true,
old_quantity : 750,
quantity_consumed : 500,
quantity_lost : 250,
detailed : [
{
start_date : moment(new Date(getLastDaysComplexe), 'YYYY-MM-DD').subtract(14, 'days'),
end_date : moment(new Date(getLastDaysComplexe), 'YYYY-MM-DD').subtract(12, 'days'),
unit_cost : null,
quantity_consumed : 125,
quantity_lost : 0,
isInvalid : false,
isValid : true,
identifier : '5671481b-dbba-4001-877e-58b20fd664da',
_error : null,
},
{
start_date : moment(new Date(getLastDaysComplexe), 'YYYY-MM-DD').subtract(6, 'days'),
end_date : moment(new Date(getLastDaysComplexe), 'YYYY-MM-DD').subtract(4, 'days'),
unit_cost : null,
quantity_consumed : 375,
quantity_lost : 250,
isInvalid : false,
isValid : true,
identifier : 'd3ad7fc4-b0d5-40ad-8182-02d306700c79',
_error : null,
},
],
start_date : '2020-11-30T23:00:00.000Z',
end_date : '2020-12-30T23:00:00.000Z',
oldQuantity : 750,
},
{
inventory_uuid : '43F3DECBFCE9940A426EE62186BC2150',
code : 'DDIS_IODP1S2_5',
label : 'PL1',
quantity : 600,
unit_cost : 9.51,
expiration_date : '2023-02-07T23:00:00.000Z',
lots : [],
isValid : null,
id : 2,
uuid : 'D080D354417D47F18E8B1561E98823D9',
text : 'Polyvidone iodée, 10%, 500ml, flacon, Unité',
_initialised : true,
old_quantity : 600,
quantity_consumed : 550,
quantity_lost : 50,
oldQuantity : 600,
},
],
};

const invalidAggregateMovement = {
depot_uuid : 'F9CAEB16168443C5A6C447DBAC1DF296',
date : getLastDays,
Expand Down Expand Up @@ -486,6 +640,67 @@ const invalidAggregateMovement = {
],
};

const adjustmentPrevention = {
depot_uuid : 'BD4B14524742E4FAA128246814140877',
date : moment(new Date(), 'YYYY-MM-DD').subtract(60, 'days'),
description : 'Adjustement',
is_exit : 0,
flux_id : 15,
user_id : 1,
lots : [
{
inventory_uuid : '43F3DECBFCE9426E940AB2150E62186C',
code : 'DDIS_IODP1S2_0',
label : 'lot 1',
quantity : 400,
unit_cost : 1.46,
lots : [],
isValid : null,
id : 0,
uuid : 'E36AFF4F99C244A897B770E34A21E658',
text : 'Polyvidone iodée, 10%, 200ml, flacon, Unité',
_initialised : true,
old_quantity : 1000,
isExpired : false,
oldQuantity : 1000,
quantityAvailable : 1000,
},
{
inventory_uuid : '43F3DECBFCE9426E940AB2150E62186C',
code : 'DDIS_IODP1S2_0',
label : 'lot 2',
quantity : 600,
unit_cost : 1.46,
lots : [],
isValid : null,
id : 1,
uuid : 'ACAA9876EF834D9F84E1BB7C2AF22777',
text : 'Polyvidone iodée, 10%, 200ml, flacon, Unité',
_initialised : true,
old_quantity : 1000,
isExpired : false,
oldQuantity : 1000,
quantityAvailable : 1000,
},
{
inventory_uuid : '43F3DECBFCE9940A426EE62186BC2150',
code : 'DDIS_IODP1S2_5',
label : 'PL1',
quantity : 400,
unit_cost : 9.51,
lots : [],
isValid : null,
id : 2,
uuid : 'D080D354417D47F18E8B1561E98823D9',
text : 'Polyvidone iodée, 10%, 500ml, flacon, Unité',
_initialised : true,
old_quantity : 1000,
isExpired : false,
oldQuantity : 1000,
quantityAvailable : 1000,
}],
};

module.exports = {
movementFirstLots,
movementFromDonation,
Expand All @@ -507,4 +722,6 @@ module.exports = {
movementStandardAggregate,
movementComplexeAggregate,
invalidAggregateMovement,
movementOverConsumptionAggregate,
adjustmentPrevention,
};
25 changes: 24 additions & 1 deletion test/integration/stock/stock.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* global expect, agent */

const moment = require('moment');
const helpers = require('../helpers');
const shared = require('./shared');

Expand Down Expand Up @@ -290,4 +290,27 @@ describe('(/stock/) The Stock HTTP API', () => {
.query({ voucherReference : 'VO.TPA.300000' });
helpers.api.listed(res, 0);
});

// Prevent negative stock quantities
it(`POST /stock/lots/movements Prevent negative stock quantities
when distribute lots to patients from a depot`, async () => {

shared.movementOutPatient.date = moment(new Date(), 'YYYY-MM-DD').subtract(1, 'days');

const res = await agent.post('/stock/lots/movements').send(shared.movementOutPatient);
expect(res).to.have.status(400);
});

it(`POST /stock/aggregated_consumption movements Prevent negative stock quantities
create complexe aggregate stock consumption`, async () => {
const res = await agent.post('/stock/aggregated_consumption').send(shared.movementOverConsumptionAggregate);
expect(res).to.have.status(400);
});

it(`POST /stock/inventory_adjustment movements Prevent negative stock quantities
create complexe aggregate stock consumption`, async () => {
const res = await agent.post('/stock/inventory_adjustment').send(shared.adjustmentPrevention);
expect(res).to.have.status(400);
});

});

0 comments on commit cac2378

Please sign in to comment.