Skip to content

Commit

Permalink
fix(server): Recsructure and minimize the number of communication bet…
Browse files Browse the repository at this point in the history
…ween the client and the server
  • Loading branch information
DedrickEnc committed Sep 21, 2017
1 parent d3e12ac commit b901da4
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ SESS_SECRET='XopEn BlowFISH'
SESS_RESAVE=0
SESS_UNSET='destroy'

LOG_LEVEL='warn'
# LOG_LEVEL='debug'
# LOG_LEVEL='warn'
LOG_LEVEL='debug'
UPLOAD_DIR='client/upload'

# this will toggle all MYSQL errors to be reflected to the client.
Expand Down
40 changes: 28 additions & 12 deletions client/src/modules/stock/entry/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ function StockEntryController(
}

// submit integration
function submitIntegration() {
function submitIntegration() {
var movement = {
depot_uuid : vm.depot.uuid,
entity_uuid : null,
Expand All @@ -325,17 +325,33 @@ function StockEntryController(
user_id : Session.user.id,
};

Stock.integration.create({ description : vm.movement.description })
.then(function (uuid) {
movement.lots = processLotsFromStore(vm.Stock.store.data, uuid);
return Stock.stocks.create(movement);
})
.then(function (document) {
vm.Stock.store.clear();
vm.movement = {};
ReceiptModal.stockEntryIntegrationReceipt(document.uuid, bhConstants.flux.FROM_INTEGRATION);
})
.catch(Notify.handleError);
var lots = vm.Stock.store.data.reduce(function (current, previous) {
return previous.lots.map(function (lot) {
return {
label : lot.lot,
initial_quantity : lot.quantity,
quantity : lot.quantity,
unit_cost : previous.unit_cost,
expiration_date : lot.expiration_date,
inventory_uuid : previous.inventory.uuid,
origin_uuid : uuid,
};
}).concat(current);
}, []);

var entry = {
integration : { description : vm.movement.description },
lots : lots,
movement : movement,
}

Stock.integration.create(entry)
.then(function (document) {
vm.Stock.store.clear();
vm.movement = {};
ReceiptModal.stockEntryIntegrationReceipt(document.uuid, bhConstants.flux.FROM_INTEGRATION);
})
.catch(Notify.handleError);
}

// submit donation
Expand Down
61 changes: 53 additions & 8 deletions server/controllers/stock/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,21 +336,66 @@ function listLotsOrigins(req, res, next) {
* create a new integration entry
*/
function createIntegration(req, res, next) {
const transaction = db.transaction();
const params = req.body;
const identifier = uuid.v4();
const documentUuid = uuid.v4();
let commonInfos;

const integration = {
uuid : db.bid(identifier),
project_id : req.session.project.id,
description : params.description || 'INTEGRATION',
description : params.integration.description || 'INTEGRATION',
date : new Date(),
};
const sql = `
INSERT INTO integration SET ?
`;
db.exec(sql, [integration])
.then(() => res.status(200).json(identifier))
.catch(next)
.done();
const sql = `INSERT INTO integration SET ?`;

transaction.addQuery(sql, [integration]);

params.lots.forEach((lot) => {
let lotUuid = uuid.v4();

// adding a lot insertion query into the transaction
transaction.addQuery(`INSERT INTO lot SET ?`, {
uuid : lotUuid,
label : lot.label,
initial_quantity : lot.quantity,
quantity : lot.quantity,
unit_cost : lot.unit_cost,
expiration_date : new Date(lot.expiration_date),
inventory_uuid : db.bid(lot.inventory_uuid),
origin_uuid : db.bid(lot.origin_uuid),
delay : 0,
});

// adding a movement insertion query into the transaction
transaction.addQuery(`INSERT INTO stock_movement SET ?`, {
uuid : db.bid(uuid.v4()),
lot_uuid : lotUuid,
depot_uuid : db.bid(params.movement.depot_uuid),
document_uuid : db.bid(documentUuid),
flux_id : params.movement.flux_id,
date : params.movement.date,
quantity : lot.quantity,
unit_cost : lot.unit_cost,
is_exit : 0,
user_id : params.movement.user_id,
});

// An arry of common info, to send to the store procedure in order to insert to the posting journal
commonInfos = [ db.bid(documentUuid), params.movement.date, req.session.enterprise.id, req.session.project.id, req.session.enterprise.currency_id, req.session.user.id ];

// writting all records relative to the movement in the posting journal table
transaction.addQuery('CALL PostPurchase(?)', [commonInfos]);
});

// execute all operations as one transaction
transaction.execute()
.then(() => {
res.status(201).json({ uuid : documentUuid });
})
.catch(next)
.done();
}

/**
Expand Down

0 comments on commit b901da4

Please sign in to comment.