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

fix: fixed issue in irl events filter #1944

Merged
merged 1 commit into from
Oct 30, 2024
Merged
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
2 changes: 1 addition & 1 deletion apps/web-api/src/internals/pl-events.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class PLEventsInternalController {
builtQuery.where = {
AND: [
builtQuery.where,
this.eventService.buildSearchFilter(request.query),
this.eventGuestsService.buildSearchFilter(request.query),
]
};
}
Expand Down
54 changes: 42 additions & 12 deletions apps/web-api/src/pl-events/pl-event-guests.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ export class PLEventGuestsService {
} else {
events = await this.eventLocationsService.getPastEventsByLocation(locationUid);
}
events = await this.filterEventsByAttendanceAndAdminStatus(filteredEvents, events, member);
if (events.length === 0)
return [];
events = await this.filterEventsByAttendanceAndAdminStatus(filteredEvents, events, member);
const result = await this.fetchAttendees({
eventUids: events?.map(event => event.uid),
...query,
Expand Down Expand Up @@ -197,7 +197,8 @@ export class PLEventGuestsService {
event: {
select: {
uid: true,
name: true
name: true,
websiteURL: true
}
},
team: {
Expand Down Expand Up @@ -344,18 +345,17 @@ export class PLEventGuestsService {
* @returns {Promise<PLEvent[]>} Filtered array of events based on the user’s attendance, login state, and admin status.
*/
async filterEventsByAttendanceAndAdminStatus(filteredEventsUid, events: PLEvent[], member): Promise<PLEvent[]> {
// Scenario 1: If the user is logged out, remove all invite-only events
const inviteOnlyEventsUid = events.filter(event => event.type === "INVITE_ONLY").map(event => event.uid);
if (filteredEventsUid?.length > 0 && !member) {
const publicFilteredUids = filteredEventsUid.filter(uid => !inviteOnlyEventsUid.includes(uid));
return events.filter(event => publicFilteredUids.includes(event.uid));
return events.filter(event => filteredEventsUid?.includes(event.uid))
.filter(event => event.type !== "INVITE_ONLY");
}
// If the user is logged out, remove all invite-only events
if (!member) {
return events.filter(event => event.type !== "INVITE_ONLY");
}
// If the user is an admin, return all events without filtering
if (this.memberService.checkIfAdminUser(member)) {
return events;
return filteredEventsUid?.length ? events.filter(event => filteredEventsUid.includes(event.uid)): events;
}
// Scenario 2: If the user is logged in and not an admin, get invite-only events they are attending
const userAttendedEvents = await this.prisma.pLEvent.findMany({
Expand All @@ -374,11 +374,9 @@ export class PLEventGuestsService {
// Create a Set of attended invite-only event UIDs for efficient lookup
const attendedEventUids = new Set(userAttendedEvents.map(event => event.uid));
// Filter events to keep non-invite-only events and attended invite-only events
if (filteredEventsUid.length > 0) {
return filteredEventsUid.filter(eventUid => {
const event:any = events.find(event => event.uid === eventUid);
return event?.type !== "INVITE_ONLY" || attendedEventUids.has(event?.uid);
});
if (filteredEventsUid?.length > 0) {
return events.filter(event => filteredEventsUid?.includes(event.uid))
.filter(event => event.type !== "INVITE_ONLY" || attendedEventUids.has(event?.uid));
}
return events.filter(event =>
event.type !== "INVITE_ONLY" || attendedEventUids.has(event.uid)
Expand Down Expand Up @@ -783,4 +781,36 @@ export class PLEventGuestsService {
this.handleErrors(err);
}
}

/**
* This method constructs a dynamic query to search for the given text by either
* member name or team name based on query parameters.
* @param query An object containing `searchBy` (either 'member' or 'team') and `searchText` (the name to search for)
* @returns Constructed query based on the given text input, using a `startsWith` filter.
*/
buildSearchFilter(query) {
const { searchBy, searchText } = query;
if (searchBy === "member") {
return {
member: {
name: {
startsWith: searchText,
mode: 'insensitive',
},
}
};
} else if (searchBy === "team") {
return {
team: {
name: {
startsWith: searchText,
mode: 'insensitive'
}
}
};
} else {
return {};
}
};

}
7 changes: 6 additions & 1 deletion apps/web-api/src/pl-events/pl-event-locations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ export class PLEventLocationsService {
logo: true,
banner: true,
resources: true,
additionalInfo: true
additionalInfo: true,
_count: {
select: {
eventGuests: true
}
}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion apps/web-api/src/pl-events/pl-events.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,5 +242,4 @@ export class PLEventsService {
}
return {};
};

}
Loading