-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/116 webinars backend #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
achneerov
wants to merge
11
commits into
develop
Choose a base branch
from
feature/116-webinars-backend
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+229
−0
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3f0984c
feature: added the necessary changes
276186f
small fix
2330015
feat(webinars): remove unused parseBooleanField function
achneerov c6fe492
Update app/(authenticated)/webinars/schema.ts
achneerov 4b9708b
Update app/(authenticated)/webinars/actions.ts
achneerov 3abefab
Update app/(authenticated)/webinars/schema.ts
achneerov f6670c2
Update app/(authenticated)/webinars/actions.ts
achneerov 11a96d3
Update app/(authenticated)/webinars/actions.ts
achneerov 439e686
Update app/(authenticated)/webinars/actions.ts
achneerov 44fde7b
Update app/(authenticated)/webinars/actions.ts
achneerov 396325f
Update app/(authenticated)/webinars/actions.ts
achneerov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| "use server"; | ||
|
|
||
| import { updateTag } from "next/cache"; | ||
| import { eq } from "drizzle-orm"; | ||
| import { db } from "@/lib/db"; | ||
| import { webinars } from "@/lib/db/schema"; | ||
| import { requireAdmin } from "@/lib/auth/require-admin"; | ||
| import { | ||
| createWebinarSchema, | ||
| deleteWebinarSchema, | ||
| parseBooleanField, | ||
| updateWebinarSchema, | ||
| } from "./schema"; | ||
|
|
||
| export type WebinarActionState = { | ||
| errors?: Record<string, string[]>; | ||
| message?: string; | ||
| } | null; | ||
|
|
||
| const WEBINARS_TAG = "webinars"; | ||
|
|
||
| function field(formData: FormData, name: string): string | undefined { | ||
| const value = formData.get(name); | ||
| return value === null ? undefined : value.toString(); | ||
| } | ||
|
|
||
| function invalidateWebinars() { | ||
| updateTag(WEBINARS_TAG); | ||
| } | ||
|
|
||
| function unauthorized(): WebinarActionState { | ||
| return { errors: { _form: ["Unauthorized"] } }; | ||
| } | ||
|
|
||
| export async function createWebinar( | ||
| _prev: WebinarActionState, | ||
| formData: FormData, | ||
| ): Promise<WebinarActionState> { | ||
| try { | ||
| await requireAdmin(); | ||
| } catch { | ||
| return unauthorized(); | ||
| } | ||
|
|
||
| const parsed = createWebinarSchema.safeParse({ | ||
| title: field(formData, "title"), | ||
| description: field(formData, "description"), | ||
| tier: field(formData, "tier"), | ||
| duration_minutes: field(formData, "duration_minutes"), | ||
| youtube_url: field(formData, "youtube_url") || undefined, | ||
| is_active: field(formData, "is_active"), | ||
| ? parseBooleanField(formData.get("is_active")) | ||
| : true, | ||
| }); | ||
|
|
||
| if (!parsed.success) { | ||
| return { errors: parsed.error.flatten().fieldErrors }; | ||
| } | ||
|
|
||
| try { | ||
| await db.insert(webinars).values({ | ||
| title: parsed.data.title, | ||
| description: parsed.data.description || null, | ||
| tier: parsed.data.tier, | ||
| durationMinutes: parsed.data.duration_minutes, | ||
| youtubeUrl: parsed.data.youtube_url || null, | ||
| isActive: parsed.data.is_active !== "false", | ||
| }); | ||
| } catch (error) { | ||
| console.error(error); | ||
| return { errors: { _form: ["Could not create webinar"] } }; | ||
| } | ||
|
|
||
| invalidateWebinars(); | ||
| return { message: "Webinar created." }; | ||
| } | ||
|
|
||
| export async function updateWebinar( | ||
| _prev: WebinarActionState, | ||
| formData: FormData, | ||
| ): Promise<WebinarActionState> { | ||
| try { | ||
| await requireAdmin(); | ||
| } catch { | ||
| return unauthorized(); | ||
| } | ||
|
|
||
| const parsed = updateWebinarSchema.safeParse({ | ||
| webinar_id: field(formData, "webinar_id"), | ||
| title: formData.has("title") ? field(formData, "title") : undefined, | ||
| description: formData.has("description") | ||
| ? (field(formData, "description") ?? "") | ||
| : undefined, | ||
| tier: formData.has("tier") ? field(formData, "tier") : undefined, | ||
| duration_minutes: formData.has("duration_minutes") | ||
| ? field(formData, "duration_minutes") | ||
| : undefined, | ||
| youtube_url: formData.has("youtube_url") | ||
| ? (field(formData, "youtube_url") ?? "") | ||
| : undefined, | ||
| is_active: formData.has("is_active") | ||
| ? parseBooleanField(formData.get("is_active")) | ||
| : undefined, | ||
| }); | ||
|
|
||
| if (!parsed.success) { | ||
| return { errors: parsed.error.flatten().fieldErrors }; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| const patch: Partial<typeof webinars.$inferInsert> = { | ||
| updatedAt: new Date(), | ||
| }; | ||
| if (parsed.data.title !== undefined) patch.title = parsed.data.title; | ||
| if (parsed.data.description !== undefined) { | ||
| patch.description = parsed.data.description || null; | ||
| } | ||
| if (parsed.data.tier !== undefined) patch.tier = parsed.data.tier; | ||
| if (parsed.data.duration_minutes !== undefined) { | ||
| patch.durationMinutes = parsed.data.duration_minutes; | ||
| } | ||
| if (parsed.data.youtube_url !== undefined) { | ||
| patch.youtubeUrl = parsed.data.youtube_url || null; | ||
| } | ||
| if (parsed.data.is_active !== undefined) { | ||
| patch.isActive = parsed.data.is_active === "true"; | ||
| } | ||
|
|
||
| const updated = await db | ||
| .update(webinars) | ||
| .set(patch) | ||
| .where(eq(webinars.id, parsed.data.webinar_id)) | ||
| .returning({ id: webinars.id }); | ||
|
|
||
| if (!updated.length) return { errors: { _form: ["Webinar not found"] } }; | ||
|
|
||
| invalidateWebinars(); | ||
| return { message: "Webinar updated." }; | ||
| } | ||
|
|
||
| export async function deleteWebinar( | ||
| _prev: WebinarActionState, | ||
| formData: FormData, | ||
| ): Promise<WebinarActionState> { | ||
| try { | ||
| await requireAdmin(); | ||
| } catch { | ||
| return unauthorized(); | ||
| } | ||
|
|
||
| const parsed = deleteWebinarSchema.safeParse({ | ||
| webinar_id: field(formData, "webinar_id"), | ||
| }); | ||
|
|
||
| if (!parsed.success) { | ||
| return { errors: parsed.error.flatten().fieldErrors }; | ||
| } | ||
|
|
||
| let deleted; | ||
| try { | ||
| deleted = await db | ||
| .delete(webinars) | ||
| .where(eq(webinars.id, parsed.data.webinar_id)) | ||
| .returning({ id: webinars.id }); | ||
| } catch (error) { | ||
| console.error(error); | ||
| return { errors: { _form: ["Could not delete webinar"] } }; | ||
| } | ||
|
|
||
| if (!deleted.length) { | ||
| return { errors: { _form: ["Webinar not found"] } }; | ||
| } | ||
|
|
||
| invalidateWebinars(); | ||
| return { message: "Webinar deleted." }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { z } from "zod"; | ||
|
|
||
| export const webinarTierSchema = z.enum(["free", "premium"]); | ||
|
|
||
| const youtubeUrlSchema = z | ||
| .string() | ||
| .url("Enter a valid YouTube URL") | ||
| .refine( | ||
| (value) => { | ||
| try { | ||
| const hostname = new URL(value).hostname.toLowerCase(); | ||
| return [ | ||
| "youtube.com", | ||
| "www.youtube.com", | ||
| "youtu.be", | ||
| "www.youtu.be", | ||
| ].includes(hostname); | ||
| } catch { | ||
| return false; | ||
| } | ||
| }, | ||
| { message: "URL must be a YouTube URL" }, | ||
| ); | ||
|
|
||
| const durationSchema = z.coerce | ||
| .number() | ||
| .int("Duration must be a whole number") | ||
| .min(1, "Duration must be at least 1 minute") | ||
| .max(24 * 60, "Duration cannot exceed 24 hours"); | ||
|
|
||
| export const createWebinarSchema = z.object({ | ||
| title: z.string().trim().min(1, "Title is required").max(500), | ||
| description: z.string().trim().max(5000).optional(), | ||
| tier: webinarTierSchema, | ||
| duration_minutes: durationSchema, | ||
| youtube_url: youtubeUrlSchema.optional(), | ||
| is_active: z.enum(["true", "false"]).optional(), | ||
| }); | ||
|
|
||
| export const updateWebinarSchema = z.object({ | ||
| webinar_id: z.string().uuid("Invalid webinar"), | ||
| title: z.string().trim().min(1, "Title cannot be empty").max(500).optional(), | ||
| description: z.string().trim().max(5000).optional(), | ||
| tier: webinarTierSchema.optional(), | ||
| duration_minutes: durationSchema.optional(), | ||
| youtube_url: z.union([youtubeUrlSchema, z.literal("")]).optional(), | ||
| is_active: z.enum(["true", "false"]).optional(), | ||
| }); | ||
|
|
||
| export const deleteWebinarSchema = z.object({ | ||
| webinar_id: z.string().uuid("Invalid webinar"), | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dude I tried that before and it lowkey crashes because if const hostname = new URL(value).hostname.toLowerCase(); fails then this crashes:
return [
"youtube.com",
"www.youtube.com",
"youtu.be",
"www.youtu.be",
].includes(hostname);