-
Notifications
You must be signed in to change notification settings - Fork 6
Enhance site content management and support for Supabase client #70
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
Merged
+1,095
−25
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
52b940c
update schema
Harish-Naruto 29ffade
add Client util to avoid Parameter Tunneling
Harish-Naruto 6955f6b
add service to support dynamic site content
Harish-Naruto 6206abe
fix server Api doc rendering and path
Harish-Naruto 26ea8a8
Site content test
Harish-Naruto 0a123f4
change supabase client
Harish-Naruto 5130843
change test to support supabase client
Harish-Naruto ed5acdb
fix double delete
Harish-Naruto 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
34 changes: 34 additions & 0 deletions
34
prisma/migrations/20260614203131_add_dynamic_site_feature/migration.sql
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,34 @@ | ||
| -- CreateTable | ||
| CREATE TABLE "SitePageContent" ( | ||
| "id" INTEGER NOT NULL DEFAULT 1, | ||
| "heroImageUrl" TEXT, | ||
| "heroCaption" TEXT, | ||
| "heroAltText" TEXT, | ||
| "galleryPhotos" JSONB NOT NULL DEFAULT '[]', | ||
| "updatedById" TEXT, | ||
| "updatedAt" TIMESTAMP(3) NOT NULL, | ||
|
|
||
| CONSTRAINT "SitePageContent_pkey" PRIMARY KEY ("id") | ||
| ); | ||
|
|
||
| -- CreateTable | ||
| CREATE TABLE "SiteAction" ( | ||
| "id" SERIAL NOT NULL, | ||
| "key" TEXT NOT NULL, | ||
| "label" TEXT, | ||
| "url" TEXT, | ||
| "isVisible" BOOLEAN NOT NULL DEFAULT false, | ||
| "updatedById" TEXT, | ||
| "updatedAt" TIMESTAMP(3) NOT NULL, | ||
|
|
||
| CONSTRAINT "SiteAction_pkey" PRIMARY KEY ("id") | ||
| ); | ||
|
|
||
| -- CreateIndex | ||
| CREATE UNIQUE INDEX "SiteAction_key_key" ON "SiteAction"("key"); | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "SitePageContent" ADD CONSTRAINT "SitePageContent_updatedById_fkey" FOREIGN KEY ("updatedById") REFERENCES "Member"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "SiteAction" ADD CONSTRAINT "SiteAction_updatedById_fkey" FOREIGN KEY ("updatedById") REFERENCES "Member"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
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
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
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
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
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,220 @@ | ||
| import { Request, Response } from "express"; | ||
| import * as siteContentService from "../services/site-content.service"; | ||
| import { uploadImage, deleteImage } from "../utils/imageUtils"; | ||
| import { supabase } from "../utils/supabaseClient"; | ||
| import { ApiError } from "../utils/apiError"; | ||
|
|
||
| function parseSiteContentData(body: Record<string, unknown>) { | ||
| let siteContentData = body.siteContentData ?? body; | ||
|
|
||
| if (typeof siteContentData === "string") { | ||
| try { | ||
| siteContentData = JSON.parse(siteContentData); | ||
| } catch { | ||
| throw new ApiError("Invalid JSON in siteContentData field", 400); | ||
| } | ||
| } | ||
|
|
||
| return siteContentData as Record<string, unknown>; | ||
| } | ||
|
|
||
| function parseActionData(body: Record<string, unknown>) { | ||
| let actionData = body.actionData ?? body; | ||
|
|
||
| if (typeof actionData === "string") { | ||
| try { | ||
| actionData = JSON.parse(actionData); | ||
| } catch { | ||
| throw new ApiError("Invalid JSON in actionData field", 400); | ||
| } | ||
| } | ||
|
|
||
| return actionData as Record<string, unknown>; | ||
| } | ||
|
|
||
| function parsePhotoData(body: Record<string, unknown>) { | ||
| let photoData = body.photoData ?? body; | ||
|
|
||
| if (typeof photoData === "string") { | ||
| try { | ||
| photoData = JSON.parse(photoData); | ||
| } catch { | ||
| throw new ApiError("Invalid JSON in photoData field", 400); | ||
| } | ||
| } | ||
|
|
||
| return photoData as Record<string, unknown>; | ||
| } | ||
|
|
||
| export const getSiteContent = async (_req: Request, res: Response) => { | ||
| const content = await siteContentService.getSiteContent(); | ||
|
|
||
| res.status(200).json({ | ||
| success: true, | ||
| data: content, | ||
| }); | ||
| }; | ||
|
|
||
| export const updateSiteContent = async (req: Request, res: Response) => { | ||
| const siteContentData = parseSiteContentData(req.body); | ||
| const adminId = siteContentData.adminId as string | undefined; | ||
|
|
||
| if (!adminId) { | ||
| throw new ApiError("adminId is required", 400); | ||
| } | ||
|
Harish-Naruto marked this conversation as resolved.
|
||
|
|
||
| const file = req.file; | ||
| let heroImageUrl: string | undefined; | ||
|
|
||
| if (file) { | ||
| const current = await siteContentService.getSiteContent(); | ||
| heroImageUrl = await uploadImage( | ||
| supabase, | ||
| file, | ||
| "group-photos", | ||
| current.hero.imageUrl ?? undefined, | ||
| ); | ||
| } | ||
|
|
||
| const content = await siteContentService.updateSitePageContent(adminId, { | ||
| heroCaption: siteContentData.heroCaption as string | null | undefined, | ||
| heroAltText: siteContentData.heroAltText as string | null | undefined, | ||
| heroImageUrl, | ||
| }); | ||
|
|
||
| res.status(200).json({ | ||
| success: true, | ||
| data: content, | ||
| }); | ||
| }; | ||
|
|
||
| export const updateSiteAction = async (req: Request, res: Response) => { | ||
| const key = req.params.key; | ||
| if (!key) { | ||
| throw new ApiError("Action key is required", 400); | ||
| } | ||
|
|
||
| const actionData = parseActionData(req.body); | ||
| const adminId = actionData.adminId as string | undefined; | ||
|
|
||
| if (!adminId) { | ||
| throw new ApiError("adminId is required", 400); | ||
| } | ||
|
|
||
| const content = await siteContentService.updateSiteAction(adminId, key, { | ||
| label: actionData.label as string | null | undefined, | ||
| url: actionData.url as string | null | undefined, | ||
| isVisible: actionData.isVisible as boolean | undefined, | ||
| }); | ||
|
|
||
| res.status(200).json({ | ||
| success: true, | ||
| data: content, | ||
| }); | ||
| }; | ||
|
|
||
| export const addGalleryPhoto = async (req: Request, res: Response) => { | ||
| const file = req.file; | ||
| if (!file) { | ||
| throw new ApiError("Image file is required", 400); | ||
| } | ||
|
|
||
| const photoData = parsePhotoData(req.body); | ||
| const adminId = photoData.adminId as string | undefined; | ||
|
|
||
| if (!adminId) { | ||
| throw new ApiError("adminId is required", 400); | ||
| } | ||
|
|
||
| const imageUrl = await uploadImage(supabase, file, "group-photos"); | ||
| if (!imageUrl) { | ||
| throw new ApiError("Image URL is missing", 400); | ||
| } | ||
|
|
||
| const content = await siteContentService.addGalleryPhoto(adminId, { | ||
| imageUrl, | ||
| caption: photoData.caption as string | undefined, | ||
| altText: photoData.altText as string | undefined, | ||
| sortOrder: photoData.sortOrder as number | undefined, | ||
| }); | ||
|
|
||
| res.status(201).json({ | ||
| success: true, | ||
| data: content, | ||
| }); | ||
| }; | ||
|
|
||
| export const updateGalleryPhoto = async (req: Request, res: Response) => { | ||
| const photoId = req.params.photoId; | ||
| if (!photoId) { | ||
| throw new ApiError("Photo ID is required", 400); | ||
| } | ||
|
|
||
| const photoData = parsePhotoData(req.body); | ||
| const adminId = photoData.adminId as string | undefined; | ||
|
|
||
| if (!adminId) { | ||
| throw new ApiError("adminId is required", 400); | ||
| } | ||
|
|
||
| const file = req.file; | ||
| let imageUrl: string | undefined; | ||
|
|
||
| if (file) { | ||
| const existing = await siteContentService.getGalleryPhoto(adminId, photoId); | ||
| imageUrl = await uploadImage( | ||
| supabase, | ||
| file, | ||
| "group-photos", | ||
| existing.imageUrl, | ||
| ); | ||
|
Harish-Naruto marked this conversation as resolved.
|
||
| } | ||
|
|
||
| const hasUpdate = | ||
| imageUrl !== undefined || | ||
| photoData.caption !== undefined || | ||
| photoData.altText !== undefined || | ||
| photoData.sortOrder !== undefined; | ||
|
|
||
| if (!hasUpdate) { | ||
| throw new ApiError( | ||
| "At least one field (image, caption, altText, or sortOrder) must be provided", | ||
| 400, | ||
| ); | ||
| } | ||
|
|
||
| const { content} = | ||
| await siteContentService.updateGalleryPhoto(adminId, photoId, { | ||
| imageUrl, | ||
| caption: photoData.caption as string | null | undefined, | ||
| altText: photoData.altText as string | null | undefined, | ||
| sortOrder: photoData.sortOrder as number | undefined, | ||
| }); | ||
|
|
||
|
|
||
| res.status(200).json({ | ||
| success: true, | ||
| data: content, | ||
| }); | ||
| }; | ||
|
|
||
| export const deleteGalleryPhoto = async (req: Request, res: Response) => { | ||
| const photoId = req.params.photoId; | ||
| const adminId = req.body.adminId as string | undefined; | ||
|
|
||
| if (!photoId) { | ||
| throw new ApiError("Photo ID is required", 400); | ||
| } | ||
|
|
||
| if (!adminId) { | ||
| throw new ApiError("adminId is required", 400); | ||
| } | ||
|
|
||
| const imageUrl = await siteContentService.deleteGalleryPhoto(adminId, photoId); | ||
| await deleteImage(supabase, imageUrl); | ||
|
|
||
| res.status(200).json({ | ||
| success: true, | ||
| message: "Gallery photo deleted successfully", | ||
| }); | ||
| }; | ||
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
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
Oops, something went wrong.
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.
Seed the built-in
SiteActionkeys before shipping the update-only API.The migration creates an empty
SiteActiontable, but the supplied service path first doesfindUnique({ where: { key } })and throws 404 when the row is missing; the stack listsupdateSiteActionbut no create action route/service. On a fresh database, keys such asrecruitmentcannot be managed until seeded out-of-band. Seed every supported key here, or change the service to upsert bykey.Example migration direction
CREATE UNIQUE INDEX "SiteAction_key_key" ON "SiteAction"("key"); + +-- Seed every fixed action key supported by the site-content API. +-- Keep this list in sync with the API/types. +INSERT INTO "SiteAction" ("key", "label", "url", "isVisible", "updatedAt") +VALUES + ('recruitment', NULL, NULL, false, CURRENT_TIMESTAMP) +ON CONFLICT ("key") DO NOTHING;🤖 Prompt for AI Agents