Skip to content

Commit e31753a

Browse files
authored
update gql mutation handlers (#4254)
1 parent 128650c commit e31753a

File tree

3 files changed

+82
-17
lines changed

3 files changed

+82
-17
lines changed

backend/native/backpack-api/src/db/friendships.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ async function deleteFriendRequest({ from, to }: { from: string; to: string }) {
750750
);
751751
}
752752

753-
function getSortedUsers(from: string, to: string) {
753+
export function getSortedUsers(from: string, to: string) {
754754
let user1 = "";
755755
let user2 = "";
756756
if (from < to) {

backend/native/backpack-api/src/routes/graphql/clients/hasura.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,4 +469,76 @@ export class Hasura {
469469

470470
return createConnection(nodes, false, false);
471471
}
472+
473+
/**
474+
* Updates the notification cursor for the argued user if applicable.
475+
* @param {string} userId
476+
* @param {number} lastNotificationId
477+
* @memberof Hasura
478+
*/
479+
async updateNotificationCursor(userId: string, lastNotificationId: number) {
480+
const current = await this.#chain("query")(
481+
{
482+
auth_notification_cursor: [
483+
{ where: { uuid: { _eq: userId } } },
484+
{ last_read_notificaiton: true },
485+
],
486+
},
487+
{ operationName: "GetCurrentNotificationCursor" }
488+
);
489+
490+
const currId = current.auth_notification_cursor[0]?.last_read_notificaiton;
491+
if (currId && currId >= lastNotificationId) {
492+
return;
493+
}
494+
495+
await this.#chain("mutation")(
496+
{
497+
insert_auth_notification_cursor_one: [
498+
{
499+
object: {
500+
uuid: userId,
501+
last_read_notificaiton: lastNotificationId,
502+
},
503+
on_conflict: {
504+
// @ts-ignore
505+
update_columns: ["last_read_notificaiton"],
506+
// @ts-ignore
507+
constraint: "notification_cursor_pkey",
508+
},
509+
},
510+
{
511+
uuid: true,
512+
},
513+
],
514+
},
515+
{ operationName: "UpdateNotificationCursor" }
516+
);
517+
}
518+
519+
/**
520+
* Try to update the view status for a list of notification IDs.
521+
* @param {string} userId
522+
* @param {number[]} ids
523+
* @returns {Promise<number | undefined>}
524+
* @memberof Hasura
525+
*/
526+
async updateNotificationViewed(
527+
userId: string,
528+
ids: number[]
529+
): Promise<number | undefined> {
530+
const resp = await this.#chain("mutation")(
531+
{
532+
update_auth_notifications: [
533+
{
534+
_set: { viewed: true },
535+
where: { id: { _in: ids }, uuid: { _eq: userId } },
536+
},
537+
{ affected_rows: true },
538+
],
539+
},
540+
{ operationName: "UpdateNotificationsViewed" }
541+
);
542+
return resp.update_auth_notifications?.affected_rows;
543+
}
472544
}
Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import type { GraphQLResolveInfo } from "graphql";
22

3-
import {
4-
updateCursor,
5-
updateNotificationSeen,
6-
} from "../../../../db/notifications";
73
import type { ApiContext } from "../../context";
84
import type {
95
MutationMarkNotificationsAsReadArgs,
@@ -25,17 +21,14 @@ export const markNotificationsAsReadMutationResolver: MutationResolvers["markNot
2521
ctx: ApiContext,
2622
_info: GraphQLResolveInfo
2723
): Promise<number> => {
28-
// TODO: move implementation into contextual Hasura client
2924
const lastNotificationId = [...args.ids].sort((a, b) => b - a)[0];
30-
await updateCursor({
31-
uuid: ctx.authorization.userId ?? "",
32-
lastNotificationId,
33-
});
34-
35-
// TODO: move implementation into contextual Hasura client
36-
const res = await updateNotificationSeen({
37-
notificationIds: args.ids,
38-
uuid: ctx.authorization.userId ?? "",
39-
});
40-
return res.update_auth_notifications?.affected_rows ?? 0;
25+
await ctx.dataSources.hasura.updateNotificationCursor(
26+
ctx.authorization.userId ?? "",
27+
lastNotificationId
28+
);
29+
const affectedRows = await ctx.dataSources.hasura.updateNotificationViewed(
30+
ctx.authorization.userId ?? "",
31+
args.ids
32+
);
33+
return affectedRows ?? 0;
4134
};

0 commit comments

Comments
 (0)