Files
triggerdotdev--trigger.dev/apps/webapp/app/services/triggers/initializeTrigger.server.ts
Matt Aitken 4b3b418abb
🚀 Publish Trigger.dev Docker / units (push) Failing after 4s
🚀 Publish Trigger.dev Docker / typecheck (push) Failing after 5s
🚀 Publish Trigger.dev Docker / e2e (push) Failing after 4s
🚀 Publish Trigger.dev Docker / publish (push) Has been skipped
Set endpoint URLs to null, instead of deleting them (#878)
* Added Endpoint deletedAt column

* Only show endpoints where they’re not deleted

* Don’t delete Endpoints, set the deletedAt and change their slug name

* Only perform indexing if the endpoint isn’t deleted

* Have a nullable URL for endpoints

* Deal with null URLs throughout the app

* Re-running and retrying behaves properly when there’s no endpoint URL

* Remove console.log

* Better error message when doing a run
2024-01-30 10:45:12 +00:00

91 lines
2.4 KiB
TypeScript

import { InitializeTriggerBody, REGISTER_SOURCE_EVENT_V1 } from "@trigger.dev/core";
import type { PrismaClient } from "~/db.server";
import { prisma } from "~/db.server";
import { AuthenticatedEnvironment } from "../apiAuth.server";
import { EndpointApi } from "../endpointApi.server";
import { RegisterTriggerSourceServiceV1 } from "./registerTriggerSourceV1.server";
import { IngestSendEvent } from "../events/ingestSendEvent.server";
export class InitializeTriggerService {
#prismaClient: PrismaClient;
#registerTriggerSource = new RegisterTriggerSourceServiceV1();
#sendEvent = new IngestSendEvent();
constructor(prismaClient: PrismaClient = prisma) {
this.#prismaClient = prismaClient;
}
public async call({
environment,
payload,
endpointSlug,
id,
}: {
environment: AuthenticatedEnvironment;
payload: InitializeTriggerBody;
id: string;
endpointSlug: string;
}) {
const endpoint = await this.#prismaClient.endpoint.findUniqueOrThrow({
where: {
environmentId_slug: {
environmentId: environment.id,
slug: endpointSlug,
},
},
});
if (!endpoint.url) {
throw new Error("This environment's endpoint doesn't have a URL set");
}
const dynamicTrigger = await this.#prismaClient.dynamicTrigger.findUniqueOrThrow({
where: {
endpointId_slug_type: {
endpointId: endpoint.id,
slug: id,
type: "EVENT",
},
},
});
const clientApi = new EndpointApi(environment.apiKey, endpoint.url);
const registerMetadata = await clientApi.initializeTrigger(dynamicTrigger.slug, payload.params);
if (!registerMetadata) {
throw new Error("Could not initialize trigger");
}
const registration = await this.#registerTriggerSource.call({
environment,
payload: registerMetadata,
id: dynamicTrigger.slug,
endpointSlug,
key: payload.id,
accountId: payload.accountId,
registrationMetadata: payload.metadata,
});
if (!registration) {
return;
}
await this.#sendEvent.call(
environment,
{
id: registration.id,
name: REGISTER_SOURCE_EVENT_V1,
source: "trigger.dev",
payload: {
...registration,
dynamicTriggerId: dynamicTrigger.slug,
},
},
{ accountId: payload.accountId }
);
return registration;
}
}