Skip to content

Test: src/graphql/types/Organization/creator.ts #3196

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

Merged
merged 10 commits into from
Feb 14, 2025
150 changes: 84 additions & 66 deletions src/graphql/types/Organization/creator.ts
Original file line number Diff line number Diff line change
@@ -1,87 +1,105 @@
import { User } from "~/src/graphql/types/User/User";
import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError";
import type { GraphQLContext } from "../../context";
import { Organization } from "./Organization";
import type { Organization as Organizationtype } from "./Organization";

Organization.implement({
fields: (t) => ({
creator: t.field({
description: "User who created the organization.",
resolve: async (parent, _args, ctx) => {
if (!ctx.currentClient.isAuthenticated) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthenticated",
},
});
}
export const OrganizationCreatorResolver = async (
parent: Organizationtype,
_args: unknown,
ctx: GraphQLContext,
) => {
if (!ctx.currentClient.isAuthenticated) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthenticated",
},
});
}

const currentUserId = ctx.currentClient.user.id;
try {
const currentUserId = ctx.currentClient.user.id;

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

if (currentUser === undefined) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthenticated",
},
});
}
if (currentUser === undefined) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthenticated",
},
});
}

const currentUserOrganizationMembership =
currentUser.organizationMembershipsWhereMember[0];
const currentUserOrganizationMembership =
currentUser.organizationMembershipsWhereMember[0];

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

if (parent.creatorId === null) {
return null;
}
if (parent.creatorId === null) {
return null;
}

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

// Creator id existing but the associated user not existing is either a business logic error which means that the corresponding data in the database is in a corrupted state or it is a rare race condition. It must be investigated and fixed as soon as possible to prevent further data corruption if the former case is true.
if (existingUser === undefined) {
ctx.log.warn(
"Postgres select operation returned an empty array for an organization's creator id that isn't null.",
);
// Creator id existing but the associated user not existing is either a business logic error which means that the corresponding data in the database is in a corrupted state or it is a rare race condition. It must be investigated and fixed as soon as possible to prevent further data corruption if the former case is true.
if (existingUser === undefined) {
ctx.log.warn(
"Postgres select operation returned an empty array for an organization's creator id that isn't null.",
);

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

return existingUser;
return existingUser;
} catch (error) {
if (error instanceof TalawaGraphQLError) {
throw error;
}
ctx.log.error(error);
throw new TalawaGraphQLError({
message: "Internal server error",
extensions: {
code: "unexpected",
},
});
}
};
Organization.implement({
fields: (t) => ({
creator: t.field({
description: "User who created the organization.",
resolve: OrganizationCreatorResolver,
type: User,
}),
}),
Expand Down
Loading
Loading