Skip to content

Commit c38085b

Browse files
committed
made service type immutable and made a function to update the scheduled at property
1 parent 9edd24b commit c38085b

1 file changed

Lines changed: 24 additions & 28 deletions

File tree

app/dashboard/services/actions.ts

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,12 @@ export async function createService(
8181
}
8282

8383
let scheduledAtValue: unknown = null;
84-
if (type === "booking" && scheduled_at && scheduled_at.trim()) {
85-
try {
86-
const json = JSON.parse(scheduled_at);
87-
if (!Array.isArray(json)) {
88-
return { errors: { scheduled_at: ["Must be a JSON array"] } };
89-
}
90-
scheduledAtValue = json;
91-
} catch {
92-
return { errors: { scheduled_at: ["Must be valid JSON"] } };
84+
if (type === "booking" && scheduled_at) {
85+
const result = parseScheduledAt(scheduled_at);
86+
if (!result.ok) {
87+
return { errors: { scheduled_at: [result.error] } };
9388
}
89+
scheduledAtValue = result.value;
9490
}
9591

9692
let createdProductId: string | null = null;
@@ -110,6 +106,7 @@ export async function createService(
110106
stripeProductId: productId,
111107
status: "active",
112108
});
109+
// true product deletion in stripe requires archiving the price so letting the product as inactive is fine
113110
} catch (e) {
114111
if (createdProductId) {
115112
try {
@@ -139,7 +136,6 @@ const updateFields = z.object({
139136
service_id: z.string().uuid(),
140137
title: z.string().min(1, "Title cannot be empty").max(500).optional(),
141138
description: z.string().max(5000).optional(),
142-
type: serviceTypeSchema.optional(),
143139
duration_minutes: z.coerce.number().int().min(1).max(24 * 60).optional(),
144140
scheduled_at: z.string().optional(),
145141
price_cad: z.string().min(1, "Price cannot be empty").optional(),
@@ -168,7 +164,6 @@ export async function updateService(
168164
service_id: field(formData, "service_id"),
169165
title: field(formData, "title"),
170166
description: field(formData, "description"),
171-
type: field(formData, "type"),
172167
duration_minutes: field(formData, "duration_minutes"),
173168
scheduled_at: field(formData, "scheduled_at"),
174169
price_cad: field(formData, "price_cad"),
@@ -181,7 +176,6 @@ export async function updateService(
181176
service_id,
182177
title,
183178
description,
184-
type,
185179
duration_minutes,
186180
scheduled_at,
187181
price_cad,
@@ -197,22 +191,11 @@ export async function updateService(
197191
}
198192

199193
let scheduledAtValue: unknown;
200-
if (scheduled_at !== undefined) {
201-
const trimmed = scheduled_at.trim();
202-
if (trimmed === "") {
203-
scheduledAtValue = null;
204-
} else {
205-
try {
206-
const json = JSON.parse(trimmed);
207-
if (!Array.isArray(json)) {
208-
return { errors: { scheduled_at: ["Must be a JSON array"] } };
209-
}
210-
scheduledAtValue = json;
211-
} catch {
212-
return { errors: { scheduled_at: ["Must be valid JSON"] } };
213-
}
214-
}
194+
const result = parseScheduledAt(scheduled_at);
195+
if (!result.ok){
196+
return {errors:{scheduled_at: [result.error]}}
215197
}
198+
scheduledAtValue = result.value;
216199

217200
const [row] = await db
218201
.select()
@@ -242,7 +225,6 @@ export async function updateService(
242225
}
243226

244227
const dbPatch: Partial<typeof services.$inferInsert> = {};
245-
if (type !== undefined) dbPatch.type = type;
246228
if (duration_minutes !== undefined) dbPatch.durationMinutes = duration_minutes;
247229
if (scheduled_at !== undefined) dbPatch.scheduledAt = scheduledAtValue;
248230

@@ -337,3 +319,17 @@ export async function setServiceStatus(
337319
bustServicesCache();
338320
return { message: "Service status updated." };
339321
}
322+
323+
324+
function parseScheduledAt(raw: string | undefined): {ok: true; value:unknown} | {ok:false, error:string}
325+
{
326+
if (!raw || !raw.trim()) return {ok:true,value: null};
327+
try {
328+
const json = JSON.parse(raw.trim());
329+
if (!Array.isArray(json)) return {ok:false, error: "Must be a JSON array"};
330+
return {ok: true, value: json};
331+
332+
}catch {
333+
return {ok: false, error: "Must be valid JSON"};
334+
}
335+
}

0 commit comments

Comments
 (0)