sendou.ink/app/features/notifications/NotificationRepository.server.ts
Kalle b4cc185d1d
Some checks are pending
Tests and checks on push / run-checks-and-tests (push) Waiting to run
Updates translation progress / update-translation-progress-issue (push) Waiting to run
Scrims (#2211)
* Initial

* Progress

* Initial UI

* Can submit request

* Progress

* Show text if no scrims

* Can cancel request, tabs

* Delete post

* Popover if can't delete

* Request rows

* Progress

* Scrim page initial

* Fix migration order

* Progress

* Progress

* Works again

* Make it compile

* Make it compile again

* Work

* Progress

* Progress

* Progress

* Associations initial

* Association visibility work

* notFoundVisibility form fields initial

* Progress

* Association leave/join + reset invite code

* Progress

* Select test

* Merge branch 'rewrite' into scrims

* Remeda for groupBy

* Select with search

* Outline styling for select

* Select done?

* Fix prop names

* Paginated badges

* Less important

* Select no results

* Handle limiting select width

* UserSearch non-working

* Fix problem from merge

* Remove UserSearch for now

* Remove todo

* Flaggable

* Remove TODOs

* i18n start + styling

* Progress

* i18n done

* Add association e2e test

* E2E tests

* Done?

* Couple leftovers
2025-04-20 22:51:23 +03:00

124 lines
2.9 KiB
TypeScript

import { sub } from "date-fns";
import { db } from "~/db/sql";
import type { NotificationSubscription, TablesInsertable } from "~/db/tables";
import { dateToDatabaseTimestamp } from "../../utils/dates";
import { NOTIFICATIONS } from "./notifications-contants";
import type { Notification } from "./notifications-types";
export function insert(
notification: Notification,
users: Array<Omit<TablesInsertable["NotificationUser"], "notificationId">>,
) {
return db.transaction().execute(async (trx) => {
const inserted = await trx
.insertInto("Notification")
.values({
type: notification.type,
pictureUrl: notification.pictureUrl,
meta: notification.meta ? JSON.stringify(notification.meta) : null,
})
.returning("id")
.executeTakeFirstOrThrow();
await trx
.insertInto("NotificationUser")
.values(
users.map(({ userId, seen }) => ({
userId,
notificationId: inserted.id,
seen,
})),
)
.execute();
});
}
export function findByUserId(
userId: number,
{ limit }: { limit?: number } = {},
) {
return db
.selectFrom("NotificationUser")
.innerJoin(
"Notification",
"Notification.id",
"NotificationUser.notificationId",
)
.select([
"Notification.id",
"Notification.createdAt",
"NotificationUser.seen",
"Notification.type",
"Notification.meta",
"Notification.pictureUrl",
])
.where("NotificationUser.userId", "=", userId)
.limit(limit ?? NOTIFICATIONS.MAX_SHOWN)
.orderBy("Notification.id", "desc")
.execute() as Promise<
Array<Notification & { id: number; createdAt: number; seen: number }>
>;
}
export function findAllByType<T extends Notification["type"]>(type: T) {
return db
.selectFrom("Notification")
.select(["type", "meta", "pictureUrl"])
.where("type", "=", type)
.execute() as Promise<Array<Extract<Notification, { type: T }>>>;
}
export function markAsSeen({
notificationIds,
userId,
}: {
notificationIds: number[];
userId: number;
}) {
return db
.updateTable("NotificationUser")
.set("seen", 1)
.where("NotificationUser.notificationId", "in", notificationIds)
.where("NotificationUser.userId", "=", userId)
.execute();
}
export function deleteOld() {
return db
.deleteFrom("Notification")
.where(
"createdAt",
"<",
dateToDatabaseTimestamp(sub(new Date(), { days: 14 })),
)
.executeTakeFirst();
}
export function addSubscription(args: {
userId: number;
subscription: NotificationSubscription;
}) {
return db
.insertInto("NotificationUserSubscription")
.values({
userId: args.userId,
subscription: JSON.stringify(args.subscription),
})
.execute();
}
export function subscriptionsByUserIds(userIds: number[]) {
return db
.selectFrom("NotificationUserSubscription")
.select(["id", "subscription"])
.where("userId", "in", userIds)
.execute();
}
export function deleteSubscriptionById(id: number) {
return db
.deleteFrom("NotificationUserSubscription")
.where("id", "=", id)
.execute();
}