in api/v1/src/policies/dataManager.js [207:324]
async function createOrUpdatePolicy(projectId, policyId, data) {
console.log(`createOrUpdateAccount called with policyId: ${policyId} and data: ${JSON.stringify(data)}`);
let _policyId = policyId;
if (policyId) {
const currentPolicy = await getPolicy(projectId, policyId);
console.log(`currentPolicy response: ${JSON.stringify(currentPolicy)}`);
if (currentPolicy.success) {
if (currentPolicy && currentPolicy.data.rowId !== data.rowId) {
// If user is updating an existing record, compare the rowId to ensure they're making updates from the latest record.
return { success: false, code: 500, errors: ["STALE"] };
}
_policyId = currentPolicy.data.policyId;
}
}
if (!_policyId) {
_policyId = uuidv4();
}
if (data.marketplace && data.marketplace.solutionId && data.marketplace.planId) {
const isMarketplaceUnique = await isMarketplaceSolutionPlanUnique(projectId, policyId, data.marketplace.solutionId, data.marketplace.planId);
if (isMarketplaceUnique === false) {
const message = `Marketplace solutionId or planId is already defined. The combination of a solutionId and planId must be unique across all policies.`;
return { success: false, code: 500, errors: [message] };
}
}
const rowId = uuidv4();
const isDeleted = false;
const createdAt = new Date().toISOString();
let fields = [...cfg.cdsPolicyTableFields], values = [...cfg.cdsPolicyTableFields];
// reformat datasets object for saving
let datasets = data.datasets;
if (datasets.length === 0) {
// If there are no supplied datasets, remove datasets column field and value from insert statement.
delete data.datasets;
const index = fields.indexOf('datasets');
if (index > -1) {
fields.splice(index, 1);
values.splice(index, 1);
}
}
// reformat buckets object for saving
let buckets = data.buckets;
if (buckets.length === 0) {
// If there are no supplied buckets, remove buckets column field and value from insert statement.
delete data.buckets;
const index = fields.indexOf('buckets');
if (index > -1) {
fields.splice(index, 1);
values.splice(index, 1);
}
}
// reformat topics object for saving
let topics = data.topics;
if (topics.length === 0) {
// If there are no supplied topics, remove topics column field and value from insert statement.
delete data.topics;
const index = fields.indexOf('topics');
if (index > -1) {
fields.splice(index, 1);
values.splice(index, 1);
}
}
// reformat rowAccessTags object for saving
let rowAccessTags = data.rowAccessTags;
if (rowAccessTags.length === 0) {
// If there are no supplied rowAccessTags, remove rowAccessTags column field and value from insert statement.
delete data.rowAccessTags;
const index = fields.indexOf('rowAccessTags');
if (index > -1) {
fields.splice(index, 1);
values.splice(index, 1);
}
} else {
data.rowAccessTags = rowAccessTags.map(t => { return { tag: t }; })
}
if (!(data.marketplace && data.marketplace.solutionId && data.marketplace.planId)) {
delete data.marketplace;
const index = fields.indexOf('marketplace');
if (index > -1) {
fields.splice(index, 1);
values.splice(index, 1);
}
}
fields = Array.from(fields).join();
values = Array.from(values).map(i => '@' + i).join();
data = {
...data,
...{
rowId: rowId,
policyId: _policyId,
isDeleted: isDeleted,
createdAt: createdAt
}
};
console.log(data);
const result = await _insertData(projectId, fields, values, data);
if (result) {
try {
await metaManager.performPolicyUpdates(projectId, [_policyId]);
} catch (err) {
return { success: false, code: 500, errors: [err.message] };
}
return { success: true, data: data };
} else {
const message = `Policy did not create with data values: '${data}'`;
return { success: false, code: 500, errors: [message] };
}
}