Skip to content

Commit a96bcbc

Browse files
committed
fix(services): add coachId handling in create and update service functions
1 parent 1cc23cc commit a96bcbc

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

app/(authenticated)/services/actions.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ export async function createService(
165165
// so they still run when baseFields fails on unrelated fields.
166166
const typeRaw = formData.get("type")?.toString();
167167
let scheduledAtValue: ProgramSchedule | null = null;
168+
let coachIdValue: string | null = null;
168169
if (typeRaw === "programs") {
169170
const result = parseProgramSchedule(formData);
170171
if (!result.ok) {
@@ -175,6 +176,7 @@ export async function createService(
175176
} else if (typeRaw === "private_lessons") {
176177
const coach = parseCoachId(formData);
177178
if (!coach.ok) Object.assign(errors, coach.errors);
179+
else coachIdValue = coach.value;
178180
}
179181

180182
if (Object.keys(errors).length > 0) {
@@ -203,6 +205,7 @@ export async function createService(
203205
slots: scheduledAtValue?.slots ?? null,
204206
durationMinutes: duration_minutes,
205207
stripeProductId: productId,
208+
coachId: coachIdValue,
206209
status: "active",
207210
});
208211
} catch (e) {
@@ -298,13 +301,18 @@ export async function updateService(
298301
}
299302

300303
let scheduledAtValue: ProgramSchedule | undefined;
304+
let coachIdValue: string | undefined;
301305
if (row.type === "programs" && formData.has("start_date")) {
302306
const result = parseProgramSchedule(formData);
303307
if (!result.ok) {
304308
Object.assign(errors, result.errors);
305309
} else {
306310
scheduledAtValue = result.value;
307311
}
312+
} else if (row.type === "private_lessons" && formData.has("coach_id")) {
313+
const coach = parseCoachId(formData);
314+
if (!coach.ok) Object.assign(errors, coach.errors);
315+
else coachIdValue = coach.value;
308316
}
309317

310318
if (Object.keys(errors).length > 0) {
@@ -334,6 +342,7 @@ export async function updateService(
334342
dbPatch.endDate = scheduledAtValue.endDate;
335343
dbPatch.slots = scheduledAtValue.slots;
336344
}
345+
if (coachIdValue !== undefined) dbPatch.coachId = coachIdValue;
337346

338347
if (Object.keys(dbPatch).length > 0) {
339348
dbPatch.updatedAt = new Date();

app/(authenticated)/services/queries.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import { getStripeServiceData } from "@/lib/stripe";
66
import type { ProgramSchedule } from "@/app/(authenticated)/services/actions";
77

88
const SERVICES_TAG = "services";
9+
10+
const UUID_RE =
11+
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
912
const COACHES_TAG = "coaches";
1013

1114
export type ServiceStatus = "active" | "disabled" | "archived" | "deleted";
@@ -18,6 +21,7 @@ export type ServiceView = {
1821
durationMinutes: number;
1922
status: ServiceStatus;
2023
stripeProductId: string;
24+
coachId: string | null;
2125
createdAt: Date;
2226
updatedAt: Date;
2327
title: string | null;
@@ -37,7 +41,9 @@ function rowToSchedule(
3741
};
3842
}
3943

40-
async function buildServiceView(row: typeof services.$inferSelect): Promise<ServiceView> {
44+
async function buildServiceView(
45+
row: typeof services.$inferSelect,
46+
): Promise<ServiceView> {
4147
const stripeData = await getStripeServiceData(row.stripeProductId);
4248
return {
4349
id: row.id,
@@ -46,6 +52,7 @@ async function buildServiceView(row: typeof services.$inferSelect): Promise<Serv
4652
durationMinutes: row.durationMinutes,
4753
status: row.status,
4854
stripeProductId: row.stripeProductId,
55+
coachId: row.coachId,
4956
createdAt: row.createdAt,
5057
updatedAt: row.updatedAt,
5158
title: stripeData?.title ?? null,
@@ -93,6 +100,8 @@ export async function getService(id: string): Promise<ServiceView | null> {
93100
"use cache";
94101
cacheTag(SERVICES_TAG);
95102

103+
if (!UUID_RE.test(id)) return null;
104+
96105
const [row] = await db
97106
.select()
98107
.from(services)

app/(authenticated)/services/service-dialog.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,9 @@ export function ServiceDialog(props: Props) {
248248
const [type, setType] = React.useState<"programs" | "private_lessons">(
249249
service?.type ?? "programs",
250250
);
251-
const [coachId, setCoachId] = React.useState<string>("");
251+
const [coachId, setCoachId] = React.useState<string>(
252+
service?.coachId ?? "",
253+
);
252254
const [title, setTitle] = React.useState<string>(service?.title ?? "");
253255
const [description, setDescription] = React.useState<string>(
254256
service?.description ?? "",
@@ -267,7 +269,7 @@ export function ServiceDialog(props: Props) {
267269
React.useEffect(() => {
268270
if (service) {
269271
setType(service.type);
270-
setCoachId("");
272+
setCoachId(service.coachId ?? "");
271273
setTitle(service.title ?? "");
272274
setDescription(service.description ?? "");
273275
setDurationMinutes(String(service.durationMinutes ?? 60));

0 commit comments

Comments
 (0)