Skip to content

Commit

Permalink
squash me
Browse files Browse the repository at this point in the history
  • Loading branch information
pwnage101 committed Nov 20, 2023
1 parent b7d0db8 commit f5b7614
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ const ProvisioningFormSubmissionButton = () => {
const predefinedQueryDisplayName = PREDEFINED_QUERY_DISPLAY_NAMES[
policy.catalogMetadata.predefinedQueryType
];
const catalogCreatedResponse = getOrCreateCatalog(
const catalogCreatedResponse = getOrCreateCatalog({
enterpriseCustomerUuid: formData.enterpriseUUID,
catalogQueryId: queryTypeToQueryId[policy.catalogMetadata.predefinedQueryType],
title: `${formData.enterpriseUUID} - ${predefinedQueryDisplayName}`,
).catch((error) => {
}).catch((error) => {
throw error;
});
// Update the catalogUuid in the policy form data for use later during policy creation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ const SaveEditsButton = () => {
const predefinedQueryDisplayName = PREDEFINED_QUERY_DISPLAY_NAMES[
policy.catalogMetadata.predefinedQueryType
];
const catalogCreatedResponse = getOrCreateCatalog(
const catalogCreatedResponse = getOrCreateCatalog({
enterpriseCustomerUuid: formData.enterpriseUUID,
catalogQueryId: queryTypeToQueryId[policy.catalogMetadata.predefinedQueryType],
title: `${formData.enterpriseUUID} - ${predefinedQueryDisplayName}`,
).catch((error) => {
}).catch((error) => {
throw error;
});
// Update the catalogUuid in the policy form data for use later during policy patching.
Expand All @@ -155,6 +155,9 @@ const SaveEditsButton = () => {
} else {
// We did not find a predefined catalog query type, so we assume a custom catalog has been selected. Handle
// fourth/sixth cases in the matrix (do nothing since we're using a custom catalog).
//
// Note: No need to call `setCatalogUuid()` because the catalogUuid policy context data was already populated
// via a callback for the custom catalog dropdown.
}
}));
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions src/Configuration/Provisioning/data/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export const PREDEFINED_QUERY_DISPLAY_NAMES = {
};
// For convenience, create an enum to map internal predefined query keys to themselves.
export const PREDEFINED_QUERIES_ENUM = Object.fromEntries(
Object.keys(PREDEFINED_QUERY_DISPLAY_NAMES),
).map(e => [e, e]);
Object.keys(PREDEFINED_QUERY_DISPLAY_NAMES).map(e => [e, e]),
);

const PROVISIONING_PAGE_TEXT = {
DASHBOARD: {
Expand Down
33 changes: 16 additions & 17 deletions src/Configuration/Provisioning/data/tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ const samplePatchResponses = {
};

jest.mock('@edx/frontend-platform/auth', () => ({
// TODO TROY+HAMZAH: update this HTTP client mock to respond differently to different endpoints (catalogs, policies,
// subsidies, etc). Also, figure out a way to be able to call hasBeenCalledWith on this mock so that we can actually
// test the API request payloads.
getAuthenticatedHttpClient: () => ({
get: () => Promise.resolve({
data: [{
Expand All @@ -290,12 +293,12 @@ jest.mock('@edx/frontend-platform/auth', () => ({

describe('getOrCreateCatalog', () => {
it('returns the correct data', async () => {
const data = await getOrCreateCatalog([
'abf9f43b-1872-4c26-a2e6-1598fc57fbdd',
10,
'abf9f43b-1872-4c26-a2e6-1598fc57fbdd - test123',
]);
expect(data.getOrCreateCatalog).toEqual(sampleResponses.data.getOrCreateCatalog);
const data = await getOrCreateCatalog({
enterpriseCustomerUuid: 'c6aaf182-bcae-4d14-84cd-d538b7ec08f0',
catalogQueryId: 10,
title: 'c6aaf182-bcae-4d14-84cd-d538b7ec08f0 - test123',
});
expect(data).toEqual(sampleResponses.data.getOrCreateCatalog);
});
});

Expand All @@ -312,7 +315,7 @@ describe('createSubsidy', () => {
default_revenue_category: 'partner-no-rev-prepay',
default_internal_only: true,
});
expect(data.createSubsidy).toEqual(sampleResponses.data.createSubsidy);
expect(data).toEqual(sampleResponses.data.createSubsidy);
});
});

Expand All @@ -326,7 +329,7 @@ describe('patchSubsidy', () => {
internal_only: true,
revenue_category: 'partner-no-rev-prepay',
});
expect(data.patchSubsidy).toEqual(samplePatchResponses.data.patchSubsidy);
expect(data).toEqual(samplePatchResponses.data.patchSubsidy);
});
});

Expand All @@ -344,7 +347,7 @@ describe('createPolicies', () => {
per_learner_enrollment_limit: null,
spend_limit: 1200,
});
expect(data.createPolicy).toEqual(sampleResponses.data.createPolicy);
expect(data).toEqual(sampleResponses.data.createPolicy);
});
});

Expand All @@ -357,7 +360,7 @@ describe('patchPolicy', () => {
per_learner_spend_limit: 12,
spend_limit: 1200,
});
expect(data.patchPolicy).toEqual(samplePatchResponses.data.patchPolicy);
expect(data).toEqual(samplePatchResponses.data.patchPolicy);
});
});

Expand Down Expand Up @@ -429,27 +432,23 @@ describe('transformPolicyData', () => {

describe('transformPatchPolicyData', () => {
it('returns an empty array when no policies are passed', async () => {
const output = await transformPatchPolicyPayload({ policies: [] }, [], []);
const output = await transformPatchPolicyPayload({ policies: [] });
expect(output).toEqual([]);
});
it('returns the correct data', async () => {
const mockFormData = {
policies: [{
policy_type: 'PerLearnerSpendCreditAccessPolicy',
accountDescription: 'awesome policy description',
catalogQueryMetadata: {
catalogQuery: {
catalogUuid: '2afb0a7f-103d-43c3-8b1a-db8c5b3ba1f4',
},
},
catalogUuid: '2afb0a7f-103d-43c3-8b1a-db8c5b3ba1f4',
perLearnerCap: true,
perLearnerCapAmount: 2000,
uuid: '12324232',
accountValue: 1200,
}],
subsidyUuid: '205f11a4-0303-4407-a2e7-80261ef8fb8f',
};
const output = await transformPatchPolicyPayload(mockFormData, [samplePatchResponses.getOrCreateCatalog]);
const output = await transformPatchPolicyPayload(mockFormData);
expect(output).toEqual([{
description: 'awesome policy description',
catalogUuid: '2afb0a7f-103d-43c3-8b1a-db8c5b3ba1f4',
Expand Down
46 changes: 33 additions & 13 deletions src/Configuration/Provisioning/data/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,19 +479,29 @@ export async function patchPolicy({
* @param {Object} subsidyCreationResponse - The response from the subsidy creation API
* @returns - Returns an array of policy data objects that can be used to create new policies
*/
export function transformPolicyData(formData, catalogCreationResponse, subsidyCreationResponse) {
export function transformPolicyData(formData, subsidyCreationResponse) {
const { enterpriseUUID, policies } = formData;
if (
policies.length === 0
|| catalogCreationResponse.length === 0
|| subsidyCreationResponse.length === 0
) { return []; }
const payloads = policies.map((policy, index) => ({
description: policy.accountDescription?.length > 0
? policy.accountDescription
: `${policy.accountName}, Initial Policy Value: $${policy.accountValue}, Initial Subsidy Value: $${policies.reduce((acc, { accountValue }) => acc + parseInt(accountValue, 10), 0)}`,
const payloads = policies.map((policy) => ({
// Add this and test as part of ENT-7987:
// displayName: generatePolicyName(formData, index),
description: (
policy.accountDescription?.length > 0
? policy.accountDescription
: (
`${policy.accountName}, `
+ `Initial Policy Value: $${policy.accountValue}, `
+ `Initial Subsidy Value: $${policies.reduce((acc, { accountValue }) => acc + parseInt(accountValue, 10), 0)}`
)
),
enterpriseCustomerUuid: enterpriseUUID,
catalogUuid: catalogCreationResponse[0][index].uuid,
// By the time that this function is run, policy.catalogUuid is either:
// 1. The UUID of a managed catalog, created by this provisioning tool and derived from a predefined catalog query.
// 2. The UUID of a custom manually-created catalog, specified using the Unique/Curated catalog dropdown selector.
catalogUuid: policy.catalogUuid,
subsidyUuid: subsidyCreationResponse[0].uuid,
perLearnerSpendLimit: policy.perLearnerCap ? parseInt(policy.perLearnerCapAmount, 10) * 100 : null,
spendLimit: parseInt(policy.accountValue, 10) * 100,
Expand All @@ -510,18 +520,28 @@ export function transformPatchPolicyPayload(formData) {
const { subsidyUuid, policies } = formData;
if (
policies.length === 0
// || catalogSavedResponse.length === 0
|| !subsidyUuid
) { return []; }
const payloads = policies.map((policy) => ({
description: policy.accountDescription?.length > 0
? policy.accountDescription
: `${policy.accountName}, Initial Policy Value: $${policy.accountValue}, Initial Subsidy Value: $${policies.reduce((acc, { accountValue }) => acc + parseInt(accountValue, 10), 0)}`,
catalogUuid: policy.catalogMetadata.catalogUuid,
uuid: policy.uuid,
// Add this and test as part of ENT-7987:
// displayName: generatePolicyName(formData, index),
description: (
policy.accountDescription?.length > 0
? policy.accountDescription
: (
`${policy.accountName}, `
+ `Initial Policy Value: $${policy.accountValue}, `
+ `Initial Subsidy Value: $${policies.reduce((acc, { accountValue }) => acc + parseInt(accountValue, 10), 0)}`
)
),
// By the time that this function is run, policy.catalogUuid is either:
// 1. The UUID of a managed catalog, created by this provisioning tool and derived from a predefined catalog query.
// 2. The UUID of a custom manually-created catalog, specified using the Unique/Curated catalog dropdown selector.
catalogUuid: policy.catalogUuid,
subsidyUuid,
perLearnerSpendLimit: policy.perLearnerCap ? parseInt(policy.perLearnerCapAmount, 10) * 100 : null,
spendLimit: policy.accountValue,
uuid: policy.uuid,
}));
return payloads;
}
Expand Down

0 comments on commit f5b7614

Please sign in to comment.