Skip to content

Commit c444fed

Browse files
committed
event-creator-test
1 parent 5255740 commit c444fed

File tree

2 files changed

+458
-68
lines changed

2 files changed

+458
-68
lines changed

src/graphql/types/Event/creator.ts

Lines changed: 87 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,108 @@
11
import { User } from "~/src/graphql/types/User/User";
22
import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError";
3+
import type { GraphQLContext } from "../../context";
34
import { Event } from "./Event";
5+
import type { Event as EventType } from "./Event";
46

5-
Event.implement({
6-
fields: (t) => ({
7-
creator: t.field({
8-
description: "User who created the event.",
9-
resolve: async (parent, _args, ctx) => {
10-
if (!ctx.currentClient.isAuthenticated) {
11-
throw new TalawaGraphQLError({
12-
extensions: {
13-
code: "unauthenticated",
14-
},
15-
});
16-
}
7+
export const eventCreatorResolver = async (
8+
parent: EventType,
9+
_args: unknown,
10+
ctx: GraphQLContext,
11+
) => {
12+
if (!ctx.currentClient.isAuthenticated) {
13+
throw new TalawaGraphQLError({
14+
extensions: {
15+
code: "unauthenticated",
16+
},
17+
});
18+
}
1719

18-
const currentUserId = ctx.currentClient.user.id;
20+
try {
21+
const currentUserId = ctx.currentClient.user.id;
1922

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-
},
23+
const currentUser = await ctx.drizzleClient.query.usersTable.findFirst({
24+
with: {
25+
organizationMembershipsWhereMember: {
26+
columns: {
27+
role: true,
2928
},
30-
where: (fields, operators) => operators.eq(fields.id, currentUserId),
31-
});
29+
where: (fields, operators) =>
30+
operators.eq(fields.organizationId, parent.organizationId),
31+
},
32+
},
33+
where: (fields, operators) => operators.eq(fields.id, currentUserId),
34+
});
3235

33-
if (currentUser === undefined) {
34-
throw new TalawaGraphQLError({
35-
extensions: {
36-
code: "unauthenticated",
37-
},
38-
});
39-
}
36+
if (currentUser === undefined) {
37+
throw new TalawaGraphQLError({
38+
extensions: {
39+
code: "unauthenticated",
40+
},
41+
});
42+
}
4043

41-
const currentUserOrganizationMembership =
42-
currentUser.organizationMembershipsWhereMember[0];
44+
const currentUserOrganizationMembership =
45+
currentUser.organizationMembershipsWhereMember[0];
4346

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-
}
47+
if (
48+
currentUser.role !== "administrator" &&
49+
(currentUserOrganizationMembership === undefined ||
50+
currentUserOrganizationMembership.role !== "administrator")
51+
) {
52+
throw new TalawaGraphQLError({
53+
extensions: {
54+
code: "unauthorized_action",
55+
},
56+
});
57+
}
5558

56-
if (parent.creatorId === null) {
57-
return null;
58-
}
59+
if (parent.creatorId === null) {
60+
return null;
61+
}
5962

60-
if (parent.creatorId === currentUserId) {
61-
return currentUser;
62-
}
63+
if (parent.creatorId === currentUserId) {
64+
return currentUser;
65+
}
6366

64-
const creatorId = parent.creatorId;
67+
const creatorId = parent.creatorId;
6568

66-
const existingUser = await ctx.drizzleClient.query.usersTable.findFirst(
67-
{
68-
where: (fields, operators) => operators.eq(fields.id, creatorId),
69-
},
70-
);
69+
const existingUser = await ctx.drizzleClient.query.usersTable.findFirst({
70+
where: (fields, operators) => operators.eq(fields.id, creatorId),
71+
});
7172

72-
// Creator 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 an event's creator id that isn't null.",
76-
);
73+
// Creator 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.
74+
if (existingUser === undefined) {
75+
ctx.log.error(
76+
"Postgres select operation returned an empty array for an event's creator id that isn't null.",
77+
);
7778

78-
throw new TalawaGraphQLError({
79-
extensions: {
80-
code: "unexpected",
81-
},
82-
});
83-
}
79+
throw new TalawaGraphQLError({
80+
extensions: {
81+
code: "unexpected",
82+
},
83+
});
84+
}
8485

85-
return existingUser;
86+
return existingUser;
87+
} catch (error) {
88+
if (error instanceof TalawaGraphQLError) {
89+
throw error;
90+
}
91+
ctx.log.error(error);
92+
throw new TalawaGraphQLError({
93+
message: "Internal server error",
94+
extensions: {
95+
code: "unexpected",
8696
},
97+
});
98+
}
99+
};
100+
101+
Event.implement({
102+
fields: (t) => ({
103+
creator: t.field({
104+
description: "User who created the event.",
105+
resolve: eventCreatorResolver,
87106
type: User,
88107
}),
89108
}),

0 commit comments

Comments
 (0)