Skip to content

Commit

Permalink
Refactor getReservablePidsFromAnotherLibrary to `useReservableFromA…
Browse files Browse the repository at this point in the history
…notherLibrary` hook

Added validation with FBS to verify manifestation's reservable status (must be false) for Overbygningsmaterial/OpenOrder reservations.

in addition, I have commented out the tests that were for `getReservablePidsFromAnotherLibrary` as they must be rewritten for the new hook
  • Loading branch information
kasperbirch1 committed Nov 20, 2023
1 parent 283755e commit 8975521
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 90 deletions.
10 changes: 5 additions & 5 deletions src/components/material/material-buttons/MaterialButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { FC } from "react";
import { AccessTypeCode } from "../../../core/dbc-gateway/generated/graphql";
import {
getAllFaustIds,
getManifestationType,
getReservablePidsFromAnotherLibrary
getManifestationType
} from "../../../core/utils/helpers/general";
import { ButtonSize } from "../../../core/utils/types/button";
import { Manifestation } from "../../../core/utils/types/entities";
Expand All @@ -14,6 +13,7 @@ import MaterialButtonsOnline from "./online/MaterialButtonsOnline";
import MaterialButtonsFindOnShelf from "./physical/MaterialButtonsFindOnShelf";
import MaterialButtonsPhysical from "./physical/MaterialButtonsPhysical";
import MaterialButtonReservableFromAnotherLibrary from "./physical/MaterialButtonReservableFromAnotherLibrary";
import useReservableFromAnotherLibrary from "../../../core/utils/useReservableFromAnotherLibrary";

export interface MaterialButtonsProps {
manifestations: Manifestation[];
Expand All @@ -35,10 +35,10 @@ const MaterialButtons: FC<MaterialButtonsProps> = ({
// articles appear as a part of journal/periodical publications and can't be
// physically loaned for themseleves.

const isReservableFromAnotherLibrary =
getReservablePidsFromAnotherLibrary(manifestations).length > 0;
const { reservablePidsFromAnotherLibrary } =
useReservableFromAnotherLibrary(manifestations);

if (isReservableFromAnotherLibrary) {
if (reservablePidsFromAnotherLibrary.length > 0) {
return (
<MaterialButtonReservableFromAnotherLibrary
size={size}
Expand Down
24 changes: 12 additions & 12 deletions src/components/reservation/ReservationModalBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
getAllPids,
getMaterialTypes,
getManifestationType,
materialIsFiction,
getReservablePidsFromAnotherLibrary
materialIsFiction
} from "../../core/utils/helpers/general";
import { useText } from "../../core/utils/text";
import { Button } from "../Buttons/Button";
Expand Down Expand Up @@ -63,6 +62,7 @@ import {
import ModalMessage from "../message/modal-message/ModalMessage";
import configuration, { getConf } from "../../core/configuration";
import { usePatronData } from "../../core/utils/helpers/user";
import useReservableFromAnotherLibrary from "../../core/utils/useReservableFromAnotherLibrary";

type ReservationModalProps = {
selectedManifestations: Manifestation[];
Expand Down Expand Up @@ -123,15 +123,19 @@ export const ReservationModalBody = ({
work,
allPids
);
const manifestationsToReserve = getManifestationsToReserve(
reservableManifestations ?? [],
!!selectedPeriodical
);

const { reservablePidsFromAnotherLibrary } = useReservableFromAnotherLibrary(
manifestationsToReserve
);

// If we don't have all data for displaying the view render nothing.
if (!userResponse.data || !holdingsResponse.data) {
return null;
}
const manifestationsToReserve = getManifestationsToReserve(
reservableManifestations ?? [],
!!selectedPeriodical
);
const { data: userData } = userResponse as { data: AuthenticatedPatronV6 };
const { data: holdingsData } = holdingsResponse as {
data: HoldingsForBibliographicalRecordV3[];
Expand All @@ -144,22 +148,18 @@ export const ReservationModalBody = ({
? getFutureDateString(selectedInterest)
: null;

const pidsFromAnotherLibrary = getReservablePidsFromAnotherLibrary(
manifestationsToReserve
);

const saveReservation = () => {
if (!manifestationsToReserve || manifestationsToReserve.length < 1) {
return;
}

if (pidsFromAnotherLibrary.length > 0 && patron) {
if (reservablePidsFromAnotherLibrary.length > 0 && patron) {
const { patronId, name, emailAddress, preferredPickupBranch } = patron;

mutateOpenOrder(
{
input: {
pids: [...pidsFromAnotherLibrary],
pids: [...reservablePidsFromAnotherLibrary],
pickUpBranch: selectedBranch
? removePrefixFromBranchId(selectedBranch)
: removePrefixFromBranchId(preferredPickupBranch),
Expand Down
137 changes: 64 additions & 73 deletions src/core/utils/helpers/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,85 +508,76 @@ export const getContributors = (short: boolean, creators: string[]) => {
return creators[0];
};

export const getReservablePidsFromAnotherLibrary = (
manifestations: Manifestation[]
) => {
const matchingManifestations = manifestations.filter(({ catalogueCodes }) =>
catalogueCodes?.otherCatalogues.some((code) => code.startsWith("OVE"))
);

return matchingManifestations.map(({ pid }) => pid);
};

export default {};

/* ********************************* Vitest Section ********************************* */
if (import.meta.vitest) {
const { describe, expect, it } = import.meta.vitest;

describe("getReservablePidsFromAnotherLibrary", () => {
it("should return true for isReservable and correct pids if manifestations are reservable on another library (catalogueCodes starts with 'OVE')", () => {
const result = getReservablePidsFromAnotherLibrary([
{
pid: "870970-basis:135721719",
catalogueCodes: {
otherCatalogues: ["OVE123"],
nationalBibliography: ["ABE123"]
}
},
{
pid: "870970-basis:135721719",
catalogueCodes: {
otherCatalogues: ["OVE124"],
nationalBibliography: ["ABE456"]
}
}
] as unknown as Manifestation[]);

expect(result.length > 0).toBe(true);
expect(result).toEqual([
"870970-basis:135721719",
"870970-basis:135721719"
]);
});

it("should return false for isReservable and empty pids array if no manifestations are reservable on another library", () => {
const result = getReservablePidsFromAnotherLibrary([
{
pid: "pid1",
catalogueCodes: {
otherCatalogues: ["ABE789"],
nationalBibliography: ["ABE123"]
}
}
] as unknown as Manifestation[]);

expect(result.length > 0).toBe(false);
expect(result).toEqual([]);
});

it("should filter out non-reservable manifestations and return only reservable pids", () => {
const result = getReservablePidsFromAnotherLibrary([
{
pid: "870970-basis:135721719",
catalogueCodes: {
otherCatalogues: ["OVE123"],
nationalBibliography: ["ABE123"]
}
},
{
pid: "870970-basis:111111111",
catalogueCodes: {
otherCatalogues: ["ABE789"],
nationalBibliography: ["ABE456"]
}
}
] as unknown as Manifestation[]);

expect(result.length > 0).toBe(true);
expect(result).toEqual(["870970-basis:135721719"]);
});
});
// TODO: test for getReservablePidsFromAnotherLibrary should be change to test useReservableFromAnotherLibrary hook instead
// describe("getReservablePidsFromAnotherLibrary", () => {
// it("should return true for isReservable and correct pids if manifestations are reservable on another library (catalogueCodes starts with 'OVE')", () => {
// const result = getReservablePidsFromAnotherLibrary([
// {
// pid: "870970-basis:135721719",
// catalogueCodes: {
// otherCatalogues: ["OVE123"],
// nationalBibliography: ["ABE123"]
// }
// },
// {
// pid: "870970-basis:135721719",
// catalogueCodes: {
// otherCatalogues: ["OVE124"],
// nationalBibliography: ["ABE456"]
// }
// }
// ] as unknown as Manifestation[]);

// expect(result.length > 0).toBe(true);
// expect(result).toEqual([
// "870970-basis:135721719",
// "870970-basis:135721719"
// ]);
// });

// it("should return false for isReservable and empty pids array if no manifestations are reservable on another library", () => {
// const result = getReservablePidsFromAnotherLibrary([
// {
// pid: "pid1",
// catalogueCodes: {
// otherCatalogues: ["ABE789"],
// nationalBibliography: ["ABE123"]
// }
// }
// ] as unknown as Manifestation[]);

// expect(result.length > 0).toBe(false);
// expect(result).toEqual([]);
// });

// it("should filter out non-reservable manifestations and return only reservable pids", () => {
// const result = getReservablePidsFromAnotherLibrary([
// {
// pid: "870970-basis:135721719",
// catalogueCodes: {
// otherCatalogues: ["OVE123"],
// nationalBibliography: ["ABE123"]
// }
// },
// {
// pid: "870970-basis:111111111",
// catalogueCodes: {
// otherCatalogues: ["ABE789"],
// nationalBibliography: ["ABE456"]
// }
// }
// ] as unknown as Manifestation[]);

// expect(result.length > 0).toBe(true);
// expect(result).toEqual(["870970-basis:135721719"]);
// });
// });

describe("getMaterialTypes", () => {
const manifestations = [
Expand Down
28 changes: 28 additions & 0 deletions src/core/utils/useReservableFromAnotherLibrary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useConfig } from "./config";
import { getAllFaustIds } from "./helpers/general";
import { useGetHoldings } from "../../apps/material/helper";
import { Manifestation } from "./types/entities";

const useReservableFromAnotherLibrary = (manifestations: Manifestation[]) => {
const config = useConfig();
const { data: holdingsData } = useGetHoldings({
faustIds: getAllFaustIds(manifestations),
config
});

// If there is no holdings data or if there are holdings that are reservable, we return an empty array.
// Because we use the array length to determine if we should show the button or not.
if (holdingsData?.some(({ reservable }) => reservable === true)) {
return { reservablePidsFromAnotherLibrary: [] };
}

const reservablePidsFromAnotherLibrary = manifestations
.filter(({ catalogueCodes }) =>
catalogueCodes?.otherCatalogues.some((code) => code.startsWith("OVE"))
)
.map(({ pid }) => pid);

return { reservablePidsFromAnotherLibrary };
};

export default useReservableFromAnotherLibrary;

0 comments on commit 8975521

Please sign in to comment.