| import { Request, Response } from 'express';
|
| import prisma from '../models/prisma';
|
|
|
|
|
| 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' });
|
| }
|
| };
|
|
|
|
|
| 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' });
|
| }
|
| };
|
|
|
|
|
| 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) {
|
|
|
| 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) {
|
|
|
| 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' });
|
| }
|
| };
|
|
|