|
1 | 1 | import type { communitiesTable } from "~/src/drizzle/tables/communities";
|
2 | 2 | import { builder } from "~/src/graphql/builder";
|
| 3 | +import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError"; |
| 4 | +import type { GraphQLContext } from "../../context"; |
| 5 | +import type { User } from "../User/User"; |
3 | 6 |
|
4 | 7 | export type Community = typeof communitiesTable.$inferSelect;
|
5 | 8 |
|
6 | 9 | export const Community = builder.objectRef<Community>("Community");
|
7 | 10 |
|
| 11 | +export type CommunityResolvers = { |
| 12 | + updater: ( |
| 13 | + parent: Community, |
| 14 | + _args: unknown, |
| 15 | + context: GraphQLContext, |
| 16 | + ) => Promise<User | null>; |
| 17 | +}; |
| 18 | + |
| 19 | +export const CommunityResolver: CommunityResolvers = { |
| 20 | + updater: async (parent, _args, context) => { |
| 21 | + try { |
| 22 | + if (!context.currentClient.isAuthenticated) { |
| 23 | + throw new TalawaGraphQLError({ |
| 24 | + message: "User is not authenticated", |
| 25 | + extensions: { code: "unauthenticated" }, |
| 26 | + }); |
| 27 | + } |
| 28 | + if (!parent.updaterId) { |
| 29 | + return null; |
| 30 | + } |
| 31 | + const updaterId = parent.updaterId; |
| 32 | + |
| 33 | + const existingUser = |
| 34 | + await context.drizzleClient.query.usersTable.findFirst({ |
| 35 | + where: (users, { eq }) => eq(users.id, updaterId), // Must use updaterId here |
| 36 | + }); |
| 37 | + |
| 38 | + if (existingUser === undefined) { |
| 39 | + context.log.warn(`No user found for updaterId: ${updaterId}`); |
| 40 | + throw new TalawaGraphQLError({ |
| 41 | + message: "Updater user not found", |
| 42 | + extensions: { |
| 43 | + code: "arguments_associated_resources_not_found", |
| 44 | + issues: [{ argumentPath: ["updaterId"] }], |
| 45 | + }, |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + const updater = await context.drizzleClient.query.usersTable.findFirst({ |
| 50 | + where: (users, { eq, and, isNull }) => |
| 51 | + parent.updaterId ? eq(users.id, parent.updaterId) : isNull(users.id), |
| 52 | + }); |
| 53 | + |
| 54 | + if (!updater) { |
| 55 | + context.log.warn(`No user found for updaterId: ${parent.updaterId}`); |
| 56 | + throw new TalawaGraphQLError({ |
| 57 | + message: "Updater user not found", |
| 58 | + extensions: { |
| 59 | + code: "arguments_associated_resources_not_found", |
| 60 | + issues: [{ argumentPath: ["updaterId"] }], |
| 61 | + }, |
| 62 | + }); |
| 63 | + } |
| 64 | + return updater; |
| 65 | + } catch (error) { |
| 66 | + context.log.error("Database error in community updater resolver", { |
| 67 | + error, |
| 68 | + }); |
| 69 | + throw error; |
| 70 | + } |
| 71 | + }, |
| 72 | +}; |
| 73 | + |
8 | 74 | Community.implement({
|
9 | 75 | description:
|
10 | 76 | "Communitys are controlled spaces of collections of users who associate with the purpose those communities exist for.",
|
|
0 commit comments