2024-08-26 19:40:15 +02:00
|
|
|
import type { Notification as ApiNotification } from "@versia/client/types";
|
2024-04-13 14:20:12 +02:00
|
|
|
import type { InferSelectModel } from "drizzle-orm";
|
2024-05-29 02:59:49 +02:00
|
|
|
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";
|
2024-10-04 15:22:48 +02:00
|
|
|
import type { StatusWithRelations } from "./status.ts";
|
2024-04-11 14:12:16 +02:00
|
|
|
import {
|
|
|
|
|
type UserWithRelations,
|
|
|
|
|
transformOutputToUserWithRelations,
|
2024-04-13 14:20:12 +02:00
|
|
|
userExtrasTemplate,
|
|
|
|
|
userRelations,
|
2024-10-04 15:22:48 +02:00
|
|
|
} from "./user.ts";
|
2024-04-11 13:39:07 +02:00
|
|
|
|
2024-04-17 08:36:01 +02:00
|
|
|
export type Notification = InferSelectModel<typeof Notifications>;
|
2023-11-23 05:10:37 +01:00
|
|
|
|
|
|
|
|
export type NotificationWithRelations = Notification & {
|
2024-04-07 07:30:49 +02:00
|
|
|
status: StatusWithRelations | null;
|
|
|
|
|
account: UserWithRelations;
|
2023-11-23 05:10:37 +01:00
|
|
|
};
|
|
|
|
|
|
2024-04-11 14:12:16 +02:00
|
|
|
export const findManyNotifications = async (
|
2024-04-17 08:36:01 +02:00
|
|
|
query: Parameters<typeof db.query.Notifications.findMany>[0],
|
2024-05-09 01:19:53 +02:00
|
|
|
userId?: string,
|
2024-04-11 14:12:16 +02:00
|
|
|
): Promise<NotificationWithRelations[]> => {
|
2024-04-17 08:36:01 +02:00
|
|
|
const output = await db.query.Notifications.findMany({
|
2024-04-11 14:12:16 +02:00
|
|
|
...query,
|
|
|
|
|
with: {
|
|
|
|
|
...query?.with,
|
|
|
|
|
account: {
|
|
|
|
|
with: {
|
|
|
|
|
...userRelations,
|
|
|
|
|
},
|
2024-04-17 08:36:01 +02:00
|
|
|
extras: userExtrasTemplate("Notifications_account"),
|
2024-04-11 14:12:16 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
extras: {
|
|
|
|
|
...query?.extras,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return await Promise.all(
|
|
|
|
|
output.map(async (notif) => ({
|
|
|
|
|
...notif,
|
|
|
|
|
account: transformOutputToUserWithRelations(notif.account),
|
2024-06-13 02:45:07 +02:00
|
|
|
status: (await Note.fromId(notif.noteId, userId))?.data ?? null,
|
2024-04-11 14:12:16 +02:00
|
|
|
})),
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-13 04:26:43 +02:00
|
|
|
export const notificationToApi = async (
|
2024-04-07 07:30:49 +02:00
|
|
|
notification: NotificationWithRelations,
|
2024-06-29 08:36:15 +02:00
|
|
|
): Promise<ApiNotification> => {
|
2024-04-25 05:40:27 +02:00
|
|
|
const account = new User(notification.account);
|
2024-04-07 07:30:49 +02:00
|
|
|
return {
|
2024-06-13 04:26:43 +02:00
|
|
|
account: account.toApi(),
|
2024-04-07 07:30:49 +02:00
|
|
|
created_at: new Date(notification.createdAt).toISOString(),
|
|
|
|
|
id: notification.id,
|
|
|
|
|
type: notification.type,
|
|
|
|
|
status: notification.status
|
2024-06-13 04:26:43 +02:00
|
|
|
? await new Note(notification.status).toApi(account)
|
2024-04-07 07:30:49 +02:00
|
|
|
: undefined,
|
|
|
|
|
};
|
2023-11-23 05:10:37 +01:00
|
|
|
};
|