Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 46.22 KB | None | 0 0
  1. /*
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. */
  14.  
  15. /* global getAssetRegistry getFactory emit */
  16.  
  17. /**
  18. * Sample transaction processor function.
  19. * @param {org.doptrace.setupDemo} setupDemo The sample transaction instance.
  20. * @transaction
  21. */
  22.  
  23. function setupDemo(setupDemo) {
  24.  
  25. var factory = getFactory();
  26. var NS = 'org.doptrace';
  27.  
  28. /**
  29. * VALUE CHAIN OPERATORS
  30. */
  31.  
  32. // criar Producer
  33. var producer = factory.newResource(NS, 'Producer', 'PROD_001');
  34. producer.phoneNumber = "9444444444";
  35. producer.email = "producer@email.com";
  36. producer.street = "Rua do prod";
  37. producer.zip = "4900-200";
  38. producer.city = "Viana do Castelo";
  39. producer.country = "Portugal";
  40. producer.name = "Quinta Vianense";
  41. producer.description = 'Produtores de leite';
  42.  
  43. // criar Industry_Retailer
  44. var industry_retailer = factory.newResource(NS, 'Industry_Retailer', 'IR_001');
  45. industry_retailer.phoneNumber = "955555555";
  46. industry_retailer.email = "industry_retailer@email.com";
  47. industry_retailer.street = "Rua da industra";
  48. industry_retailer.zip = "4900-210";
  49. industry_retailer.city = "Viana do Castelo";
  50. industry_retailer.country = "Portugal";
  51.  
  52. industry_retailer.name = "Milky Way";
  53. industry_retailer.description = 'Industria e retalhe de produtos lacticinios';
  54.  
  55.  
  56. // criar CertificationEntity
  57. var certificationEntity = factory.newResource(NS, 'CertificationEntity', 'CE_001');
  58. certificationEntity.phoneNumber = "966666666";
  59. certificationEntity.email = "certificationEntity@email.com";
  60. certificationEntity.street = "Rua da entidade";
  61. certificationEntity.zip = "4900-212";
  62. certificationEntity.city = "Viana do Castelo";
  63. certificationEntity.country = "Portugal";
  64. certificationEntity.name = "CE";
  65. certificationEntity.description = 'Entidade de certificação de produtos DOP';
  66.  
  67.  
  68. // criar Logistics
  69. var logistics = factory.newResource(NS, 'Logistics', 'L_001');
  70.  
  71. logistics.phoneNumber = "955555555";
  72. logistics.email = "logistics@email.com";
  73.  
  74. logistics.street = "Rua do transporte";
  75. logistics.zip = "4900-100";
  76. logistics.city = "Viana do Castelo";
  77. logistics.country = "Portugal";
  78.  
  79. logistics.name = "Logistica AltoMinho";
  80. logistics.description = 'Companhia de distribuição';
  81.  
  82.  
  83. /**
  84. * PARTICIPANTS
  85. */
  86.  
  87. // criar SysAdmin (luisalves é username [sysadmin id])
  88. var sysAdmin = factory.newResource(NS, 'SysAdmin', 'luisalves');
  89. sysAdmin.phoneNumber = "911111111";
  90. sysAdmin.email = "admin@email.com";
  91. sysAdmin.firstname = "Luis";
  92. sysAdmin.lastname = "Alves";
  93. sysAdmin.password = "123";
  94.  
  95. // criar WorkerAdmin
  96. var workerAdmin = factory.newResource(NS, 'WorkerAdmin', 'tiagocarvalhido');
  97.  
  98. workerAdmin.phoneNumber = "922222222";
  99. workerAdmin.email = "workeradmin@email.com";
  100.  
  101. workerAdmin.firstname = "Tiago";
  102. workerAdmin.lastname = "Carvalhido";
  103. workerAdmin.password = "123"
  104. workerAdmin.operator = factory.newRelationship(NS, 'Producer', 'PROD_001');
  105.  
  106. // criar Worker
  107. var worker = factory.newResource(NS, 'Worker', 'joselima');
  108.  
  109. worker.phoneNumber = "933333333";
  110. worker.email = "worker@email.com";
  111.  
  112. worker.firstname = "Jose";
  113. worker.lastname = "Lima";
  114. worker.password = "123"
  115. worker.operator = factory.newRelationship(NS, 'Producer', 'PROD_001');
  116.  
  117. /**
  118. * EVENTS
  119. */
  120.  
  121. /**
  122. * BATCH
  123. */
  124.  
  125.  
  126. // assets
  127. return getAssetRegistry(NS + '.Producer')
  128. .then(function(producerRegistry) {
  129. return producerRegistry.addAll([producer]);
  130. })
  131. .then(function() {
  132. return getAssetRegistry(NS + '.Industry_Retailer');
  133. })
  134. .then(function(industry_retailerRegistry) {
  135. return industry_retailerRegistry.addAll([industry_retailer]);
  136. })
  137. .then(function() {
  138. return getAssetRegistry(NS + '.CertificationEntity');
  139. })
  140. .then(function(certificationEntityRegistry) {
  141. return certificationEntityRegistry.addAll([certificationEntity]);
  142. })
  143. .then(function() {
  144. return getAssetRegistry(NS + '.Logistics');
  145. })
  146. .then(function(logisticsRegistry) {
  147. return logisticsRegistry.addAll([logistics]);
  148. })
  149. // participants
  150. .then(function() {
  151. return getParticipantRegistry(NS + '.SysAdmin');
  152. })
  153. .then(function(sysAdminRegistry) {
  154. return sysAdminRegistry.addAll([sysAdmin]);
  155. })
  156. .then(function() {
  157. return getParticipantRegistry(NS + '.WorkerAdmin');
  158. })
  159. .then(function(workerAdminRegistry) {
  160. return workerAdminRegistry.addAll([workerAdmin]);
  161. })
  162. .then(function() {
  163. return getParticipantRegistry(NS + '.Worker');
  164. })
  165. .then(function(workerRegistry) {
  166. return workerRegistry.addAll([worker]);
  167. });
  168.  
  169. }
  170.  
  171. /**
  172. * Sample transaction processor function.
  173. * @param {org.doptrace.transactionProductRegistration} productRegistration The sample transaction instance
  174. * @transaction
  175. */
  176.  
  177. function productRegistation(productRegistation) {
  178.  
  179. var array = new Array();
  180.  
  181. //Validação da data de expiração
  182. var now = new Date();
  183.  
  184. var expirationDate = new Date(productRegistation.productRegistration.newBatch.expirationDate);
  185.  
  186.  
  187. return getAssetRegistry('org.doptrace.Producer').then(function(playerRegistry) {
  188. return playerRegistry.exists(productRegistation.productRegistration.operator.getIdentifier())
  189. }).then(function(exists) {
  190.  
  191. return getParticipantRegistry('org.doptrace.Worker').then(function(playerRegistry) {
  192. return playerRegistry.exists(productRegistation.productRegistration.worker.getIdentifier())
  193. }).then(function(existsWorker) {
  194.  
  195. return getAssetRegistry('org.doptrace.Product').then(function(playerRegistry) {
  196.  
  197. return playerRegistry.exists(productRegistation.productRegistration.newBatch.product.getIdentifier())
  198. }).then(function(existsProduct) {
  199.  
  200. if( expirationDate <= now){
  201.  
  202. throw Error('Data de validade não pode ser inferior nem igual à data de criação do Lote');
  203.  
  204.  
  205.  
  206.  
  207. }else if (!exists) {
  208. throw Error('Produtor não registado !');
  209.  
  210. }else if(!existsWorker){
  211.  
  212. throw Error('Worker não registado !');
  213.  
  214. }else if(!existsProduct){
  215.  
  216. throw Error('Produto não registado !');
  217.  
  218. } else if(exists &&existsWorker && existsProduct && productRegistation.productRegistration.newBatch != null) {
  219.  
  220. getAssetRegistry('org.doptrace.ProductRegistration')
  221. .then(function(assetRegistry) {
  222. var factory = getFactory();
  223. var newAsset = factory.newResource(
  224. 'org.doptrace',
  225. 'ProductRegistration',
  226. productRegistation.productRegistration.id);
  227.  
  228. newAsset.description = productRegistation.productRegistration.description;
  229. newAsset.latitude = productRegistation.productRegistration.latitude;
  230. newAsset.longitude = productRegistation.productRegistration.longitude;
  231. newAsset.dateTime = now;
  232. newAsset.worker = productRegistation.productRegistration.worker;
  233. newAsset.operator = productRegistation.productRegistration.operator;
  234. array.push(newAsset);
  235.  
  236.  
  237. return assetRegistry.add(newAsset);
  238.  
  239.  
  240.  
  241. ;
  242. }).then(function(batchRegistry){
  243. getAssetRegistry('org.doptrace.Batch')
  244. .then(function(batchRegistry2) {
  245.  
  246.  
  247.  
  248. var factory = getFactory();
  249.  
  250. var newBatch = factory.newResource(
  251. 'org.doptrace',
  252. 'Batch',
  253. productRegistation.productRegistration.newBatch.id);
  254.  
  255.  
  256.  
  257. newBatch.amount = productRegistation.productRegistration.newBatch.amount;
  258. newBatch.unit = productRegistation.productRegistration.newBatch.unit;
  259. newBatch.creationDate = now;
  260. newBatch.expirationDate = productRegistation.productRegistration.newBatch.expirationDate;
  261. newBatch.state = 'REGISTERED';
  262. newBatch.certificated = false;
  263. newBatch.previousEvents = array
  264. newBatch.previousOperator = null;
  265. newBatch.currentOperator = productRegistation.productRegistration.operator;
  266. newBatch.product = productRegistation.productRegistration.newBatch.product;
  267.  
  268. return batchRegistry2.add(newBatch);
  269.  
  270.  
  271.  
  272.  
  273.  
  274. });;
  275.  
  276. }
  277.  
  278. )}else{
  279. throw Error('Novo Lote null');
  280. }})}
  281. )})}
  282.  
  283.  
  284.  
  285.  
  286. /**
  287. * Sample transaction processor function.
  288. * @param {org.doptrace.transactionQualityAssessement} quality The sample transaction instance
  289. * @transaction
  290. */
  291.  
  292. function quality(quality) {
  293.  
  294. var array = new Array();
  295.  
  296. var now = new Date();
  297.  
  298. var expirationDate = new Date(quality.quality.newBatch.expirationDate);
  299.  
  300.  
  301. return getAssetRegistry('org.doptrace.Industry_Retailer').then(function(playerRegistry) {
  302. return playerRegistry.exists(quality.quality.operator.getIdentifier())
  303. }).then(function(existsIndustry) {
  304.  
  305. return getAssetRegistry('org.doptrace.Batch').then(function(playerRegistry) {
  306.  
  307. return playerRegistry.exists(quality.quality.assessedBatch.getIdentifier())
  308. }).then(async function(existsBatch) {
  309.  
  310. let updateRegistry = await getAssetRegistry('org.doptrace.Batch');
  311.  
  312. if( expirationDate <= now){
  313.  
  314. throw Error('Data de validade não pode ser inferior nem igual à data de criação do Lote');
  315.  
  316.  
  317. }else if (!existsIndustry) {
  318.  
  319. throw Error('Industria não registado !');
  320.  
  321. }else if(!existsBatch){
  322.  
  323. throw Error('Lote não registado !');
  324.  
  325. }else if( existsIndustry && existsBatch && quality.quality.amountDowned == 0.00){
  326.  
  327. getAssetRegistry('org.doptrace.QualityAssessment')
  328. .then(function(assetRegistry) {
  329. var factory = getFactory();
  330. var newAsset = factory.newResource(
  331. 'org.doptrace',
  332. 'QualityAssessment',
  333. quality.quality.id);
  334.  
  335. newAsset.description = quality.quality.description;
  336. newAsset.latitude = quality.quality.latitude;
  337. newAsset.longitude = quality.quality.longitude;
  338. newAsset.dateTime = now;
  339. newAsset.worker = quality.quality.worker;
  340. newAsset.operator = quality.quality.operator;
  341. newAsset.assessedBatch = quality.quality.assessedBatch
  342. newAsset.amountDowned = quality.quality.amountDowned
  343. array.push(newAsset)
  344.  
  345. return assetRegistry.add(newAsset);
  346.  
  347. }).then(function(batchRegistry){
  348. getAssetRegistry('org.doptrace.Batch')
  349. .then(function(assetRegistry) {
  350.  
  351. quality.quality.assessedBatch.currentOperator = quality.quality.operator
  352.  
  353.  
  354. assetRegistry.update(quality.quality.assessedBatch);
  355.  
  356.  
  357. })})}else if(existsBatch && existsIndustry && quality.quality.amountDowned != 0.00 ) {
  358.  
  359. getAssetRegistry('org.doptrace.QualityAssessment')
  360. .then(function(assetRegistry) {
  361. var factory = getFactory();
  362. var newAsset = factory.newResource(
  363. 'org.doptrace',
  364. 'QualityAssessment',
  365. quality.quality.id);
  366.  
  367. newAsset.description = quality.quality.description;
  368. newAsset.latitude = quality.quality.latitude;
  369. newAsset.longitude = quality.quality.longitude;
  370. newAsset.dateTime = now;
  371. newAsset.worker = quality.quality.worker;
  372. newAsset.operator = quality.quality.operator;
  373. newAsset.assessedBatch = quality.quality.assessedBatch
  374. newAsset.amountDowned = quality.quality.amountDowned
  375. array.push(newAsset)
  376.  
  377.  
  378. return assetRegistry.add(newAsset);
  379.  
  380.  
  381.  
  382. ;
  383. }).then(function(batchRegistry){
  384. getAssetRegistry('org.doptrace.Batch')
  385. .then(function(batchRegistry2) {
  386.  
  387.  
  388.  
  389. var factory = getFactory();
  390.  
  391. var newBatch = factory.newResource(
  392. 'org.doptrace',
  393. 'Batch',
  394. quality.quality.newBatch.id);
  395.  
  396. newBatch.amount = quality.quality.assessedBatch.amount - quality.quality.amountDowned;
  397. newBatch.unit = quality.quality.assessedBatch.unit;
  398. newBatch.creationDate = now;
  399. newBatch.expirationDate = expirationDate
  400. newBatch.state = 'REGISTERED'
  401. newBatch.certificated = false
  402. newBatch.previousEvents = quality.quality.assessedBatch.previousEvents.concat(array)
  403.  
  404.  
  405.  
  406. newBatch.previousOperator = quality.quality.assessedBatch.currentOperator
  407.  
  408.  
  409. newBatch.currentOperator = quality.quality.operator;
  410.  
  411. newBatch.product = quality.quality.assessedBatch.product;
  412.  
  413. return batchRegistry2.add(newBatch);
  414.  
  415.  
  416. });;
  417.  
  418.  
  419.  
  420. }
  421.  
  422.  
  423. )}else{
  424. throw Error('Erro na transação');
  425. }
  426. quality.quality.assessedBatch.state = 'DISABLED'
  427.  
  428. updateRegistry.update(quality.quality.assessedBatch);
  429.  
  430. }
  431.  
  432. )})}
  433. /**
  434. * Sample transaction processor function.
  435. * @param {org.doptrace.transactionTransformation} transformation The sample transaction instance
  436. * @transaction
  437. */
  438.  
  439.  
  440. async function transformation(transformation){
  441.  
  442. //Tamanho dos arrays de Lotes e do array de quantidades
  443. var batchLength = transformation.transformation.usedBatches.length;
  444. var amountLength = transformation.transformation.amountPerBatch.length;
  445.  
  446. //Array de Erros para verificação do batch
  447. var errors = new Array();
  448.  
  449. //Array para concatenação de Eventos
  450. var array = new Array();
  451.  
  452. //Variavel para correr os ciclos For
  453. let i;
  454.  
  455. var now = new Date();
  456.  
  457. var expirationDate = new Date(transformation.transformation.newBatch.expirationDate);
  458.  
  459.  
  460.  
  461. //Verificação do Industry Retailer
  462. let registryIndustry = await getAssetRegistry('org.doptrace.Industry_Retailer');
  463.  
  464. let resultIndustry = await registryIndustry.exists(transformation.transformation.operator.getIdentifier());
  465.  
  466. if(!resultIndustry){
  467.  
  468. throw Error('Industria nao registada !')
  469.  
  470. }else if(transformation.transformation.usedBatches.length <= 0){
  471.  
  472. throw Error('Preencha os lotes usados para transformação !')
  473.  
  474. }
  475. else if( expirationDate <= now){
  476.  
  477. throw Error('Data de validade não pode ser inferior nem igual à data de criação do Lote');
  478.  
  479. }
  480.  
  481. //Verificação dos lotes usados
  482. for( i=0 ;i<batchLength;i++) {
  483.  
  484. let registryBatch = await getAssetRegistry('org.doptrace.Batch');
  485.  
  486.  
  487. let resultBatch = await registryBatch.exists(transformation.transformation.usedBatches[i].getIdentifier());
  488.  
  489. if(resultIndustry && resultBatch && transformation.transformation.usedBatches != null){
  490.  
  491. let transformRegistry = await getAssetRegistry('org.doptrace.Transformation');
  492.  
  493. var factory = getFactory();
  494.  
  495. var newAsset = factory.newResource(
  496. 'org.doptrace',
  497. 'Transformation',
  498. transformation.transformation.id);
  499.  
  500. newAsset.description = transformation.transformation.description;
  501. newAsset.latitude = transformation.transformation.latitude;
  502. newAsset.longitude = transformation.transformation.longitude;
  503. newAsset.dateTime = now
  504. newAsset.worker = transformation.transformation.worker;
  505. newAsset.processes = transformation.transformation.processes;
  506. newAsset.operator = transformation.transformation.operator
  507. newAsset.usedBatches = transformation.transformation.usedBatches;
  508. newAsset.amountPerBatch = transformation.transformation.amountPerBatch;
  509. array.push(newAsset)
  510.  
  511. if(batchLength != amountLength){
  512.  
  513. throw Error('Erro na transação, introduza quantidade corresponde a lotes utilizados');
  514.  
  515. }else{
  516.  
  517. let registryBatch = await getAssetRegistry('org.doptrace.Batch');
  518.  
  519. for( i=0 ; i < batchLength; i++){
  520.  
  521.  
  522.  
  523. var factory = getFactory();
  524.  
  525. var random = Math.floor(Math.random() * (1000 - 100) + 100) / 100;
  526.  
  527.  
  528. if(transformation.transformation.usedBatches[i].amount > transformation.transformation.amountPerBatch[i]){
  529. var newBatch = factory.newResource(
  530. 'org.doptrace',
  531. 'Batch',
  532. transformation.transformation.newBatch.id + random);
  533.  
  534. newBatch.unit = transformation.transformation.usedBatches[i].unit;
  535. newBatch.creationDate = now
  536. newBatch.expirationDate = transformation.transformation.usedBatches[i].expirationDate;
  537. newBatch.state = 'TRANSFORMED'
  538. newBatch.certificated = false
  539.  
  540. newBatch.previousOperator = transformation.transformation.usedBatches.slice(-1)[0].currentOperator
  541.  
  542. newBatch.previousEvents = transformation.transformation.usedBatches[i].previousEvents.concat(array)
  543.  
  544. newBatch.currentOperator = transformation.transformation.operator;
  545.  
  546.  
  547. newBatch.product = transformation.transformation.usedBatches[i].product;
  548. newBatch.amount = transformation.transformation.usedBatches[i].amount - transformation.transformation.amountPerBatch[i];
  549.  
  550. await registryBatch.addAll([newBatch]);
  551.  
  552. }else if(transformation.transformation.usedBatches[i].amount < transformation.transformation.amountPerBatch[i]){
  553. throw Error('Quantidade usada no lote '+ transformation.transformation.usedBatches[i] + ' excede a quantidade possivel');
  554.  
  555. } else if(transformation.transformation.usedBatches[i].amount == transformation.transformation.amountPerBatch[i]){
  556.  
  557. console.log('Não houve criação de um lote de sobra');
  558. }
  559.  
  560.  
  561. transformation.transformation.usedBatches[i].state = 'DISABLED'
  562.  
  563. await registryBatch.update(transformation.transformation.usedBatches[i]);
  564.  
  565. console.log('entrei aqui');
  566.  
  567.  
  568. }
  569.  
  570. var newBatch = factory.newResource(
  571. 'org.doptrace',
  572. 'Batch',
  573. transformation.transformation.newBatch.id);
  574.  
  575. newBatch.unit = transformation.transformation.newBatch.unit;
  576. newBatch.creationDate = now
  577. newBatch.expirationDate = transformation.transformation.newBatch.expirationDate;
  578. newBatch.state = 'TRANSFORMED'
  579. newBatch.certificated = false
  580. newBatch.previousOperator = transformation.transformation.newBatch.operator
  581. newBatch.previousEvents = transformation.transformation.newBatch.previousEvents.concat(array)
  582. newBatch.currentOperator = transformation.transformation.operator;
  583.  
  584. newBatch.product = transformation.transformation.newBatch.product;
  585. newBatch.amount = transformation.transformation.newBatch.amount;
  586.  
  587. await registryBatch.add(newBatch);
  588.  
  589.  
  590. //Adiciona o novo asset Transformation ao ledger
  591. return transformRegistry.add(newAsset);
  592.  
  593. }
  594. }else{
  595. errors.push(new Error('Lote ' + transformation.transformation.usedBatches[i].getIdentifier() + ' não se encontra registado ! '));
  596. }
  597.  
  598.  
  599. }
  600.  
  601.  
  602.  
  603. throw errors
  604.  
  605. }
  606.  
  607.  
  608. /**
  609. * Sample transaction processor function.
  610. * @param {org.doptrace.transactionStorage} storage The sample transaction instance
  611. * @transaction
  612. */
  613.  
  614. async function storage(storage){
  615.  
  616. //Tamanho dos arrays de Lotes
  617. var batchLength = storage.storage.storedBatches.length;
  618.  
  619. //Array de Erros para verificação do batch
  620. var errors = new Array();
  621.  
  622. //Array para concatenação de Eventos
  623. var array = new Array();
  624.  
  625. //Variavel para correr os ciclos For
  626. let i;
  627.  
  628. var now = new Date();
  629.  
  630. //Verificação do Industry Retailer
  631. let registryIndustry = await getAssetRegistry('org.doptrace.Industry_Retailer');
  632.  
  633. let resultIndustry = await registryIndustry.exists(storage.storage.operator.getIdentifier());
  634.  
  635. if(!resultIndustry){
  636.  
  637. throw Error('Industria nao registada !')
  638.  
  639. }else if(storage.storage.storedBatches.length < 1){
  640.  
  641. throw Error('Preencha os lotes usados para Armazenar !')
  642.  
  643. }
  644.  
  645. //Verificação dos lotes usados
  646. for( i= 0 ; i< batchLength; i++) {
  647.  
  648. let registryBatch = await getAssetRegistry('org.doptrace.Batch');
  649.  
  650. let resultBatch = await registryBatch.exists(storage.storage.storedBatches[i].getIdentifier());
  651.  
  652. if(resultIndustry && resultBatch && storage.storage.storedBatches != null){
  653.  
  654. let storageRegistry = await getAssetRegistry('org.doptrace.Storage');
  655.  
  656. var factory = getFactory();
  657.  
  658. var newAsset = factory.newResource(
  659. 'org.doptrace',
  660. 'Storage',
  661. storage.storage.id);
  662.  
  663. newAsset.description = storage.storage.description;
  664. newAsset.latitude = storage.storage.latitude;
  665. newAsset.longitude = storage.storage.longitude;
  666. newAsset.dateTime = now
  667. newAsset.worker = storage.storage.worker;
  668. newAsset.operator = storage.storage.operator
  669. newAsset.storedBatches = storage.storage.storedBatches;
  670. array.push(newAsset)
  671.  
  672. console.log('STORAGE');
  673.  
  674. await storageRegistry.add(newAsset);
  675.  
  676.  
  677.  
  678. }else{
  679. errors.push(new Error('Lote ' + storage.storage.storedBatches[i].getIdentifier() + ' não se encontra registado ! '));
  680. }
  681.  
  682.  
  683. console.log('STORAGE_DOIS');
  684.  
  685.  
  686.  
  687. }
  688.  
  689. for(i= 0 ;i < batchLength; i++){
  690.  
  691. console.log('STORAGE_TRES');
  692.  
  693. let registry = await getAssetRegistry('org.doptrace.Batch');
  694.  
  695. storage.storage.storedBatches[i].previousOperator = storage.storage.storedBatches.slice(-1)[0].currentOperator
  696.  
  697. console.log('STORAGE QUATRO');
  698.  
  699. storage.storage.storedBatches[i].previousEvents = storage.storage.storedBatches[i].previousEvents.concat(array);
  700.  
  701. storage.storage.storedBatches[i].state = 'STORED'
  702.  
  703.  
  704.  
  705. storage.storage.storedBatches[i].currentOperator = storage.storage.operator
  706.  
  707.  
  708.  
  709.  
  710.  
  711. return registry.update(storage.storage.storedBatches[i]);
  712.  
  713. console.log('STORAGE CINCO');
  714.  
  715. }
  716.  
  717. throw errors
  718.  
  719. }
  720.  
  721.  
  722.  
  723.  
  724. /**
  725. * Sample transaction processor function.
  726. * @param {org.doptrace.transactionAnalysis} analysis The sample transaction instance
  727. * @transaction
  728. */
  729.  
  730. async function analysis(analysis){
  731.  
  732. //Array para concatenação de Eventos
  733. var array = new Array();
  734.  
  735. //Variavel para correr os ciclos For
  736. let i;
  737.  
  738. //Verificação do Industry Retailer
  739. let registryCertification = await getAssetRegistry('org.doptrace.CertificationEntity');
  740.  
  741. let resultCertification = await registryCertification.exists(analysis.analysis.operator.getIdentifier());
  742.  
  743. if(!resultCertification){
  744.  
  745. throw Error('Entidade de certificação nao registada !')
  746. }
  747.  
  748. let registryBatch = await getAssetRegistry('org.doptrace.Batch');
  749.  
  750. let resultBatch = await registryBatch.exists(analysis.analysis.batch.getIdentifier());
  751.  
  752. if(resultCertification && resultBatch){
  753.  
  754. let analysisRegistry = await getAssetRegistry('org.doptrace.Analysis');
  755.  
  756. var factory = getFactory();
  757.  
  758. var newAsset = factory.newResource(
  759. 'org.doptrace',
  760. 'Analysis',
  761. analysis.analysis.id);
  762.  
  763. newAsset.description = analysis.analysis.description;
  764. newAsset.latitude = analysis.analysis.latitude;
  765. newAsset.longitude = analysis.analysis.longitude;
  766. newAsset.dateTime = new Date();
  767. newAsset.worker = analysis.analysis.worker;
  768. newAsset.certificated = true;
  769. newAsset.operator = analysis.analysis.operator
  770. newAsset.batch = analysis.analysis.batch;
  771. array.push(newAsset)
  772.  
  773.  
  774. await analysisRegistry.add(newAsset);
  775.  
  776.  
  777.  
  778.  
  779. }else{
  780.  
  781. throw Error('Erro na transação');
  782. }
  783.  
  784. let registry = await getAssetRegistry('org.doptrace.Batch');
  785.  
  786. analysis.analysis.batch.previousOperator = analysis.analysis.batch.currentOperator
  787.  
  788. analysis.analysis.batch.previousEvents = analysis.analysis.batch.previousEvents.concat(array);
  789.  
  790. analysis.analysis.batch.certificated = true
  791.  
  792. analysis.analysis.batch.state = 'CERTIFIED'
  793.  
  794. analysis.analysis.batch.currentOperator = analysis.analysis.operator
  795.  
  796.  
  797.  
  798. return registry.update(analysis.analysis.batch);
  799.  
  800. }
  801.  
  802.  
  803.  
  804.  
  805. /**
  806. * Sample transaction processor function.
  807. * @param {org.doptrace.transactionDiscard} discard The sample transaction instance
  808. * @transaction
  809. */
  810.  
  811. async function discard(discard){
  812.  
  813. //Tamanho dos arrays de Lotes
  814. var batchLength = discard.discard.discardedBatches.length;
  815.  
  816. //Array de Erros para verificação do batch
  817. var errors = new Array();
  818.  
  819. //Array para concatenação de Eventos
  820. var array = new Array();
  821.  
  822. //Variavel para correr os ciclos For
  823. let i;
  824.  
  825. var now = new Date();
  826.  
  827. //Verificação do Logistics Company
  828. let registryLogistics = await getAssetRegistry('org.doptrace.Industry_Retailer');
  829.  
  830. let resultLogistics = await registryLogistics.exists(discard.discard.operator.getIdentifier());
  831.  
  832. if(!resultLogistics){
  833.  
  834. throw Error('Industria não registada !')
  835.  
  836. }else if(discard.discard.discardedBatches.length < 1){
  837.  
  838. throw Error('Preencha os lotes usados para Baixa !')
  839.  
  840. }
  841.  
  842. console.log('NEM AQUI ?')
  843.  
  844. //Verificação dos lotes usados
  845. for( i= 0 ; i <batchLength; i++) {
  846.  
  847.  
  848. console.log('12');
  849. let registryBatch = await getAssetRegistry('org.doptrace.Batch');
  850.  
  851. let resultBatch = await registryBatch.exists(discard.discard.discardedBatches[i].getIdentifier());
  852.  
  853.  
  854. if(resultLogistics && resultBatch && discard.discard.discardedBatches != null){
  855.  
  856. console.log('13');
  857.  
  858. let discardRegistry = await getAssetRegistry('org.doptrace.Discard');
  859.  
  860. var factory = getFactory();
  861.  
  862. var newAsset = factory.newResource(
  863. 'org.doptrace',
  864. 'Discard',
  865. discard.discard.id);
  866.  
  867. newAsset.description = discard.discard.description;
  868. newAsset.latitude = discard.discard.latitude;
  869. newAsset.longitude = discard.discard.longitude;
  870. newAsset.dateTime = now
  871. newAsset.worker = discard.discard.worker;
  872. newAsset.motive = discard.discard.motive;
  873. newAsset.operator = discard.discard.operator
  874. newAsset.discardedBatches = discard.discard.discardedBatches;
  875. array.push(newAsset)
  876.  
  877. console.log('14');
  878.  
  879. await discardRegistry.add(newAsset);
  880.  
  881. console.log('DISCARD NEGATIVO');
  882.  
  883. }else{
  884.  
  885. errors.push(new Error('Lote ' + discard.discard.discardedBatches[i].getIdentifier() + ' não se encontra registado ! '));
  886. }
  887.  
  888.  
  889.  
  890.  
  891.  
  892. }
  893.  
  894.  
  895. for(i = 0;i < batchLength;i++){
  896.  
  897. let registry = await getAssetRegistry('org.doptrace.Batch');
  898.  
  899. discard.discard.discardedBatches[i].previousEvents = discard.discard.discardedBatches[i].previousEvents.concat(array);
  900.  
  901. discard.discard.discardedBatches[i].previousOperator = discard.discard.discardedBatches.slice(-1)[0].currentOperator
  902.  
  903. discard.discard.discardedBatches[i].currentOperator = discard.discard.operator;
  904.  
  905. discard.discard.discardedBatches[i].state = 'DISCARDED'
  906.  
  907.  
  908. return registry.update(discard.discard.discardedBatches[i]);
  909.  
  910.  
  911. }
  912.  
  913. throw errors
  914.  
  915.  
  916. }
  917.  
  918.  
  919. /**
  920. * Sample transaction processor function.
  921. * @param {org.doptrace.transactionSale} sale The sample transaction instance
  922. * @transaction
  923. */
  924.  
  925. async function sale(sale){
  926.  
  927. var now = new Date();
  928.  
  929. //Tamanho dos arrays de Lotes
  930. var batchLength = sale.sale.soldBatches.length;
  931.  
  932. //Tamanho dos array amountPerBatch
  933. var amountPerBatch = sale.sale.amountPerBatch.length;
  934.  
  935. //Tamanho do array salePrice
  936. var salePrice = sale.sale.salePrice.length;
  937.  
  938. //Array de Erros para verificação do batch
  939. var errors = new Array();
  940.  
  941. //Array para concatenação de Eventos
  942. var array = new Array();
  943.  
  944. //Variavel para correr os ciclos For
  945. let i;
  946.  
  947. // Random
  948. var random = Math.floor(Math.random() * (1000 - 100) + 100) / 100;
  949.  
  950. //Verificação do Industry Retailer
  951. let registryIndustry = await getAssetRegistry('org.doptrace.Industry_Retailer');
  952.  
  953. let resultSeller = await registryIndustry.exists(sale.sale.seller.getIdentifier());
  954.  
  955. let resultBuyer = await registryIndustry.exists(sale.sale.buyer.getIdentifier());
  956.  
  957. if(!resultSeller){
  958.  
  959. throw Error('Vendedor nao registado !')
  960.  
  961. }else if(sale.sale.soldBatches.length <= 0){
  962. throw Error('Preencha os lotes usados para a venda !')
  963.  
  964. }else if(!resultBuyer){
  965.  
  966. throw Error('Comprador não registado')
  967. }
  968.  
  969. if(batchLength != amountPerBatch || batchLength != salePrice || amountPerBatch !=salePrice){
  970. throw Error('Prencha campos correspondentes');
  971.  
  972. }else{
  973.  
  974. //Verificação dos lotes usados
  975. for( i=0 ;i<batchLength ;i++) {
  976.  
  977. let registryBatch = await getAssetRegistry('org.doptrace.Batch');
  978.  
  979. let resultBatch = await registryBatch.exists(sale.sale.soldBatches[i].getIdentifier());
  980.  
  981. if(resultSeller && resultBuyer && resultBatch && sale.sale.soldBatches != null){
  982.  
  983. let saleRegistry = await getAssetRegistry('org.doptrace.Sale');
  984.  
  985. var factory = getFactory();
  986.  
  987. var newAsset = factory.newResource(
  988. 'org.doptrace',
  989. 'Sale',
  990. sale.sale.id);
  991.  
  992. newAsset.description = sale.sale.description;
  993. newAsset.latitude = sale.sale.latitude;
  994. newAsset.longitude = sale.sale.longitude;
  995. newAsset.dateTime = now;
  996. newAsset.worker = sale.sale.worker;
  997. newAsset.seller = sale.sale.seller;
  998. newAsset.buyer = sale.sale.buyer;
  999. newAsset.salePrice = sale.sale.salePrice;
  1000. newAsset.amountPerBatch = sale.sale.amountPerBatch
  1001. newAsset.soldBatches = sale.sale.soldBatches;
  1002. array.push(newAsset)
  1003.  
  1004. await saleRegistry.add(newAsset);
  1005.  
  1006. let registryBatch = await getAssetRegistry('org.doptrace.Batch');
  1007.  
  1008. for( i=0 ; i < batchLength; i++){
  1009.  
  1010.  
  1011.  
  1012. var factory = getFactory();
  1013.  
  1014. if(sale.sale.soldBatches[i].amount > sale.sale.amountPerBatch[i]){
  1015.  
  1016.  
  1017.  
  1018. var newBatch = factory.newResource(
  1019. 'org.doptrace',
  1020. 'Batch',
  1021. sale.sale.newBatch.id + random);
  1022.  
  1023. newBatch.unit = sale.sale.soldBatches[i].unit;
  1024. newBatch.creationDate = now
  1025. newBatch.expirationDate = sale.sale.soldBatches[i].expirationDate;
  1026. newBatch.state = 'SOLD';
  1027.  
  1028. newBatch.previousOperator = sale.sale.soldBatches.slice(-1)[0].currentOperator
  1029.  
  1030. newBatch.certificated = sale.sale.soldBatches[i].certificated;
  1031. newBatch.previousEvents = sale.sale.soldBatches[i].previousEvents.concat(array)
  1032.  
  1033. newBatch.currentOperator = sale.sale.buyer;
  1034.  
  1035.  
  1036. newBatch.product = sale.sale.soldBatches[i].product;
  1037. newBatch.amount = sale.sale.amountPerBatch[i];
  1038.  
  1039. await registryBatch.addAll([newBatch]);
  1040.  
  1041.  
  1042. //Criação do batch de sobra
  1043. var newBatch = factory.newResource(
  1044. 'org.doptrace',
  1045. 'Batch',
  1046. sale.sale.newBatch.id + 'SaleBatch' + random);
  1047.  
  1048. newBatch.unit = sale.sale.soldBatches[i].unit;
  1049. newBatch.creationDate = now;
  1050. newBatch.expirationDate = sale.sale.soldBatches[i].expirationDate;
  1051. newBatch.previousOperator = sale.sale.soldBatches.slice(-1)[0].currentOperator
  1052. newBatch.state = sale.sale.soldBatches[i].state;
  1053. newBatch.certificated = sale.sale.soldBatches[i].certificated;
  1054. newBatch.previousEvents = sale.sale.soldBatches[i].previousEvents.concat(array)
  1055. newBatch.currentOperator = sale.sale.buyer;
  1056.  
  1057.  
  1058.  
  1059. newBatch.product = sale.sale.soldBatches[i].product;
  1060. newBatch.amount = sale.sale.soldBatches[i].amount - sale.sale.amountPerBatch[i];
  1061.  
  1062. await registryBatch.addAll([newBatch]);
  1063.  
  1064. }else if(sale.sale.soldBatches[i].amount < sale.sale.amountPerBatch[i]){
  1065.  
  1066. throw Error('Quantidade usada no lote '+ sale.sale.soldBatches[i] + ' excede a quantidade possivel');
  1067.  
  1068. } else if(sale.sale.soldBatches[i].amount == sale.sale.amountPerBatch[i]){
  1069.  
  1070. console.log('Não houve criação de um lote de sobra');
  1071. }
  1072.  
  1073. sale.sale.soldBatches[i].state = 'DISABLED'
  1074.  
  1075. await registryBatch.update(sale.sale.soldBatches[i]);
  1076.  
  1077.  
  1078. }
  1079.  
  1080.  
  1081.  
  1082.  
  1083. //Adiciona o novo asset Sale ao ledger
  1084. return saleRegistry.add(newAsset);
  1085.  
  1086.  
  1087.  
  1088.  
  1089.  
  1090.  
  1091. }
  1092.  
  1093. errors.push(new Error('Lote ' + sale.sale.soldBatches[i].getIdentifier() + ' não se encontra registado ! '));
  1094.  
  1095.  
  1096. }
  1097.  
  1098. sale.sale.soldBatches[i].state = 'DISABLED'
  1099.  
  1100. await registryBatch.update(sale.sale.soldBatches[i]);
  1101. }
  1102.  
  1103. throw errors
  1104.  
  1105. }
  1106.  
  1107.  
  1108. /**
  1109. * Sample transaction processor function.
  1110. * @param {org.doptrace.transactionTransport} transport The sample transaction instance
  1111. * @transaction
  1112. */
  1113.  
  1114. async function transport(transport){
  1115.  
  1116.  
  1117.  
  1118. //Tamanho dos arrays de Lotes
  1119. var batchLength = transport.transport.transportedBatches.length;
  1120.  
  1121. //Array de Erros para verificação do batch
  1122. var errors = new Array();
  1123.  
  1124. //Array para concatenação de Eventos
  1125. var array = new Array();
  1126.  
  1127. //Variavel para correr os ciclos For
  1128. let i;
  1129.  
  1130. //Verificação do Logistics Company
  1131. let registryLogistics = await getAssetRegistry('org.doptrace.Logistics');
  1132.  
  1133. let resultLogistics = await registryLogistics.exists(transport.transport.operator.getIdentifier());
  1134.  
  1135. if(!resultLogistics){
  1136.  
  1137. throw Error('Companhia de Logistica nao registada !')
  1138.  
  1139. }else if(transport.transport.transportedBatches.length < 1){
  1140.  
  1141. throw Error('Preencha os lotes usados para transporte !')
  1142.  
  1143. }
  1144. let registryIndustry = await getAssetRegistry('org.doptrace.Industry_Retailer');
  1145.  
  1146. let resultIndustry = await registryIndustry.exists(transport.transport.destinationAddress.getIdentifier())
  1147.  
  1148. if(!resultIndustry){
  1149.  
  1150. throw Error('Industria não registada');
  1151. }
  1152.  
  1153. //Verificação dos lotes usados
  1154. for( i= 0 ; i<batchLength; i++) {
  1155.  
  1156. let registryBatch = await getAssetRegistry('org.doptrace.Batch');
  1157.  
  1158. let resultBatch = await registryBatch.exists(transport.transport.transportedBatches[i].getIdentifier());
  1159.  
  1160. if(resultIndustry && resultLogistics && resultBatch && transport.transport.transportedBatches != null){
  1161.  
  1162. let storageRegistry = await getAssetRegistry('org.doptrace.Transport');
  1163.  
  1164. var factory = getFactory();
  1165.  
  1166. var newAsset = factory.newResource(
  1167. 'org.doptrace',
  1168. 'Transport',
  1169. transport.transport.id);
  1170.  
  1171. newAsset.description = transport.transport.description;
  1172. newAsset.latitude = transport.transport.latitude;
  1173. newAsset.longitude = transport.transport.longitude;
  1174. newAsset.dateTime = new Date();
  1175. newAsset.worker = transport.transport.worker;
  1176. newAsset.operator = transport.transport.operator;
  1177. newAsset.destinationAddress = transport.transport.destinationAddress
  1178. newAsset.transportedBatches = transport.transport.transportedBatches;
  1179. array.push(newAsset)
  1180.  
  1181.  
  1182. await storageRegistry.add(newAsset);
  1183.  
  1184.  
  1185.  
  1186.  
  1187.  
  1188.  
  1189.  
  1190. }else{
  1191. errors.push(new Error('Lote ' + transport.transport.transportedBatches[i].getIdentifier() + ' não se encontra registado ! '));
  1192. }
  1193.  
  1194.  
  1195.  
  1196.  
  1197.  
  1198. }
  1199.  
  1200. for(i=0 ;i < batchLength; i++){
  1201.  
  1202. let registry = await getAssetRegistry('org.doptrace.Batch');
  1203.  
  1204.  
  1205. transport.transport.transportedBatches[i].previousEvents = transport.transport.transportedBatches[i].previousEvents.concat(array);
  1206.  
  1207. transport.transport.transportedBatches[i].previousOperator = transport.transport.transportedBatches.slice(-1)[0].currentOperator
  1208.  
  1209. transport.transport.transportedBatches[i].currentOperator = transport.transport.operator;
  1210. transport.transport.transportedBatches[i].state = 'IN_TRANSIT'
  1211.  
  1212.  
  1213.  
  1214. //Usar nas outras transações
  1215.  
  1216. return registry.update(transport.transport.transportedBatches[i]);
  1217.  
  1218.  
  1219. }
  1220.  
  1221. throw errors
  1222.  
  1223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement