Skip to content

Commit

Permalink
Merge branch 'develop-postgres' into issue-3063
Browse files Browse the repository at this point in the history
  • Loading branch information
palisadoes authored Feb 14, 2025
2 parents eedee8c + 51c11d2 commit 49b826e
Show file tree
Hide file tree
Showing 2 changed files with 457 additions and 66 deletions.
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

0 comments on commit 49b826e

Please sign in to comment.