File size: 2,542 Bytes
d53e5b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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' });
    }
};