mirror of
https://github.com/felipegcoutinho/openmonetis.git
synced 2026-03-10 04:51:47 +00:00
refactor: remover funções, tipos e exports não utilizados
Remove createActionHandler, validateHashToken, decimalSchema, optionalPeriodSchema, dateStringSchema, amountSchema, FeedbackDialog standalone, CalendarEventType, parseDateKey, entre outros. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import { revalidatePath, revalidateTag } from "next/cache";
|
||||
import { z } from "zod";
|
||||
import { getUser } from "@/lib/auth/server";
|
||||
import type { ActionResult } from "./types";
|
||||
import { errorResult } from "./types";
|
||||
|
||||
@@ -58,72 +57,3 @@ export function revalidateForEntity(
|
||||
revalidateTag("dashboard", "max");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Options for action handler
|
||||
*/
|
||||
interface ActionHandlerOptions {
|
||||
/** Paths to revalidate after successful execution */
|
||||
revalidatePaths?: string[];
|
||||
/** Entity to revalidate (uses predefined config) */
|
||||
revalidateEntity?: keyof typeof revalidateConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a standardized action handler with automatic user auth and error handling
|
||||
*
|
||||
* @param schema - Zod schema for input validation
|
||||
* @param handler - Handler function that receives validated data and userId
|
||||
* @param options - Additional options for the action
|
||||
* @returns Action function that can be called from client
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* export const createItemAction = createActionHandler(
|
||||
* createItemSchema,
|
||||
* async (data, userId) => {
|
||||
* await db.insert(items).values({ ...data, userId });
|
||||
* return "Item criado com sucesso.";
|
||||
* },
|
||||
* { revalidateEntity: 'items' }
|
||||
* );
|
||||
* ```
|
||||
*/
|
||||
export function createActionHandler<TInput, TResult = string>(
|
||||
schema: z.ZodSchema<TInput>,
|
||||
handler: (data: TInput, userId: string) => Promise<TResult>,
|
||||
options?: ActionHandlerOptions,
|
||||
) {
|
||||
return async (input: unknown): Promise<ActionResult<TResult>> => {
|
||||
try {
|
||||
// Get authenticated user
|
||||
const user = await getUser();
|
||||
|
||||
// Validate input
|
||||
const data = schema.parse(input);
|
||||
|
||||
// Execute handler
|
||||
const result = await handler(data, user.id);
|
||||
|
||||
// Revalidate paths if configured
|
||||
if (options?.revalidateEntity) {
|
||||
revalidateForEntity(options.revalidateEntity);
|
||||
} else if (options?.revalidatePaths) {
|
||||
options.revalidatePaths.forEach((path) => revalidatePath(path));
|
||||
}
|
||||
|
||||
// Return success with message (if result is string) or data
|
||||
if (typeof result === "string") {
|
||||
return { success: true, message: result };
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Operação realizada com sucesso.",
|
||||
data: result,
|
||||
};
|
||||
} catch (error) {
|
||||
return handleActionError(error);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user