Files
triggerdotdev--trigger.dev/apps/webapp/app/presenters/EventPresenter.server.ts
Kritik Jiyaviya 25a152517e feat: Events list page (#824)
* feat: add events list

* Changed the Events icon in the sidemenu

---------

Co-authored-by: Matt Aitken <matt@mattaitken.com>
2024-01-05 12:04:31 +00:00

69 lines
1.6 KiB
TypeScript

import { PrismaClient, prisma } from "~/db.server";
export type Event = NonNullable<Awaited<ReturnType<EventPresenter["call"]>>>;
export class EventPresenter {
#prismaClient: PrismaClient;
constructor(prismaClient: PrismaClient = prisma) {
this.#prismaClient = prismaClient;
}
public async call({
userId,
projectSlug,
organizationSlug,
eventId,
}: {
userId: string;
projectSlug: string;
organizationSlug: string;
eventId: string;
}) {
// Find the organization that the user is a member of
const organization = await this.#prismaClient.organization.findFirstOrThrow({
where: {
slug: organizationSlug,
members: { some: { userId } },
},
});
// Find the project scoped to the organization
const project = await this.#prismaClient.project.findFirstOrThrow({
where: {
slug: projectSlug,
organizationId: organization.id,
},
});
const event = await this.#prismaClient.eventRecord.findFirst({
select: {
id: true,
name: true,
payload: true,
context: true,
timestamp: true,
deliveredAt: true,
},
where: {
id: eventId,
projectId: project.id,
organizationId: organization.id,
},
});
if (!event) {
throw new Error("Could not find Event");
}
return {
id: event.id,
name: event.name,
timestamp: event.timestamp,
payload: JSON.stringify(event.payload, null, 2),
context: JSON.stringify(event.context, null, 2),
deliveredAt: event.deliveredAt,
};
}
}