|
1 | 1 | import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError";
|
| 2 | +import type { GraphQLContext } from "../../context"; |
2 | 3 | import { Advertisement } from "./Advertisement";
|
| 4 | +import type { Advertisement as AdvertisementType } from "./Advertisement"; |
3 | 5 |
|
4 |
| -Advertisement.implement({ |
5 |
| - fields: (t) => ({ |
6 |
| - updatedAt: t.field({ |
7 |
| - description: "Date time at the time the advertisement was last updated.", |
8 |
| - resolve: async (parent, _args, ctx) => { |
9 |
| - if (!ctx.currentClient.isAuthenticated) { |
10 |
| - throw new TalawaGraphQLError({ |
11 |
| - extensions: { |
12 |
| - code: "unauthenticated", |
13 |
| - }, |
14 |
| - }); |
15 |
| - } |
16 |
| - |
17 |
| - const currentUserId = ctx.currentClient.user.id; |
18 |
| - |
19 |
| - const currentUser = await ctx.drizzleClient.query.usersTable.findFirst({ |
| 6 | +export const advertisementUpdatedAtResolver = async ( |
| 7 | + parent: AdvertisementType, |
| 8 | + _args: unknown, |
| 9 | + ctx: GraphQLContext, |
| 10 | +) => { |
| 11 | + try { |
| 12 | + if (!parent.updatedAt) { |
| 13 | + throw new TalawaGraphQLError({ |
| 14 | + message: "Missing updatedAt value for the advertisement", |
| 15 | + extensions: { |
| 16 | + code: "unexpected", |
| 17 | + }, |
| 18 | + }); |
| 19 | + } |
| 20 | + if (!ctx.currentClient.isAuthenticated) { |
| 21 | + throw new TalawaGraphQLError({ |
| 22 | + extensions: { |
| 23 | + code: "unauthenticated", |
| 24 | + }, |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + const currentUserId = ctx.currentClient.user.id; |
| 29 | + |
| 30 | + const currentUser = await ctx.drizzleClient.query.usersTable.findFirst({ |
| 31 | + columns: { |
| 32 | + role: true, |
| 33 | + }, |
| 34 | + with: { |
| 35 | + organizationMembershipsWhereMember: { |
20 | 36 | columns: {
|
21 | 37 | role: true,
|
22 | 38 | },
|
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 |
| - }); |
34 |
| - |
35 |
| - if (currentUser === undefined) { |
36 |
| - throw new TalawaGraphQLError({ |
37 |
| - extensions: { |
38 |
| - code: "unauthenticated", |
39 |
| - }, |
40 |
| - }); |
41 |
| - } |
42 |
| - |
43 |
| - const currentUserOrganizationMembership = |
44 |
| - currentUser.organizationMembershipsWhereMember[0]; |
45 |
| - |
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 |
| - } |
57 |
| - |
58 |
| - return parent.updatedAt; |
| 39 | + where: (fields, operators) => |
| 40 | + operators.eq(fields.organizationId, parent.organizationId), |
| 41 | + }, |
| 42 | + }, |
| 43 | + where: (fields, operators) => operators.eq(fields.id, currentUserId), |
| 44 | + }); |
| 45 | + |
| 46 | + if (currentUser === undefined) { |
| 47 | + throw new TalawaGraphQLError({ |
| 48 | + extensions: { |
| 49 | + code: "unauthenticated", |
| 50 | + }, |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + const currentUserOrganizationMembership = |
| 55 | + currentUser.organizationMembershipsWhereMember[0]; |
| 56 | + |
| 57 | + if ( |
| 58 | + currentUser.role !== "administrator" && |
| 59 | + (currentUserOrganizationMembership === undefined || |
| 60 | + currentUserOrganizationMembership.role !== "administrator") |
| 61 | + ) { |
| 62 | + throw new TalawaGraphQLError({ |
| 63 | + extensions: { |
| 64 | + code: "unauthorized_action", |
| 65 | + }, |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + return parent.updatedAt; |
| 70 | + } catch (error) { |
| 71 | + if (error instanceof TalawaGraphQLError) { |
| 72 | + throw error; |
| 73 | + } |
| 74 | + ctx.log.error(error); |
| 75 | + throw new TalawaGraphQLError({ |
| 76 | + message: "Internal server error", |
| 77 | + extensions: { |
| 78 | + code: "unexpected", |
59 | 79 | },
|
| 80 | + }); |
| 81 | + } |
| 82 | +}; |
| 83 | + |
| 84 | +Advertisement.implement({ |
| 85 | + fields: (t) => ({ |
| 86 | + updatedAt: t.field({ |
| 87 | + description: "Date time at the time the advertisement was last updated.", |
| 88 | + resolve: advertisementUpdatedAtResolver, |
60 | 89 | type: "DateTime",
|
61 | 90 | }),
|
62 | 91 | }),
|
|
0 commit comments