diff --git a/src/api/graphql/resolvers/nonprofits.resolvers.ts b/src/api/graphql/resolvers/nonprofits.resolvers.ts index f38ed8a..0048236 100644 --- a/src/api/graphql/resolvers/nonprofits.resolvers.ts +++ b/src/api/graphql/resolvers/nonprofits.resolvers.ts @@ -6,6 +6,7 @@ import { getNonprofitsWithFilters, updateNonprofit, updateNonprofitSchema, + getChapterIdsByNames, } from '../../../core'; import { GraphQLError } from 'graphql'; import { z } from 'zod'; @@ -14,24 +15,42 @@ import type { NonprofitSortOption, } from '../../../core/services/nonprofits.service'; -// Infer TypeScript types directly from your Zod schemas type CreateNonprofitInput = z.infer; type UpdateNonprofitInput = z.infer; interface NonprofitsQueryArgs { chapterIds?: string[]; + chapterNames?: string[]; statuses?: StatusType[]; sort?: NonprofitSortOption[]; } export const nonprofitResolvers = { Query: { - nonprofits: ( + nonprofits: async ( _parent: unknown, - { chapterIds, statuses, sort }: NonprofitsQueryArgs + { chapterIds, chapterNames, statuses, sort }: NonprofitsQueryArgs ) => { - return getNonprofitsWithFilters({ chapterIds, statuses, sort }); + let resolvedChapterIds = chapterIds; + + // If frontend passes chapterNames, convert them to ids + if ( + (!resolvedChapterIds || resolvedChapterIds.length === 0) && + chapterNames?.length + ) { + resolvedChapterIds = await getChapterIdsByNames(chapterNames); + + // If user selected chapters but none matched, return empty list + if (!resolvedChapterIds || resolvedChapterIds.length === 0) return []; + } + + return getNonprofitsWithFilters({ + chapterIds: resolvedChapterIds, + statuses, + sort, + }); }, + nonprofit: async (_parent: unknown, { id }: { id: string }) => { const nonprofit = await getNonprofitById(id); if (!nonprofit) { @@ -48,6 +67,7 @@ export const nonprofitResolvers = { return nonprofit; }, }, + Mutation: { createNonprofit: ( _parent: unknown, @@ -56,6 +76,7 @@ export const nonprofitResolvers = { const validatedInput = createNonprofitSchema.parse(input); return createNonprofit(validatedInput); }, + updateNonprofit: ( _parent: unknown, { @@ -66,6 +87,7 @@ export const nonprofitResolvers = { const validatedInput = updateNonprofitSchema.parse(input); return updateNonprofit(nonprofit_id, validatedInput); }, + deleteNonprofit: (_parent: unknown, { id }: { id: string }) => { return deleteNonprofit(id); }, diff --git a/src/api/graphql/schemas/nonprofits.schema.ts b/src/api/graphql/schemas/nonprofits.schema.ts index 46e71eb..7feda8d 100644 --- a/src/api/graphql/schemas/nonprofits.schema.ts +++ b/src/api/graphql/schemas/nonprofits.schema.ts @@ -21,6 +21,7 @@ export const nonprofitSchemaString = ` type Query { nonprofits( chapterIds: [ID!] + chapterNames: [String!] statuses: [StatusType!] sort: [NonprofitSortOption!] ): [Nonprofit!]! diff --git a/src/core/services/nonprofits.service.ts b/src/core/services/nonprofits.service.ts index 0b65630..e933788 100644 --- a/src/core/services/nonprofits.service.ts +++ b/src/core/services/nonprofits.service.ts @@ -519,3 +519,15 @@ export async function deleteNonprofit(id: string): Promise { throw new DatabaseError('Failed to delete nonprofit'); } } + +export async function getChapterIdsByNames(names: string[]): Promise { + const cleaned = [...new Set(names.map((n) => n.trim()).filter(Boolean))]; + if (cleaned.length === 0) return []; + + const chapters = await prisma.chapters.findMany({ + where: { name: { in: cleaned } }, + select: { chapter_id: true }, + }); + + return chapters.map((c) => c.chapter_id); +}