11"use server" ;
22import { createClientServerComponent } from "@/utils/supabase/server" ;
33import { deleteImage , getImagePath } from "@/utils/uploadImage" ;
4+ import { createPostSchema , editPostSchema } from "./validation" ;
5+ import { ZodError } from "zod" ;
46
57
68export const getUserRole = async ( userId : Number | null ) : Promise < string | null > => {
@@ -43,15 +45,22 @@ type AddPostParams = {
4345 tag_ids ?: number [ ] | null ;
4446} ;
4547
46- export const addPost = async ( {
47- title,
48- content,
49- author_id,
50- image_url,
51- content_image_ids,
52- tag_ids,
53- } : AddPostParams ) => {
48+ export const addPost = async (
49+ payload
50+ : AddPostParams ) => {
5451 try {
52+ // Validation
53+ const parsed = createPostSchema . parse ( payload ) ;
54+
55+ const {
56+ title,
57+ content,
58+ author_id,
59+ image_url,
60+ content_image_ids,
61+ tag_ids,
62+ } = parsed ;
63+
5564 const supabase = await createClientServerComponent ( ) ;
5665
5766 // 1. Insert the post
@@ -105,6 +114,10 @@ export const addPost = async ({
105114 return newPost ;
106115 } catch ( error ) {
107116 console . error ( "Error in addPost:" , error ) ;
117+
118+ if ( error instanceof ZodError ) {
119+ throw new Error ( error . errors [ 0 ] ?. message ?? "Invalid post data" ) ;
120+ }
108121 throw error ;
109122 }
110123} ;
@@ -155,16 +168,22 @@ type EditPostParams = {
155168 tag_ids ?: number [ ] | null ;
156169} ;
157170
158- export const editPost = async ( {
159- id,
160- title,
161- content,
162- author_id,
163- image_url,
164- content_image_ids,
165- tag_ids,
166- } : EditPostParams ) => {
171+ export const editPost = async ( payload : EditPostParams ) => {
167172 try {
173+ // Validation
174+ const parsed = editPostSchema . parse ( payload ) ;
175+
176+ const {
177+ id,
178+ title,
179+ content,
180+ author_id,
181+ image_url,
182+ content_image_ids,
183+ tag_ids,
184+ } = parsed ;
185+
186+
168187 const supabase = await createClientServerComponent ( ) ;
169188
170189 // 1. Fetch existing post for old image cleanup
@@ -255,6 +274,9 @@ export const editPost = async ({
255274 return updatedPost ;
256275 } catch ( error ) {
257276 console . error ( "Error editing post:" , error ) ;
277+ if ( error instanceof ZodError ) {
278+ throw new Error ( error . errors [ 0 ] ?. message ?? "Invalid post data" ) ;
279+ }
258280 throw error ;
259281 }
260282} ;
@@ -553,3 +575,29 @@ export async function deletePostComment(comment_id: string) {
553575 throw error ;
554576 }
555577}
578+
579+ export const hasReachedDailyPostLimit = async ( author_id : string , limit = 2 ) => {
580+ try {
581+ const supabase = await createClientServerComponent ( ) ;
582+
583+ // Timestamp 24 hours ago
584+ const twentyFourHoursAgo = new Date ( Date . now ( ) - 24 * 60 * 60 * 1000 ) . toISOString ( ) ;
585+
586+ const { count, error } = await supabase
587+ . from ( "posts" )
588+ . select ( "id" , { count : "exact" , head : true } ) // head: true avoids fetching rows
589+ . eq ( "author_id" , author_id )
590+ . gte ( "created_at" , twentyFourHoursAgo ) ;
591+
592+ if ( error ) {
593+ console . error ( "Error checking daily post limit:" , error ) ;
594+ throw error ;
595+ }
596+
597+ // Return true if count >= limit
598+ return ( count ?? 0 ) >= limit ;
599+ } catch ( error ) {
600+ console . error ( "Error in hasReachedDailyPostLimit:" , error ) ;
601+ throw error ;
602+ }
603+ } ;
0 commit comments