I´d like to create a booking platform for a beauty shop (nail design). the customer shall be able to book a treatment. an admin backend is needed to manage articles and their durations.
This commit is contained in:
96
src/server/rpc/bookings.ts
Normal file
96
src/server/rpc/bookings.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import { call, os } from "@orpc/server";
|
||||
import { z } from "zod";
|
||||
import { randomUUID } from "crypto";
|
||||
import { createKV } from "@/server/lib/create-kv";
|
||||
|
||||
const BookingSchema = z.object({
|
||||
id: z.string(),
|
||||
treatmentId: z.string(),
|
||||
customerName: z.string(),
|
||||
customerEmail: z.string(),
|
||||
customerPhone: z.string(),
|
||||
appointmentDate: z.string(), // ISO date string
|
||||
appointmentTime: z.string(), // HH:MM format
|
||||
status: z.enum(["pending", "confirmed", "cancelled", "completed"]),
|
||||
notes: z.string().optional(),
|
||||
createdAt: z.string(),
|
||||
});
|
||||
|
||||
type Booking = z.output<typeof BookingSchema>;
|
||||
|
||||
const kv = createKV<Booking>("bookings");
|
||||
|
||||
const create = os
|
||||
.input(BookingSchema.omit({ id: true, createdAt: true, status: true }))
|
||||
.handler(async ({ input }) => {
|
||||
const id = randomUUID();
|
||||
const booking = {
|
||||
id,
|
||||
...input,
|
||||
status: "pending" as const,
|
||||
createdAt: new Date().toISOString()
|
||||
};
|
||||
await kv.setItem(id, booking);
|
||||
return booking;
|
||||
});
|
||||
|
||||
const updateStatus = os
|
||||
.input(z.object({
|
||||
id: z.string(),
|
||||
status: z.enum(["pending", "confirmed", "cancelled", "completed"])
|
||||
}))
|
||||
.handler(async ({ input }) => {
|
||||
const booking = await kv.getItem(input.id);
|
||||
if (!booking) throw new Error("Booking not found");
|
||||
|
||||
const updatedBooking = { ...booking, status: input.status };
|
||||
await kv.setItem(input.id, updatedBooking);
|
||||
return updatedBooking;
|
||||
});
|
||||
|
||||
const remove = os.input(z.string()).handler(async ({ input }) => {
|
||||
await kv.removeItem(input);
|
||||
});
|
||||
|
||||
const list = os.handler(async () => {
|
||||
return kv.getAllItems();
|
||||
});
|
||||
|
||||
const get = os.input(z.string()).handler(async ({ input }) => {
|
||||
return kv.getItem(input);
|
||||
});
|
||||
|
||||
const getByDate = os
|
||||
.input(z.string()) // YYYY-MM-DD format
|
||||
.handler(async ({ input }) => {
|
||||
const allBookings = await kv.getAllItems();
|
||||
return allBookings.filter(booking => booking.appointmentDate === input);
|
||||
});
|
||||
|
||||
const live = {
|
||||
list: os.handler(async function* ({ signal }) {
|
||||
yield call(list, {}, { signal });
|
||||
for await (const _ of kv.subscribe()) {
|
||||
yield call(list, {}, { signal });
|
||||
}
|
||||
}),
|
||||
|
||||
byDate: os
|
||||
.input(z.string())
|
||||
.handler(async function* ({ input, signal }) {
|
||||
yield call(getByDate, input, { signal });
|
||||
for await (const _ of kv.subscribe()) {
|
||||
yield call(getByDate, input, { signal });
|
||||
}
|
||||
}),
|
||||
};
|
||||
|
||||
export const router = {
|
||||
create,
|
||||
updateStatus,
|
||||
remove,
|
||||
list,
|
||||
get,
|
||||
getByDate,
|
||||
live,
|
||||
};
|
@@ -1,5 +1,9 @@
|
||||
import { demo } from "./demo";
|
||||
|
||||
export const router = {
|
||||
demo,
|
||||
};
|
||||
import { demo } from "./demo";
|
||||
import { router as treatments } from "./treatments";
|
||||
import { router as bookings } from "./bookings";
|
||||
|
||||
export const router = {
|
||||
demo,
|
||||
treatments,
|
||||
bookings,
|
||||
};
|
||||
|
63
src/server/rpc/treatments.ts
Normal file
63
src/server/rpc/treatments.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { call, os } from "@orpc/server";
|
||||
import { z } from "zod";
|
||||
import { randomUUID } from "crypto";
|
||||
import { createKV } from "@/server/lib/create-kv";
|
||||
|
||||
const TreatmentSchema = z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
description: z.string(),
|
||||
duration: z.number(), // duration in minutes
|
||||
price: z.number(), // price in cents
|
||||
category: z.string(),
|
||||
});
|
||||
|
||||
type Treatment = z.output<typeof TreatmentSchema>;
|
||||
|
||||
const kv = createKV<Treatment>("treatments");
|
||||
|
||||
const create = os
|
||||
.input(TreatmentSchema.omit({ id: true }))
|
||||
.handler(async ({ input }) => {
|
||||
const id = randomUUID();
|
||||
const treatment = { id, ...input };
|
||||
await kv.setItem(id, treatment);
|
||||
return treatment;
|
||||
});
|
||||
|
||||
const update = os
|
||||
.input(TreatmentSchema)
|
||||
.handler(async ({ input }) => {
|
||||
await kv.setItem(input.id, input);
|
||||
return input;
|
||||
});
|
||||
|
||||
const remove = os.input(z.string()).handler(async ({ input }) => {
|
||||
await kv.removeItem(input);
|
||||
});
|
||||
|
||||
const list = os.handler(async () => {
|
||||
return kv.getAllItems();
|
||||
});
|
||||
|
||||
const get = os.input(z.string()).handler(async ({ input }) => {
|
||||
return kv.getItem(input);
|
||||
});
|
||||
|
||||
const live = {
|
||||
list: os.handler(async function* ({ signal }) {
|
||||
yield call(list, {}, { signal });
|
||||
for await (const _ of kv.subscribe()) {
|
||||
yield call(list, {}, { signal });
|
||||
}
|
||||
}),
|
||||
};
|
||||
|
||||
export const router = {
|
||||
create,
|
||||
update,
|
||||
remove,
|
||||
list,
|
||||
get,
|
||||
live,
|
||||
};
|
Reference in New Issue
Block a user