-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathapi.v1.schedules.$scheduleId.activate.ts
More file actions
76 lines (63 loc) · 2.22 KB
/
api.v1.schedules.$scheduleId.activate.ts
File metadata and controls
76 lines (63 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import type { ActionFunctionArgs } from "@remix-run/server-runtime";
import { json } from "@remix-run/server-runtime";
import { z } from "zod";
import { prisma } from "~/db.server";
import { scheduleUniqWhereClause, scheduleWhereClause } from "~/models/schedules.server";
import { ViewSchedulePresenter } from "~/presenters/v3/ViewSchedulePresenter.server";
import { authenticateApiRequest } from "~/services/apiAuth.server";
const ParamsSchema = z.object({
scheduleId: z.string(),
});
export async function action({ request, params }: ActionFunctionArgs) {
// Ensure this is a POST request
if (request.method.toUpperCase() !== "POST") {
return { status: 405, body: "Method Not Allowed" };
}
// Authenticate the request
const authenticationResult = await authenticateApiRequest(request);
if (!authenticationResult) {
return json({ error: "Invalid or Missing API Key" }, { status: 401 });
}
const parsedParams = ParamsSchema.safeParse(params);
if (!parsedParams.success) {
return json(
{ error: "Invalid request parameters", issues: parsedParams.error.issues },
{ status: 400 }
);
}
try {
const existingSchedule = await prisma.taskSchedule.findFirst({
where: scheduleWhereClause(
authenticationResult.environment.projectId,
parsedParams.data.scheduleId
),
});
if (!existingSchedule) {
return json({ error: "Schedule not found" }, { status: 404 });
}
await prisma.taskSchedule.update({
where: scheduleUniqWhereClause(
authenticationResult.environment.projectId,
parsedParams.data.scheduleId
),
data: {
active: true,
},
});
const presenter = new ViewSchedulePresenter();
const result = await presenter.call({
projectId: authenticationResult.environment.projectId,
friendlyId: parsedParams.data.scheduleId,
environmentId: authenticationResult.environment.id,
});
if (!result) {
return json({ error: "Schedule not found" }, { status: 404 });
}
return json(presenter.toJSONResponse(result), { status: 200 });
} catch (error) {
return json(
{ error: error instanceof Error ? error.message : "Internal Server Error" },
{ status: 500 }
);
}
}