Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: Added test file for src/graphql/types/Venue/updater.ts #3300

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 76 additions & 70 deletions src/graphql/types/Venue/updater.ts
Original file line number Diff line number Diff line change
@@ -1,89 +1,95 @@
import type { GraphQLContext } from "~/src/graphql/context";
import { User } from "~/src/graphql/types/User/User";
import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError";
import { Venue } from "./Venue";
import type { Venue as VenueType } from "./Venue";

Venue.implement({
fields: (t) => ({
updater: t.field({
description: "User who last updated the venue.",
resolve: async (parent, _args, ctx) => {
if (!ctx.currentClient.isAuthenticated) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthenticated",
},
});
}

const currentUserId = ctx.currentClient.user.id;
export const resolveUpdater = async (
parent: VenueType,
_args: Record<string, never>,
ctx: GraphQLContext,
): Promise<User | null> => {
if (!ctx.currentClient.isAuthenticated) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthenticated",
},
});
}

const currentUser = await ctx.drizzleClient.query.usersTable.findFirst({
with: {
organizationMembershipsWhereMember: {
columns: {
role: true,
},
where: (fields, operators) =>
operators.eq(fields.organizationId, parent.organizationId),
},
},
where: (fields, operators) => operators.eq(fields.id, currentUserId),
});
const currentUserId = ctx.currentClient.user.id;

if (currentUser === undefined) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthenticated",
},
});
}
const currentUser = await ctx.drizzleClient.query.usersTable.findFirst({
with: {
organizationMembershipsWhereMember: {
columns: {
role: true,
},
where: (fields, operators) =>
operators.eq(fields.organizationId, parent.organizationId),
},
},
where: (fields, operators) => operators.eq(fields.id, currentUserId),
});

const currentUserOrganizationMembership =
currentUser.organizationMembershipsWhereMember[0];
if (currentUser === undefined) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthenticated",
},
});
}

if (
currentUser.role !== "administrator" &&
(currentUserOrganizationMembership === undefined ||
currentUserOrganizationMembership.role !== "administrator")
) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthorized_action",
},
});
}
const currentUserOrganizationMembership =
currentUser.organizationMembershipsWhereMember[0];

if (parent.updaterId === null) {
return null;
}
if (
currentUser.role !== "administrator" &&
(currentUserOrganizationMembership === undefined ||
currentUserOrganizationMembership.role !== "administrator")
) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthorized_action",
},
});
}

if (parent.updaterId === currentUserId) {
return currentUser;
}
if (parent.updaterId === null) {
return null;
}

const updaterId = parent.updaterId;
if (parent.updaterId === currentUserId) {
return currentUser;
}

const existingUser = await ctx.drizzleClient.query.usersTable.findFirst(
{
where: (fields, operators) => operators.eq(fields.id, updaterId),
},
);
const updaterId = parent.updaterId;

// 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.
if (existingUser === undefined) {
ctx.log.error(
"Postgres select operation returned an empty array for a venue's updater id that isn't null.",
);
const existingUser = await ctx.drizzleClient.query.usersTable.findFirst({
where: (fields, operators) => operators.eq(fields.id, updaterId),
});

throw new TalawaGraphQLError({
extensions: {
code: "unexpected",
},
});
}
// 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.
if (existingUser === undefined) {
ctx.log.error(
"Postgres select operation returned an empty array for a venue's updater id that isn't null.",
);

return existingUser;
throw new TalawaGraphQLError({
extensions: {
code: "unexpected",
},
});
}

return existingUser;
};

Venue.implement({
fields: (t) => ({
updater: t.field({
description: "User who last updated the venue.",
resolve: resolveUpdater,
type: User,
}),
}),
Expand Down
Loading
Loading