Atreyee
Atreyee

Reputation: 1

Handling Related objects in Hyperledger composer

Model:

namespace org.acme.model

enum CustomerSegment {
o P
o E
}

asset Account identified by Account_No {
  o String Account_No
  --> Customer Account_Holder
  o String Account_Type
  o Integer Balance
}

participant Customer identified by Customer_ID {
  o String Customer_ID
  o String First_Name
  o CustomerSegment Customer_Segment
}

transaction UpdateCustomerSegment{
  --> Account A
  --> Customer C
}

transaction changeBalance{
  --> Account A
  o Integer newBalance
}

  event UCS_Event{
    -->Account A
    o String oldsegment
    o String newsegment
  }

event BalanceChangEvent{
  -->Account A
  o Integer oldbalance
  o Integer newbalance
}

Script:

  /**
     * Place an order for a vehicle
     * @param {org.acme.model.UpdateCustomerSegment} ucs - the transaction that calculated Customer Segment
     * @transaction
     */

function UpdateCustomerSegment(ucs)
{
  var CustomerID=ucs.C.Customer_ID;
  var oldCS=ucs.C.Customer_Segment;

  if(ucs.A.Balance>3000)
    ucs.C.Customer_Segment="E"
  else
    ucs.C.Customer_Segment="P"

  var cust = getParticipantRegistry('org.acme.model.Customer')
  .then(function(participantRegistry){
  return participantRegistry.update(ucs.C);
  })

  .then(function(){
    //Emit Event
    var event_g= getFactory().newEvent('org.acme.model','UCS_Event');
    event_g.A=ucs.A;
    event_g.oldsegment=oldCS;
    event_g.newsegment=ucs.C.Customer_Segment
    emit(event_g);
  })

  return cust;
}

What I need to do is - Pass only the Account No. to the transaction. the transaction should fetch the appropriate Customer - where customer ID is same as Account's Account Holder, and update the Customer's Customer Segment

which I am unable to do. is it even doable. I am new, so not sure.

in the above transaction I am passing both the Account No. and Customer ID

2nd Question Can we update a Participant & Asset both in the same transaction ? if yes how.

3rd Question: how to call one transaction from within another.

Upvotes: 0

Views: 146

Answers (1)

Dan Selman
Dan Selman

Reputation: 2297

Your model looks good, now you need to write a Transaction Processor function that subscribes to UpdateCustomerSegment transaction and that implements your logic to change the customer segment of the customer, and then persist the customer.

The basic-sample-network includes a similar simple transaction processor:

/**
 * Sample transaction processor function.
 * @param {org.acme.sample.SampleTransaction} tx The sample transaction instance.
 * @transaction
 */
function sampleTransaction(tx) {

    // Save the old value of the asset.
    var oldValue = tx.asset.value;

    // Update the asset with the new value.
    tx.asset.value = tx.newValue;

    // Get the asset registry for the asset.
    return getAssetRegistry('org.acme.sample.SampleAsset')
        .then(function (assetRegistry) {

            // Update the asset in the asset registry.
            return assetRegistry.update(tx.asset);

        })
        .then(function () {

            // Emit an event for the modified asset.
            var event = getFactory().newEvent('org.acme.sample', 'SampleEvent');
            event.asset = tx.asset;
            event.oldValue = oldValue;
            event.newValue = tx.newValue;
            emit(event);

        });

}

Upvotes: 1

Related Questions