Merge remote-tracking branch 'origin/master' into v3

This commit is contained in:
cv
2024-01-12 10:05:10 +01:00
26 changed files with 2130 additions and 193 deletions
@@ -137,7 +137,10 @@ const actionHandlers: Record<string, ActionHandler> = {
}
// Indexes in frontend are 1 based
PlaybackService.startByIndex(eventIndex - 1);
const success = PlaybackService.startByIndex(eventIndex - 1);
if (!success) {
throw new Error(`Event index not recognised or out of range ${eventIndex}`);
}
return { payload: 'success' };
},
startid: (payload) => {
@@ -179,7 +182,6 @@ const actionHandlers: Record<string, ActionHandler> = {
if (eventIndex <= 0) {
throw new Error(`Event index out of range ${eventIndex}`);
}
// Indexes in frontend are 1 based
PlaybackService.loadByIndex(eventIndex - 1);
return { payload: 'success' };
@@ -200,7 +202,6 @@ const actionHandlers: Record<string, ActionHandler> = {
return { payload: 'success' };
},
};
/**
* Returns a value of type number, converting if necessary
* Otherwise throws
@@ -42,6 +42,8 @@ import { deleteFile } from '../utils/parserUtils.js';
import { validateProjectFiles } from './ontimeController.validate.js';
import { dbModel } from '../models/dataModel.js';
import { sheet } from '../utils/sheetsAuth.js';
// Create controller for GET request to '/ontime/poll'
// Returns data for current state
export const poll = async (_req, res) => {
@@ -668,3 +670,122 @@ export const deleteProjectFile: RequestHandler = async (req, res) => {
res.status(500).send({ message: error.toString() });
}
};
// SHEET Functions
/**
* @description SETP-1 POST Client Secrect
* @returns parsed result
*/
export async function uploadSheetClientFile(req, res) {
if (!req.file.path) {
res.status(400).send({ message: 'File not found' });
return;
}
try {
const client = JSON.parse(fs.readFileSync(req.file.path as string, 'utf-8'));
await sheet.saveClientSecrets(client);
res.status(200).send('OK');
} catch (error) {
res.status(500).send({ message: error.toString() });
}
fs.unlink(req.file.path, (err) => {
if (err) logger.error(LogOrigin.Server, err.message);
});
}
/**
* @description STEP-1 GET Client Secrect status
*/
export const getClientSecrect = async (req, res) => {
try {
const clientSecrectExists = await sheet.testClientSecret();
if (clientSecrectExists) {
res.status(200).send();
} else {
res.status(500).send({ message: 'The Client ID does not exist' });
}
} catch (error) {
res.status(500).send({ message: error.toString() });
}
};
/**
* @description STEP-2 GET sheet authentication url
*/
export async function getAuthenticationUrl(req, res) {
try {
const authUrl = await sheet.openAuthServer();
res.status(200).send(authUrl);
} catch (error) {
res.status(500).send({ message: error.toString() });
}
}
/**
* @description STEP-2 GET sheet authentication status
*/
export const getAuthentication = async (req, res) => {
try {
await sheet.testAuthentication();
res.status(200).send();
} catch (error) {
res.status(500).send({ message: error.toString() });
}
};
/**
* @description STEP-3 POST sheet id
* @returns list of worksheets
*/
export const postId = async (req, res) => {
try {
const { id } = req.body;
if (id.lenght < 40) {
res.status(400).send({ message: 'ID is usualy 44 characters long' });
}
const state = await sheet.testSheetId(id);
res.status(200).send(state);
} catch (error) {
res.status(500).send({ message: error.toString() });
}
};
/**
* @description STEP-4 POST worksheet
*/
export const postWorksheet = async (req, res) => {
try {
const { worksheet, id } = req.body;
const state = await sheet.testWorksheet(worksheet, id);
res.status(200).send(state);
} catch (error) {
res.status(500).send({ message: error.toString() });
}
};
/**
* @description STEP-5 POST download undown to sheet
* @returns parsed result
*/
export async function pullSheet(req, res) {
try {
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() });
}
}
/**
* @description STEP-5 POST upload rundown to sheet
*/
export async function pushSheet(req, res) {
try {
const { id, options } = req.body;
await sheet.push(id, options);
res.status(200).send();
} catch (error) {
res.status(500).send({ message: error.toString() });
}
}
@@ -266,3 +266,32 @@ export const validateProjectFiles = (projectFiles: { filename?: string; newFilen
return errors;
};
export const validateSheetid = [
body('id').exists().isString(),
(req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) return res.status(422).json({ errors: errors.array() });
next();
},
];
export const validateWorksheet = [
body('id').exists().isString(),
body('worksheet').exists().isString(),
(req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) return res.status(422).json({ errors: errors.array() });
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();
},
];