{"size": 1395, "ext": "js", "max_stars_repo_path": "ngo-lambda/index.js", "max_stars_repo_name": "hasibdevops/non-profit-blockchain", "max_stars_repo_stars_event_min_datetime": "2021-05-17T01:18:12.000Z", "max_stars_repo_stars_event_max_datetime": "2021-05-17T01:18:12.000Z", "max_issues_repo_path": "ngo-lambda/index.js", "max_issues_repo_name": "hasibdevops/non-profit-blockchain", "max_issues_repo_issues_event_min_datetime": "2020-09-07T14:33:30.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-02T05:57:01.000Z", "max_forks_repo_path": "ngo-lambda/index.js", "max_forks_repo_name": "anivish4012/ngos", "max_forks_repo_forks_event_min_datetime": "2019-10-13T18:56:13.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-22T17:42:45.000Z"}
{"max_stars_count": 1, "max_issues_count": 10, "max_forks_count": 12, "avg_line_length": 27.9, "max_line_length": 66, "alphanum_fraction": 0.5691756272}
/*
    Query Fabric via a lambda function
*/

'use strict';

const config = require("./config");
const query = require("./query");
const syncCrypto = require("./syncCrypto");
const logger = require("./logging").getLogger("lambdaFunction");

function buildRequest(donorName) {
    const argsString = JSON.stringify({donorUserName: donorName});
	const request = {
        chaincodeId: config.chaincodeId,
        fcn: 'queryDonor',
        args: [argsString],
        chainId: config.channelName,
    };
    
    return request;
};

async function handler(event) {
    const promise = new Promise(async (resolve, reject) => {
        let donorName = event.donorName;
        if (!donorName) {
            return resolve("Please provide a donor name")
        }
        try {
            logger.info("=== Handler Function Start ===");
        
            logger.info("Downloading credentials from S3...");
            await syncCrypto.downloadCredentials();
                
            logger.info("Querying donor");
            const request = buildRequest(donorName);
            let result = await query(request);
        
            logger.info("=== Handler Function End ===");  
            resolve("Donor is " + result);
        } catch (err) {
            logger.error("Error: " + err);
            reject(Error(err));
        }
    });
    return promise;
};

module.exports = {handler};