import { BetterAuthClientPlugin, BetterAuthPlugin } from "better-auth"; import { createAuthEndpoint, sessionMiddleware } from "better-auth/api"; import { z } from "zod"; export const project = () => ({ id: "project", schema: { project: { fields: { name: { type: "string", required: true }, description: { type: "string" }, createdAt: { type: "date", required: false }, updatedAt: { type: "date", required: false }, organizationId: { type: "string", required: true, references: { model: "organization", field: "id", onDelete: "cascade", }, }, }, }, }, endpoints: { createProject: createAuthEndpoint( "/project/create", { method: "POST", use: [sessionMiddleware], body: z.object({ name: z.string().min(1, "Nome é obrigatório"), description: z.string().optional(), organizationId: z .string() .min(1, "ID da organização é obrigatório"), }), }, async (ctx) => { const { context, body } = ctx; if (!body.organizationId) { throw ctx.error(400, { message: "ID da organização é obrigatório", }); } if (!body.name) { throw ctx.error(400, { message: "Nome é obrigatório" }); } const projectData = { ...body, createdAt: new Date(), updatedAt: new Date(), }; const newProject = await context.adapter.create({ model: "project", data: projectData, }); if (!newProject) { throw ctx.error(500, { message: "Erro ao criar projeto" }); } return newProject; } ), allProjects: createAuthEndpoint( "/project/all", { method: "GET", use: [sessionMiddleware], }, async (ctx) => { const { context } = ctx; const projects = await context.adapter.findMany({ model: "project", }); return projects || []; } ), allOrganizationProjects: createAuthEndpoint( "/project/all-organization-projects", { method: "GET", use: [sessionMiddleware], query: z.object({ organizationId: z .string() .min(1, "ID da organização é obrigatório"), }), }, async (ctx) => { const { context, query } = ctx; const projects = await context.adapter.findMany({ model: "project", where: [ { field: "organizationId", operator: "eq", value: query.organizationId, }, ], }); return projects; } ), }, } satisfies BetterAuthPlugin); export const projectClientPlugin = () => { return { id: "projectPlugin", $InferServerPlugin: {} as ReturnType, getActions: ($fetch) => ({ // createProject: async ( // data: { // name: string; // description?: string; // organizationId: string; // }, // fetchOptions?: BetterFetchOption // ) => { // const res = $fetch("/project/create", { // method: "POST", // body: data, // ...fetchOptions, // }); // return res; // }, useAllProjects: async () => await $fetch("/project/all"), useAllOrganizationProjects: async (organizationId: string) => { return await $fetch("/project/all-organization-projects", { query: { organizationId }, }); }, // Hooks reativos com atom stores (para usar sem await) // useAllProjects: () => // $fetch.adaptStore("/project/all").useFetch(), // useOrganizationProjects: (organizationId?: string) => // $fetch // .adaptStore("/project/all-organization-projects", { // query: { organizationId }, // }) // .useFetch(), }), } satisfies BetterAuthClientPlugin; };