in api/v1/src/policies/dataManager.js [64:138]
async function listUserPolicies(projectId, email) {
try {
const bigqueryUtil = new BigQueryUtil(projectId);
const table = bigqueryUtil.getTableFqdn(projectId, cfg.cdsDatasetId, cfg.cdsPolicyViewId);
let fields = new Set(cfg.cdsPolicyViewFields);
fields.delete('isDeleted');
fields = Array.from(fields).map(i => 'cp.' + i).join();
const accountTable = bigqueryUtil.getTableFqdn(projectId, cfg.cdsDatasetId, cfg.cdsAccountViewId);
let sqlQuery = `WITH currentAccount AS (
SELECT policies.policyId
FROM \`${accountTable}\` ca
CROSS JOIN UNNEST(policies) policies
WHERE lower(email) = @email AND
(ca.isDeleted IS FALSE OR ca.isDeleted IS NULL)
)
SELECT
bigQueryEnabled,
storageEnabled,
pubsubEnabled,
CASE WHEN bigQueryEnabled IS TRUE THEN datasets ELSE NULL END as datasets,
CASE WHEN bigQueryEnabled IS TRUE THEN rowAccessTags ELSE NULL END as rowAccessTags,
CASE WHEN bigQueryEnabled IS TRUE THEN isTableBased ELSE NULL END as isTableBased,
CASE WHEN storageEnabled IS TRUE THEN buckets ELSE NULL END as buckets,
CASE WHEN pubsubEnabled IS TRUE THEN topics ELSE NULL END as topics,
marketplace
FROM \`${table}\` cp
JOIN currentAccount ca ON ca.policyId = cp.policyId
WHERE
(cp.marketplace IS NOT NULL AND cp.marketplace.solutionId IS NOT NULL AND cp.marketplace.planId IS NOT NULL)
AND (cp.isDeleted IS FALSE OR cp.isDeleted IS NULL)`;
let options = {
query: sqlQuery,
params: { email: email.toLowerCase() }
};
const [rows] = await bigqueryUtil.executeQuery(options);
rows.forEach(e => {
e.status = 'Active';
});
const accountManager = require('../accounts/dataManager');
const account = await accountManager.getAccount(projectId, null, email, 'user');
if (account) {
const accountNames = account.marketplace.map(e => e.accountName);
if (accountNames && accountNames.length > 0) {
const procurementUtil = new CommerceProcurementUtil(projectId);
let accountFilter = '';
accountNames.forEach(e => {
if (accountFilter != '') {
accountFilter += ' OR ';
}
const name = e.substring(e.lastIndexOf('/') + 1);
accountFilter += `account=${name}`;
});
const filterString = `state=ENTITLEMENT_ACTIVATION_REQUESTED AND (${accountFilter})`;
const result = await procurementUtil.listEntitlements(filterString);
if (result) {
let entitlements = result.entitlements || [];
if (entitlements && entitlements.length > 0) {
entitlements.forEach(e => {
const name = e.name.substring(e.name.lastIndexOf('/') + 1);
rows.push({ marketplace: { solutionId: e.product, planId: e.plan, name: name, message: e.messageToUser }, status: 'Pending Approval' });
});
}
}
}
}
return { success: true, data: rows };
} catch (err) {
console.error(err);
return { success: false, code: 500, errors: ['Unable to retrieve user products'] };
}
}