diff --git a/apps/web-api/src/internals/pl-events.controller.ts b/apps/web-api/src/internals/pl-events.controller.ts index 45a6b165b..9f5f34725 100644 --- a/apps/web-api/src/internals/pl-events.controller.ts +++ b/apps/web-api/src/internals/pl-events.controller.ts @@ -38,7 +38,7 @@ export class PLEventsInternalController { builtQuery.where = { AND: [ builtQuery.where, - this.eventService.buildSearchFilter(request.query), + this.eventService.buildMemberOrTeamFilter(request.query), ] }; } diff --git a/apps/web-api/src/pl-events/pl-event-guests.service.ts b/apps/web-api/src/pl-events/pl-event-guests.service.ts index 0b2a38bc0..adda87ce5 100644 --- a/apps/web-api/src/pl-events/pl-event-guests.service.ts +++ b/apps/web-api/src/pl-events/pl-event-guests.service.ts @@ -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, @@ -344,18 +344,17 @@ export class PLEventGuestsService { * @returns {Promise} Filtered array of events based on the user’s attendance, login state, and admin status. */ async filterEventsByAttendanceAndAdminStatus(filteredEventsUid, events: PLEvent[], member): Promise { - // 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({ @@ -374,11 +373,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) diff --git a/apps/web-api/src/pl-events/pl-event-locations.service.ts b/apps/web-api/src/pl-events/pl-event-locations.service.ts index a65f8921e..ed8090b6d 100644 --- a/apps/web-api/src/pl-events/pl-event-locations.service.ts +++ b/apps/web-api/src/pl-events/pl-event-locations.service.ts @@ -96,7 +96,12 @@ export class PLEventLocationsService { logo: true, banner: true, resources: true, - additionalInfo: true + additionalInfo: true, + _count: { + select: { + eventGuests: true + } + } } } } diff --git a/apps/web-api/src/pl-events/pl-events.service.ts b/apps/web-api/src/pl-events/pl-events.service.ts index 15738eda4..d81dd2a19 100644 --- a/apps/web-api/src/pl-events/pl-events.service.ts +++ b/apps/web-api/src/pl-events/pl-events.service.ts @@ -243,4 +243,36 @@ export class PLEventsService { return {}; }; + /** + * This method construct the dynamic query to search the given text in either + * by member name , by team name from query params + * This method builts a query to enable search by team name or member name. + * @param query name of the team or member which is to be fetched + * @returns Constructed query based on given text(name) input. + */ + buildMemberOrTeamFilter(query) { + const { searchBy, searchText } = query; + if (searchBy === "member") { + return { + member: { + name: { + contains : searchText, + mode: 'insensitive', + }, + } + } + } else if (searchBy === "team") { + return { + team: { + name: { + contains: searchText, + mode: 'insensitive' + } + } + }; + } + else { + return {}; + } + }; }