import { prisma } from '../prisma.js' export const getOffers = async(req, res) => { const offers = await prisma.oferta.findMany() res.json(offers) } export const getOffer = async(req, res) => { const { offerId } = req.params const id = parseInt(offerId) const offer = await prisma.oferta.findFirst({ where: { id } }) res.json(offer); } export const createOffer = async (req, res) => { const { titulo, empresa, ubicacion, salario, contrato, descripcion } = req.body; const authorId = req.user.id const salarioRegex = /\d/ if (!titulo || !empresa || !ubicacion || !salario || !contrato || !descripcion) return res.status(400).json({ error: ""}) if (titulo.length < 3 || titulo.length > 64) { return res.status(400).json({ error: 'El titulo debe tener entre 3 y 64 caracteres' })} if (empresa.length < 2 || empresa.length > 32) { return res.status(400).json({ error: 'El nombre de la empresa debe tener entre 2 y 32 caracteres' })} if (ubicacion.length < 3 || ubicacion.length > 16) { return res.status(400).json({ error: 'La ubicacion debe tener entre 3 y 16 caracteres' })} if (salario.length < 3 || salario.length > 10) { return res.status(400).json({ error: 'El salario debe seguir este formato: 500€ o 20€/hora' })} if (!salarioRegex.test(salario)) return res.status(400).json({ error: "El salario debe contener un numero"}) if (descripcion.length < 10 || descripcion.length > 256) { return res.status(400).json({ error: 'La descripcion debe tener entre 10 y 256 caracteres' })} const contratosValidos = ["freelance", "completo", "medio"] if (!contratosValidos.includes(contrato)) return res.status(400).json({ error: "contrato no valido" }) try { const offer = await prisma.oferta.create({ data: { titulo, empresa, ubicacion, salario, descripcion, contrato, authorId } }) res.status(201).json(offer) } catch (err) { res.status(400).json({ error: "Ha ocurrido un error al crear la oferta de trabajo." }) } } export const deleteOffer = async (req, res) => { const { offerId } = req.params; const authorId = req.user.id; const id = parseInt(offerId) try { const offer = await prisma.offer.findFirst({ where: { id }}) if (!offer) return res.status(404).json({ error: "Invalid project" }) if (authorId !== offer.authorId) return res.status(403).json({ error: "Missing permissions" }) await prisma.offer.delete({ where: { id } }) res.status(200).json({ message: "Project has been deleted" }) } catch (err) { res.status(400).json({ error: "Ha ocurrido un error al borrar el proyecto" }) } }