From 9820c71a2d007e80343755922785e3b7cc89bb33 Mon Sep 17 00:00:00 2001 From: arc-alex Date: Sat, 25 Nov 2023 20:03:30 +0100 Subject: [PATCH] move util functions to separat file for testing --- apps/server/src/utils/googleSheetUtils.ts | 100 ++++++++++++++++++++ apps/server/src/utils/sheetsAuth.ts | 108 +--------------------- 2 files changed, 105 insertions(+), 103 deletions(-) create mode 100644 apps/server/src/utils/googleSheetUtils.ts diff --git a/apps/server/src/utils/googleSheetUtils.ts b/apps/server/src/utils/googleSheetUtils.ts new file mode 100644 index 000000000..b0838dcac --- /dev/null +++ b/apps/server/src/utils/googleSheetUtils.ts @@ -0,0 +1,100 @@ +import { sheets_v4 } from '@googleapis/sheets'; +import { millisToString } from 'ontime-utils'; +import { OntimeRundownEntry, isOntimeEvent } from 'ontime-types'; + +/** + * + * @param {number} row - The row number of the cell reference. Row 1 is row number 0. + * @param {number} column - The column number of the cell reference. A is column number 0. + * @returns {string} - Returns a cell reference as a string using A1 Notation + * @author https://www.labnol.org/convert-column-a1-notation-210601 + * @example + * + * getA1Notation(2, 4) returns "E3" + * getA1Notation(26, 4) returns "AA3" + * + */ +export function getA1Notation(row: number, column: number): string { + if (row < 0 || column < 0) { + throw new Error('Index can not be less than 0'); + } + const a1Notation = [`${row + 1}`]; + const totalAlphabets = 'Z'.charCodeAt(0) - 'A'.charCodeAt(0) + 1; + let block = column; + while (block >= 0) { + a1Notation.unshift(String.fromCharCode((block % totalAlphabets) + 'A'.charCodeAt(0))); + block = Math.floor(block / totalAlphabets) - 1; + } + return a1Notation.join(''); +} + +/** + * @description - creates updateCells request from ontime event + * @param {OntimeRundownEntry} event + * @param {number} index - index of the event + * @param {number} worksheetId + * @param {any} metadata - object with all the cell positions of the title of each attribute + * @param {number} titleCol - smallest col index + * @returns {sheets_v4.Schema} - list of update requests + */ +export function cellRequenstFromEvent( + event: OntimeRundownEntry, + index: number, + worksheetId: number, + metadata, + titleCol: number, +): sheets_v4.Schema$Request { + const r: sheets_v4.Schema$CellData[] = []; + const tmp = Object.entries(metadata) + .filter(([_, value]) => value !== undefined) + .sort(([_a, a], [_b, b]) => a['col'] - b['col']); + + tmp.forEach(([_, value], index, arr) => { + if (index != 0) { + if (arr[index - 1][1]['col'] + 1 < value['col']) { + arr.splice(index, 0, ['blank', { col: arr[index - 1][1]['col'] + 1 }]); + } + } + }); + + tmp.forEach(([key, _]) => { + if (isOntimeEvent(event)) { + if (key === 'blank') { + r.push({}); + } else if (key === 'colour') { + r.push({ + userEnteredValue: { stringValue: event.colour }, + }); + } else if (typeof event[key] === 'number') { + r.push({ + userEnteredValue: { stringValue: millisToString(event[key], true) }, + }); + } else if (typeof event[key] === 'string') { + r.push({ + userEnteredValue: { stringValue: event[key] }, + }); + } else if (typeof event[key] === 'boolean') { + r.push({ + userEnteredValue: { stringValue: event[key] ? 'x' : '' }, + }); + } else { + r.push({}); + } + } + }); + return { + updateCells: { + start: { + sheetId: worksheetId, + rowIndex: index + Object.values(metadata)[0]['row'] + 1, + columnIndex: titleCol, + }, + fields: 'userEnteredValue', + rows: [ + { + values: r, + }, + ], + }, + }; +} diff --git a/apps/server/src/utils/sheetsAuth.ts b/apps/server/src/utils/sheetsAuth.ts index 8b165f3d1..ced4b5872 100644 --- a/apps/server/src/utils/sheetsAuth.ts +++ b/apps/server/src/utils/sheetsAuth.ts @@ -5,13 +5,13 @@ import http from 'http'; import { URL } from 'url'; import { logger } from '../classes/Logger.js'; import { getAppDataPath } from '../setup.js'; -import { DatabaseModel, LogOrigin, OntimeRundownEntry, isOntimeEvent } from 'ontime-types'; -import { millisToString } from 'ontime-utils'; +import { DatabaseModel, LogOrigin } from 'ontime-types'; 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'; +import { cellRequenstFromEvent, getA1Notation } from './googleSheetUtils.js'; type ResponseOK = { data: Partial; @@ -75,10 +75,7 @@ class sheet { if (spreadsheets.status === 200) { const w = spreadsheets.data.sheets.find((p) => p.properties.title == worksheet); if (w !== undefined) { - const endCell = this.getA1Notation( - w.properties.gridProperties.rowCount, - w.properties.gridProperties.columnCount, - ); + const endCell = getA1Notation(w.properties.gridProperties.rowCount, w.properties.gridProperties.columnCount); return { worksheetId: w.properties.sheetId, range: worksheet + '!A1:' + endCell }; } else { return true; @@ -146,7 +143,7 @@ class sheet { //update the corespunding row with event data rundown.forEach((entry, index) => updateRundown.push( - this.cellRequenstFromEvent(entry, index, this.worksheetId, rundownMetadata, titleCol as number), + cellRequenstFromEvent(entry, index, this.worksheetId, rundownMetadata, titleCol as number), ), ); const writeResponds = await sheets({ version: 'v4', auth: sheet.client }).spreadsheets.batchUpdate({ @@ -237,76 +234,7 @@ class sheet { return false; } - /** - * @description - creates updateCells request from ontime event - * @param {OntimeRundownEntry} event - * @param {number} index - index of the event - * @param {number} worksheetId - * @param {any} metadata - object with all the cell positions of the title of each attribute - * @param {number} titleCol - smallest col index - * @returns {sheets_v4.Schema} - list of update requests - */ - private cellRequenstFromEvent( - event: OntimeRundownEntry, - index: number, - worksheetId: number, - metadata, - titleCol: number, - ): sheets_v4.Schema$Request { - const r: sheets_v4.Schema$CellData[] = []; - const tmp = Object.entries(metadata) - .filter(([_, value]) => value !== undefined) - .sort(([_a, a], [_b, b]) => a['col'] - b['col']); - - tmp.forEach(([_, value], index, arr) => { - if (index != 0) { - if (arr[index - 1][1]['col'] + 1 < value['col']) { - arr.splice(index, 0, ['blank', { col: arr[index - 1][1]['col'] + 1 }]); - } - } - }); - - tmp.forEach(([key, _]) => { - if (isOntimeEvent(event)) { - if (key === 'blank') { - r.push({}); - } else if (key === 'colour') { - r.push({ - userEnteredValue: { stringValue: event.colour }, - }); - } else if (typeof event[key] === 'number') { - r.push({ - userEnteredValue: { stringValue: millisToString(event[key], true) }, - }); - } else if (typeof event[key] === 'string') { - r.push({ - userEnteredValue: { stringValue: event[key] }, - }); - } else if (typeof event[key] === 'boolean') { - r.push({ - userEnteredValue: { stringValue: event[key] ? 'x' : '' }, - }); - } else { - r.push({}); - } - } - }); - return { - updateCells: { - start: { - sheetId: worksheetId, - rowIndex: index + Object.values(metadata)[0]['row'] + 1, - columnIndex: titleCol, - }, - fields: 'userEnteredValue', - rows: [ - { - values: r, - }, - ], - }, - }; - } + private authServerTimeout; /** @@ -414,32 +342,6 @@ class sheet { ); return authorizeUrl; } - - /** - * - * @param {number} row - The row number of the cell reference. Row 1 is row number 0. - * @param {number} column - The column number of the cell reference. A is column number 0. - * @returns {string} - Returns a cell reference as a string using A1 Notation - * @author https://www.labnol.org/convert-column-a1-notation-210601 - * @example - * - * getA1Notation(2, 4) returns "E3" - * getA1Notation(26, 4) returns "AA3" - * - */ - private getA1Notation(row: number, column: number): string { - if (row < 0 || column < 0) { - throw new Error('Index can not be less than 0'); - } - const a1Notation = [`${row + 1}`]; - const totalAlphabets = 'Z'.charCodeAt(0) - 'A'.charCodeAt(0) + 1; - let block = column; - while (block >= 0) { - a1Notation.unshift(String.fromCharCode((block % totalAlphabets) + 'A'.charCodeAt(0))); - block = Math.floor(block / totalAlphabets) - 1; - } - return a1Notation.join(''); - } } // Copyright 2020 Google LLC