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

migrated test/rotes/graphql/Mutation/updateChatMessage to integration test #3344

Open
wants to merge 5 commits into
base: develop-postgres
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1582,7 +1582,7 @@ input MutationCreateChatMessageInput {
chatId: ID!

"""Global identifier of the associated parent message."""
parentMessageId: ID!
parentMessageId: ID
}

""""""
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/inputs/MutationCreateChatMessageInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const MutationCreateChatMessageInput = builder
}),
parentMessageId: t.id({
description: "Global identifier of the associated parent message.",
required: true,
required: false, //message could be either a reply or a standAlone message
}),
}),
});
250 changes: 120 additions & 130 deletions src/graphql/types/Mutation/updateChatMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,168 +8,158 @@ import {
} from "~/src/graphql/inputs/MutationUpdateChatMessageInput";
import { ChatMessage } from "~/src/graphql/types/ChatMessage/ChatMessage";
import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError";
import type { GraphQLContext } from "../../context";

// Create a schema for the mutation arguments:
const mutationUpdateChatMessageArgumentsSchema = z.object({
input: mutationUpdateChatMessageInputSchema,
});

// Export the resolver function so your test can import it:
export async function updateChatMessageResolver(
_parent: unknown,
args: { input: { body: string; id: string } },
ctx: GraphQLContext,
) {
if (!ctx.currentClient.isAuthenticated) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthenticated",
},
});
}
// Wire the resolver into your GraphQL schema:
builder.mutationField("updateChatMessage", (t) =>
t.field({
type: ChatMessage,
description: "Mutation field to update a chat message.",
args: {
input: t.arg({
type: MutationUpdateChatMessageInput,
required: true,
}),
},
resolve: async (_parent, args, ctx) => {
if (!ctx.currentClient.isAuthenticated) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthenticated",
},
});
}

const {
success,
data: parsedArgs,
error,
} = mutationUpdateChatMessageArgumentsSchema.safeParse(args);
const {
success,
data: parsedArgs,
error,
} = mutationUpdateChatMessageArgumentsSchema.safeParse(args);

if (!success) {
throw new TalawaGraphQLError({
extensions: {
code: "invalid_arguments",
issues: error.issues.map((issue) => ({
argumentPath: issue.path,
message: issue.message,
})),
},
});
}
if (!success) {
throw new TalawaGraphQLError({
extensions: {
code: "invalid_arguments",
issues: error.issues.map((issue) => ({
argumentPath: issue.path,
message: issue.message,
})),
},
});
}

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

const [currentUser, existingChatMessage] = await Promise.all([
ctx.drizzleClient.query.usersTable.findFirst({
columns: {
role: true,
},
where: (fields, operators) => operators.eq(fields.id, currentUserId),
}),
ctx.drizzleClient.query.chatMessagesTable.findFirst({
columns: {
creatorId: true,
},
with: {
chat: {
const [currentUser, existingChatMessage] = await Promise.all([
ctx.drizzleClient.query.usersTable.findFirst({
columns: {
role: true,
},
where: (fields, operators) => operators.eq(fields.id, currentUserId),
}),
ctx.drizzleClient.query.chatMessagesTable.findFirst({
columns: {
avatarMimeType: true,
creatorId: true,
},
with: {
chatMembershipsWhereChat: {
chat: {
columns: {
role: true,
},
where: (fields, operators) =>
operators.eq(fields.memberId, currentUserId),
},
organization: {
columns: {
countryCode: true,
avatarMimeType: true,
},
with: {
membershipsWhereOrganization: {
chatMembershipsWhereChat: {
columns: {
role: true,
},
where: (fields, operators) =>
operators.eq(fields.memberId, currentUserId),
},
organization: {
columns: {
countryCode: true,
},
with: {
membershipsWhereOrganization: {
columns: {
role: true,
},
where: (fields, operators) =>
operators.eq(fields.memberId, currentUserId),
},
},
},
},
},
},
},
},
where: (fields, operators) =>
operators.eq(fields.id, parsedArgs.input.id),
}),
]);

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

if (existingChatMessage === undefined) {
throw new TalawaGraphQLError({
extensions: {
code: "arguments_associated_resources_not_found",
issues: [
{
argumentPath: ["input", "id"],
where: (fields, operators) =>
operators.eq(fields.id, parsedArgs.input.id),
}),
]);
if (currentUser === undefined) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthenticated",
},
],
},
});
}

const currentUserOrganizationMembership =
existingChatMessage.chat.organization.membershipsWhereOrganization[0];
const currentUserChatMembership =
existingChatMessage.chat.chatMembershipsWhereChat[0];
});
}

if (
(currentUser.role !== "administrator" &&
(currentUserOrganizationMembership === undefined ||
(currentUserOrganizationMembership.role !== "administrator" &&
currentUserChatMembership === undefined))) ||
currentUserId !== existingChatMessage.creatorId
) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthorized_action_on_arguments_associated_resources",
issues: [
{
argumentPath: ["input", "id"],
if (existingChatMessage === undefined) {
throw new TalawaGraphQLError({
extensions: {
code: "arguments_associated_resources_not_found",
issues: [
{
argumentPath: ["input", "id"],
},
],
},
],
},
});
}
});
}

const [updatedChatMessage] = await ctx.drizzleClient
.update(chatMessagesTable)
.set({
body: parsedArgs.input.body,
})
.where(eq(chatMessagesTable.id, parsedArgs.input.id))
.returning();
const currentUserOrganizationMembership =
existingChatMessage.chat.organization.membershipsWhereOrganization[0];
const currentUserChatMembership =
existingChatMessage.chat.chatMembershipsWhereChat[0];

if (updatedChatMessage === undefined) {
throw new TalawaGraphQLError({
extensions: {
code: "unexpected",
},
});
}
if (
(currentUser.role !== "administrator" &&
(currentUserOrganizationMembership === undefined ||
(currentUserOrganizationMembership.role !== "administrator" &&
currentUserChatMembership === undefined))) ||
currentUserId !== existingChatMessage.creatorId
) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthorized_action_on_arguments_associated_resources",
issues: [
{
argumentPath: ["input", "id"],
},
],
},
});
}
const [updatedChatMessage] = await ctx.drizzleClient
.update(chatMessagesTable)
.set({
body: parsedArgs.input.body,
})
.where(eq(chatMessagesTable.id, parsedArgs.input.id))
.returning();

return updatedChatMessage;
}
if (updatedChatMessage === undefined) {
throw new TalawaGraphQLError({
extensions: {
code: "unexpected",
},
});
}

// Wire the resolver into your GraphQL schema:
builder.mutationField("updateChatMessage", (t) =>
t.field({
type: ChatMessage,
description: "Mutation field to update a chat message.",
args: {
input: t.arg({
type: MutationUpdateChatMessageInput,
required: true,
}),
return updatedChatMessage;
},
resolve: updateChatMessageResolver,
}),
);
Loading
Loading