sheet settings with feedback

This commit is contained in:
arc-alex
2023-11-23 17:17:13 +01:00
parent b15b4b48a7
commit 4f1fa2053f
8 changed files with 199 additions and 38 deletions
+55 -17
View File
@@ -394,6 +394,29 @@ export async function previewExcel(req, res) {
}
}
/**
* Meant to create a new project file, it will clear only fields which are specific to a project
* @param req
* @param res
*/
export const postNew: RequestHandler = async (req, res) => {
try {
const newProjectData: ProjectData = {
title: req.body?.title ?? '',
description: req.body?.description ?? '',
publicUrl: req.body?.publicUrl ?? '',
publicInfo: req.body?.publicInfo ?? '',
backstageUrl: req.body?.backstageUrl ?? '',
backstageInfo: req.body?.backstageInfo ?? '',
};
const newData = await DataProvider.setProjectData(newProjectData);
await deleteAllEvents();
res.status(201).send(newData);
} catch (error) {
res.status(400).send({ message: error.toString() });
}
};
/**
* downloads and parses an sheet
* @returns parsed result
@@ -424,7 +447,7 @@ export async function pushSheet(req, res) {
* uploads Client secrets file
* @returns parsed result
*/
export async function sheetClientFile(req, res) {
export async function uploadGoogleSheetClientFile(req, res) {
if (!req.file.path) {
res.status(400).send({ message: 'File not found' });
return;
@@ -432,8 +455,8 @@ export async function sheetClientFile(req, res) {
try {
const client = JSON.parse(fs.readFileSync(req.file.path as string, 'utf-8'));
await Sheet.saveClientSecrets(client);
res.status(200).send('OK');
const auth = await Sheet.saveClientSecrets(client);
res.status(200).send(auth);
} catch (error) {
res.status(500).send({ message: error.toString() });
}
@@ -463,24 +486,39 @@ export async function sheetAuthUrl(req, res) {
}
/**
* Meant to create a new project file, it will clear only fields which are specific to a project
* @param req
* @param res
* @description Get google sheet Settings
* @method GET
*/
export const postNew: RequestHandler = async (req, res) => {
export const getGoogleSheetSettings = async (req, res) => {
const sheet = DataProvider.getGoogleSheet();
res.status(200).send(sheet);
};
/**
* @description Change view Settings
* @method POST
*/
export const postGoogleSheetSettings = async (req, res) => {
if (failEmptyObjects(req.body, res)) {
return;
}
try {
const newProjectData: ProjectData = {
title: req.body?.title ?? '',
description: req.body?.description ?? '',
publicUrl: req.body?.publicUrl ?? '',
publicInfo: req.body?.publicInfo ?? '',
backstageUrl: req.body?.backstageUrl ?? '',
backstageInfo: req.body?.backstageInfo ?? '',
const newData = {
id: req.body.id,
worksheet: req.body.worksheet,
};
const newData = await DataProvider.setProjectData(newProjectData);
await deleteAllEvents();
res.status(201).send(newData);
await DataProvider.setGoogleSheet(newData);
res.status(200).send(newData);
} catch (error) {
res.status(400).send({ message: error.toString() });
}
};
/**
* @description Get google sheet state
* @method GET
*/
export const getGoogleSheetState = async (req, res) => {
res.status(200).send(await Sheet.getSheetState());
};
@@ -145,3 +145,13 @@ export const validateSheetPreview = [
next();
},
];
export const validateGoogleSheetSettings = [
body('id').isString().optional({ nullable: false }),
body('worksheet').isString().optional({ nullable: false }),
(req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) return res.status(422).json({ errors: errors.array() });
next();
},
];
+16 -3
View File
@@ -21,13 +21,17 @@ import {
previewExcel,
sheetAuthUrl,
sheetAuthState,
sheetClientFile,
uploadGoogleSheetClientFile,
previewSheet,
pushSheet,
getGoogleSheetSettings,
postGoogleSheetSettings,
getGoogleSheetState,
} from '../controllers/ontimeController.js';
import {
validateAliases,
validateGoogleSheetSettings,
validateOSC,
validateOscSubscription,
validatePatchProjectFile,
@@ -95,7 +99,7 @@ router.post('/osc-subscriptions', validateOscSubscription, postOscSubscriptions)
router.post('/new', projectSanitiser, postNew);
// create route between controller and '/ontime/sheet-client' endpoint
router.post('/sheet-clientsecrect', uploadFile, sheetClientFile);
router.post('/sheet-clientsecrect', uploadFile, uploadGoogleSheetClientFile);
// create route between controller and '/ontime/sheet-authstatus' endpoint
router.get('/sheet-authstatus', sheetAuthState);
@@ -107,4 +111,13 @@ router.get('/sheet-authurl', sheetAuthUrl);
router.post('/sheet-preview', validateSheetPreview, previewSheet);
// create route between controller and '/ontime/preview-sheet' endpoint
router.post('/sheet-push', pushSheet);
router.post('/sheet-push', pushSheet);
// create route between controller and '/ontime/sheet-settings' endpoint
router.get('/sheet-settings', getGoogleSheetSettings);
// create route between controller and '/ontime/sheet-settings' endpoint
router.post('/sheet-settings', validateGoogleSheetSettings, postGoogleSheetSettings);
// create route between controller and '/ontime/sheet-state' endpoint
router.get('/sheet-state', getGoogleSheetState);
+35 -3
View File
@@ -11,6 +11,7 @@ import { parseExcel } from './parser.js';
import { parseProject, parseRundown, parseUserFields } from './parserFunctions.js';
import { ensureDirectory } from './fileManagement.js';
import { DataProvider } from '../classes/data-provider/DataProvider.js';
import { GoogleSheetState } from 'ontime-types';
type ResponseOK = {
data: Partial<DatabaseModel>;
@@ -24,6 +25,27 @@ class sheet {
private readonly token = this.sheetsFolder + '/token.json';
private static authUrl: null | string = null;
public async getSheetState(): Promise<GoogleSheetState> {
const ret: GoogleSheetState = {
auth: false,
id: false,
worksheet: false,
};
ret.auth = await this.authorized();
if (ret.auth) {
const settings = DataProvider.getGoogleSheet();
const x = await this.exist(settings.id, settings.worksheet);
if (x === true) {
ret.id = true;
} else if (x !== false) {
ret.id = true;
ret.worksheet = true;
}
}
logger.info(LogOrigin.Server, `Sheet State: ${ret}`);
return ret;
}
/**
* checks the authorized state
* @returns {Promise<boolean>}
@@ -42,10 +64,13 @@ class sheet {
* test existance of sheet and workssheet and get is index
* @param {string} sheetId - https://docs.google.com/spreadsheets/d/[[spreadsheetId]]/edit#gid=0
* @param {string} worksheet - the name of the worksheet containg ontime data
* @returns {Promise<false | {worksheetId: number, range: string}>} - false if not found | id of worksheet
* @returns {Promise<false | {worksheetId: number, range: string}>} - false if not found | true if sheetId existes | id of worksheet and rage of worksheet
* @throws
*/
public async exist(sheetId: string, worksheet: string): Promise<false | { worksheetId: number; range: string }> {
public async exist(
sheetId: string,
worksheet: string,
): Promise<false | true | { worksheetId: number; range: string }> {
const spreadsheets = await sheets({ version: 'v4', auth: sheet.client }).spreadsheets.get({
spreadsheetId: sheetId,
});
@@ -58,6 +83,8 @@ class sheet {
w.properties.gridProperties.columnCount,
);
return { worksheetId: w.properties.sheetId, range: worksheet + '!A1:' + endCell };
} else {
return true;
}
}
return false;
@@ -73,6 +100,8 @@ class sheet {
const sheetInfo = await this.exist(sheetId, worksheet);
if (!sheetInfo) {
throw new Error(`Sheet not dose not exits`);
} else if (sheetInfo === true) {
throw new Error(`Worksheet not dose not exits`);
}
if (!isExcelImportMap(options)) {
@@ -176,6 +205,8 @@ class sheet {
const sheetInfo = await this.exist(sheetId, worksheet);
if (!sheetInfo) {
throw new Error(`Sheet not dose not exits`);
} else if (sheetInfo === true) {
throw new Error(`Worksheet not dose not exits`);
}
const rq = await sheets({ version: 'v4', auth: sheet.client }).spreadsheets.values.get({
@@ -203,7 +234,7 @@ class sheet {
* saves Object to appdata path as client_secret.json
* @param {} secrets
*/
public async saveClientSecrets(secrets) {
public async saveClientSecrets(secrets): Promise<GoogleSheetState> {
ensureDirectory(this.sheetsFolder);
logger.info(LogOrigin.Server, 'Sheets: got new client_secret');
//TODO: test that this is actualy a client file?
@@ -211,6 +242,7 @@ class sheet {
sheet.client = null;
sheet.authUrl = null;
await writeFile(this.client_secret, JSON.stringify(secrets), 'utf-8');
return await this.getSheetState();
}
/**