async function getAccount()

in api/v1/src/accounts/dataManager.js [389:437]


async function getAccount(projectId, accountId, email, emailType) {
    const bigqueryUtil = new BigQueryUtil(projectId);
    const table = bigqueryUtil.getTableFqdn(projectId, cfg.cdsDatasetId, cfg.cdsAccountViewId);
    const policyTable = bigqueryUtil.getTableFqdn(projectId, cfg.cdsDatasetId, cfg.cdsPolicyViewId);
    const limit = 2;
    let filter = 'WHERE accountId = @accountId AND isDeleted IS FALSE';
    let params = {};
    if (accountId) {
        params.accountId = accountId;
    }
    else if (email && emailType) {
        filter = 'WHERE email = @email AND emailType = @emailType AND isDeleted IS FALSE';
        params = { email: email, emailType: emailType };
    }

    const sqlQuery = `SELECT ca.* except(policies),
    array(
        select as struct
            pm.policyId,
            pm.name,
            pm.marketplace.solutionId,
            pm.marketplace.planId
        from unnest(ca.policies) p
        join \`${policyTable}\` pm on p.policyId = pm.policyId
        where pm.isDeleted is false
       ) as policies
FROM \`${table}\` ca
${filter} LIMIT ${limit};`

    const options = {
        query: sqlQuery,
        params: params
    };

    try {
        const [rows] = await bigqueryUtil.executeQuery(options);
        if (rows.length === 1) {
            const account = checkProcurementEntitlement(projectId, rows[0]);
            return account;
        } else {
            const message = `Account '${accountId}:${email}:${emailType}' does not exist within table: '${table}'`;
            console.warn(message);
            return null;
        }
    } catch (err) {
        console.error(`Error in getAccount when searching for '${accountId}:${email}:${emailType}': ${err.message}`);
        return null;
    }
}