Skip to content

Commit

Permalink
fix: [#188414550] license data for correct business is updated when m…
Browse files Browse the repository at this point in the history
…ultiple businesses exist
  • Loading branch information
FarazA22 committed Nov 5, 2024
1 parent c2b0359 commit f95452c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
22 changes: 18 additions & 4 deletions web/src/lib/data-hooks/useUserData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@ export const useUserData = (): UseUserDataResponse => {
fetchedUserId.current = data?.user.id;

let licenseDataFromDatabaseDataMoreRecent = false;
if (data?.businesses[data.currentBusinessId] && updateQueue?.currentBusiness()) {
const currBusinessIdFromUpdateQueue = updateQueue?.current()?.currentBusinessId;

if (
currBusinessIdFromUpdateQueue &&
data?.businesses[currBusinessIdFromUpdateQueue] &&
updateQueue?.currentBusiness()
) {
licenseDataFromDatabaseDataMoreRecent = isLicenseDataFromDatabaseDataMoreRecent({
businessFromDb: data?.businesses[data.currentBusinessId],
businessFromDb: data?.businesses[currBusinessIdFromUpdateQueue],
businessFromUpdateQueue: updateQueue?.currentBusiness(),
});
}
Expand All @@ -68,8 +74,16 @@ export const useUserData = (): UseUserDataResponse => {
setUpdateQueue(new UpdateQueueFactory(data, update));
} else if (updateQueue?.current() === undefined && data) {
updateQueue?.queue(data);
} else if (licenseDataFromDatabaseDataMoreRecent && updateQueue && data) {
const mergedData = modifyCurrentBusiness(updateQueue?.current(), licenseDataModifyingFunction(data));
} else if (
currBusinessIdFromUpdateQueue &&
licenseDataFromDatabaseDataMoreRecent &&
updateQueue &&
data
) {
const mergedData = modifyCurrentBusiness(
updateQueue?.current(),
licenseDataModifyingFunction(data, currBusinessIdFromUpdateQueue)
);
updateQueue?.queue(mergedData);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
39 changes: 37 additions & 2 deletions web/src/lib/utils/licenseStatus.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { getLicenseStatusResultsFromLicenses } from "@/lib/utils/licenseStatus";
import { generateLicenseDetails, taskIdLicenseNameMapping } from "@businessnjgovnavigator/shared/";
import { getLicenseStatusResultsFromLicenses, licenseDataModifyingFunction } from "@/lib/utils/licenseStatus";
import {
generateBusiness,
generateLicenseData,
generateLicenseDetails,
generateUserDataForBusiness,
randomElementFromArray,
taskIdLicenseNameMapping,
} from "@businessnjgovnavigator/shared/";

const licenseNames = Object.values(taskIdLicenseNameMapping);

Expand Down Expand Up @@ -32,4 +39,32 @@ describe("licenseStatus", () => {
});
});
});

describe("licenseDataModifyingFunction", () => {
it("returns a fn that updates the business that was passed in as the argument when invoked", () => {
// Used variable names that refer to the specific use case to simplify readability
const currBusinessIdFromUpdateQueue = "id 1";

const businessFromUpdateQueue = generateBusiness({
id: currBusinessIdFromUpdateQueue,
licenseData: undefined,
});

const licensesFromDb = {
[randomElementFromArray(Object.values(taskIdLicenseNameMapping))]: {
...generateLicenseDetails({}),
},
};
const userDataFromDb = generateUserDataForBusiness(
generateBusiness({
id: currBusinessIdFromUpdateQueue,
licenseData: generateLicenseData({ licenses: licensesFromDb }),
})
);
const returnedFn = licenseDataModifyingFunction(userDataFromDb, currBusinessIdFromUpdateQueue);

const result = returnedFn(businessFromUpdateQueue).licenseData!.licenses;
expect(result).toEqual(licensesFromDb);
});
});
});
6 changes: 3 additions & 3 deletions web/src/lib/utils/licenseStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export const getLicenseStatusResultsFromLicenses = (licenses: Licenses): License
return result;
};

export const licenseDataModifyingFunction = (dbUserData: UserData) => {
export const licenseDataModifyingFunction = (dbUserData: UserData, currBusinessId: string) => {
return (business: Business): Business => {
const licenses = dbUserData?.businesses[dbUserData.currentBusinessId]?.licenseData?.licenses || {};
const licenses = dbUserData?.businesses[currBusinessId]?.licenseData?.licenses || {};
const licenseStatusResults = getLicenseStatusResultsFromLicenses(licenses);

return {
Expand All @@ -33,7 +33,7 @@ export const licenseDataModifyingFunction = (dbUserData: UserData) => {
...getNonLicenseTasks(business),
...getLicenseTasksProgress(licenseStatusResults),
},
licenseData: dbUserData?.businesses[dbUserData.currentBusinessId]?.licenseData,
licenseData: dbUserData?.businesses[currBusinessId]?.licenseData,
};
};
};

0 comments on commit f95452c

Please sign in to comment.