Skip to content

Commit

Permalink
fix: only modify admin query when session exists
Browse files Browse the repository at this point in the history
  • Loading branch information
IanFonzie committed Nov 6, 2023
1 parent 85c9a86 commit e37bf22
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/back-end/lib/db/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ function generateOrganizationQuery(connection: Connection) {
})
.as("numTeamMembers"),
connection.raw(
'(select coalesce(json_agg(sa), \'[]\' :: json) as "serviceAreas" from "twuOrganizationServiceAreas" tosa join "serviceAreas" sa ON tosa."serviceArea" = sa.id where tosa.organization = ?)',
'(select coalesce(json_agg(sa), \'[]\' :: json) as "serviceAreas" from "twuOrganizationServiceAreas" tosa join "serviceAreas" sa on tosa."serviceArea" = sa.id where tosa.organization = ?)',
connection.ref("organizations.id")
)
);
Expand Down Expand Up @@ -318,25 +318,30 @@ export const readManyOrganizations = tryDb<
[Session, boolean?, number?, number?],
ReadManyResponseBody
>(async (connection, session, allowInactive = false, page, pageSize) => {
let query = generateOrganizationQuery(connection).select(
connection.raw('exists(?) as "viewerIsOrgAdmin"', [
connection
.select("user")
.from("affiliations")
.where({
organization: connection.ref("organizations.id"),
membershipStatus: MembershipStatus.Active,
membershipType: MembershipType.Admin,
user: session?.user.id
})
.first()
])
);
let query = generateOrganizationQuery(connection);

if (!allowInactive) {
query = query.andWhere({ "organizations.active": true });
}

// Used to render links for viewing organizations.
if (session) {
query = query.select(
connection.raw('exists(?) as "viewerIsOrgAdmin"', [
connection
.select("user")
.from("affiliations")
.where({
organization: connection.ref("organizations.id"),
membershipStatus: MembershipStatus.Active,
membershipType: MembershipType.Admin,
user: session.user.id
})
.first()
])
);
}

// Default is to only have one page because we are requesting everything.
let numPages = 1;

Expand Down

0 comments on commit e37bf22

Please sign in to comment.