Skip to content

Commit

Permalink
Update organisation file name [#87]
Browse files Browse the repository at this point in the history
  • Loading branch information
sin2san committed Jun 9, 2024
1 parent 0906fcb commit fa407e6
Showing 1 changed file with 41 additions and 30 deletions.
71 changes: 41 additions & 30 deletions src/data/Model/Organisation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import {
SnapshotOptions,
QuerySnapshot,
where,
Query,
query,
QueryConstraint,
getDoc,
doc,
limit,
startAfter,
DocumentSnapshot,
getCountFromServer,
} from "firebase/firestore";
import { Collections } from "../../services/Firebase/Names";
import { db } from "../../services/Firebase/FirebaseConfig";
Expand Down Expand Up @@ -119,16 +121,17 @@ export type OrganisationListingQueryFilters = {
export async function getOrganisationsForListingsPage(
filters?: OrganisationListingQueryFilters,
skipOrgName?: string,
limitNum: number = 0
): Promise<Organisation[]> {
console.log(
`Getting organisation with filters: ${JSON.stringify(filters, null, 2)}`
);

limitNum: number = 0,
lastVisible?: DocumentSnapshot<DocumentData>
): Promise<{
organisations: Organisation[];
lastVisible: DocumentSnapshot<DocumentData> | null;
totalCount: number;
}> {
const queryConstraints: QueryConstraint[] = [];
let onlyServicesFilter = false;

if (filters !== undefined) {
if (filters) {
if ((filters.specialisations?.length ?? 0) > 10) {
return Promise.reject(
new RangeError(
Expand Down Expand Up @@ -170,45 +173,53 @@ export async function getOrganisationsForListingsPage(
}

if (filters.services !== undefined && !queryConstraints.length) {
// must perform this condition at the end
// current limitation in firestore does not allow the use of
// 'array-contains-any' and 'in' clause in the same query

onlyServicesFilter = true;
queryConstraints.push(
where("services", "array-contains-any", filters.services)
);
}
}

if (limitNum > 0) {
queryConstraints.push(limit(limitNum));
if (skipOrgName) {
queryConstraints.push(where("name", "!=", skipOrgName));
}

if (skipOrgName !== undefined) {
queryConstraints.push(where("name", "!=", skipOrgName));
if (lastVisible) {
queryConstraints.push(startAfter(lastVisible));
}
// define the organisation collection reference
const orgsRef: Query<Organisation> = collection(
db,
Collections.organisations
).withConverter<Organisation>(organisationConverter);

// get organisations with the supplied filters
const orgsRef = collection(db, Collections.organisations).withConverter(
organisationConverter
);

// Get total count
const totalCountSnapshot = await getCountFromServer(
query(orgsRef, ...queryConstraints)
);
const totalCount = totalCountSnapshot.data().count;

if (limitNum > 0) {
queryConstraints.push(limit(limitNum));
}

// Get paginated organisations
const querySnapshot: QuerySnapshot<Organisation> = await getDocs(
query(orgsRef, ...queryConstraints)
);

const orgs: Organisation[] = [];
querySnapshot.forEach((doc) => orgs.push(doc.data()));

if (filters?.services !== undefined && !onlyServicesFilter) {
return orgs.filter((org) =>
org.services.some((s) => filters?.services?.includes(s))
);
}
querySnapshot.forEach((doc) => {
orgs.push(doc.data());
});

return orgs;
return {
organisations: orgs,
lastVisible:
querySnapshot.docs.length > 0
? querySnapshot.docs[querySnapshot.docs.length - 1]
: null,
totalCount: totalCount,
};
}

// get an organisation for profile page
Expand Down

0 comments on commit fa407e6

Please sign in to comment.