/
vinderman
/
presentation
Обзор
Документация
Войти
/
vinderman
/
presentation
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
node/backend-app/src/controllers/events/index.ts
1 строка
3 KB
kalev
initial commit
06 май 2024, 12:05
06 май 2024, 12:05
4258fa9
Код
Авторство
О чём код?
import { Router } from 'express' import DB, { DBType } from '../db.js' import { mergeApiResponse } from '../apiResponse.js' import { produce } from 'immer' import { CreateMeetingRequest, CreateMeetingTemplateRequest, Meeting, MeetingDocument, MeetingQuestion, MeetingQuestionTypeEnum, MeetingStatusEnum, MeetingTypeEnum, UpdateMeetingRequest, VotingTypeEnum } from '../api/data-contracts.js' import { mapMeetingTemplateToMeetingTemplateDTO, mapMeetingToMeetingDTO } from './mappers/meetings.js' import { v4 } from 'uuid' import multer from 'multer' import fs from 'fs' import path from 'path' import Zip from 'adm-zip' function getVotingType(data: CreateMeetingRequest): VotingTypeEnum { if (data.type === MeetingTypeEnum.IK) { return data.votingType as VotingTypeEnum } if (data.type === MeetingTypeEnum.SD) { return VotingTypeEnum.VOTE } if (data.type === MeetingTypeEnum.OSVIP) { return VotingTypeEnum.PIECE } return VotingTypeEnum.VOTE } const meetings = Router() meetings.get('/', (req, res) => { const db = DB.read() const meetings: Meeting[] = db.meetings.map((meeting) => mapMeetingToMeetingDTO(meeting, db) ) res.send(mergeApiResponse({ data: { items: meetings } })) }) const upload = multer() meetings.post('/:id/documents', upload.single('file'), (req, res) => { const file = req.file if (!file) { res.send({}).status(400) return } // обрабатываем русские символы file.originalname = Buffer.from(file.originalname, 'ascii').toString('utf8') const lastDotIndex = file.originalname.lastIndexOf('.') const extension = file.originalname.substring( lastDotIndex + 1, file.originalname.length ) const fileName = file.originalname.substring(0, lastDotIndex) fs.writeFileSync( path.resolve(`./meeting-documents/${fileName}.${extension}`), file.buffer, { encoding: 'utf-8' } ) const document = { id: v4(), name: file.originalname, size: file.size } as MeetingDocument const db = DB.read() const newDb = produce(db, (draft) => { // @ts-ignore const meeting = draft.meetings.find( (m) => String(m.id) === req.params.id ) as Meeting if (!meeting.documents) { meeting.documents = [] } meeting.documents.push(document) }) DB.write(newDb) res.send(mergeApiResponse({ data: document })) }) meetings.delete('/:id/documents/:documentId', (req, res) => { const { id, documentId } = req.params const db = DB.read() const newDb = produce(db, (draft) => { const meeting = draft.meetings.find((m) => String(m.id) === id) // @ts-ignore const documents = meeting.documents as MeetingDocument[] const document = documents.find( (d: MeetingDocument) => d.id === documentId ) as MeetingDocument const index = documents.findIndex((d) => d.id === documentId) fs.rmSync(path.resolve(`./meeting-documents/${document.name}`)) documents.splice(index, 1) }) DB.write(newDb) res.send().status(200) }) meetings.get('/:id/documents/download', (req, res) => { const zip = new Zip() zip.addLocalFolder('./meeting-documents') zip.writeZip('./meeting-documents/documents.zip') res.download('./meeting-documents/documents.zip') }) meetings.get('/:id/documents/:documentId/download', (req, res) => { res.download(path.resolve('./meeting-documents/file.txt'), 'file.txt') }) export default meetings