замечания 3

This commit is contained in:
2025-11-02 12:40:42 +03:00
parent 35493a09b5
commit 0d1dcf21c1
29 changed files with 1498 additions and 1827 deletions

View File

@@ -2,6 +2,7 @@ const express = require('express')
const fs = require('fs')
const path = require('path')
const router = express.Router()
const BuyDocument = require('../models/BuyDocument')
// Create remote-assets/docs directory if it doesn't exist
const docsDir = path.join(__dirname, '../../remote-assets/docs')
@@ -9,155 +10,189 @@ if (!fs.existsSync(docsDir)) {
fs.mkdirSync(docsDir, { recursive: true })
}
// In-memory store for documents metadata
const buyDocs = []
function generateId() {
return `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`
}
// GET /buy/docs?ownerCompanyId=...
router.get('/docs', (req, res) => {
const { ownerCompanyId } = req.query
console.log('[BUY API] GET /docs', { ownerCompanyId, totalDocs: buyDocs.length })
let result = buyDocs
if (ownerCompanyId) {
result = result.filter((d) => d.ownerCompanyId === ownerCompanyId)
router.get('/docs', async (req, res) => {
try {
const { ownerCompanyId } = req.query
console.log('[BUY API] GET /docs', { ownerCompanyId })
let query = {}
if (ownerCompanyId) {
query.ownerCompanyId = ownerCompanyId
}
const docs = await BuyDocument.find(query).sort({ createdAt: -1 })
const result = docs.map(doc => ({
...doc.toObject(),
url: `/api/buy/docs/${doc.id}/file`
}))
res.json(result)
} catch (error) {
console.error('[BUY API] Error fetching docs:', error)
res.status(500).json({ error: 'Failed to fetch documents' })
}
result = result.map(doc => ({
...doc,
url: `/api/buy/docs/${doc.id}/file`
}))
res.json(result)
})
// POST /buy/docs
router.post('/docs', (req, res) => {
const { ownerCompanyId, name, type, fileData } = req.body || {}
console.log('[BUY API] POST /docs', { ownerCompanyId, name, type })
if (!ownerCompanyId || !name || !type) {
return res.status(400).json({ error: 'ownerCompanyId, name and type are required' })
}
if (!fileData) {
return res.status(400).json({ error: 'fileData is required' })
}
const id = generateId()
// Save file to disk
router.post('/docs', async (req, res) => {
try {
const { ownerCompanyId, name, type, fileData } = req.body || {}
console.log('[BUY API] POST /docs', { ownerCompanyId, name, type })
if (!ownerCompanyId || !name || !type) {
return res.status(400).json({ error: 'ownerCompanyId, name and type are required' })
}
if (!fileData) {
return res.status(400).json({ error: 'fileData is required' })
}
const id = generateId()
// Save file to disk
const binaryData = Buffer.from(fileData, 'base64')
const filePath = path.join(docsDir, `${id}.${type}`)
fs.writeFileSync(filePath, binaryData)
console.log(`[BUY API] File saved to ${filePath}, size: ${binaryData.length} bytes`)
const size = binaryData.length
const url = `/api/buy/docs/${id}/file`
const doc = {
const doc = await BuyDocument.create({
id,
ownerCompanyId,
name,
type,
size,
url,
filePath,
acceptedBy: [],
createdAt: new Date().toISOString(),
}
buyDocs.unshift(doc)
acceptedBy: []
})
console.log('[BUY API] Document created:', id)
res.status(201).json(doc)
res.status(201).json({
...doc.toObject(),
url: `/api/buy/docs/${doc.id}/file`
})
} catch (e) {
console.error(`[BUY API] Error saving file: ${e.message}`)
res.status(500).json({ error: 'Failed to save file' })
}
})
router.post('/docs/:id/accept', (req, res) => {
const { id } = req.params
const { companyId } = req.body || {}
console.log('[BUY API] POST /docs/:id/accept', { id, companyId })
const doc = buyDocs.find((d) => d.id === id)
if (!doc) {
console.log('[BUY API] Document not found:', id)
return res.status(404).json({ error: 'Document not found' })
router.post('/docs/:id/accept', async (req, res) => {
try {
const { id } = req.params
const { companyId } = req.body || {}
console.log('[BUY API] POST /docs/:id/accept', { id, companyId })
if (!companyId) {
return res.status(400).json({ error: 'companyId is required' })
}
const doc = await BuyDocument.findOne({ id })
if (!doc) {
console.log('[BUY API] Document not found:', id)
return res.status(404).json({ error: 'Document not found' })
}
if (!doc.acceptedBy.includes(companyId)) {
doc.acceptedBy.push(companyId)
await doc.save()
}
res.json({ id: doc.id, acceptedBy: doc.acceptedBy })
} catch (error) {
console.error('[BUY API] Error accepting document:', error)
res.status(500).json({ error: 'Failed to accept document' })
}
if (!companyId) {
return res.status(400).json({ error: 'companyId is required' })
}
if (!doc.acceptedBy.includes(companyId)) {
doc.acceptedBy.push(companyId)
}
res.json({ id: doc.id, acceptedBy: doc.acceptedBy })
})
router.get('/docs/:id/delete', (req, res) => {
const { id } = req.params
console.log('[BUY API] GET /docs/:id/delete', { id, totalDocs: buyDocs.length })
const index = buyDocs.findIndex((d) => d.id === id)
if (index === -1) {
console.log('[BUY API] Document not found for deletion:', id)
return res.status(404).json({ error: 'Document not found' })
}
const deletedDoc = buyDocs.splice(index, 1)[0]
// Delete file from disk
if (deletedDoc.filePath && fs.existsSync(deletedDoc.filePath)) {
try {
fs.unlinkSync(deletedDoc.filePath)
console.log(`[BUY API] File deleted: ${deletedDoc.filePath}`)
} catch (e) {
console.error(`[BUY API] Error deleting file: ${e.message}`)
router.get('/docs/:id/delete', async (req, res) => {
try {
const { id } = req.params
console.log('[BUY API] GET /docs/:id/delete', { id })
const doc = await BuyDocument.findOne({ id })
if (!doc) {
console.log('[BUY API] Document not found for deletion:', id)
return res.status(404).json({ error: 'Document not found' })
}
// Delete file from disk
if (doc.filePath && fs.existsSync(doc.filePath)) {
try {
fs.unlinkSync(doc.filePath)
console.log(`[BUY API] File deleted: ${doc.filePath}`)
} catch (e) {
console.error(`[BUY API] Error deleting file: ${e.message}`)
}
}
await BuyDocument.deleteOne({ id })
console.log('[BUY API] Document deleted via GET:', id)
res.json({ id: doc.id, success: true })
} catch (error) {
console.error('[BUY API] Error deleting document:', error)
res.status(500).json({ error: 'Failed to delete document' })
}
console.log('[BUY API] Document deleted via GET:', id, { remainingDocs: buyDocs.length })
res.json({ id: deletedDoc.id, success: true })
})
router.delete('/docs/:id', (req, res) => {
const { id } = req.params
console.log('[BUY API] DELETE /docs/:id', { id, totalDocs: buyDocs.length })
const index = buyDocs.findIndex((d) => d.id === id)
if (index === -1) {
console.log('[BUY API] Document not found for deletion:', id)
return res.status(404).json({ error: 'Document not found' })
}
const deletedDoc = buyDocs.splice(index, 1)[0]
// Delete file from disk
if (deletedDoc.filePath && fs.existsSync(deletedDoc.filePath)) {
try {
fs.unlinkSync(deletedDoc.filePath)
console.log(`[BUY API] File deleted: ${deletedDoc.filePath}`)
} catch (e) {
console.error(`[BUY API] Error deleting file: ${e.message}`)
router.delete('/docs/:id', async (req, res) => {
try {
const { id } = req.params
console.log('[BUY API] DELETE /docs/:id', { id })
const doc = await BuyDocument.findOne({ id })
if (!doc) {
console.log('[BUY API] Document not found for deletion:', id)
return res.status(404).json({ error: 'Document not found' })
}
// Delete file from disk
if (doc.filePath && fs.existsSync(doc.filePath)) {
try {
fs.unlinkSync(doc.filePath)
console.log(`[BUY API] File deleted: ${doc.filePath}`)
} catch (e) {
console.error(`[BUY API] Error deleting file: ${e.message}`)
}
}
await BuyDocument.deleteOne({ id })
console.log('[BUY API] Document deleted:', id)
res.json({ id: doc.id, success: true })
} catch (error) {
console.error('[BUY API] Error deleting document:', error)
res.status(500).json({ error: 'Failed to delete document' })
}
console.log('[BUY API] Document deleted:', id, { remainingDocs: buyDocs.length })
res.json({ id: deletedDoc.id, success: true })
})
// GET /buy/docs/:id/file - Serve the file
router.get('/docs/:id/file', (req, res) => {
const { id } = req.params
console.log('[BUY API] GET /docs/:id/file', { id })
const doc = buyDocs.find(d => d.id === id)
if (!doc) {
console.log('[BUY API] Document not found:', id)
return res.status(404).json({ error: 'Document not found' })
}
const filePath = path.join(docsDir, `${id}.${doc.type}`)
if (!fs.existsSync(filePath)) {
console.log('[BUY API] File not found on disk:', filePath)
return res.status(404).json({ error: 'File not found on disk' })
}
router.get('/docs/:id/file', async (req, res) => {
try {
const { id } = req.params
console.log('[BUY API] GET /docs/:id/file', { id })
const doc = await BuyDocument.findOne({ id })
if (!doc) {
console.log('[BUY API] Document not found:', id)
return res.status(404).json({ error: 'Document not found' })
}
const filePath = path.join(docsDir, `${id}.${doc.type}`)
if (!fs.existsSync(filePath)) {
console.log('[BUY API] File not found on disk:', filePath)
return res.status(404).json({ error: 'File not found on disk' })
}
const fileBuffer = fs.readFileSync(filePath)
const mimeTypes = {
@@ -170,7 +205,6 @@ router.get('/docs/:id/file', (req, res) => {
const sanitizedName = doc.name.replace(/[^\w\s\-\.]/g, '_')
res.setHeader('Content-Type', mimeType)
// RFC 5987 encoding: filename for ASCII fallback, filename* for UTF-8 with percent-encoding
const encodedFilename = encodeURIComponent(`${doc.name}.${doc.type}`)
res.setHeader('Content-Disposition', `attachment; filename="${sanitizedName}.${doc.type}"; filename*=UTF-8''${encodedFilename}`)
res.setHeader('Content-Length', fileBuffer.length)