mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor(database): ♻️ Move Notifications to their own ORM abstractions
This commit is contained in:
parent
14ace17ad4
commit
e732a3df03
16 changed files with 440 additions and 401 deletions
|
|
@ -1,62 +0,0 @@
|
|||
import type { Notification as ApiNotification } from "@versia/client/types";
|
||||
import { Note, User, db } from "@versia/kit/db";
|
||||
import type { Notifications } from "@versia/kit/tables";
|
||||
import type { InferSelectModel } from "drizzle-orm";
|
||||
import type { StatusWithRelations } from "./status.ts";
|
||||
import {
|
||||
type UserWithRelations,
|
||||
transformOutputToUserWithRelations,
|
||||
userExtrasTemplate,
|
||||
userRelations,
|
||||
} from "./user.ts";
|
||||
|
||||
export type Notification = InferSelectModel<typeof Notifications>;
|
||||
|
||||
export type NotificationWithRelations = Notification & {
|
||||
status: StatusWithRelations | null;
|
||||
account: UserWithRelations;
|
||||
};
|
||||
|
||||
export const findManyNotifications = async (
|
||||
query: Parameters<typeof db.query.Notifications.findMany>[0],
|
||||
userId?: string,
|
||||
): Promise<NotificationWithRelations[]> => {
|
||||
const output = await db.query.Notifications.findMany({
|
||||
...query,
|
||||
with: {
|
||||
...query?.with,
|
||||
account: {
|
||||
with: {
|
||||
...userRelations,
|
||||
},
|
||||
extras: userExtrasTemplate("Notifications_account"),
|
||||
},
|
||||
},
|
||||
extras: {
|
||||
...query?.extras,
|
||||
},
|
||||
});
|
||||
|
||||
return await Promise.all(
|
||||
output.map(async (notif) => ({
|
||||
...notif,
|
||||
account: transformOutputToUserWithRelations(notif.account),
|
||||
status: (await Note.fromId(notif.noteId, userId))?.data ?? null,
|
||||
})),
|
||||
);
|
||||
};
|
||||
|
||||
export const notificationToApi = async (
|
||||
notification: NotificationWithRelations,
|
||||
): Promise<ApiNotification> => {
|
||||
const account = new User(notification.account);
|
||||
return {
|
||||
account: account.toApi(),
|
||||
created_at: new Date(notification.createdAt).toISOString(),
|
||||
id: notification.id,
|
||||
type: notification.type,
|
||||
status: notification.status
|
||||
? await new Note(notification.status).toApi(account)
|
||||
: undefined,
|
||||
};
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue