"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const express_1 = require("express"); const client_1 = require("@prisma/client"); const router = (0, express_1.Router)(); const prisma = new client_1.PrismaClient(); // Get all price books router.get('/', async (req, res) => { try { const priceBooks = await prisma.priceBook.findMany({ where: { isActive: true }, include: { tables: { orderBy: { tableNumber: 'asc' }, }, }, orderBy: { code: 'asc' }, }); res.json(priceBooks); } catch (error) { res.status(500).json({ error: 'Failed to fetch price books' }); } }); // Get price book by ID router.get('/:id', async (req, res) => { try { const priceBook = await prisma.priceBook.findUnique({ where: { id: req.params.id }, include: { tables: { orderBy: { tableNumber: 'asc' }, include: { items: { orderBy: { paragraph: 'asc' }, }, }, }, }, }); if (!priceBook) { return res.status(404).json({ error: 'Price book not found' }); } res.json(priceBook); } catch (error) { res.status(500).json({ error: 'Failed to fetch price book' }); } }); // Get price table by ID router.get('/tables/:tableId', async (req, res) => { try { const table = await prisma.priceTable.findUnique({ where: { id: req.params.tableId }, include: { items: { orderBy: { paragraph: 'asc' } }, priceBook: true, }, }); if (!table) { return res.status(404).json({ error: 'Price table not found' }); } res.json(table); } catch (error) { res.status(500).json({ error: 'Failed to fetch price table' }); } }); // Маппинг направления изысканий на код справочника СБЦ const DIRECTION_TO_PRICE_BOOK_CODE = { geodesy: 'SBC-GEODESY-2004', geology: 'SBC-GEOLOGY-1999', ecology: 'SBC-GEOLOGY-1999', hydrology: 'SBC-HYDROLOGY-2001', }; // Search price items (поддержка directionCode для выбора наименований из СБЦ) router.get('/items/search', async (req, res) => { try { const { query, priceBookId, directionCode, tableNumber, limit = 50 } = req.query; const where = {}; if (query) { where.OR = [ { workType: { contains: String(query), mode: 'insensitive' } }, { paragraph: { contains: String(query), mode: 'insensitive' } }, { description: { contains: String(query), mode: 'insensitive' } }, ]; } if (priceBookId) { where.priceBookId = String(priceBookId); } else if (directionCode) { const code = DIRECTION_TO_PRICE_BOOK_CODE[String(directionCode)]; if (code) { const book = await prisma.priceBook.findFirst({ where: { code } }); if (book) where.priceBookId = book.id; } } if (tableNumber) { where.priceTable = { tableNumber: Number(tableNumber) }; } const items = await prisma.priceItem.findMany({ where, include: { priceBook: { select: { code: true, name: true } }, priceTable: { select: { tableNumber: true, name: true, unit: true } }, }, take: Number(limit), orderBy: { paragraph: 'asc' }, }); res.json(items); } catch (error) { res.status(500).json({ error: 'Failed to search price items' }); } }); // Get price item by ID router.get('/items/:itemId', async (req, res) => { try { const item = await prisma.priceItem.findUnique({ where: { id: req.params.itemId }, include: { priceBook: true, priceTable: true, }, }); if (!item) { return res.status(404).json({ error: 'Price item not found' }); } res.json(item); } catch (error) { res.status(500).json({ error: 'Failed to fetch price item' }); } }); // Get all coefficients router.get('/coefficients/all', async (req, res) => { try { const { type } = req.query; const where = { isActive: true }; if (type) { where.type = String(type); } const coefficients = await prisma.coefficient.findMany({ where, orderBy: [{ type: 'asc' }, { code: 'asc' }], }); res.json(coefficients); } catch (error) { res.status(500).json({ error: 'Failed to fetch coefficients' }); } }); // Get inflation indices router.get('/inflation-indices', async (req, res) => { try { const indices = await prisma.inflationIndex.findMany({ where: { isActive: true }, orderBy: { effectiveFrom: 'desc' }, }); res.json(indices); } catch (error) { res.status(500).json({ error: 'Failed to fetch inflation indices' }); } }); // Get survey directions router.get('/directions/all', async (req, res) => { try { const directions = await prisma.surveyDirection.findMany({ where: { isActive: true }, orderBy: { name: 'asc' }, }); res.json(directions); } catch (error) { res.status(500).json({ error: 'Failed to fetch survey directions' }); } }); exports.default = router; //# sourceMappingURL=priceBooks.js.map