|
| 1 | +import type { GraphQLContext } from "~/src/graphql/context"; |
1 | 2 | import { User } from "~/src/graphql/types/User/User";
|
2 | 3 | import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError";
|
3 | 4 | import { Post } from "./Post";
|
| 5 | +import type { Post as PostType } from "./Post"; |
4 | 6 |
|
5 |
| -Post.implement({ |
6 |
| - fields: (t) => ({ |
7 |
| - updater: t.field({ |
8 |
| - description: "User who last updated the post.", |
9 |
| - resolve: async (parent, _args, ctx) => { |
10 |
| - if (!ctx.currentClient.isAuthenticated) { |
11 |
| - throw new TalawaGraphQLError({ |
12 |
| - extensions: { |
13 |
| - code: "unauthenticated", |
14 |
| - }, |
15 |
| - }); |
16 |
| - } |
17 |
| - |
18 |
| - const currentUserId = ctx.currentClient.user.id; |
| 7 | +export const resolveUpdater = async ( |
| 8 | + parent: PostType, |
| 9 | + _args: Record<string, never>, |
| 10 | + ctx: GraphQLContext, |
| 11 | +) => { |
| 12 | + if (!ctx.currentClient.isAuthenticated) { |
| 13 | + throw new TalawaGraphQLError({ |
| 14 | + extensions: { |
| 15 | + code: "unauthenticated", |
| 16 | + }, |
| 17 | + }); |
| 18 | + } |
19 | 19 |
|
20 |
| - const currentUser = await ctx.drizzleClient.query.usersTable.findFirst({ |
21 |
| - with: { |
22 |
| - organizationMembershipsWhereMember: { |
23 |
| - columns: { |
24 |
| - role: true, |
25 |
| - }, |
26 |
| - where: (fields, operators) => |
27 |
| - operators.eq(fields.organizationId, parent.organizationId), |
28 |
| - }, |
29 |
| - }, |
30 |
| - where: (fields, operators) => operators.eq(fields.id, currentUserId), |
31 |
| - }); |
| 20 | + const currentUserId = ctx.currentClient.user.id; |
32 | 21 |
|
33 |
| - if (currentUser === undefined) { |
34 |
| - throw new TalawaGraphQLError({ |
35 |
| - extensions: { |
36 |
| - code: "unauthenticated", |
37 |
| - }, |
38 |
| - }); |
39 |
| - } |
| 22 | + const currentUser = await ctx.drizzleClient.query.usersTable.findFirst({ |
| 23 | + with: { |
| 24 | + organizationMembershipsWhereMember: { |
| 25 | + columns: { |
| 26 | + role: true, |
| 27 | + }, |
| 28 | + where: (fields, operators) => |
| 29 | + operators.eq(fields.organizationId, parent.organizationId), |
| 30 | + }, |
| 31 | + }, |
| 32 | + where: (fields, operators) => operators.eq(fields.id, currentUserId), |
| 33 | + }); |
40 | 34 |
|
41 |
| - const currentUserOrganizationMembership = |
42 |
| - currentUser.organizationMembershipsWhereMember[0]; |
| 35 | + if (currentUser === undefined) { |
| 36 | + throw new TalawaGraphQLError({ |
| 37 | + extensions: { |
| 38 | + code: "unauthenticated", |
| 39 | + }, |
| 40 | + }); |
| 41 | + } |
43 | 42 |
|
44 |
| - if ( |
45 |
| - currentUser.role !== "administrator" && |
46 |
| - (currentUserOrganizationMembership === undefined || |
47 |
| - currentUserOrganizationMembership.role !== "administrator") |
48 |
| - ) { |
49 |
| - throw new TalawaGraphQLError({ |
50 |
| - extensions: { |
51 |
| - code: "unauthorized_action", |
52 |
| - }, |
53 |
| - }); |
54 |
| - } |
| 43 | + const currentUserOrganizationMembership = |
| 44 | + currentUser.organizationMembershipsWhereMember[0]; |
55 | 45 |
|
56 |
| - if (parent.updaterId === null) { |
57 |
| - return null; |
58 |
| - } |
| 46 | + if ( |
| 47 | + currentUser.role !== "administrator" && |
| 48 | + (currentUserOrganizationMembership === undefined || |
| 49 | + currentUserOrganizationMembership.role !== "administrator") |
| 50 | + ) { |
| 51 | + throw new TalawaGraphQLError({ |
| 52 | + extensions: { |
| 53 | + code: "unauthorized_action", |
| 54 | + }, |
| 55 | + }); |
| 56 | + } |
59 | 57 |
|
60 |
| - if (parent.updaterId === currentUserId) { |
61 |
| - return currentUser; |
62 |
| - } |
| 58 | + if (parent.updaterId === null) { |
| 59 | + return null; |
| 60 | + } |
63 | 61 |
|
64 |
| - const updaterId = parent.updaterId; |
| 62 | + if (parent.updaterId === currentUserId) { |
| 63 | + return currentUser; |
| 64 | + } |
65 | 65 |
|
66 |
| - const existingUser = await ctx.drizzleClient.query.usersTable.findFirst( |
67 |
| - { |
68 |
| - where: (fields, operators) => operators.eq(fields.id, updaterId), |
69 |
| - }, |
70 |
| - ); |
| 66 | + const updaterId = parent.updaterId; |
71 | 67 |
|
72 |
| - // Updater id existing but the associated user not existing is a business logic error and probably means that the corresponding data in the database is in a corrupted state. It must be investigated and fixed as soon as possible to prevent additional data corruption. |
73 |
| - if (existingUser === undefined) { |
74 |
| - ctx.log.error( |
75 |
| - "Postgres select operation returned an empty array for a post's updater id that isn't null.", |
76 |
| - ); |
77 |
| - throw new TalawaGraphQLError({ |
78 |
| - extensions: { |
79 |
| - code: "unexpected", |
80 |
| - }, |
81 |
| - }); |
82 |
| - } |
| 68 | + const existingUser = await ctx.drizzleClient.query.usersTable.findFirst({ |
| 69 | + where: (fields, operators) => operators.eq(fields.id, updaterId), |
| 70 | + }); |
83 | 71 |
|
84 |
| - return existingUser; |
| 72 | + // Updater id existing but the associated user not existing is a business logic error and probably means that the corresponding data in the database is in a corrupted state. It must be investigated and fixed as soon as possible to prevent additional data corruption. |
| 73 | + if (existingUser === undefined) { |
| 74 | + ctx.log.error( |
| 75 | + "Postgres select operation returned an empty array for a post's updater id that isn't null.", |
| 76 | + ); |
| 77 | + throw new TalawaGraphQLError({ |
| 78 | + extensions: { |
| 79 | + code: "unexpected", |
85 | 80 | },
|
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + return existingUser; |
| 85 | +}; |
| 86 | + |
| 87 | +Post.implement({ |
| 88 | + fields: (t) => ({ |
| 89 | + updater: t.field({ |
| 90 | + description: "User who last updated the post.", |
| 91 | + resolve: resolveUpdater, |
86 | 92 | type: User,
|
87 | 93 | }),
|
88 | 94 | }),
|
|
0 commit comments