在一次交易中将多个资产添加为所有者

时间:2019-03-19 16:57:59

标签: hyperledger-composer

我正在尝试编写代码,在这里我先处理一些材料,然后将这些材料组合到一个模块中。当我交易一个模块时,材料必须是交易的一部分。

当我编写新代码时,事务“ internProducerModule”有效,但是如果我将其复制到新文件中,则会出现错误:“ TypeError:无法设置未定义的属性'所有者'”

正如我所提到的,如果我复制代码是行不通的,但是当我在异步函数InternProducerModule中删除其中所有带有“ material1,...,material4)的代码(行)并将其写为新代码时有效。

这是代码: 模型文件:

namespace org.master

// BASE DEFINTIONS

abstract participant Business identified by businessId {
  o String businessId
}

participant MaterialSupplier extends Business {
  o String materialSupplierName
  o Double accountBalance
  // Alle vorhandenen Materialien anzeigen
}

concept MaterialDetails {
  --> MaterialSupplier make
  o String materialName
  o String materialColour optional
  o String batch
  o Double amount
}

concept MaterialTransferLogEntry {
  --> Business seller
  --> Business buyer
  --> Material material
  o DateTime arrivalDateTime
}

asset Material identified by materialId {
  --> Business owner
  o String materialId
  o MaterialDetails materialDetails
  o QualityControlMaterial [] qualityControlsMaterial optional
  o MaterialTransferLogEntry[] logEntries
}

transaction InternMaterialSupplier {
  --> MaterialSupplier seller
  --> MaterialSupplier buyer
  --> Material material
  o Double unitCount
  o DateTime arrivalDateTime
}

transaction MaterialSupplierToProducer {
  --> MaterialSupplier seller
  --> Producer buyer
  --> Material material
  o Double unitCount
  o Double unitPrice
  o Double minQuality 
  o Double maxQuality 
  o Double Penalty 
  o DateTime arrivalDateTime
  --> MaterialSupplier materialSupplier
  --> Producer producer
}

participant Producer extends Business {
  o String producerName
  o Double accountBalance
  // Alle vorhandenen Materialien + Module anzeigen
}

concept ModuleDetails {
  --> Producer make
  o String moduleName
  o String batch
  o Double amount
}

concept ModuleTransferLogEntry {
  --> Business seller
  --> Business buyer
  --> Module module
  --> Material material1 optional
  --> Material material2 optional
  --> Material material3 optional
  --> Material material4 optional
  --> Material material5 optional
  o DateTime arrivalDateTime
}

asset Module identified by moduleId {
  --> Business owner
  o String moduleId
  o ModuleDetails moduleDetails
  o QualityControlModule [] qualityControlsModule optional
  o ModuleTransferLogEntry[] logEntries
}

transaction InternProducerMaterial {
  --> Producer seller
  --> Producer buyer
  --> Material material
  o Double unitCount
  o DateTime arrivalDateTime
}

transaction InternProducerModule {
  --> Producer seller
  --> Producer buyer
  --> Module module
  o Double unitCount
  --> Material material1 optional
  o Double unitCount1 optional
  --> Material material2 optional
  o Double unitCount2 optional
  --> Material material3 optional
  o Double unitCount3 optional
  --> Material material4 optional
  o Double unitCount4 optional
  --> Material material5 optional
  o Double unitCount5 optional
  o DateTime arrivalDateTime
}

transaction QualityControlMaterial {
  o Double qualityDegree
  --> Material material
}

transaction QualityControlModule {
  o Double qualityDegree
}

脚本文件:

/**
 * @param {org.master.InternMaterialSupplier} internMaterialSupplier - the internMaterialSupplier to be processed
 * @transaction
 */
async function internMaterialSupplier(internMaterialSupplier) { // eslint-disable-line no-unused-vars
    console.log('internMaterialSupplier');

    const namespace = 'org.master';
    const factory = getFactory();

    const seller = internMaterialSupplier.seller;
    const buyer = internMaterialSupplier.buyer;
    const material = internMaterialSupplier.material;

    //change material owner
    material.owner = buyer;

    //MaterialTransaction for log
    const materialTransferLogEntry = factory.newConcept(namespace, 'MaterialTransferLogEntry');
    materialTransferLogEntry.material = factory.newRelationship(namespace, 'Material', material.getIdentifier());
    materialTransferLogEntry.seller = factory.newRelationship(namespace, 'Business', seller.getIdentifier());
    materialTransferLogEntry.buyer = factory.newRelationship(namespace, 'Business', buyer.getIdentifier());
    materialTransferLogEntry.arrivalDateTime = internMaterialSupplier.arrivalDateTime;
    if (!material.logEntries) {
        material.logEntries = [];
    }

    material.logEntries.push(materialTransferLogEntry);

    const assetRegistry = await getAssetRegistry(material.getFullyQualifiedType());
    await assetRegistry.update(material);
}

/**
 * @param {org.master.MaterialSupplierToProducer} materialSupplierToProducer - the materialSupplierToProducer to be processed
 * @transaction
 */
async function test2(materialSupplierToProducer) {
   console.log('materialSupplierToProducer');

    const namespace = 'org.master';
    const factory = getFactory();

    const seller = materialSupplierToProducer.seller;
    const buyer = materialSupplierToProducer.buyer;
    const material = materialSupplierToProducer.material;

    //change material owner
    material.owner = buyer;

    //MaterialTransaction for log
    const materialTransferLogEntry = factory.newConcept(namespace, 'MaterialTransferLogEntry');
    materialTransferLogEntry.material = factory.newRelationship(namespace, 'Material', material.getIdentifier());
    materialTransferLogEntry.seller = factory.newRelationship(namespace, 'Business', seller.getIdentifier());
    materialTransferLogEntry.buyer = factory.newRelationship(namespace, 'Business', buyer.getIdentifier());
    materialTransferLogEntry.arrivalDateTime = materialSupplierToProducer.arrivalDateTime;
    if (!material.logEntries) {
        material.logEntries = [];
    }

    material.logEntries.push(materialTransferLogEntry);

    const assetRegistry = await getAssetRegistry(material.getFullyQualifiedType());
    await assetRegistry.update(material);

    let payOut = materialSupplierToProducer.unitPrice * materialSupplierToProducer.unitCount;
    console.log('Payout: ' + payOut);

    materialSupplierToProducer.materialSupplier.accountBalance += payOut;
    materialSupplierToProducer.producer.accountBalance -= payOut;

    console.log('MaterialSupplier: ' + materialSupplierToProducer.$identifier + 'new balance: ' + materialSupplierToProducer.materialSupplier.accountBalance);
    console.log('Producer: ' + materialSupplierToProducer.$identifier + 'new balance: ' + materialSupplierToProducer.producer.accountBalance);

    const materialSupplierRegistry = await getParticipantRegistry('org.master.MaterialSupplier');
    await materialSupplierRegistry.update(materialSupplierToProducer.materialSupplier);

    const producerRegistry = await getParticipantRegistry('org.master.Producer');
    await producerRegistry.update(materialSupplierToProducer.producer);
}

/**
 * @param {org.master.InternProducerMaterial} internProducerMaterial - the internProducerMaterial to be processed
 * @transaction
 */
async function internProducerMaterial(internProducerMaterial) { // eslint-disable-line no-unused-vars
    console.log('internProducerMaterial');

    const namespace = 'org.master';
    const factory = getFactory();

    const seller = internProducerMaterial.seller;
    const buyer = internProducerMaterial.buyer;
    const material = internProducerMaterial.material;

    //change material owner
    material.owner = buyer;

    //MaterialTransaction for log
    const materialTransferLogEntry = factory.newConcept(namespace, 'MaterialTransferLogEntry');
    materialTransferLogEntry.material = factory.newRelationship(namespace, 'Material', material.getIdentifier());
    materialTransferLogEntry.seller = factory.newRelationship(namespace, 'Business', seller.getIdentifier());
    materialTransferLogEntry.buyer = factory.newRelationship(namespace, 'Business', buyer.getIdentifier());
    materialTransferLogEntry.arrivalDateTime = internProducerMaterial.arrivalDateTime;
    if (!material.logEntries) {
        material.logEntries = [];
    }

    material.logEntries.push(materialTransferLogEntry);

    const assetRegistry = await getAssetRegistry(material.getFullyQualifiedType());
    await assetRegistry.update(material);
}

// Fehlt: Update des aktuellen Besitzers der Materialien
/**
 * @param {org.master.InternProducerModule} internProducerModule - the internProducerModule to be processed
 * @transaction
 */
async function internProducerModule(internProducerModule) { // eslint-disable-line no-unused-vars
    console.log('internProducerModule');

    const namespace = 'org.master';
    const factory = getFactory();

    const seller = internProducerModule.seller;
    const buyer = internProducerModule.buyer;
    const module = internProducerModule.module;
    const material1 = internProducerModule.material1;


    //change module owner
    module.owner = buyer;
    material1.owner = buyer;

    //ModuleTransaction for log
    const moduleTransferLogEntry = factory.newConcept(namespace, 'ModuleTransferLogEntry');
    moduleTransferLogEntry.module = factory.newRelationship(namespace, 'Module', module.getIdentifier());
    moduleTransferLogEntry.material1 = factory.newRelationship(namespace, 'Material', material1.getIdentifier());
    moduleTransferLogEntry.seller = factory.newRelationship(namespace, 'Business', seller.getIdentifier());
    moduleTransferLogEntry.buyer = factory.newRelationship(namespace, 'Business', buyer.getIdentifier());
    moduleTransferLogEntry.arrivalDateTime = internProducerModule.arrivalDateTime;
    if (!module.logEntries) {
        module.logEntries = [];
    }

    module.logEntries.push(moduleTransferLogEntry);
    material1.logEntries.push(moduleTransferLogEntry);

    const assetRegistry = await getAssetRegistry(module.getFullyQualifiedType());
    await assetRegistry.update(module);
}

我希望你们能帮助我!

0 个答案:

没有答案