Skip to content

You can now delete an endpoint #875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export function ConfigureEndpointSheet({ slug, endpoint, onClose }: ConfigureEnd
const refreshEndpointFetcher = useFetcher();
const refreshingEndpoint = refreshEndpointFetcher.state !== "idle";

const deleteEndpointFetcher = useFetcher();
const deletingEndpoint = deleteEndpointFetcher.state !== "idle";

const revalidator = useRevalidator();
const events = useEventSource(endpointStreamingPath({ id: endpoint.environment.id }), {
event: "message",
Expand All @@ -70,12 +73,30 @@ export function ConfigureEndpointSheet({ slug, endpoint, onClose }: ConfigureEnd
>
<SheetContent size="lg">
<SheetHeader>
<Header1>
<div className="flex items-center gap-2">
<EnvironmentLabel environment={{ type: endpoint.environment.type }} />
<Header1>Configure endpoint</Header1>
</div>
</Header1>
<div className="flex w-full items-center justify-between">
<Header1>
<div className="flex items-center gap-2">
<EnvironmentLabel environment={{ type: endpoint.environment.type }} />
<Header1>Configure endpoint</Header1>
</div>
</Header1>
{endpoint.state === "configured" && (
<deleteEndpointFetcher.Form
method="post"
action={`/resources/environments/${endpoint.environment.id}/endpoint/${endpoint.id}`}
>
<input type="hidden" name="action" value="delete" />
<Button
variant="danger/small"
type="submit"
disabled={deletingEndpoint}
LeadingIcon={deletingEndpoint ? "spinner-white" : undefined}
>
{deletingEndpoint ? "Deleting" : "Delete"}
</Button>
</deleteEndpointFetcher.Form>
)}
</div>
</SheetHeader>
<SheetBody>
<setEndpointUrlFetcher.Form
Expand Down Expand Up @@ -123,6 +144,7 @@ export function ConfigureEndpointSheet({ slug, endpoint, onClose }: ConfigureEnd
method="post"
action={`/resources/environments/${endpoint.environment.id}/endpoint/${endpoint.id}`}
>
<input type="hidden" name="action" value="refresh" />
<Callout
variant="info"
icon={
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,52 @@
import { ActionFunctionArgs, json } from "@remix-run/server-runtime";
import { z } from "zod";
import { DeleteEndpointIndexService } from "~/services/endpoints/deleteEndpointService";
import { IndexEndpointService } from "~/services/endpoints/indexEndpoint.server";
import { requireUserId } from "~/services/session.server";
import { workerQueue } from "~/services/worker.server";

const ParamsSchema = z.object({
environmentParam: z.string(),
endpointParam: z.string(),
});

export async function action({ params }: ActionFunctionArgs) {
const { endpointParam } = ParamsSchema.parse(params);
const BodySchema = z.discriminatedUnion("action", [
z.object({ action: z.literal("refresh") }),
z.object({ action: z.literal("delete") }),
]);

export async function action({ request, params }: ActionFunctionArgs) {
const userId = await requireUserId(request);
if (request.method !== "POST") {
throw new Response(null, { status: 405 });
}

try {
const service = new IndexEndpointService();
await service.call(endpointParam, "MANUAL");
const { endpointParam } = ParamsSchema.parse(params);
const form = await request.formData();
const formObject = Object.fromEntries(form.entries());
const { action } = BodySchema.parse(formObject);

switch (action) {
case "refresh": {
const service = new IndexEndpointService();
await service.call(endpointParam, "MANUAL");

// Enqueue the endpoint to be probed in 10 seconds
await workerQueue.enqueue(
"probeEndpoint",
{ id: endpointParam },
{ jobKey: `probe:${endpointParam}`, runAt: new Date(Date.now() + 10000) }
);
// Enqueue the endpoint to be probed in 10 seconds
await workerQueue.enqueue(
"probeEndpoint",
{ id: endpointParam },
{ jobKey: `probe:${endpointParam}`, runAt: new Date(Date.now() + 10000) }
);

return json({ success: true });
return json({ success: true });
}
case "delete": {
const service = new DeleteEndpointIndexService();
await service.call(endpointParam, userId);
return json({ success: true });
}
}
} catch (e) {
return json({ success: false, error: e }, { status: 400 });
}
Expand Down
25 changes: 25 additions & 0 deletions apps/webapp/app/services/endpoints/deleteEndpointService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { PrismaClient } from "@trigger.dev/database";
import { prisma } from "~/db.server";

export class DeleteEndpointIndexService {
#prismaClient: PrismaClient;

constructor(prismaClient: PrismaClient = prisma) {
this.#prismaClient = prismaClient;
}

public async call(id: string, userId: string): Promise<void> {
await this.#prismaClient.endpoint.delete({
where: {
id,
organization: {
members: {
some: {
userId,
},
},
},
},
});
}
}