add ExcelImport

This commit is contained in:
arc-alex
2024-01-06 18:21:22 +01:00
parent 341d554bc5
commit 08a32a7265
6 changed files with 215 additions and 148 deletions
@@ -554,10 +554,10 @@ export const postWorksheet = async (req, res) => {
* @description STEP-5 POST download undown to sheet
* @returns parsed result
*/
export async function previewSheet(req, res) {
export async function pullSheet(req, res) {
try {
const { id, worksheet } = req.body;
const data = await sheet.pull(id, worksheet);
const { id, options } = req.body;
const data = await sheet.pull(id, options);
res.status(200).send(data);
} catch (error) {
res.status(500).send({ message: error.toString() });
@@ -569,8 +569,8 @@ export async function previewSheet(req, res) {
*/
export async function pushSheet(req, res) {
try {
const { id, worksheet } = req.data;
await sheet.push(id, worksheet);
const { id, options } = req.body;
await sheet.push(id, options);
res.status(200).send();
} catch (error) {
res.status(500).send({ message: error.toString() });
@@ -171,3 +171,13 @@ export const validateWorksheet = [
next();
},
];
export const validateSheetOptions = [
body('id').exists().isString(),
// body('options').exists().isObject(), TODO:
(req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) return res.status(422).json({ errors: errors.array() });
next();
},
];
+4 -4
View File
@@ -23,7 +23,7 @@ import {
postHTTP,
getAuthenticationUrl,
uploadSheetClientFile,
previewSheet,
pullSheet,
pushSheet,
postId,
getAuthentication,
@@ -41,6 +41,7 @@ import {
validateOscSubscription,
validateSheetid,
validateWorksheet,
validateSheetOptions,
} from '../controllers/ontimeController.validate.js';
import { projectSanitiser } from '../controllers/projectController.validate.js';
@@ -106,7 +107,6 @@ router.post('/http', validateHTTP, postHTTP);
// create route between controller and '/ontime/new' endpoint
router.post('/new', projectSanitiser, postNew);
//SETP-1
router.post('/sheet/clientsecrect', uploadFile, uploadSheetClientFile);
router.get('/sheet/clientsecrect', uploadFile, getClientSecrect);
@@ -122,7 +122,7 @@ router.post('/sheet/id', validateSheetid, postId);
router.post('/sheet/worksheet', validateWorksheet, postId);
//STEP-5 download and generate preview
router.post('/sheet-preview', validateWorksheet, previewSheet);
router.post('/sheet/pull', validateSheetOptions, pullSheet);
//STEP-5 upload
router.post('/sheet-push', validateWorksheet, pushSheet);
router.post('/sheet-push', validateSheetOptions, pushSheet);
+11 -9
View File
@@ -13,6 +13,7 @@ import { ensureDirectory } from './fileManagement.js';
import { cellRequenstFromEvent, cellRequenstFromProjectData, getA1Notation } from './sheetUtils.js';
import { parseExcel } from './parser.js';
import { parseProject, parseRundown, parseUserFields } from './parserFunctions.js';
import { ExcelImportMap } from 'ontime-utils';
type ResponseOK = {
data: Partial<DatabaseModel>;
@@ -265,11 +266,11 @@ class Sheet {
/**
* @description SETP 5 - Upload the rundown to sheet
* @param {string} id - id of the sheet https://docs.google.com/spreadsheets/d/[[spreadsheetId]]/edit#gid=0
* @param {string} worksheet - the name of the worksheet containing ontime data
* @param {ExcelImportMap} options
* @throws
*/
public async push(id: string, worksheet: string) {
const { worksheetId, range } = await this.exist(id, worksheet);
public async push(id: string, options: ExcelImportMap) {
const { worksheetId, range } = await this.exist(id, options.worksheet);
const rq = await sheets({ version: 'v4', auth: Sheet.client }).spreadsheets.values.get({
spreadsheetId: id,
@@ -278,7 +279,7 @@ class Sheet {
range: range,
});
if (rq.status === 200) {
const { rundownMetadata, projectMetadata } = parseExcel(rq.data.values);
const { rundownMetadata, projectMetadata } = parseExcel(rq.data.values, options);
const rundown = DataProvider.getRundown();
const projectData = DataProvider.getProjectData();
const titleRow = Object.values(rundownMetadata)[0]['row'];
@@ -344,12 +345,12 @@ class Sheet {
/**
* @description SETP 5 - Downpload the rundown from sheet
* @param {string} id - id of the sheet https://docs.google.com/spreadsheets/d/[[spreadsheetId]]/edit#gid=0
* @param {string} worksheet - the name of the worksheet containing ontime data
* @param {ExcelImportMap} options
* @returns {Promise<Partial<ResponseOK>>}
* @throws
*/
public async pull(id: string, worksheet: string): Promise<Partial<ResponseOK>> {
const { range } = await this.exist(id, worksheet);
public async pull(id: string, options: ExcelImportMap): Promise<Partial<ResponseOK>> {
const { range } = await this.exist(id, options.worksheet);
const res: Partial<ResponseOK> = {};
@@ -362,12 +363,13 @@ class Sheet {
if (rq.status === 200) {
res.data = {};
const dataFromSheet = parseExcel(rq.data.values);
const dataFromSheet = parseExcel(rq.data.values, options);
res.data.rundown = parseRundown(dataFromSheet);
if (res.data.rundown.length < 1) {
throw new Error(`Sheet: Could not find data to import in the worksheet`);
}
console.log(parseProject(dataFromSheet));
console.log(dataFromSheet.project);
console.log(dataFromSheet.userFields);
res.data.project = parseProject(dataFromSheet);
res.data.userFields = parseUserFields(dataFromSheet);
return res;