@stamhoofd/backend-statistics-syncer 2.152.0 → 2.160.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +7 -7
- package/README.md +2 -2
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/migrations.js +19 -0
- package/dist/migrations.js.map +1 -0
- package/dist/src/boot.js +75 -0
- package/dist/src/boot.js.map +1 -0
- package/dist/src/crons.js +39 -0
- package/dist/src/crons.js.map +1 -0
- package/dist/src/database.js +88 -0
- package/dist/src/database.js.map +1 -0
- package/dist/src/migrate.js +38 -0
- package/dist/src/migrate.js.map +1 -0
- package/dist/src/migrations/1785958573-platform-statistics-schema.sql +382 -0
- package/dist/src/migrations/1786203367-postal-code-coordinates.sql +1203 -0
- package/dist/src/periods.js +86 -0
- package/dist/src/periods.js.map +1 -0
- package/dist/src/rows.js +209 -0
- package/dist/src/rows.js.map +1 -0
- package/dist/src/schema.js +28 -0
- package/dist/src/schema.js.map +1 -0
- package/dist/src/sources.js +10 -0
- package/dist/src/sources.js.map +1 -0
- package/dist/src/sync-state.js +36 -0
- package/dist/src/sync-state.js.map +1 -0
- package/dist/src/sync.js +608 -0
- package/dist/src/sync.js.map +1 -0
- package/dist/src/writer.js +63 -0
- package/dist/src/writer.js.map +1 -0
- package/dist-types/index.d.ts +2 -0
- package/dist-types/index.d.ts.map +1 -0
- package/dist-types/migrations.d.ts +2 -0
- package/dist-types/migrations.d.ts.map +1 -0
- package/dist-types/src/boot.d.ts +8 -0
- package/dist-types/src/boot.d.ts.map +1 -0
- package/dist-types/src/crons.d.ts +21 -0
- package/dist-types/src/crons.d.ts.map +1 -0
- package/dist-types/src/database.d.ts +28 -0
- package/dist-types/src/database.d.ts.map +1 -0
- package/dist-types/src/migrate.d.ts +2 -0
- package/dist-types/src/migrate.d.ts.map +1 -0
- package/dist-types/src/periods.d.ts +51 -0
- package/dist-types/src/periods.d.ts.map +1 -0
- package/dist-types/src/rows.d.ts +212 -0
- package/dist-types/src/rows.d.ts.map +1 -0
- package/dist-types/src/schema.d.ts +9 -0
- package/dist-types/src/schema.d.ts.map +1 -0
- package/dist-types/src/sources.d.ts +10 -0
- package/dist-types/src/sources.d.ts.map +1 -0
- package/dist-types/src/sync-state.d.ts +20 -0
- package/dist-types/src/sync-state.d.ts.map +1 -0
- package/dist-types/src/sync.d.ts +37 -0
- package/dist-types/src/sync.d.ts.map +1 -0
- package/dist-types/src/writer.d.ts +24 -0
- package/dist-types/src/writer.d.ts.map +1 -0
- package/index.ts +5 -8
- package/migrations.ts +16 -11
- package/package.json +63 -50
- package/src/boot.ts +7 -3
- package/src/crons.test.ts +33 -8
- package/src/crons.ts +7 -1
- package/src/database.test.ts +48 -11
- package/src/database.ts +30 -7
- package/src/migrate.ts +1 -1
- package/src/periods.ts +5 -2
- package/src/rows.test.ts +96 -16
- package/src/rows.ts +28 -6
- package/src/schema.test.ts +48 -19
- package/src/schema.ts +14 -4
- package/src/sync-state.test.ts +3 -1
- package/src/sync-state.ts +13 -4
- package/src/sync.test.ts +145 -32
- package/src/sync.ts +422 -91
- package/src/writer.ts +32 -10
- package/tsconfig.build.json +4 -11
- package/tsconfig.json +1 -1
- package/tsconfig.test.json +1 -3
- package/tsconfig.types.json +7 -0
- package/tests/vitest.global.setup.ts +0 -29
- package/tests/vitest.setup.ts +0 -24
- package/vitest.config.js +0 -18
package/package.json
CHANGED
|
@@ -1,51 +1,64 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
"
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
"
|
|
51
|
-
}
|
|
2
|
+
"name": "@stamhoofd/backend-statistics-syncer",
|
|
3
|
+
"version": "2.160.0",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "git+https://github.com/stamhoofd/stamhoofd.git",
|
|
7
|
+
"directory": "backend/app/statistics-syncer"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist-types/index.d.ts",
|
|
14
|
+
"require": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"types": "./dist-types/index.d.ts",
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"dist-types",
|
|
21
|
+
"src",
|
|
22
|
+
"*.ts",
|
|
23
|
+
"tsconfig*.json"
|
|
24
|
+
],
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@stamhoofd/common-encoding": "2.160.0",
|
|
27
|
+
"@stamhoofd/common-errors": "2.160.0",
|
|
28
|
+
"@stamhoofd/node-database": "2.160.0",
|
|
29
|
+
"@stamhoofd/node-logging": "2.160.0",
|
|
30
|
+
"@stamhoofd/node-observability": "2.160.0",
|
|
31
|
+
"@stamhoofd/backend-env": "2.160.0",
|
|
32
|
+
"@stamhoofd/crons": "2.160.0",
|
|
33
|
+
"@stamhoofd/logging": "2.160.0",
|
|
34
|
+
"@stamhoofd/models": "2.160.0",
|
|
35
|
+
"@stamhoofd/queues": "2.160.0",
|
|
36
|
+
"@stamhoofd/node-sql": "2.160.0",
|
|
37
|
+
"@stamhoofd/structures": "2.160.0",
|
|
38
|
+
"@stamhoofd/types": "2.160.0",
|
|
39
|
+
"@stamhoofd/utility": "2.160.0",
|
|
40
|
+
"mysql2": "3.24.6"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"oxlint-plugin-stamhoofd": "2.160.0",
|
|
44
|
+
"@stamhoofd/test-utils": "2.160.0",
|
|
45
|
+
"@types/node": "22.20.4",
|
|
46
|
+
"vitest": "4.1.12"
|
|
47
|
+
},
|
|
48
|
+
"license": "UNLICENCED",
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public"
|
|
51
|
+
},
|
|
52
|
+
"scripts": {
|
|
53
|
+
"build:types": "tsc --project tsconfig.types.json",
|
|
54
|
+
"build": "tsc --build tsconfig.build.json && mkdir -p ./dist/src/migrations && rsync --delete --exclude='*.ts' --exclude='*.js' --exclude='*.map' -r --checksum ./src/migrations/ ./dist/src/migrations/",
|
|
55
|
+
"build:full": "pnpm run clear && pnpm run build",
|
|
56
|
+
"clear": "rm -rf ./dist && rm -f *.tsbuildinfo .cache/*.tsbuildinfo",
|
|
57
|
+
"lint": "oxlint . --quiet --type-aware --threads 2",
|
|
58
|
+
"migrations": "pnpm run build:full && node ./dist/migrations.js",
|
|
59
|
+
"start": "pnpm run build && node --enable-source-maps ./dist/index.js",
|
|
60
|
+
"typecheck": "tsc --project tsconfig.test.json --noEmit --composite false --incremental --tsBuildInfoFile .cache/typecheck.tsbuildinfo",
|
|
61
|
+
"test": "vitest",
|
|
62
|
+
"test:coverage": "vitest --coverage"
|
|
63
|
+
}
|
|
64
|
+
}
|
package/src/boot.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { Column, Database } from '@
|
|
1
|
+
import { Column, Database } from '@stamhoofd/node-database';
|
|
2
|
+
import { ApplicationLifecycle } from '@stamhoofd/node-observability';
|
|
2
3
|
import { startCrons, stopCrons, waitForCrons } from '@stamhoofd/crons';
|
|
3
4
|
import { loadLogger } from '@stamhoofd/logging';
|
|
4
5
|
import { Version } from '@stamhoofd/structures';
|
|
@@ -34,9 +35,10 @@ function loadGlobalTranslateFunction() {
|
|
|
34
35
|
(global as any).$getCountry = () => 'BE';
|
|
35
36
|
}
|
|
36
37
|
|
|
37
|
-
export const boot = async (options: { killProcess: boolean }) => {
|
|
38
|
-
|
|
38
|
+
export const boot = async (options: { killProcess: boolean; lifecycle?: ApplicationLifecycle }) => {
|
|
39
|
+
const lifecycle = options.lifecycle ?? new ApplicationLifecycle('stats');
|
|
39
40
|
loadLogger();
|
|
41
|
+
console.debug('Started Statistics.');
|
|
40
42
|
|
|
41
43
|
loadGlobalTranslateFunction();
|
|
42
44
|
|
|
@@ -54,6 +56,7 @@ export const boot = async (options: { killProcess: boolean }) => {
|
|
|
54
56
|
}
|
|
55
57
|
|
|
56
58
|
// Should not be needed, but added for security as sometimes a promise hangs somewhere
|
|
59
|
+
await lifecycle.close();
|
|
57
60
|
if (options.killProcess) {
|
|
58
61
|
process.exit(0);
|
|
59
62
|
}
|
|
@@ -80,5 +83,6 @@ export const boot = async (options: { killProcess: boolean }) => {
|
|
|
80
83
|
// This service runs no seeds of its own.
|
|
81
84
|
startCrons(statisticsCronOptions);
|
|
82
85
|
|
|
86
|
+
await lifecycle.ready();
|
|
83
87
|
return { shutdown };
|
|
84
88
|
};
|
package/src/crons.test.ts
CHANGED
|
@@ -15,18 +15,30 @@ describe('Cron.platform-statistics-sync', () => {
|
|
|
15
15
|
it('only runs at boot', () => {
|
|
16
16
|
TestUtils.setEnvironment('environment', 'production');
|
|
17
17
|
|
|
18
|
-
expect(
|
|
18
|
+
expect(
|
|
19
|
+
shouldRunStatisticsSync({ now: new Date(2026, 0, 5, 13, 30), lastRun: null }),
|
|
20
|
+
).toBe(true);
|
|
19
21
|
});
|
|
20
22
|
|
|
21
23
|
it('only runs at night', () => {
|
|
22
24
|
TestUtils.setEnvironment('environment', 'production');
|
|
23
25
|
|
|
24
26
|
for (const hour of [0, 2, 6, 12, 23]) {
|
|
25
|
-
expect(
|
|
27
|
+
expect(
|
|
28
|
+
shouldRunStatisticsSync({
|
|
29
|
+
now: new Date(2026, 0, 5, hour, 30),
|
|
30
|
+
lastRun: new Date(2023, 0, 5, hour, 30),
|
|
31
|
+
}),
|
|
32
|
+
).toBe(false);
|
|
26
33
|
}
|
|
27
34
|
|
|
28
35
|
for (const hour of [3, 4, 5]) {
|
|
29
|
-
expect(
|
|
36
|
+
expect(
|
|
37
|
+
shouldRunStatisticsSync({
|
|
38
|
+
now: new Date(2026, 0, 5, hour, 30),
|
|
39
|
+
lastRun: new Date(2023, 0, 5, hour, 30),
|
|
40
|
+
}),
|
|
41
|
+
).toBe(true);
|
|
30
42
|
}
|
|
31
43
|
});
|
|
32
44
|
|
|
@@ -36,17 +48,30 @@ describe('Cron.platform-statistics-sync', () => {
|
|
|
36
48
|
const firstRun = new Date(2026, 0, 5, 3, 0);
|
|
37
49
|
|
|
38
50
|
// A run that never finished leaves lastRun untouched, so the next tick picks it up again.
|
|
39
|
-
expect(
|
|
51
|
+
expect(
|
|
52
|
+
shouldRunStatisticsSync({ now: new Date(2026, 0, 5, 3, 5), lastRun: null }),
|
|
53
|
+
).toBe(true);
|
|
40
54
|
|
|
41
|
-
expect(
|
|
42
|
-
|
|
43
|
-
|
|
55
|
+
expect(
|
|
56
|
+
shouldRunStatisticsSync({ now: new Date(2026, 0, 5, 3, 5), lastRun: firstRun }),
|
|
57
|
+
).toBe(false);
|
|
58
|
+
expect(
|
|
59
|
+
shouldRunStatisticsSync({ now: new Date(2026, 0, 5, 5, 55), lastRun: firstRun }),
|
|
60
|
+
).toBe(false);
|
|
61
|
+
expect(
|
|
62
|
+
shouldRunStatisticsSync({ now: new Date(2026, 0, 6, 3, 0), lastRun: firstRun }),
|
|
63
|
+
).toBe(true);
|
|
44
64
|
});
|
|
45
65
|
|
|
46
66
|
it('runs on every tick in development, where waiting for the night is not an option', () => {
|
|
47
67
|
TestUtils.setEnvironment('environment', 'development');
|
|
48
68
|
|
|
49
|
-
expect(
|
|
69
|
+
expect(
|
|
70
|
+
shouldRunStatisticsSync({
|
|
71
|
+
now: new Date(2026, 0, 5, 14, 0),
|
|
72
|
+
lastRun: new Date(2026, 0, 5, 13, 59),
|
|
73
|
+
}),
|
|
74
|
+
).toBe(true);
|
|
50
75
|
});
|
|
51
76
|
});
|
|
52
77
|
});
|
package/src/crons.ts
CHANGED
|
@@ -7,7 +7,13 @@ registerCron('syncPlatformStatistics', syncPlatformStatistics);
|
|
|
7
7
|
* Both passes read the whole administration, which is far more than the five-minute cron cadence is
|
|
8
8
|
* meant for and does not need to be current to the minute: the reports they feed are read by day.
|
|
9
9
|
*/
|
|
10
|
-
export function shouldRunStatisticsSync({
|
|
10
|
+
export function shouldRunStatisticsSync({
|
|
11
|
+
now,
|
|
12
|
+
lastRun,
|
|
13
|
+
}: {
|
|
14
|
+
now: Date;
|
|
15
|
+
lastRun: Date | null;
|
|
16
|
+
}): boolean {
|
|
11
17
|
if (STAMHOOFD.environment === 'development') {
|
|
12
18
|
return true;
|
|
13
19
|
}
|
package/src/database.test.ts
CHANGED
|
@@ -10,20 +10,32 @@ describe('getStatisticsDatabaseConfig', () => {
|
|
|
10
10
|
});
|
|
11
11
|
|
|
12
12
|
it('refuses the main database even when it inherits its port instead of naming one', () => {
|
|
13
|
-
TestUtils.setEnvironment('stamhoofdDatabase', {
|
|
14
|
-
|
|
13
|
+
TestUtils.setEnvironment('stamhoofdDatabase', {
|
|
14
|
+
...STAMHOOFD.stamhoofdDatabase,
|
|
15
|
+
DB_PORT: 3307,
|
|
16
|
+
});
|
|
17
|
+
TestUtils.setEnvironment('statisticsDatabase', {
|
|
18
|
+
...STAMHOOFD.stamhoofdDatabase,
|
|
19
|
+
DB_PORT: undefined,
|
|
20
|
+
});
|
|
15
21
|
|
|
16
22
|
expect(() => getStatisticsDatabaseConfig()).toThrow('has to be a separate one');
|
|
17
23
|
});
|
|
18
24
|
|
|
19
25
|
it('accepts the same database name on another server, which is a database of its own', () => {
|
|
20
|
-
TestUtils.setEnvironment('statisticsDatabase', {
|
|
26
|
+
TestUtils.setEnvironment('statisticsDatabase', {
|
|
27
|
+
...STAMHOOFD.stamhoofdDatabase,
|
|
28
|
+
DB_HOST: 'statistics.example',
|
|
29
|
+
});
|
|
21
30
|
|
|
22
31
|
expect(getStatisticsDatabaseConfig().DB_HOST).toBe('statistics.example');
|
|
23
32
|
});
|
|
24
33
|
|
|
25
34
|
it('refuses to run when no statistics database is configured', () => {
|
|
26
|
-
TestUtils.setEnvironment(
|
|
35
|
+
TestUtils.setEnvironment(
|
|
36
|
+
'statisticsDatabase',
|
|
37
|
+
undefined as unknown as StatisticsEnvironment['statisticsDatabase'],
|
|
38
|
+
);
|
|
27
39
|
|
|
28
40
|
expect(() => getStatisticsDatabaseConfig()).toThrow('is not set');
|
|
29
41
|
});
|
|
@@ -39,25 +51,46 @@ describe('getStatisticsPoolOptions', () => {
|
|
|
39
51
|
});
|
|
40
52
|
|
|
41
53
|
it('falls back to the default port on another server, whose ports are its own', () => {
|
|
42
|
-
TestUtils.setEnvironment('statisticsDatabase', {
|
|
54
|
+
TestUtils.setEnvironment('statisticsDatabase', {
|
|
55
|
+
...STAMHOOFD.statisticsDatabase,
|
|
56
|
+
DB_HOST: 'statistics.example',
|
|
57
|
+
...certs,
|
|
58
|
+
});
|
|
43
59
|
|
|
44
60
|
expect(getStatisticsPoolOptions().port).toBe(3306);
|
|
45
61
|
});
|
|
46
62
|
|
|
47
63
|
it('uses the port it was given', () => {
|
|
48
|
-
TestUtils.setEnvironment('statisticsDatabase', {
|
|
64
|
+
TestUtils.setEnvironment('statisticsDatabase', {
|
|
65
|
+
...STAMHOOFD.statisticsDatabase,
|
|
66
|
+
DB_HOST: 'statistics.example',
|
|
67
|
+
DB_PORT: 3399,
|
|
68
|
+
...certs,
|
|
69
|
+
});
|
|
49
70
|
|
|
50
71
|
expect(getStatisticsPoolOptions().port).toBe(3399);
|
|
51
72
|
});
|
|
52
73
|
|
|
53
74
|
it('presents the client certificate to another server, whose login is created with REQUIRE X509', () => {
|
|
54
|
-
TestUtils.setEnvironment('statisticsDatabase', {
|
|
55
|
-
|
|
56
|
-
|
|
75
|
+
TestUtils.setEnvironment('statisticsDatabase', {
|
|
76
|
+
...STAMHOOFD.statisticsDatabase,
|
|
77
|
+
DB_HOST: 'statistics.example',
|
|
78
|
+
...certs,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
expect(getStatisticsPoolOptions()).toMatchObject({
|
|
82
|
+
useSSL: true,
|
|
83
|
+
ca: '/certs/ca.pem',
|
|
84
|
+
cert: '/certs/cert.pem',
|
|
85
|
+
key: '/certs/key.pem',
|
|
86
|
+
});
|
|
57
87
|
});
|
|
58
88
|
|
|
59
89
|
it('refuses to connect to another server without a client certificate', () => {
|
|
60
|
-
TestUtils.setEnvironment('statisticsDatabase', {
|
|
90
|
+
TestUtils.setEnvironment('statisticsDatabase', {
|
|
91
|
+
...STAMHOOFD.statisticsDatabase,
|
|
92
|
+
DB_HOST: 'statistics.example',
|
|
93
|
+
});
|
|
61
94
|
|
|
62
95
|
expect(() => getStatisticsPoolOptions()).toThrow(/DB_CERT and DB_KEY/);
|
|
63
96
|
});
|
|
@@ -65,6 +98,10 @@ describe('getStatisticsPoolOptions', () => {
|
|
|
65
98
|
it('does not ask for TLS while both databases are on one MySQL', () => {
|
|
66
99
|
expect(STAMHOOFD.statisticsDatabase.DB_HOST).toBe(STAMHOOFD.stamhoofdDatabase.DB_HOST);
|
|
67
100
|
|
|
68
|
-
expect(getStatisticsPoolOptions()).toMatchObject({
|
|
101
|
+
expect(getStatisticsPoolOptions()).toMatchObject({
|
|
102
|
+
useSSL: false,
|
|
103
|
+
cert: undefined,
|
|
104
|
+
key: undefined,
|
|
105
|
+
});
|
|
69
106
|
});
|
|
70
107
|
});
|
package/src/database.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DatabaseInstance } from '@
|
|
1
|
+
import { DatabaseInstance } from '@stamhoofd/node-database';
|
|
2
2
|
import type { StatisticsEnvironment } from '@stamhoofd/types/Environment';
|
|
3
3
|
|
|
4
4
|
export type StatisticsDatabaseConfig = StatisticsEnvironment['statisticsDatabase'];
|
|
@@ -9,7 +9,9 @@ export function getStatisticsDatabaseConfig(): StatisticsDatabaseConfig {
|
|
|
9
9
|
const config = STAMHOOFD.statisticsDatabase;
|
|
10
10
|
|
|
11
11
|
if (!config?.DB_DATABASE) {
|
|
12
|
-
throw new Error(
|
|
12
|
+
throw new Error(
|
|
13
|
+
'STAMHOOFD.statisticsDatabase is not set: configure the platform statistics database before running migrations',
|
|
14
|
+
);
|
|
13
15
|
}
|
|
14
16
|
|
|
15
17
|
// The statistics migrations create tables that share their names with the main database
|
|
@@ -17,8 +19,15 @@ export function getStatisticsDatabaseConfig(): StatisticsDatabaseConfig {
|
|
|
17
19
|
// it, so the one configuration that could damage it is refused outright. Each lives on its own
|
|
18
20
|
// server, so it takes a match on the server as well as on the name to be the same database.
|
|
19
21
|
const main = STAMHOOFD.stamhoofdDatabase;
|
|
20
|
-
if (
|
|
21
|
-
|
|
22
|
+
if (
|
|
23
|
+
main &&
|
|
24
|
+
config.DB_DATABASE === main.DB_DATABASE &&
|
|
25
|
+
config.DB_HOST === main.DB_HOST &&
|
|
26
|
+
(config.DB_PORT ?? main.DB_PORT) === main.DB_PORT
|
|
27
|
+
) {
|
|
28
|
+
throw new Error(
|
|
29
|
+
`STAMHOOFD.statisticsDatabase is the main database (${config.DB_DATABASE} on ${config.DB_HOST}): the platform statistics database has to be a separate one`,
|
|
30
|
+
);
|
|
22
31
|
}
|
|
23
32
|
|
|
24
33
|
return config;
|
|
@@ -51,14 +60,25 @@ function getStatisticsPort(config: StatisticsDatabaseConfig): number | undefined
|
|
|
51
60
|
* Where to reach the statistics database. It lives next to Metabase rather than on the server this
|
|
52
61
|
* runs on, so every connection detail is spelled out instead of inherited from the main database.
|
|
53
62
|
*/
|
|
54
|
-
export function getStatisticsPoolOptions(): {
|
|
63
|
+
export function getStatisticsPoolOptions(): {
|
|
64
|
+
host: string;
|
|
65
|
+
user: string;
|
|
66
|
+
password: string;
|
|
67
|
+
port: number | undefined;
|
|
68
|
+
useSSL: boolean;
|
|
69
|
+
ca?: string;
|
|
70
|
+
cert?: string;
|
|
71
|
+
key?: string;
|
|
72
|
+
} {
|
|
55
73
|
const config = getStatisticsDatabaseConfig();
|
|
56
74
|
const useSSL = isOnAnotherServer(config);
|
|
57
75
|
|
|
58
76
|
// The login is created with REQUIRE X509: MySQL reports a missing client certificate as a plain
|
|
59
77
|
// "access denied", so refuse early with a message that names the actual cause.
|
|
60
78
|
if (useSSL && (!config.DB_CERT || !config.DB_KEY)) {
|
|
61
|
-
throw new Error(
|
|
79
|
+
throw new Error(
|
|
80
|
+
'STAMHOOFD.statisticsDatabase.DB_CERT and DB_KEY are required to connect to a statistics database on another server',
|
|
81
|
+
);
|
|
62
82
|
}
|
|
63
83
|
|
|
64
84
|
return {
|
|
@@ -85,7 +105,10 @@ let connection: DatabaseInstance | undefined;
|
|
|
85
105
|
* MySQL to point a single connection at.
|
|
86
106
|
*/
|
|
87
107
|
export function getStatisticsConnection(): DatabaseInstance {
|
|
88
|
-
connection ??= new DatabaseInstance({
|
|
108
|
+
connection ??= new DatabaseInstance({
|
|
109
|
+
...getStatisticsPoolOptions(),
|
|
110
|
+
database: getStatisticsDatabaseConfig().DB_DATABASE,
|
|
111
|
+
});
|
|
89
112
|
return connection;
|
|
90
113
|
}
|
|
91
114
|
|
package/src/migrate.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Column } from '@
|
|
1
|
+
import { Column } from '@stamhoofd/node-database';
|
|
2
2
|
import { Version } from '@stamhoofd/structures';
|
|
3
3
|
import { endStatisticsConnection } from './database.js';
|
|
4
4
|
import { runStatisticsMigrations } from './schema.js';
|
package/src/periods.ts
CHANGED
|
@@ -50,7 +50,7 @@ async function loadPeriodIds(where: string, params: unknown[]): Promise<Set<stri
|
|
|
50
50
|
{ nestTables: false },
|
|
51
51
|
);
|
|
52
52
|
|
|
53
|
-
return new Set((rows as unknown as { id: string }[]).map(row => row.id));
|
|
53
|
+
return new Set((rows as unknown as { id: string }[]).map((row) => row.id));
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
const settledWhere = '`cutoffAt` IS NOT NULL AND `cutoffAt` <= ?';
|
|
@@ -107,6 +107,9 @@ export async function releaseUnlockedPeriods(): Promise<void> {
|
|
|
107
107
|
* MySQL cannot be handed an empty IN list, and "belongs to no period" is never frozen: a row without
|
|
108
108
|
* a period is not part of any year's settled numbers.
|
|
109
109
|
*/
|
|
110
|
-
export function isFrozen(
|
|
110
|
+
export function isFrozen(
|
|
111
|
+
frozenPeriodIds: Set<string>,
|
|
112
|
+
periodId: string | null | undefined,
|
|
113
|
+
): boolean {
|
|
111
114
|
return typeof periodId === 'string' && frozenPeriodIds.has(periodId);
|
|
112
115
|
}
|
package/src/rows.test.ts
CHANGED
|
@@ -1,10 +1,22 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
flattenDefaultAgeGroup,
|
|
4
|
+
flattenGroup,
|
|
5
|
+
flattenMember,
|
|
6
|
+
flattenMembership,
|
|
7
|
+
flattenOrganization,
|
|
8
|
+
flattenPlatform,
|
|
9
|
+
flattenRegistration,
|
|
10
|
+
} from './rows.js';
|
|
3
11
|
|
|
4
12
|
describe('flattenMember', () => {
|
|
5
13
|
const member = {
|
|
6
14
|
id: 'member-1',
|
|
7
|
-
details: {
|
|
15
|
+
details: {
|
|
16
|
+
birthDay: new Date(2011, 4, 17),
|
|
17
|
+
gender: 'Female',
|
|
18
|
+
address: { postalCode: '9000' },
|
|
19
|
+
},
|
|
8
20
|
organizationId: 'org-1',
|
|
9
21
|
createdAt: new Date(2025, 0, 1),
|
|
10
22
|
updatedAt: new Date(2026, 0, 1),
|
|
@@ -22,13 +34,39 @@ describe('flattenMember', () => {
|
|
|
22
34
|
it('writes the calendar date the application means, not the instant behind it', () => {
|
|
23
35
|
const brusselsMidnight = new Date('2011-05-16T22:00:00.000Z');
|
|
24
36
|
|
|
25
|
-
expect(
|
|
37
|
+
expect(
|
|
38
|
+
flattenMember(
|
|
39
|
+
{ ...member, details: { ...member.details, birthDay: brusselsMidnight } },
|
|
40
|
+
'period-1',
|
|
41
|
+
).birthDate,
|
|
42
|
+
).toBe('2011-05-17');
|
|
26
43
|
});
|
|
27
44
|
|
|
28
45
|
it('never carries a name or a way to contact the member', () => {
|
|
29
|
-
const row = flattenMember(
|
|
30
|
-
|
|
31
|
-
|
|
46
|
+
const row = flattenMember(
|
|
47
|
+
{
|
|
48
|
+
...member,
|
|
49
|
+
details: {
|
|
50
|
+
...member.details,
|
|
51
|
+
firstName: 'Jan',
|
|
52
|
+
lastName: 'Jansen',
|
|
53
|
+
email: 'jan@example.com',
|
|
54
|
+
} as any,
|
|
55
|
+
},
|
|
56
|
+
'period-1',
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
expect(Object.keys(row).sort()).toEqual([
|
|
60
|
+
'birthDate',
|
|
61
|
+
'createdAt',
|
|
62
|
+
'gender',
|
|
63
|
+
'id',
|
|
64
|
+
'lastRegisteredAt',
|
|
65
|
+
'organizationId',
|
|
66
|
+
'periodId',
|
|
67
|
+
'postalCode',
|
|
68
|
+
'updatedAt',
|
|
69
|
+
]);
|
|
32
70
|
expect(JSON.stringify(row)).not.toContain('Jan');
|
|
33
71
|
expect(JSON.stringify(row)).not.toContain('example.com');
|
|
34
72
|
});
|
|
@@ -47,11 +85,25 @@ describe('flattenMember', () => {
|
|
|
47
85
|
});
|
|
48
86
|
|
|
49
87
|
it('keeps a member without a birth date', () => {
|
|
50
|
-
expect(
|
|
88
|
+
expect(
|
|
89
|
+
flattenMember(
|
|
90
|
+
{ ...member, details: { birthDay: null, gender: 'Other', address: null } },
|
|
91
|
+
'period-1',
|
|
92
|
+
).birthDate,
|
|
93
|
+
).toBeNull();
|
|
51
94
|
});
|
|
52
95
|
|
|
53
96
|
it('keeps the postal code the member map needs, and nothing else of the address', () => {
|
|
54
|
-
const row = flattenMember(
|
|
97
|
+
const row = flattenMember(
|
|
98
|
+
{
|
|
99
|
+
...member,
|
|
100
|
+
details: {
|
|
101
|
+
...member.details,
|
|
102
|
+
address: { postalCode: '9000', street: 'Somewhere', city: 'Gent' } as any,
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
'period-1',
|
|
106
|
+
);
|
|
55
107
|
|
|
56
108
|
expect(row.postalCode).toBe('9000');
|
|
57
109
|
expect(JSON.stringify(row)).not.toContain('Somewhere');
|
|
@@ -59,7 +111,10 @@ describe('flattenMember', () => {
|
|
|
59
111
|
});
|
|
60
112
|
|
|
61
113
|
it('has no postal code for a member without an address', () => {
|
|
62
|
-
expect(
|
|
114
|
+
expect(
|
|
115
|
+
flattenMember({ ...member, details: { ...member.details, address: null } }, 'period-1')
|
|
116
|
+
.postalCode,
|
|
117
|
+
).toBeNull();
|
|
63
118
|
});
|
|
64
119
|
});
|
|
65
120
|
|
|
@@ -85,7 +140,12 @@ describe('flattenOrganization', () => {
|
|
|
85
140
|
});
|
|
86
141
|
|
|
87
142
|
it('stores a missing postal code as null rather than an empty string', () => {
|
|
88
|
-
expect(
|
|
143
|
+
expect(
|
|
144
|
+
flattenOrganization(
|
|
145
|
+
{ ...organization, address: { postalCode: '', city: '' } },
|
|
146
|
+
'period-1',
|
|
147
|
+
).postalCode,
|
|
148
|
+
).toBeNull();
|
|
89
149
|
});
|
|
90
150
|
|
|
91
151
|
/**
|
|
@@ -119,7 +179,10 @@ describe('flattenPlatform', () => {
|
|
|
119
179
|
});
|
|
120
180
|
|
|
121
181
|
it('stores no organization for a platform that runs none', () => {
|
|
122
|
-
expect(
|
|
182
|
+
expect(
|
|
183
|
+
flattenPlatform({ ...platform, membershipOrganizationId: null })
|
|
184
|
+
.membershipOrganizationId,
|
|
185
|
+
).toBeNull();
|
|
123
186
|
});
|
|
124
187
|
|
|
125
188
|
/**
|
|
@@ -128,7 +191,10 @@ describe('flattenPlatform', () => {
|
|
|
128
191
|
* the report to word, rather than being given a name here that nobody uses.
|
|
129
192
|
*/
|
|
130
193
|
it('keeps what the platform calls its verlaagd lidgeld, and nothing when it names none', () => {
|
|
131
|
-
expect(
|
|
194
|
+
expect(
|
|
195
|
+
flattenPlatform({ ...platform, config: { ...platform.config, financialSupport: null } })
|
|
196
|
+
.reducedPriceName,
|
|
197
|
+
).toBeNull();
|
|
132
198
|
});
|
|
133
199
|
});
|
|
134
200
|
|
|
@@ -156,7 +222,9 @@ describe('flattenMembership', () => {
|
|
|
156
222
|
it('records the tarief the lidgeld was charged at, and none of its prices', () => {
|
|
157
223
|
expect(flattenMembership(membership, true).reducedPrice).toBe(true);
|
|
158
224
|
expect(flattenMembership(membership, false).reducedPrice).toBe(false);
|
|
159
|
-
expect(
|
|
225
|
+
expect(
|
|
226
|
+
Object.keys(flattenMembership(membership, false)).filter((key) => /price/i.test(key)),
|
|
227
|
+
).toEqual(['reducedPrice']);
|
|
160
228
|
});
|
|
161
229
|
});
|
|
162
230
|
|
|
@@ -165,7 +233,7 @@ describe('flattenGroup', () => {
|
|
|
165
233
|
const row = flattenGroup({
|
|
166
234
|
id: 'group-1',
|
|
167
235
|
type: 'Membership',
|
|
168
|
-
settings: { name: { get: language => language === 'nl' ? 'Bevers' : 'Beavers' } },
|
|
236
|
+
settings: { name: { get: (language) => (language === 'nl' ? 'Bevers' : 'Beavers') } },
|
|
169
237
|
organizationId: 'org-1',
|
|
170
238
|
periodId: 'period-1',
|
|
171
239
|
defaultAgeGroupId: 'age-1',
|
|
@@ -205,7 +273,12 @@ describe('flattenDefaultAgeGroup', () => {
|
|
|
205
273
|
});
|
|
206
274
|
|
|
207
275
|
it('separates all but the last two with a comma', () => {
|
|
208
|
-
expect(
|
|
276
|
+
expect(
|
|
277
|
+
flattenDefaultAgeGroup(
|
|
278
|
+
{ ...group, names: ['Kapoenen', 'Welpen', 'Jonggivers'] },
|
|
279
|
+
'period-1',
|
|
280
|
+
).name,
|
|
281
|
+
).toBe('Kapoenen, Welpen of Jonggivers');
|
|
209
282
|
});
|
|
210
283
|
});
|
|
211
284
|
|
|
@@ -230,7 +303,14 @@ describe('flattenRegistration', () => {
|
|
|
230
303
|
|
|
231
304
|
expect(row.memberId).toBe('member-1');
|
|
232
305
|
expect(row.groupId).toBe('group-1');
|
|
233
|
-
for (const dropped of [
|
|
306
|
+
for (const dropped of [
|
|
307
|
+
'recordAnswers',
|
|
308
|
+
'price',
|
|
309
|
+
'pricePaid',
|
|
310
|
+
'groupPrice',
|
|
311
|
+
'paymentId',
|
|
312
|
+
'options',
|
|
313
|
+
]) {
|
|
234
314
|
expect(Object.keys(row)).not.toContain(dropped);
|
|
235
315
|
}
|
|
236
316
|
});
|