Skip to content

Commit 8ffb33f

Browse files
committed
Split the code out a bit to make it easier to follow
1 parent 008b043 commit 8ffb33f

File tree

13 files changed

+798
-722
lines changed

13 files changed

+798
-722
lines changed

src/dependencies.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ import {StatusCodes} from 'http-status-codes';
99
import {Resource} from './types/resource';
1010
import {EventName, EventOfType} from './types/domain-event';
1111
import {SharedReadModel} from './read-models/shared-state';
12+
import {LastGoogleSheetRowRead} from './read-models/shared-state/return-types';
1213

1314
export type GoogleSheetId = string;
1415

15-
// Future scans of the sheet should start at this row + 1 if set otherwise scan the entire sheet.
16-
export type LastGoogleSheetRowRead = O.Option<number>;
17-
1816
export type Dependencies = {
1917
commitEvent: (
2018
resource: Resource,

src/google/google.ts

Lines changed: 0 additions & 163 deletions
This file was deleted.

src/init-dependencies/google/pull_sheet_data.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ import * as E from 'fp-ts/Either';
77
import {pipe} from 'fp-ts/lib/function';
88
import {sheets} from '@googleapis/sheets';
99
import {GoogleAuth} from 'google-auth-library';
10-
import {columnIndexToLetter} from '../../google/extract-metadata';
1110
import {formatValidationErrors} from 'io-ts-reporters';
1211
import {DateTime} from 'luxon';
1312

1413
const DEFAULT_TIMEZONE = 'Europe/London';
1514

15+
export type ColumnLetter = string;
16+
export type ColumnIndex = number; // 0-indexed.
17+
// Doesn't support beyond 26 columns but actually thats fine for the current data.
18+
export const columnIndexToLetter = (index: ColumnIndex): ColumnLetter =>
19+
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.charAt(index);
20+
1621
// Not all the google form sheets are actually in Europe/London.
1722
// Issue first noticed because CI is in a different zone (UTC) than local test machine (BST).
1823
export const GoogleTimezone = tt.withValidate(t.string, (input, context) =>

src/google/extract-metadata.ts renamed to src/read-models/external-event-sources/google/extract-metadata.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ import * as RA from 'fp-ts/ReadonlyArray';
22
import * as O from 'fp-ts/Option';
33

44
import {Logger} from 'pino';
5-
import {GoogleSpreadsheetDataForSheet} from '../init-dependencies/google/pull_sheet_data';
5+
import {
6+
ColumnIndex,
7+
GoogleSpreadsheetDataForSheet,
8+
} from '../../../init-dependencies/google/pull_sheet_data';
69
import {array} from 'fp-ts';
710
import {pipe} from 'fp-ts/lib/function';
811

912
const EMAIL_COLUMN_NAMES = ['email address', 'email'];
1013

1114
export type GoogleSheetName = string;
1215

13-
type ColumnLetter = string;
14-
type ColumnIndex = number; // 0-indexed.
1516
// Requires a subsequent call to get the column names.
1617
export interface GoogleSheetMetadata {
1718
name: GoogleSheetName;
@@ -26,9 +27,6 @@ export interface GoogleSheetMetadata {
2627
}
2728

2829
export const MAX_COLUMN_INDEX = 25;
29-
// Doesn't support beyond 26 columns but actually thats fine for the current data.
30-
export const columnIndexToLetter = (index: ColumnIndex): ColumnLetter =>
31-
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.charAt(index);
3230

3331
export const extractGoogleSheetMetadata =
3432
(logger: Logger) =>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import {Logger} from 'pino';
2+
import * as O from 'fp-ts/Option';
3+
import {DomainEvent} from '../../../types';
4+
import {BetterSQLite3Database} from 'drizzle-orm/better-sqlite3';
5+
6+
import {GoogleHelpers} from '../../../init-dependencies/google/pull_sheet_data';
7+
8+
import {Dependencies} from '../../../dependencies';
9+
10+
import {startSpan} from '@sentry/node';
11+
12+
import {asyncApplyTrainingSheetEvents} from './training-sheet';
13+
import {asyncApplyTroubleTicketEvents} from './trouble-tickets';
14+
15+
export async function asyncApplyGoogleEvents(
16+
logger: Logger,
17+
currentState: BetterSQLite3Database,
18+
googleHelpers: GoogleHelpers,
19+
updateState: (event: DomainEvent) => void,
20+
googleRefreshIntervalMs: number,
21+
troubleTicketSheetId: O.Option<string>,
22+
cacheSheetData: Dependencies['cacheSheetData'],
23+
cacheTroubleTicketData: Dependencies['cacheTroubleTicketData']
24+
) {
25+
await startSpan(
26+
{
27+
name: 'Apply Google Events',
28+
},
29+
async () => {
30+
await asyncApplyTroubleTicketEvents(
31+
logger,
32+
googleHelpers,
33+
troubleTicketSheetId,
34+
updateState,
35+
cacheTroubleTicketData
36+
);
37+
await asyncApplyTrainingSheetEvents(
38+
logger,
39+
currentState,
40+
googleHelpers,
41+
updateState,
42+
googleRefreshIntervalMs,
43+
cacheSheetData
44+
);
45+
}
46+
);
47+
}

0 commit comments

Comments
 (0)