-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path_actions.ts
More file actions
94 lines (77 loc) · 3.02 KB
/
_actions.ts
File metadata and controls
94 lines (77 loc) · 3.02 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"use server";
import { auth } from "@/auth";
import { post_EventTypesController_createEventType } from "@/cal/__generated/cal-sdk";
import { cal } from "@/cal/api";
import { revalidatePath } from "next/cache";
import { type z } from "zod";
export async function createEventType(
_prevState: { error: null | string } | { success: null | string },
formData: FormData
) {
const sesh = await auth();
if (!sesh?.user.id) {
return { error: "Unauthorized" };
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const updateEventTypeBodyData = Object.fromEntries(
Array.from(formData.entries())
.filter(([key]) => !key.toLowerCase().startsWith("$action"))
.map(([key, value]) => {
if (key === "length") return [key, Number(value)];
return [key, value];
})
);
const updateEventTypeParameters = {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
body: updateEventTypeBodyData,
} satisfies z.infer<typeof post_EventTypesController_createEventType.parameters>;
const input = post_EventTypesController_createEventType.parameters.safeParse(updateEventTypeParameters);
if (!input.success) {
return { error: "Invalid form data" };
}
const res = await cal({ user: { id: sesh?.user.id } }).post("/v2/event-types", {
body: input.data.body,
});
if (res.status === "error") {
console.error(
`[_actions] Error creating event type for user with id '${sesh.user.id}'. Bad response from Cal Platform API
-- REQUEST DETAILS --
Endpoint URL: POST /v2/event-types
Options: ${JSON.stringify(input.data.body)}
-- RESPONSE DETAILS --
responseStatus: ${JSON.stringify(res.status)}
responseData: ${JSON.stringify(res.data)}
`
);
return { error: "Unable to create the booking event (something went wrong)." };
}
const permalink = String(formData.get("permalink"));
permalink && revalidatePath(permalink);
return { success: `Event type '${res.data.title}' created successfully.` };
}
export async function deleteEventType(
_prevState: { error: null | string } | { success: null | string },
eventTypeId: number
) {
const sesh = await auth();
if (!sesh?.user.id) {
console.error("[_actions] Unauthorized user delete");
return { error: "Unauthorized" };
}
const res = await cal({ user: { id: sesh?.user.id } }).delete(`/v2/event-types/{eventTypeId}`, {
path: { eventTypeId },
});
if (res.status === "error") {
console.error(
`[_actions] Error deleting event type for user with id '${sesh.user.id}'. Bad response from Cal Platform API
-- REQUEST DETAILS --
Endpoint URL: DELETE /v2/event-types/{eventTypeId}
-- RESPONSE DETAILS --
responseStatus: ${JSON.stringify(res.status)}
responseData: ${JSON.stringify(res.data)}
`
);
return { error: "Unable to delete the booking event (something went wrong)." };
}
return { success: `Event type '${res.data.title}' created successfully.` };
}