Last active
February 4, 2019 12:00
-
-
Save Jojoooo1/6effd63099c1701c9d8b5104fa63f5b6 to your computer and use it in GitHub Desktop.
code-1.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import uuidv4 from 'uuid/v4'; | |
// import { performance } from 'perf_hooks'; | |
import { generateCodigo } from '../utils'; | |
export const solicitarCodigo = async (stub, args) => { | |
// const t0 = performance.now(); | |
let data; | |
const codigoPutStatePromises = []; | |
// 1. Parses JSON stringified request | |
try { | |
data = JSON.parse(args.toString()); | |
} catch (err) { | |
throw new Error('Não foi possivel decodificar o JSON, por favor verifique o formato'); | |
} | |
console.info('--- start solicitarCodigo ---'); | |
// 2. Verifies mandatory fields was passed | |
if (!(data.embarcador && data.quantidade)) { | |
throw new Error('Por favor preenche campo mandatorio embarcador/quantidade'); | |
} | |
// 3. Verifies embarcador value | |
const embarcadorSelected = data.embarcador.toLowerCase(); | |
const embarcadorAvailable = ['b2w', 'mercado livre', 'magazine luiza', 'via varejo', 'privalia']; | |
if (!embarcadorAvailable.includes(embarcadorSelected)) { | |
throw new Error('Embarcador nao disponivel'); | |
} | |
// 4. get MSPID of transaction proposer | |
const organizationMSPID = stub.getCreator().mspid; | |
// 5. Creates batch | |
const batch = { | |
docType: 'batch', | |
embarcador: embarcadorSelected, | |
id: uuidv4(), | |
codigos: [], | |
quantitade: data.quantitade, | |
organization: organizationMSPID | |
}; | |
// 6. Creates tracking code | |
for (let i = 0; i < data.quantidade; i += 1) { | |
let codigo; | |
while (true) { | |
codigo = { | |
batchId: batch.id, | |
docType: 'codigo', | |
id: generateCodigo(), | |
transportador: '', | |
embarcador: embarcadorSelected, | |
rota: '', | |
servico: '', | |
servico_codigo: '', | |
usado: false | |
// status: 1 | |
}; | |
// 6.1. Verifies if codigo already exist | |
/* eslint-disable no-await-in-loop */ | |
const newCodigoAsBytes = await stub.getState(codigo.id); | |
// 6.2. Loop again and generate a new code if codigo already exist | |
if (!newCodigoAsBytes.toString()) { | |
break; | |
} | |
} | |
// 6.3. Pushes codigo id into batch | |
batch.codigos.push(codigo.id); | |
// 6.4. Pushes codigo into ledger | |
codigoPutStatePromises.push(stub.putState(codigo.id, Buffer.from(JSON.stringify(codigo)))); | |
} | |
// 7. Waits for all codigo putState promises to terminate | |
await Promise.all(codigoPutStatePromises); | |
// 8. Pushes batch into ledger | |
const batchAsBytes = Buffer.from(JSON.stringify(batch)); | |
await stub.putState(batch.id, batchAsBytes); | |
// 8. Creates event | |
stub.setEvent('batchCreated', batchAsBytes); | |
console.info('=================='); | |
console.log(batch); | |
console.info('=================='); | |
console.info('--- end create solicitarCodigo ---'); | |
// const t1 = performance.now(); | |
// console.log(`Call took ${t1 - t0} ms.`); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment