in api/v1/src/procurements/dataManager.js [213:260]
async function approveEntitlement(projectId, name, status, reason) {
try {
const procurementUtil = new CommerceProcurementUtil(projectId);
const entitlement = await procurementUtil.getEntitlement(name);
const state = entitlement.state;
// Newly purchased entitlement
if (state === 'ENTITLEMENT_ACTIVATION_REQUESTED') {
if (status === 'approve') {
const account = await accountManager.findMarketplaceAccount(projectId, entitlement.account);
const policy = await policyManager.findMarketplacePolicy(projectId, entitlement.product, entitlement.plan);
console.log(`Approving entitlement for account:${JSON.stringify(account)} with policy: ${JSON.stringify(policy)}`);
const result = await procurementUtil.approveEntitlement(name);
return { success: true, data: result };
} else if (status === 'reject') {
const result = await procurementUtil.rejectEntitlement(name, reason);
return { success: true, data: result };
} else if (status === 'comment') {
const result = await procurementUtil.updateEntitlementMessage(name, reason);
return { success: true, data: result };
}
} else if (state === 'ENTITLEMENT_PENDING_PLAN_CHANGE_APPROVAL') {
// Handle approval and rejection for plan change approval
// Do an entitlement get to find the current plan name and the new pending name
// Parameter for getting the entitlement is the name: name.
// const currentPlan = entitlement.currentPlan;
if (status === 'approve') {
// Approve plan change, this would only be for a manual approve.
// An automated approval would be handled by a Pub/Sub notification.
// Remove user from current policy and add to new plan related policy.
// Re-factor removeEntitlement so that it doesn't call createOrUpdateAccount maybe, in order that we can remove and add using the same functions.
const account = await accountManager.findMarketplaceAccount(projectId, entitlement.account);
const existingPolicy = await policyManager.findMarketplacePolicy(projectId, entitlement.product, entitlement.plan);
const pendingPolicy = await policyManager.findMarketplacePolicy(projectId, entitlement.product, entitlement.newPendingPlan);
console.log(`Approving entitlement for account:${JSON.stringify(account)} from policy: ${JSON.stringify(existingPolicy)} to policy: ${JSON.stringify(pendingPolicy)}`);
const result = await procurementUtil.approvePlanChange(name, entitlement.newPendingPlan);
return { success: true, data: result };
} else if (status === 'reject') {
// No need to do anything further, existing plan and policy relations will remain the same.
const result = await procurementUtil.rejectPlanChange(name, entitlement.newPendingPlan, reason);
return { success: true, data: result };
}
}
} catch (err) {
console.error(err);
return { success: false, errors: ['Failed to approve entitlement', err] };
}
}