Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add agencies endpoint #152

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions src/__tests__/__snapshots__/schematransform.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,105 @@ type AdminData {
requestButton: Boolean
}

type LatitudeLongitude {
\\"\\"\\"Geolocation latitude\\"\\"\\"
latitude: Float!

\\"\\"\\"Geolocation longitude\\"\\"\\"
longitude: Float!
}

type AgencyList {
\\"\\"\\"Total number of agencies with branches that match query\\"\\"\\"
agencyCount: Int!

\\"\\"\\"List of agencyIds with branches that match query\\"\\"\\"
agencyIds: [String!]!

\\"\\"\\"
Total number of branches that match query (across all resulting agencies)
\\"\\"\\"
branchCount: Int!

\\"\\"\\"
List of branchIds of branches that match query (across all resulting agencies)
\\"\\"\\"
branchIds: [String!]!

\\"\\"\\"Agencies with branches that match query\\"\\"\\"
agencies: [Agency!]!
}

type Agency {
\\"\\"\\"Id of the agency (queryable)\\"\\"\\"
agencyId: String!

\\"\\"\\"Name of the agency (queryable)\\"\\"\\"
agencyName: String!

\\"\\"\\"Agency phonenumber (likely the main branch's number or null)\\"\\"\\"
agencyPhone: String

\\"\\"\\"Agency email (likely the main branch's number or null)\\"\\"\\"
agencyEmail: String

\\"\\"\\"Number of branches that match query within agency\\"\\"\\"
branchCount: Int!

\\"\\"\\"List of branchIds of branches that match query within agency\\"\\"\\"
branchIds: [String!]!

\\"\\"\\"Highlights specific to the agency\\"\\"\\"
highlights: [Highlight!]!

\\"\\"\\"Branches that match query within agency\\"\\"\\"
agencyBranches: [AgencyBranch!]!
}

type AgencyBranch {
\\"\\"\\"Id of the branch's agency (queryable)\\"\\"\\"
agencyId: String!

\\"\\"\\"Name of the branch's agency (queryable)\\"\\"\\"
agencyName: String!

\\"\\"\\"Id of the branch (queryable)\\"\\"\\"
branchId: String!

\\"\\"\\"Name of the branch (queryable)\\"\\"\\"
branchName: [String!]!

\\"\\"\\"Shortname of the branch (queryable)\\"\\"\\"
branchShortName: [String!]!

\\"\\"\\"Branch's phonenumber\\"\\"\\"
branchPhone: String

\\"\\"\\"Branch's email\\"\\"\\"
branchEmail: String

\\"\\"\\"Branch's InterLibraryLoan email\\"\\"\\"
branchIllEmail: String

\\"\\"\\"The city the branch is located in (queryable)\\"\\"\\"
city: String

\\"\\"\\"The geolocation of the branch\\"\\"\\"
geolocation: LatitudeLongitude

\\"\\"\\"Highlights specific to the branch and its agency\\"\\"\\"
highlights: [Highlight!]!

\\"\\"\\"Opening hours of the branch (can be a html-element)\\"\\"\\"
openingHours: [String!]!

\\"\\"\\"Address of the branch (queryable)\\"\\"\\"
postalAddress: String

\\"\\"\\"PostalCode of the branch (queryable)\\"\\"\\"
postalCode: Int
}

enum BorchkRequestStatus {
ok
service_not_licensed
Expand Down Expand Up @@ -2235,6 +2334,11 @@ type Query {
session: Session
howru: String
localizations(pids: [String!]!): Localizations

\\"\\"\\"
Agencies with branches that match query (queries on branch's: agencyId, agencyName, branchId, branchName, postalAddress, postalCode, city)
\\"\\"\\"
agencies(q: String!, status: String, limit: Int, offset: Int, language: LanguageCode, bibdkExcludeBranches: Boolean): AgencyList!
refWorks(pid: String!): String!
ris(pid: String!): String!
relatedSubjects(q: [String!]!, limit: Int): [String!]
Expand Down
91 changes: 91 additions & 0 deletions src/datasources/agencies.datasource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import groupBy from "lodash/groupBy";
import sum from "lodash/sum";

function parseAgencyList(agencyList) {
return agencyList.map((agency) => {
return {
agencyId: agency.agencyId,
agencyName: agency.agencyName,
agencyPhone: agency.agencyPhone,
agencyEmail: agency.agencyEmail,
highlights: agency.highlights || [],
branchCount: agency.branchCount,
branchIds: agency.branchIds,
agencyBranches:
agency?.branches?.map((branch) => {
return {
agencyId: agency.agencyId,
agencyName: agency.agencyName,
branchId: branch.branchId,
branchName: branch.branchName || [],
branchShortName: branch.branchShortName || [],
branchPhone: branch.branchPhone,
branchEmail: branch.branchEmail,
branchIllEmail: branch.branchIllEmail,
city: branch.city,
geolocation: branch.geolocation,
highlights: branch.highlights || [],
openingHours: branch.openingHours || [],
postalAddress: branch.postalAddress,
postalCode: branch.postalCode,
};
}) || [],
};
});
}

export async function load(
{
q,
status = "ALLE",
limit = 10,
offset = 0,
language = "da",
bibdkExcludeBranches = true,
},
context
) {
const librariesFound = await context.getLoader("library").load({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

så .. du lavet præcis samme forespørgsel som til library ? - og så grupperer du efter agency ? eller forstår jeg det rigtigt ?

q: q,
status: status,
limit: limit,
offset: offset,
language: language,
bibdkExcludeBranches: bibdkExcludeBranches,
});

const groupedByAgency = groupBy(librariesFound?.result, "agencyId");

const agenciesFlat = Object.values(groupedByAgency)?.map((branches) => {
return {
agencyId: branches?.map((e) => e.agencyId)?.[0],
agencyName: branches?.map((e) => e.agencyName)?.[0],
agencyPhone: branches?.map((e) => e.agencyPhone)?.[0],
agencyEmail: branches?.map((e) => e.agencyEmail)?.[0],
highlights: branches?.map((e) => e.highlights)?.[0],
branchCount: branches?.length,
branchIds: branches?.map((e) => e.branchId) || [],
branches: branches,
};
});

return {
agencyCount: agenciesFlat.length,
agencyIds:
(groupedByAgency &&
Object.keys(groupedByAgency).filter(
(agencyId) => typeof agencyId !== "undefined"
)) ||
[],
branchCount: sum(agenciesFlat.map((agency) => agency.branchCount)),
branchIds: agenciesFlat?.flatMap((agency) => agency.branchIds),
agencies: parseAgencyList(agenciesFlat),
};
}

export const options = {
redis: {
prefix: "agencyList",
ttl: 60 * 15,
},
};
1 change: 1 addition & 0 deletions src/datasources/library.datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const fields = [
"name",
"agencyName",
"agencyId",
"branchName",
"branchId",
"city",
"postalCode",
Expand Down
Loading