Skip to content

Commit

Permalink
#119 - wsCreateStuff --scenario=dfm provisions and sells 20 Alpidrin …
Browse files Browse the repository at this point in the history
…boxes
  • Loading branch information
joaoluis-pdm committed Sep 1, 2022
1 parent bc4811c commit 075d02c
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 8 deletions.
4 changes: 4 additions & 0 deletions bin/environment/batches/batchesRandom.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const getBatches = (batchCount = 11, quantity = 100, expiryOffset = 100, randomi
.map(n => genBatch(quantity, expiryOffset, randomize, 50));
}

const getBatchesForDfmDemo2 = (batchCount = 11, quantity = 100, expiryOffset = 100, randomize = true) => {
return Array.from(new Array(Math.floor((randomize ? Math.random() : 1) * batchCount) + (randomize? 1 : 0)).keys())
.map(n => genBatch(quantity, expiryOffset, randomize, 50));
}


module.exports = getBatches;
28 changes: 27 additions & 1 deletion bin/environment/batches/batchesTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,31 @@ const getSanofiBatches = function(){
return fillBatchDetails(SANOFI_BATCHES);
}

const DFM_BATCHES1 = {
'02113100000028': [ // Alpidrin
{
batchNumber: "ALPV1",
quantity: 20,
expiry: "2025/12/31",
serialNumbers: [
'93342708522623', '57142948840311', '84658726199147',
'37492904539888', '96562748897283', '54264257431848',
'03030164326594', '34366933576258', '26539110197947',
'95038364859689', '03690127622893', '75101018014992',
'01940652032034', '33116805950363', '32559689814488',
'62282353911016', '78477001947639', '48457736116806',
'94752993902481', '77150980360804'
]
}
]
}

const getDfmBatches1ForGtin = function(gtin){
const dfmBatches1 = DFM_BATCHES1[gtin];
//console.log(dfmBatches1);
return dfmBatches1;
}

module.exports = {
getMSDBatches,
getPfizerBatches,
Expand All @@ -366,5 +391,6 @@ module.exports = {
getNovoNordiskBatches,
getGskBatches,
getTakedaBatches,
getSanofiBatches
getSanofiBatches,
getDfmBatches1ForGtin
}
18 changes: 17 additions & 1 deletion bin/environment/products/productsTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,21 @@ const getSanofiProducts = function(manufName = "MAH196261986"){
return mergeManufInfo(SanofiProducts, manufName);
}


const DfmProducts = [
{
gtin: '02113100000028',
name: 'Alpidrin',
description: 'DFM 59k3 demo product'
}
]

const getDfmProducts1 = function(manufName = "MAH251339219"){ // manufName is Bayer
const dfmProducts1 = mergeManufInfo(DfmProducts, manufName);
//console.log(dfmProducts1);
return dfmProducts1;
}

module.exports = {
getPfizerProducts,
getMSDProducts,
Expand All @@ -190,5 +205,6 @@ module.exports = {
getNovoNordiskProducts,
getGskProducts,
getTakedaProducts,
getSanofiProducts
getSanofiProducts,
getDfmProducts1
}
142 changes: 136 additions & 6 deletions bin/environment/wsCreateStuff.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const MAH_MSD = credentials.MSD;
const MAH_ROCHE = credentials.ROCHE;
// #106 test one participant with credentials overridden by environment on docker-compose.yml
// MAH_ROCHE.id.secret="MAH0000000";
const MAH_BAYER = credentials.BAYER;
const WSH1 = require("../../docker/api/env/whs-1.json");
const WSH2 = require("../../docker/api/env/whs-2.json");
const PHA1 = require("../../docker/api/env/pha-1.json");
Expand All @@ -50,8 +51,10 @@ const MY_SALES = []; // array of data returned by /sale/create

let SLEEP_MS = 2000;

//const products = require('./products/productsTests');
const getBatches = require('./batches/batchesRandom');
const PRODUCTS_TEST = require('./products/productsTests');
const BATCHES_TEST = require('./batches/batchesTests');
const BATCHES_RANDOM = require('./batches/batchesRandom');



class ProductsEnum {
Expand Down Expand Up @@ -86,6 +89,12 @@ class ReceiptsEnum {
static test = "test";
};

class ScenarioEnum {
static single = "single";
static default = "default";
static dfm = "dfm"; // dfm is acdc
};

function genShipmentId(simpleShipment, identityId) {
let shipmentId;
const splitShipmentId = `${simpleShipment.shipmentId}`.split('-');
Expand Down Expand Up @@ -115,6 +124,7 @@ const defaultOps = {
sales: SalesEnum.test,
traceability: TraceabilityEnum.test,
receipts: ReceiptsEnum.test,
scenario: ScenarioEnum.default,
sleep: "2000"
}

Expand All @@ -139,6 +149,12 @@ if (process.argv.includes("--help")
console.log("\t--receipts=none|test*");
console.log("\t--traceability=none|test*");
console.log();
console.log("\t--scenario=single|default*|dfm The single scenario only creates products and batches");
console.log("\t\tfor a single specifc MAH. The default scenario will create products, batches,");
console.log("\t\tshipments, sales, receipts, more shipments, more sales, more receipts,");
console.log("\t\tthen quarantine one batch, and then recall one batch. The dfm scenario");
console.log("\t\tcreates some specific producs, batches, shipments and sales for a DFM demo.");
console.log();
console.log("* - is the default setting");
process.exit(0);
}
Expand Down Expand Up @@ -330,6 +346,7 @@ const productCreate = async function (conf, actor, product) {
};

const productsCreate = async function (conf, actor) {
// scenario default
if (conf.products === ProductsEnum.none) {
return; // don't create products
}
Expand Down Expand Up @@ -402,7 +419,7 @@ const batchesCreateTest = async function (conf, actor) {
const batchesCreateRandom = async function (conf, actor) {
for (const product of actor.products) {
const gtin = product.gtin;
const batches = getBatches(); // create a new array of batches with random batchNumber and random serial numbers
const batches = BATCHES_RANDOM.getBatches(); // create a new array of batches with random batchNumber and random serial numbers
//console.log("new batches", batches);
for (const batch of batches) {
await batchCreate(conf, actor, gtin, batch);
Expand Down Expand Up @@ -570,14 +587,65 @@ const shipmentCreateAndDeliver = async function(conf, sender, receiver, shipment
return resUConfirmed;
};


const shipmentsCreateTest = async function (conf, sender) {
if (conf.scenario==ScenarioEnum.dfm) {
return shipmentsCreateTestDfm(conf, sender);
}
return shipmentsCreateTestDefault(conf, sender);
}

const shipmentsCreateTestDfm = async function (conf, sender) {
if (!sender.id.secret.startsWith("MAH")) {
throw new Error("shipmentsCreateTestDfm can only send for an MAH");
}

const whs = WSH1;
const pha = PHA1;

const gtin1 = sender.products[0].gtin;
const batch1 = sender.batches[gtin1][0];
const batchNumber1 = batch1.batchNumber;
const quantity1 = batch1.quantity;

const shipment1MahToWhs = {
"orderId": whs.id.secret + "-" + (new Date()).toISOString(),
"requesterId": whs.id.secret,
"shipmentLines": [
{
"gtin": gtin1,
"batch": batchNumber1,
"quantity": quantity1
}
]
};

await shipmentCreateAndDeliver(conf, sender, whs, shipment1MahToWhs);

const shipment2WhsToPha = {
"orderId": pha.id.secret + "-" + (new Date()).toISOString(),
"requesterId": pha.id.secret,
"shipmentLines": [
{
"gtin": gtin1,
"batch": batchNumber1,
"quantity": quantity1
}
]
};

const resShipToPha = await shipmentCreateAndDeliver(conf, whs, pha, shipment2WhsToPha);
SHIPMENTS_ON_PHA.push(resShipToPha);
}

const shipmentsCreateTestDefault = async function (conf, sender) {
if (sender.id.secret != MAH_MSD.id.secret) {
throw new Error("shipmentsCreateTest can only sell for MSD for now");
}

const whs = WSH1;
const pha = PHA1;

const shipment1MsdToWhs = {
"orderId": whs.id.secret + "-" + (new Date()).toISOString(),
"requesterId": whs.id.secret,
Expand Down Expand Up @@ -711,6 +779,56 @@ const salesCreateTest = async function (conf, manufActor, sellerActor, mySales)


const salesCreateTest = async function (conf, manufActor, sellerActor, mySales) {
if (conf.scenario==ScenarioEnum.dfm) {
return salesCreateTestDfm(conf, manufActor, sellerActor, mySales);
}
return salesCreateTestDefault(conf, manufActor, sellerActor, mySales);
}

const salesCreateTestDfm = async function (conf, manufActor, sellerActor, mySales) {
if (!SHIPMENTS_ON_PHA.length) {
throw new Error("No shipments on pharmacies on this run!");
}
if (!SHIPMENTS_ON_PHA[0].shipmentLines.length) {
throw new Error("No shipmentLines on pharmacies on this run!");
}
const shipmentLine0 = SHIPMENTS_ON_PHA[0].shipmentLines[0];
const gtin = shipmentLine0.gtin;
const batchNumber = shipmentLine0.batch;
const batch = findMahOriginalBatch(gtin, batchNumber);
//console.log("batch", batch);

let i=0;
while (i<batch.quantity) { // sell all the batch
const saleSerialNumber = batch.serialNumbers[i];
const saleData = { // see body of http://swagger-pha1.localhost:8080/#/sale/post_sale_create
"id": sellerActor.id.secret + "-" + (new Date()).toISOString(),
"productList": [
{
"gtin": gtin,
"batchNumber": batch.batchNumber,
"serialNumber": saleSerialNumber
}
]
};

const resSale = await jsonPost(conf, sellerActor, {
path: `/traceability/sale/create`,
body: saleData
});
//console.log("Sale", resSale);
if (!resSale || !resSale.productList) {
throw new Error("sale/create "+batch.batchNumber+" at "+sellerActor.id.secret+" reply has no productList: "+JSON.stringify(resSale));
}
mySales.push(resSale);

await sleep(SLEEP_MS);

i++;
}
}

const salesCreateTestDefault = async function (conf, manufActor, sellerActor, mySales) {
if (!SHIPMENTS_ON_PHA.length) {
throw new Error("No shipments on pharmacies on this run!");
}
Expand Down Expand Up @@ -829,11 +947,23 @@ process.stdout._handle.setBlocking(true);
//console.log("Credentials", MAHS);
//console.log("Products", products.getPfizerProducts());
//console.log("Batches", MAH_MSD.batches);
if (conf.env == "single") {
if (conf.scenario == ScenarioEnum.single) {
// single only creates products and batches so far
await productsCreate(conf, MAH_ROCHE);
await batchesCreate(conf, MAH_ROCHE);
} else {
} else if (conf.scenario == ScenarioEnum.dfm) {
// replace Bayer's products and batches with DFM products
const mahActor = MAH_BAYER;
mahActor.products = PRODUCTS_TEST.getDfmProducts1(MAH_BAYER.id.secret);
mahActor.batches={};
for(const product of mahActor.products) {
mahActor.batches[product.gtin] = BATCHES_TEST.getDfmBatches1ForGtin(product.gtin);
}
await productsCreate(conf, mahActor);
await batchesCreate(conf, mahActor);
await shipmentsCreate(conf, mahActor);
await salesCreate(conf, mahActor, PHA1, MY_SALES);
} else { // default scenarios
for (const mah of MAHS) {
await productsCreate(conf, mah);
await batchesCreate(conf, mah);
Expand Down

0 comments on commit 075d02c

Please sign in to comment.