Files
triggerdotdev--trigger.dev/apps/webapp/app/presenters/WebhookSourcePresenter.server.ts
Matt Aitken d1092fcd2c
🚀 Publish Trigger.dev Docker / typecheck (push) Failing after 3s
🚀 Publish Trigger.dev Docker / e2e (push) Failing after 3s
🚀 Publish Trigger.dev Docker / units (push) Failing after 4s
🚀 Publish Trigger.dev Docker / publish (push) Has been skipped
Run filtering now works on the job runs page too (#823)
* Run filtering is now shared: runs page and job runs page

* Use optimistic location

* Improved the type in the run status filter dropdown

* Organize imports
2024-01-03 15:50:45 +00:00

108 lines
2.8 KiB
TypeScript

import { User, Webhook } from "@trigger.dev/database";
import { PrismaClient, prisma } from "~/db.server";
import { Organization } from "~/models/organization.server";
import { Project } from "~/models/project.server";
import { RunListPresenter } from "./RunListPresenter.server";
import { organizationPath, projectPath } from "~/utils/pathBuilder";
import { Direction } from "~/components/runs/RunStatuses";
export class WebhookSourcePresenter {
#prismaClient: PrismaClient;
constructor(prismaClient: PrismaClient = prisma) {
this.#prismaClient = prismaClient;
}
public async call({
userId,
projectSlug,
organizationSlug,
webhookId,
direction = "forward",
cursor,
getDeliveryRuns = false,
}: {
userId: User["id"];
projectSlug: Project["slug"];
organizationSlug: Organization["slug"];
webhookId: Webhook["id"];
direction?: Direction;
cursor?: string;
getDeliveryRuns?: boolean;
}) {
const webhook = await this.#prismaClient.webhook.findUnique({
select: {
id: true,
key: true,
active: true,
integration: {
select: {
id: true,
title: true,
slug: true,
definitionId: true,
setupStatus: true,
definition: {
select: {
icon: true,
},
},
},
},
httpEndpoint: {
select: {
key: true,
},
},
createdAt: true,
updatedAt: true,
params: true,
},
where: {
id: webhookId,
},
});
if (!webhook) {
throw new Error("Webhook source not found");
}
const runListPresenter = new RunListPresenter(this.#prismaClient);
const jobSlug = getDeliveryRuns
? getDeliveryJobSlug(webhook.key)
: getRegistrationJobSlug(webhook.key);
const runList = await runListPresenter.call({
userId,
jobSlug,
organizationSlug,
projectSlug,
direction,
cursor,
});
const orgRootPath = organizationPath({ slug: organizationSlug });
const projectRootPath = projectPath({ slug: organizationSlug }, { slug: projectSlug });
return {
trigger: {
id: webhook.id,
key: webhook.key,
active: webhook.active,
integration: webhook.integration,
integrationLink: `${orgRootPath}/integrations/${webhook.integration.slug}`,
httpEndpoint: webhook.httpEndpoint,
httpEndpointLink: `${projectRootPath}/http-endpoints/${webhook.httpEndpoint.key}`,
createdAt: webhook.createdAt,
updatedAt: webhook.updatedAt,
params: webhook.params,
runList,
},
};
}
}
const getRegistrationJobSlug = (key: string) => `webhook.register.${key}`;
const getDeliveryJobSlug = (key: string) => `webhook.deliver.${key}`;