Spaces:
Sleeping
Sleeping
Download src/controllers/admin-inquiry.controller.ts from underrate/webdev-service-backend: direct link, hf CLI and curl.
- Browser
- Download file 2.54 kB
-
https://huggingface.co/spaces/underrate/webdev-service-backend/resolve/main/src/controllers/admin-inquiry.controller.ts
- Command line
-
hf download hf://spaces/underrate/webdev-service-backend/src/controllers/admin-inquiry.controller.ts
-
curl -L -o admin-inquiry.controller.ts https://huggingface.co/spaces/underrate/webdev-service-backend/resolve/main/src/controllers/admin-inquiry.controller.ts
2.54 kB
| import { Request, Response } from 'express'; | |
| import prisma from '../models/prisma'; | |
| // GET /api/admin/inquiries | |
| export const getInquiries = async (req: Request, res: Response) => { | |
| try { | |
| const inquiries = await prisma.inquiry.findMany({ | |
| orderBy: { createdAt: 'desc' }, | |
| }); | |
| return res.status(200).json(inquiries); | |
| } catch (error) { | |
| console.error('Error fetching inquiries:', error); | |
| return res.status(500).json({ error: 'Internal server error' }); | |
| } | |
| }; | |
| // GET /api/admin/inquiries/:id | |
| export const getInquiryById = async (req: Request, res: Response) => { | |
| try { | |
| const { id } = req.params; | |
| const inquiry = await prisma.inquiry.findUnique({ | |
| where: { id }, | |
| }); | |
| if (!inquiry) { | |
| return res.status(404).json({ error: 'Inquiry not found' }); | |
| } | |
| return res.status(200).json(inquiry); | |
| } catch (error) { | |
| console.error('Error fetching inquiry:', error); | |
| return res.status(500).json({ error: 'Internal server error' }); | |
| } | |
| }; | |
| // PATCH /api/admin/inquiries/:id | |
| export const updateInquiry = async (req: Request, res: Response) => { | |
| try { | |
| const { id } = req.params; | |
| const { status, notes } = req.body; | |
| const currentInquiry = await prisma.inquiry.findUnique({ where: { id } }); | |
| if (!currentInquiry) { | |
| return res.status(404).json({ error: 'Inquiry not found' }); | |
| } | |
| const updateData: any = {}; | |
| if (status !== undefined) { | |
| // Validate status | |
| const validStatuses = ['NEW', 'IN_PROGRESS', 'QUOTED', 'WON', 'LOST']; | |
| if (!validStatuses.includes(status)) { | |
| return res.status(400).json({ error: 'Invalid status value' }); | |
| } | |
| updateData.status = status; | |
| } | |
| if (notes !== undefined) { | |
| // Strip script tags basic sanitization | |
| const sanitize = (str: string) => str.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, ''); | |
| updateData.notes = sanitize(notes); | |
| } | |
| const updatedInquiry = await prisma.inquiry.update({ | |
| where: { id }, | |
| data: updateData, | |
| }); | |
| return res.status(200).json(updatedInquiry); | |
| } catch (error) { | |
| console.error('Error updating inquiry:', error); | |
| return res.status(500).json({ error: 'Internal server error' }); | |
| } | |
| }; | |