Skip to content

Commit

Permalink
update gql mutation handlers (#4254)
Browse files Browse the repository at this point in the history
  • Loading branch information
callensm authored Jun 30, 2023
1 parent 128650c commit e31753a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 17 deletions.
2 changes: 1 addition & 1 deletion backend/native/backpack-api/src/db/friendships.ts
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ async function deleteFriendRequest({ from, to }: { from: string; to: string }) {
);
}

function getSortedUsers(from: string, to: string) {
export function getSortedUsers(from: string, to: string) {
let user1 = "";
let user2 = "";
if (from < to) {
Expand Down
72 changes: 72 additions & 0 deletions backend/native/backpack-api/src/routes/graphql/clients/hasura.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,4 +469,76 @@ export class Hasura {

return createConnection(nodes, false, false);
}

/**
* Updates the notification cursor for the argued user if applicable.
* @param {string} userId
* @param {number} lastNotificationId
* @memberof Hasura
*/
async updateNotificationCursor(userId: string, lastNotificationId: number) {
const current = await this.#chain("query")(
{
auth_notification_cursor: [
{ where: { uuid: { _eq: userId } } },
{ last_read_notificaiton: true },
],
},
{ operationName: "GetCurrentNotificationCursor" }
);

const currId = current.auth_notification_cursor[0]?.last_read_notificaiton;
if (currId && currId >= lastNotificationId) {
return;
}

await this.#chain("mutation")(
{
insert_auth_notification_cursor_one: [
{
object: {
uuid: userId,
last_read_notificaiton: lastNotificationId,
},
on_conflict: {
// @ts-ignore
update_columns: ["last_read_notificaiton"],
// @ts-ignore
constraint: "notification_cursor_pkey",
},
},
{
uuid: true,
},
],
},
{ operationName: "UpdateNotificationCursor" }
);
}

/**
* Try to update the view status for a list of notification IDs.
* @param {string} userId
* @param {number[]} ids
* @returns {Promise<number | undefined>}
* @memberof Hasura
*/
async updateNotificationViewed(
userId: string,
ids: number[]
): Promise<number | undefined> {
const resp = await this.#chain("mutation")(
{
update_auth_notifications: [
{
_set: { viewed: true },
where: { id: { _in: ids }, uuid: { _eq: userId } },
},
{ affected_rows: true },
],
},
{ operationName: "UpdateNotificationsViewed" }
);
return resp.update_auth_notifications?.affected_rows;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import type { GraphQLResolveInfo } from "graphql";

import {
updateCursor,
updateNotificationSeen,
} from "../../../../db/notifications";
import type { ApiContext } from "../../context";
import type {
MutationMarkNotificationsAsReadArgs,
Expand All @@ -25,17 +21,14 @@ export const markNotificationsAsReadMutationResolver: MutationResolvers["markNot
ctx: ApiContext,
_info: GraphQLResolveInfo
): Promise<number> => {
// TODO: move implementation into contextual Hasura client
const lastNotificationId = [...args.ids].sort((a, b) => b - a)[0];
await updateCursor({
uuid: ctx.authorization.userId ?? "",
lastNotificationId,
});

// TODO: move implementation into contextual Hasura client
const res = await updateNotificationSeen({
notificationIds: args.ids,
uuid: ctx.authorization.userId ?? "",
});
return res.update_auth_notifications?.affected_rows ?? 0;
await ctx.dataSources.hasura.updateNotificationCursor(
ctx.authorization.userId ?? "",
lastNotificationId
);
const affectedRows = await ctx.dataSources.hasura.updateNotificationViewed(
ctx.authorization.userId ?? "",
args.ids
);
return affectedRows ?? 0;
};

1 comment on commit e31753a

@vercel
Copy link

@vercel vercel bot commented on e31753a Jun 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.