mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 08:28:19 +01:00
65 lines
2 KiB
TypeScript
65 lines
2 KiB
TypeScript
import type { Notification as ApiNotification } from "@versia/client/types";
|
|
import type { InferSelectModel } from "drizzle-orm";
|
|
import { db } from "~/drizzle/db";
|
|
import type { Notifications } from "~/drizzle/schema";
|
|
import { Note } from "~/packages/database-interface/note";
|
|
import { User } from "~/packages/database-interface/user";
|
|
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,
|
|
};
|
|
};
|