Skip to content

Commit 6670799

Browse files
committed
Merge branch 'rate-limiting' of github.com:im-vedant/talawa-api into rate-limiting
2 parents bf5fca6 + c07c2e1 commit 6670799

File tree

2 files changed

+387
-107
lines changed

2 files changed

+387
-107
lines changed

src/graphql/types/AgendaItem/updater.ts

Lines changed: 110 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,130 @@
1+
import type { GraphQLContext } from "~/src/graphql/context";
12
import { User } from "~/src/graphql/types/User/User";
23
import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError";
34
import { AgendaItem } from "./AgendaItem";
5+
import type { AgendaItem as AgendaItemType } from "./AgendaItem";
46

5-
AgendaItem.implement({
6-
fields: (t) => ({
7-
updater: t.field({
8-
description: "User who last updated the agenda item.",
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;
19-
20-
const [currentUser, existingAgendaFolder] = await Promise.all([
21-
ctx.drizzleClient.query.usersTable.findFirst({
22-
where: (fields, operators) =>
23-
operators.eq(fields.id, currentUserId),
24-
}),
25-
ctx.drizzleClient.query.agendaFoldersTable.findFirst({
26-
columns: {
27-
isAgendaItemFolder: true,
28-
},
29-
where: (fields, operators) =>
30-
operators.eq(fields.id, parent.folderId),
31-
with: {
32-
event: {
33-
columns: {
34-
startAt: true,
35-
},
36-
with: {
37-
organization: {
38-
columns: {
39-
countryCode: true,
40-
},
41-
with: {
42-
membershipsWhereOrganization: {
43-
columns: {
44-
role: true,
45-
},
46-
where: (fields, operators) =>
47-
operators.eq(fields.memberId, currentUserId),
48-
},
49-
},
7+
export const resolveUpdater = async (
8+
parent: AgendaItemType,
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+
20+
const currentUserId = ctx.currentClient.user.id;
21+
22+
const [currentUser, existingAgendaFolder] = await Promise.all([
23+
ctx.drizzleClient.query.usersTable.findFirst({
24+
where: (fields, operators) => operators.eq(fields.id, currentUserId),
25+
}),
26+
ctx.drizzleClient.query.agendaFoldersTable.findFirst({
27+
columns: {
28+
isAgendaItemFolder: true,
29+
},
30+
where: (fields, operators) => operators.eq(fields.id, parent.folderId),
31+
with: {
32+
event: {
33+
columns: {
34+
startAt: true,
35+
},
36+
with: {
37+
organization: {
38+
columns: {
39+
countryCode: true,
40+
},
41+
with: {
42+
membershipsWhereOrganization: {
43+
columns: {
44+
role: true,
5045
},
46+
where: (fields, operators) =>
47+
operators.eq(fields.memberId, currentUserId),
5148
},
5249
},
5350
},
54-
}),
55-
]);
51+
},
52+
},
53+
},
54+
}),
55+
]);
5656

57-
if (currentUser === undefined) {
58-
throw new TalawaGraphQLError({
59-
extensions: {
60-
code: "unauthenticated",
61-
},
62-
});
63-
}
64-
65-
// Folder id existing but the associated agenda folder 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.
66-
if (existingAgendaFolder === undefined) {
67-
ctx.log.error(
68-
"Postgres select operation returned an empty array for an agenda item's folder id that isn't null.",
69-
);
70-
71-
throw new TalawaGraphQLError({
72-
extensions: {
73-
code: "unexpected",
74-
},
75-
});
76-
}
77-
78-
const currentUserOrganizationMembership =
79-
existingAgendaFolder.event.organization
80-
.membershipsWhereOrganization[0];
81-
82-
if (
83-
currentUser.role !== "administrator" &&
84-
(currentUserOrganizationMembership === undefined ||
85-
currentUserOrganizationMembership.role !== "administrator")
86-
) {
87-
throw new TalawaGraphQLError({
88-
extensions: {
89-
code: "unauthorized_action",
90-
},
91-
});
92-
}
57+
if (currentUser === undefined) {
58+
throw new TalawaGraphQLError({
59+
extensions: {
60+
code: "unauthenticated",
61+
},
62+
});
63+
}
9364

94-
if (parent.updaterId === null) {
95-
return null;
96-
}
65+
// Folder id existing but the associated agenda folder 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.
66+
if (existingAgendaFolder === undefined) {
67+
ctx.log.error(
68+
"Postgres select operation returned an empty array for an agenda item's folder id that isn't null.",
69+
);
9770

98-
if (parent.updaterId === currentUserId) {
99-
return currentUser;
100-
}
71+
throw new TalawaGraphQLError({
72+
extensions: {
73+
code: "unexpected",
74+
},
75+
});
76+
}
10177

102-
const updaterId = parent.updaterId;
78+
const currentUserOrganizationMembership =
79+
existingAgendaFolder.event.organization.membershipsWhereOrganization[0];
10380

104-
const existingUser = await ctx.drizzleClient.query.usersTable.findFirst(
105-
{
106-
where: (fields, operators) => operators.eq(fields.id, updaterId),
107-
},
108-
);
81+
if (
82+
currentUser.role !== "administrator" &&
83+
(currentUserOrganizationMembership === undefined ||
84+
currentUserOrganizationMembership.role !== "administrator")
85+
) {
86+
throw new TalawaGraphQLError({
87+
extensions: {
88+
code: "unauthorized_action",
89+
},
90+
});
91+
}
10992

110-
// 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.
111-
if (existingUser === undefined) {
112-
ctx.log.error(
113-
"Postgres select operation returned an empty array for an agenda item's updater id that isn't null.",
114-
);
93+
if (parent.updaterId === null) {
94+
return null;
95+
}
11596

116-
throw new TalawaGraphQLError({
117-
extensions: {
118-
code: "unexpected",
119-
},
120-
});
121-
}
97+
if (parent.updaterId === currentUserId) {
98+
return currentUser;
99+
}
100+
101+
const updaterId = parent.updaterId;
122102

123-
return existingUser;
103+
const existingUser = await ctx.drizzleClient.query.usersTable.findFirst({
104+
where: (fields, operators) => operators.eq(fields.id, updaterId),
105+
});
106+
107+
// 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.
108+
if (existingUser === undefined) {
109+
ctx.log.error(
110+
"Postgres select operation returned an empty array for an agenda item's updater id that isn't null.",
111+
);
112+
113+
throw new TalawaGraphQLError({
114+
extensions: {
115+
code: "unexpected",
124116
},
117+
});
118+
}
119+
120+
return existingUser;
121+
};
122+
123+
AgendaItem.implement({
124+
fields: (t) => ({
125+
updater: t.field({
126+
description: "User who last updated the agenda item.",
127+
resolve: resolveUpdater,
125128
type: User,
126129
}),
127130
}),

0 commit comments

Comments
 (0)