UNPKG

skailan-ai

Version:

Servicio de IA y procesamiento de lenguaje natural para Skailan

162 lines 5.81 kB
import { CreatePrompt } from "../../app/use-cases/CreatePrompt"; import { GetPromptById } from "../../app/use-cases/GetPromptById"; import { ListPrompts } from "../../app/use-cases/ListPrompts"; import { UpdatePrompt } from "../../app/use-cases/UpdatePrompt"; import { DeletePrompt } from "../../app/use-cases/DeletePrompt"; import { PromptPrismaRepository } from "../../infra/database/prisma/PromptPrismaRepository"; export const createPrompt = async (req, res) => { try { const { name, description, template, type } = req.body; const organizationId = req.organization?.id; if (!organizationId) { return res .status(400) .json({ error: "Organization ID not found in request." }); } const prisma = req.tenantPrisma; if (!prisma) { return res .status(500) .json({ error: "Prisma client not initialized for tenant." }); } const promptRepository = new PromptPrismaRepository(prisma); const createPromptUseCase = new CreatePrompt(promptRepository); const newPrompt = await createPromptUseCase.execute({ organizationId, name, description, template, type, }); res.status(201).json(newPrompt); } catch (error) { res.status(500).json({ error: error.message || "Error creating prompt." }); } }; export const getPromptById = async (req, res) => { try { const { id } = req.params; const organizationId = req.organization?.id; if (!organizationId) { return res .status(400) .json({ error: "Organization ID not found in request." }); } if (!id) { return res.status(400).json({ error: "Prompt ID not found in request." }); } const prisma = req.tenantPrisma; if (!prisma) { return res .status(500) .json({ error: "Prisma client not initialized for tenant." }); } const promptRepository = new PromptPrismaRepository(prisma); const getPromptByIdUseCase = new GetPromptById(promptRepository); const prompt = await getPromptByIdUseCase.execute({ id, organizationId, }); if (!prompt) { return res.status(404).json({ error: "Prompt not found." }); } res.status(200).json(prompt); } catch (error) { res .status(500) .json({ error: error.message || "Error retrieving prompt." }); } }; export const listPrompts = async (req, res) => { try { const organizationId = req.organization?.id; if (!organizationId) { return res .status(400) .json({ error: "Organization ID not found in request." }); } const prisma = req.tenantPrisma; if (!prisma) { return res .status(500) .json({ error: "Prisma client not initialized for tenant." }); } const promptRepository = new PromptPrismaRepository(prisma); const listPromptsUseCase = new ListPrompts(promptRepository); const prompts = await listPromptsUseCase.execute({ organizationId, }); res.status(200).json(prompts); } catch (error) { res.status(500).json({ error: error.message || "Error listing prompts." }); } }; export const updatePrompt = async (req, res) => { try { const { id } = req.params; const { name, description, template, type } = req.body; const organizationId = req.organization?.id; if (!organizationId) { return res .status(400) .json({ error: "Organization ID not found in request." }); } if (!id) { return res.status(400).json({ error: "Prompt ID not found in request." }); } const prisma = req.tenantPrisma; if (!prisma) { return res .status(500) .json({ error: "Prisma client not initialized for tenant." }); } const promptRepository = new PromptPrismaRepository(prisma); const updatePromptUseCase = new UpdatePrompt(promptRepository); const updatedPrompt = await updatePromptUseCase.execute({ id, organizationId, name, description, template, type, }); res.status(200).json(updatedPrompt); } catch (error) { res.status(500).json({ error: error.message || "Error updating prompt." }); } }; export const deletePrompt = async (req, res) => { try { const { id } = req.params; const organizationId = req.organization?.id; if (!organizationId) { return res .status(400) .json({ error: "Organization ID not found in request." }); } if (!id) { return res.status(400).json({ error: "Prompt ID not found in request." }); } const prisma = req.tenantPrisma; if (!prisma) { return res .status(500) .json({ error: "Prisma client not initialized for tenant." }); } const promptRepository = new PromptPrismaRepository(prisma); const deletePromptUseCase = new DeletePrompt(promptRepository); await deletePromptUseCase.execute({ id, organizationId, }); res.status(204).send(); } catch (error) { res.status(500).json({ error: error.message || "Error deleting prompt." }); } }; //# sourceMappingURL=promptController.js.map