async createDonation()

in ngo-identity/chaincode/src/ngo.js [676:716]


  async createDonation(stub, args) {
    console.log('============= START : createDonation ===========');
    console.log('##### createDonation arguments: ' + JSON.stringify(args));

    // args is passed as a JSON string
    let json = JSON.parse(args);
    let key = 'donation' + json['donationId'];
    json['docType'] = 'donation';

    console.log('##### createDonation donation: ' + JSON.stringify(json));

    // Confirm the NGO exists
    let ngoKey = 'ngo' + json['ngoRegistrationNumber'];
    let ngoQuery = await stub.getState(ngoKey);
    if (!ngoQuery.toString()) {
      throw new Error('##### createDonation - Cannot create donation as the NGO does not exist: ' + json['ngoRegistrationNumber']);
    }

    // Confirm the donor exists
    let donorKey = 'donor' + json['donorUserName'];
    let donorQuery = await stub.getState(donorKey);
    if (!donorQuery.toString()) {
      throw new Error('##### createDonation - Cannot create donation as the Donor does not exist: ' + json['donorUserName']);
    }

    // Check if the Donation already exists
    let donationQuery = await stub.getState(key);
    if (donationQuery.toString()) {
      throw new Error('##### createDonation - This Donation already exists: ' + json['donationId']);
    }

    await stub.putState(key, Buffer.from(JSON.stringify(json)));
    let eventData = {
      eventName: "Donation created",
      donor: json['donorUserName'],
      amount: json['donationAmount'],
      ngo: json['ngoRegistrationNumber']
    }
    createEvent(stub, eventData);
    console.log('============= END : createDonation ===========');
  }