in api/v1/src/procurements/dataManager.js [31:152]
async function listProcurements(projectId, stateFilter, accountNameFilter) {
try {
const procurementUtil = new CommerceProcurementUtil(projectId);
const bigqueryUtil = new BigQueryUtil(projectId);
/*
let filter = 'state=';
if (stateFilter && stateFilter.length > 0) {
filter += stateFilter.join(' OR state=')
} else {
filter += 'ENTITLEMENT_ACTIVATION_REQUESTED';
}
const result = await procurementUtil.listEntitlements(filter);
let entitlements = result.entitlements || [];
*/
let filterString = null;
if (accountNameFilter) {
let accountFilter = '';
accountNameFilter.forEach(e => {
if (accountFilter != '') {
accountFilter += ' OR ';
}
const name = e.substring(e.lastIndexOf('/') + 1);
accountFilter += `account=${name}`;
});
if (accountFilter) {
filterString = accountFilter;
}
}
// Should use filter, but due to bug with 'ENTITLEMENT_PENDING_PLAN_CHANGE_APPROVAL'
// passing null and filtering locally.
const result = await procurementUtil.listEntitlements(filterString);
let entitlements = [];
if (result.entitlements) {
// Work around for bug [#00012788] Error filtering on ENTITLEMENT_PENDING_PLAN_CHANGE_APPROVAL
if (stateFilter && stateFilter.length > 0) {
entitlements = underscore.filter(result.entitlements, (row) => {
return stateFilter.includes(row.state);
});
} else {
entitlements = result.entitlements;
}
}
const accountNames = underscore.uniq(entitlements.map(e => e.account));
// Query for the policy data and join that on for policy name.
const products = entitlements.map(e => e.product + '$||$' + e.plan);
if (products && products.length > 0) {
const table = bigqueryUtil.getTableFqdn(projectId, cfg.cdsDatasetId, cfg.cdsPolicyViewId);
const query = `WITH policyData AS (
SELECT
policyId,
marketplace,
CONCAT(marketplace.solutionId, '$||$', marketplace.planId) AS marketplaceId,
name,
description
FROM \`${table}\`
WHERE isDeleted IS FALSE AND marketplace IS NOT NULL
)
SELECT *
FROM policyData
WHERE marketplaceId IN UNNEST(@products)`;
const options = {
query: query,
params: { products: products },
}
const [policyRows] = await bigqueryUtil.executeQuery(options);
if (policyRows && policyRows.length > 0) {
entitlements.forEach(e => {
const policy = underscore.findWhere(policyRows, { marketplaceId: e.product + '$||$' + e.plan });
if (policy) {
e.policy = { policyId: policy.policyId, name: policy.name, description: policy.description };
}
});
}
}
// Set activated flag to false
entitlements.forEach(e => {
e.activated = false;
});
if (accountNames && accountNames.length > 0) {
const table = bigqueryUtil.getTableFqdn(projectId, cfg.cdsDatasetId, cfg.cdsAccountViewId);
const query = `SELECT a.accountId, m.accountName, a.email
FROM \`${table}\` a
CROSS JOIN UNNEST(a.marketplace) AS m
WHERE a.isDeleted IS FALSE AND m.accountName IN UNNEST(@accountNames)`;
const options = {
query: query,
params: { accountNames: accountNames },
}
const [accountRows] = await bigqueryUtil.executeQuery(options);
if (accountRows && accountRows.length > 0) {
entitlements.forEach(e => {
const account = underscore.findWhere(accountRows, { accountName: e.account });
if (account) {
e.email = account.email;
e.accountId = account.accountId;
if (e.policy) {
// Only set activated if a policy is found.
e.activated = true;
}
}
});
}
}
return { success: true, data: entitlements };
} catch (err) {
console.error(err);
return { success: false, errors: ['Failed to retrieve pending entitlement list', err] };
}
}