Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=
WORDPRESS_API_URL=http://xxx.com
WORDPRESS_USERNAME=mcld-dashboard
WORDPRESS_APP_PASSWORD="xxxx xxxx xxxx xxxx xxxx xxxx"
WORDPRESS_POST_TYPE=posts
WORDPRESS_POST_TYPE=posts

BREVO_SMTP_HOST=
BREVO_SMTP_PORT=
BREVO_SMTP_USER=
BREVO_SMTP_PASSWORD=
EMAIL_FROM=
EMAIL_TIMEZONE=
APP_URL=
35 changes: 0 additions & 35 deletions app/(authenticated)/checkout/cancel/page.tsx

This file was deleted.

38 changes: 0 additions & 38 deletions app/(authenticated)/checkout/success/page.tsx

This file was deleted.

9 changes: 9 additions & 0 deletions app/(authenticated)/services/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export async function createService(
// so they still run when baseFields fails on unrelated fields.
const typeRaw = formData.get("type")?.toString();
let scheduledAtValue: ProgramSchedule | null = null;
let coachIdValue: string | null = null;
if (typeRaw === "programs") {
const result = parseProgramSchedule(formData);
if (!result.ok) {
Expand All @@ -175,6 +176,7 @@ export async function createService(
} else if (typeRaw === "private_lessons") {
const coach = parseCoachId(formData);
if (!coach.ok) Object.assign(errors, coach.errors);
else coachIdValue = coach.value;
}

if (Object.keys(errors).length > 0) {
Expand Down Expand Up @@ -203,6 +205,7 @@ export async function createService(
slots: scheduledAtValue?.slots ?? null,
durationMinutes: duration_minutes,
stripeProductId: productId,
coachId: coachIdValue,
status: "active",
});
} catch (e) {
Expand Down Expand Up @@ -298,13 +301,18 @@ export async function updateService(
}

let scheduledAtValue: ProgramSchedule | undefined;
let coachIdValue: string | undefined;
if (row.type === "programs" && formData.has("start_date")) {
const result = parseProgramSchedule(formData);
if (!result.ok) {
Object.assign(errors, result.errors);
} else {
scheduledAtValue = result.value;
}
} else if (row.type === "private_lessons" && formData.has("coach_id")) {
const coach = parseCoachId(formData);
if (!coach.ok) Object.assign(errors, coach.errors);
else coachIdValue = coach.value;
}

if (Object.keys(errors).length > 0) {
Expand Down Expand Up @@ -334,6 +342,7 @@ export async function updateService(
dbPatch.endDate = scheduledAtValue.endDate;
dbPatch.slots = scheduledAtValue.slots;
}
if (coachIdValue !== undefined) dbPatch.coachId = coachIdValue;

if (Object.keys(dbPatch).length > 0) {
dbPatch.updatedAt = new Date();
Expand Down
11 changes: 10 additions & 1 deletion app/(authenticated)/services/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { getStripeServiceData } from "@/lib/stripe";
import type { ProgramSchedule } from "@/app/(authenticated)/services/actions";

const SERVICES_TAG = "services";

const UUID_RE =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
const COACHES_TAG = "coaches";

export type ServiceStatus = "active" | "disabled" | "archived" | "deleted";
Expand All @@ -18,6 +21,7 @@ export type ServiceView = {
durationMinutes: number;
status: ServiceStatus;
stripeProductId: string;
coachId: string | null;
createdAt: Date;
updatedAt: Date;
title: string | null;
Expand All @@ -37,7 +41,9 @@ function rowToSchedule(
};
}

async function buildServiceView(row: typeof services.$inferSelect): Promise<ServiceView> {
async function buildServiceView(
row: typeof services.$inferSelect,
): Promise<ServiceView> {
const stripeData = await getStripeServiceData(row.stripeProductId);
return {
id: row.id,
Expand All @@ -46,6 +52,7 @@ async function buildServiceView(row: typeof services.$inferSelect): Promise<Serv
durationMinutes: row.durationMinutes,
status: row.status,
stripeProductId: row.stripeProductId,
coachId: row.coachId,
createdAt: row.createdAt,
updatedAt: row.updatedAt,
title: stripeData?.title ?? null,
Expand Down Expand Up @@ -93,6 +100,8 @@ export async function getService(id: string): Promise<ServiceView | null> {
"use cache";
cacheTag(SERVICES_TAG);

if (!UUID_RE.test(id)) return null;

const [row] = await db
.select()
.from(services)
Expand Down
6 changes: 4 additions & 2 deletions app/(authenticated)/services/service-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,9 @@ export function ServiceDialog(props: Props) {
const [type, setType] = React.useState<"programs" | "private_lessons">(
service?.type ?? "programs",
);
const [coachId, setCoachId] = React.useState<string>("");
const [coachId, setCoachId] = React.useState<string>(
service?.coachId ?? "",
);
const [title, setTitle] = React.useState<string>(service?.title ?? "");
const [description, setDescription] = React.useState<string>(
service?.description ?? "",
Expand All @@ -267,7 +269,7 @@ export function ServiceDialog(props: Props) {
React.useEffect(() => {
if (service) {
setType(service.type);
setCoachId("");
setCoachId(service.coachId ?? "");
setTitle(service.title ?? "");
setDescription(service.description ?? "");
setDurationMinutes(String(service.durationMinutes ?? 60));
Expand Down
11 changes: 9 additions & 2 deletions app/api/webhooks/stripe/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "@/lib/db/schema";
import { and, eq } from "drizzle-orm";
import Stripe from "stripe";
import { notifyCoachOfBooking } from "@/app/scheduling/notifications";

const allowedEvents: Stripe.Event.Type[] = [
"checkout.session.completed",
Expand Down Expand Up @@ -51,15 +52,21 @@ export async function POST(request: NextRequest) {
const metadata = session.metadata ?? {};

if (metadata.type === "private_lesson" && metadata.coachingSessionId) {
await db
const updated = await db
.update(coachingSessions)
.set({ status: "pending", stripeOrderId: session.id })
.where(
and(
eq(coachingSessions.id, metadata.coachingSessionId),
eq(coachingSessions.status, "awaiting_payment"),
),
);
)
.returning({ id: coachingSessions.id });

const transitioned = updated[0];
if (transitioned) {
await notifyCoachOfBooking(transitioned.id);
}
return NextResponse.json({ received: true });
}

Expand Down
Loading
Loading