in api/v1/src/policies/dataManager.js [146:199]
async function listPolicies(projectId, datasetId, accountId) {
const bigqueryUtil = new BigQueryUtil(projectId);
const table = bigqueryUtil.getTableFqdn(projectId, cfg.cdsDatasetId, cfg.cdsPolicyViewId);
const fields = Array.from(cfg.cdsPolicyViewFields).join();
let sqlQuery = `with accountCounts AS (
select p.policyId, count(ca.accountId) as count
from \`${projectId}.datashare.currentAccount\` ca
cross join unnest(policies) p
where ca.isDeleted is false
group by p.policyId
)
SELECT rowId, cp.policyId, name, description, createdAt, createdBy, version, ifnull(ac.count, 0) as accountCount, marketplace
FROM \`${projectId}.datashare.currentPolicy\` cp
left join accountCounts ac on ac.policyId = cp.policyId
where cp.isDeleted is false;`;
let options = {
query: sqlQuery
};
if (datasetId) {
sqlQuery = `SELECT ${fields} FROM \`${table}\`, UNNEST(datasets) AS datasets WHERE datasets.datasetId = @datasetId;`
options = {
query: sqlQuery,
params: { datasetId: datasetId }
};
} else if (accountId) {
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);
sqlQuery = `WITH currentAccount AS (
SELECT policies.policyId
FROM \`${accountTable}\` ca
CROSS JOIN UNNEST(policies) policies
WHERE accountId = @accountId AND
(ca.isDeleted IS false OR ca.isDeleted IS null)
)
SELECT ${fields}
FROM \`${table}\` cp
LEFT JOIN currentAccount ca ON ca.policyId = cp.policyId
WHERE (cp.isDeleted IS false OR cp.isDeleted IS null)`;
options = {
query: sqlQuery,
params: { accountId: accountId }
};
}
try {
const [rows] = await bigqueryUtil.executeQuery(options);
return { success: true, data: rows };
} catch (err) {
const message = `Policies do not exist within table: '${table}'`;
return { success: false, code: 400, errors: [message] };
}
}