refactor: get worksheet names from excel upload

This commit is contained in:
Carlos Valente
2025-11-02 11:49:06 +01:00
committed by Carlos Valente
parent 162693e19e
commit b65b3fa6d2
4 changed files with 22 additions and 40 deletions
+18 -21
View File
@@ -2,35 +2,32 @@ import express from 'express';
import type { Request, Response } from 'express';
import { CustomFields, ErrorResponse, Rundown } from 'ontime-types';
import { getDataProvider } from '../../classes/data-provider/DataProvider.js';
import { getProjectCustomFields } from '../rundown/rundown.dao.js';
import { uploadExcel } from './excel.middleware.js';
import { validateFileExists, validateImportMapOptions, validateRundownExport } from './excel.validation.js';
import { generateExcelFile, generateRundownPreview, listWorksheets, readExcelFile } from './excel.service.js';
import { generateExcelFile, generateRundownPreview, readExcelFile } from './excel.service.js';
import { EXCEL_MIME } from './excel.constants.js';
import { getDataProvider } from '../../classes/data-provider/DataProvider.js';
export const router = express.Router();
router.post('/upload', uploadExcel, validateFileExists, async (req: Request, res: Response<never | ErrorResponse>) => {
try {
// file has been validated by middleware
const filePath = (req.file as Express.Multer.File).path;
await readExcelFile(filePath);
res.status(201).send();
} catch (error) {
res.status(500).send({ message: String(error) });
}
});
router.get('/worksheets', (_req: Request, res: Response<string[] | ErrorResponse>) => {
try {
const names = listWorksheets();
res.status(200).send(names);
} catch (error) {
res.status(500).send({ message: String(error) });
}
});
router.post(
'/upload',
uploadExcel,
validateFileExists,
async (req: Request, res: Response<string[] | ErrorResponse>) => {
try {
// file has been validated by middleware
const filePath = (req.file as Express.Multer.File).path;
const worksheetNames = await readExcelFile(filePath);
res.status(200).send(worksheetNames);
} catch (error) {
res.status(500).send({ message: String(error) });
}
},
);
router.post(
'/preview',
@@ -27,7 +27,7 @@ let excelData: WorkBook = xlsx.utils.book_new();
* Receives and parses an excel file
* The file is deleted after being read
*/
export async function readExcelFile(filePath: string) {
export async function readExcelFile(filePath: string): Promise<string[]> {
if (!existsSync(filePath)) {
throw new Error('Upload of excel file failed');
}
@@ -39,12 +39,7 @@ export async function readExcelFile(filePath: string) {
excelData = xlsx.readFile(filePath, { cellDates: true, cellFormula: false });
await deleteFile(filePath);
}
/**
* List all worksheets in the current spreadsheet file
*/
export function listWorksheets(): string[] {
return excelData.SheetNames;
}