refactor: export data for all entry types

This commit is contained in:
Carlos Valente
2025-09-06 07:04:27 +02:00
committed by Alex Christoffer Rasmussen
parent 2037aef080
commit 9e78ff4d1c
@@ -1,4 +1,11 @@
import { isOntimeGroup, isOntimeEvent, OntimeEvent, OntimeEntry, RGBColour } from 'ontime-types'; import {
OntimeEntry,
RGBColour,
isOntimeDelay,
OntimeEntryCommonKeys,
isOntimeGroup,
isOntimeMilestone,
} from 'ontime-types';
import { cssOrHexToColour, isLightColour, millisToString, mixColours } from 'ontime-utils'; import { cssOrHexToColour, isLightColour, millisToString, mixColours } from 'ontime-utils';
import type { sheets_v4 } from '@googleapis/sheets'; import type { sheets_v4 } from '@googleapis/sheets';
@@ -70,21 +77,21 @@ export function getA1Notation(row: number, column: number): string {
/** /**
* @description - creates updateCells request from ontime event * @description - creates updateCells request from ontime event
* @param {OntimeEntry} event * @param {OntimeEntry} entry
* @param {number} index - index of the event * @param {number} index - index of the event
* @param {number} worksheetId * @param {number} worksheetId
* @param {object} metadata - object with all the cell positions of the title of each attribute * @param {object} metadata - object with all the cell positions of the title of each attribute
* @returns {sheets_v4.Schema} - list of update requests * @returns {sheets_v4.Schema} - list of update requests
*/ */
export function cellRequestFromEvent( export function cellRequestFromEvent(
event: OntimeEntry, entry: OntimeEntry,
index: number, index: number,
worksheetId: number, worksheetId: number,
metadata: object, metadata: object,
): sheets_v4.Schema$Request { ): sheets_v4.Schema$Request {
const rowData = Object.entries(metadata) const rowData = Object.entries(metadata)
.filter(([_, value]) => value !== undefined) .filter(([_, value]) => value !== undefined)
.sort(([_a, a], [_b, b]) => a['col'] - b['col']) as [keyof OntimeEvent | 'blank', { col: number; row: number }][]; .sort(([_a, a], [_b, b]) => a['col'] - b['col']) as [keyof OntimeEntry | 'blank', { col: number; row: number }][];
const titleCol = rowData[0][1].col; const titleCol = rowData[0][1].col;
@@ -100,26 +107,26 @@ export function cellRequestFromEvent(
} }
} }
const colors = isOntimeEvent(event) || isOntimeGroup(event) ? getAccessibleColour(event.colour) : undefined; const colours = 'colour' in entry ? getAccessibleColour(entry.colour) : undefined;
const cellColor: sheets_v4.Schema$CellData = !colors const cellColor: sheets_v4.Schema$CellData = !colours
? {} ? {}
: { : {
userEnteredFormat: { userEnteredFormat: {
backgroundColor: toSheetColourLevel(colors.background), backgroundColor: toSheetColourLevel(colours.background),
textFormat: { textFormat: {
foregroundColor: toSheetColourLevel(colors.text), foregroundColor: toSheetColourLevel(colours.text),
}, },
borders: { borders: {
bottom: { bottom: {
style: 'SOLID', style: 'SOLID',
color: toSheetColourLevel(colors.border), color: toSheetColourLevel(colours.border),
}, },
}, },
}, },
}; };
const returnRows: sheets_v4.Schema$CellData[] = rowData.map(([key, _]) => { const returnRows: sheets_v4.Schema$CellData[] = rowData.map(([key, _]) => {
return { ...getCellData(key, event), ...cellColor }; return { ...getCellData(key, entry), ...cellColor };
}); });
return { return {
@@ -139,40 +146,38 @@ export function cellRequestFromEvent(
}; };
} }
function getCellData(key: keyof OntimeEvent | 'blank', event: OntimeEntry) { function getCellData(key: OntimeEntryCommonKeys | 'blank', entry: OntimeEntry) {
if (isOntimeEvent(event)) { if (isOntimeDelay(entry) || key === 'blank') {
if (key === 'blank') { return {};
return {};
}
if (key === 'colour') {
return { userEnteredValue: { stringValue: event[key] } };
}
if (key.startsWith('custom')) {
const customKey = key.split(':')[1];
return { userEnteredValue: { stringValue: event.custom[customKey] } };
}
if (typeof event[key] === 'number') {
return { userEnteredValue: { stringValue: millisToString(event[key]) } };
}
if (typeof event[key] === 'string') {
return { userEnteredValue: { stringValue: event[key] } };
}
if (typeof event[key] === 'boolean') {
return { userEnteredValue: { boolValue: event[key] } };
}
} }
if (isOntimeGroup(event)) { // we need to flatten the milestones
if (key === 'title') { if (key.startsWith('custom')) {
return { userEnteredValue: { stringValue: event[key] } }; const customKey = key.split(':')[1];
} return { userEnteredValue: { stringValue: entry.custom[customKey] } };
if (key === 'timerType') {
return { userEnteredValue: { stringValue: 'group' } };
}
} }
return {}; // we need to remap the event type to timer type in the case of groups and milestones
if (key === 'timerType') {
if (isOntimeGroup(entry)) return { userEnteredValue: { stringValue: 'group' } };
if (isOntimeMilestone(entry)) return { userEnteredValue: { stringValue: 'milestone' } };
return { userEnteredValue: { stringValue: entry.timerType } };
}
// typescript cannot guarantee that the key exists for every entry
// so we check for the key existence and assert the type
if (!(key in entry)) return {};
const value = entry[key as keyof OntimeEntry];
if (typeof value === 'number') {
return { userEnteredValue: { stringValue: millisToString(value) } };
}
if (typeof value === 'string') {
return { userEnteredValue: { stringValue: value } };
}
if (typeof value === 'boolean') {
return { userEnteredValue: { boolValue: value } };
}
} }
type googleSheetCellColour = { type googleSheetCellColour = {