@stamhoofd/backend-statistics-syncer 2.153.0 → 2.161.1
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 +1 -1
- package/dist/index.js +5 -8
- 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 +37 -23
- 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/.gitignore +0 -221
- package/tests/vitest.global.setup.ts +0 -29
- package/tests/vitest.setup.ts +0 -24
- package/vitest.config.js +0 -18
package/src/rows.ts
CHANGED
|
@@ -125,7 +125,11 @@ export type PlatformSource = {
|
|
|
125
125
|
* a request to take a language from and throws in the sync. The years are resolved in the application
|
|
126
126
|
* timezone, so a period starting at midnight in Brussels does not fall back into the previous year.
|
|
127
127
|
*/
|
|
128
|
-
export function periodName(period: {
|
|
128
|
+
export function periodName(period: {
|
|
129
|
+
customName: string | null;
|
|
130
|
+
startDate: Date;
|
|
131
|
+
endDate: Date;
|
|
132
|
+
}): string {
|
|
129
133
|
const customName = period.customName?.trim();
|
|
130
134
|
if (customName) {
|
|
131
135
|
return customName;
|
|
@@ -162,7 +166,10 @@ export function flattenRegistrationPeriod(period: RegistrationPeriodSource): Sta
|
|
|
162
166
|
* Only the postal code and city of the address survive: the report maps units by postal code and
|
|
163
167
|
* lists them by city. An organization is a legal entity rather than a natural person.
|
|
164
168
|
*/
|
|
165
|
-
export function flattenOrganization(
|
|
169
|
+
export function flattenOrganization(
|
|
170
|
+
organization: OrganizationSource,
|
|
171
|
+
periodId: string,
|
|
172
|
+
): StatisticsRow {
|
|
166
173
|
return {
|
|
167
174
|
id: organization.id,
|
|
168
175
|
periodId,
|
|
@@ -242,7 +249,10 @@ export function flattenRegistration(registration: RegistrationSource): Statistic
|
|
|
242
249
|
* member has financiële ondersteuning or an active UITPAS and only the amount is kept, so the caller
|
|
243
250
|
* passes that same status here. It is what the reports split the lidgelden by.
|
|
244
251
|
*/
|
|
245
|
-
export function flattenMembership(
|
|
252
|
+
export function flattenMembership(
|
|
253
|
+
membership: MembershipSource,
|
|
254
|
+
reducedPrice: boolean,
|
|
255
|
+
): StatisticsRow {
|
|
246
256
|
return {
|
|
247
257
|
id: membership.id,
|
|
248
258
|
memberId: membership.memberId,
|
|
@@ -264,7 +274,10 @@ export function flattenMembership(membership: MembershipSource, reducedPrice: bo
|
|
|
264
274
|
* One row per period the record runs through: it carries a date range rather than a year, and a
|
|
265
275
|
* functie held for three years was held in each of them.
|
|
266
276
|
*/
|
|
267
|
-
export function flattenResponsibilityRecord(
|
|
277
|
+
export function flattenResponsibilityRecord(
|
|
278
|
+
record: ResponsibilityRecordSource,
|
|
279
|
+
periodId: string,
|
|
280
|
+
): StatisticsRow {
|
|
268
281
|
return {
|
|
269
282
|
id: record.id,
|
|
270
283
|
periodId,
|
|
@@ -318,8 +331,17 @@ export function defaultAgeGroupName(names: string[]): string {
|
|
|
318
331
|
return Formatter.joinLast(names, ', ', ' of ');
|
|
319
332
|
}
|
|
320
333
|
|
|
321
|
-
export function flattenDefaultAgeGroup(
|
|
322
|
-
|
|
334
|
+
export function flattenDefaultAgeGroup(
|
|
335
|
+
group: { id: string; names: string[]; minAge: number | null; maxAge: number | null },
|
|
336
|
+
periodId: string,
|
|
337
|
+
): StatisticsRow {
|
|
338
|
+
return {
|
|
339
|
+
id: group.id,
|
|
340
|
+
periodId,
|
|
341
|
+
name: defaultAgeGroupName(group.names),
|
|
342
|
+
minAge: group.minAge,
|
|
343
|
+
maxAge: group.maxAge,
|
|
344
|
+
};
|
|
323
345
|
}
|
|
324
346
|
|
|
325
347
|
function emptyToNull(value: string): string | null {
|
package/src/schema.test.ts
CHANGED
|
@@ -41,7 +41,9 @@ const allowedPersonalDataColumns = new Set([
|
|
|
41
41
|
]);
|
|
42
42
|
|
|
43
43
|
describe('migration.platform-statistics-schema', () => {
|
|
44
|
-
async function getColumns(): Promise<
|
|
44
|
+
async function getColumns(): Promise<
|
|
45
|
+
{ tableName: string; columnName: string; dataType: string }[]
|
|
46
|
+
> {
|
|
45
47
|
const [rows] = await getStatisticsConnection().select(
|
|
46
48
|
'SELECT TABLE_NAME as tableName, COLUMN_NAME as columnName, COLUMN_TYPE as dataType FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = ?',
|
|
47
49
|
[getStatisticsDatabaseConfig().DB_DATABASE],
|
|
@@ -51,7 +53,7 @@ describe('migration.platform-statistics-schema', () => {
|
|
|
51
53
|
}
|
|
52
54
|
|
|
53
55
|
it('creates every table the member reports read from', async () => {
|
|
54
|
-
const tables = new Set((await getColumns()).map(column => column.tableName));
|
|
56
|
+
const tables = new Set((await getColumns()).map((column) => column.tableName));
|
|
55
57
|
|
|
56
58
|
expect([...tables].sort()).toEqual([
|
|
57
59
|
'_organizations_organization_tags',
|
|
@@ -75,14 +77,20 @@ describe('migration.platform-statistics-schema', () => {
|
|
|
75
77
|
|
|
76
78
|
it('keeps members and registrations at their source grain, so a member can hold several registrations', async () => {
|
|
77
79
|
const columns = await getColumns();
|
|
78
|
-
const registrations = columns
|
|
80
|
+
const registrations = columns
|
|
81
|
+
.filter((column) => column.tableName === 'registrations')
|
|
82
|
+
.map((column) => column.columnName);
|
|
79
83
|
|
|
80
84
|
expect(registrations).toContain('memberId');
|
|
81
85
|
expect(registrations).toContain('groupId');
|
|
82
|
-
expect(
|
|
86
|
+
expect(
|
|
87
|
+
columns
|
|
88
|
+
.filter((column) => column.tableName === 'members')
|
|
89
|
+
.map((column) => column.columnName),
|
|
90
|
+
).toContain('id');
|
|
83
91
|
|
|
84
92
|
// Nothing is pre-aggregated: no counts are stored, every figure is computed at query time.
|
|
85
|
-
expect(columns.filter(column => /count$/i.test(column.columnName))).toEqual([]);
|
|
93
|
+
expect(columns.filter((column) => /count$/i.test(column.columnName))).toEqual([]);
|
|
86
94
|
});
|
|
87
95
|
|
|
88
96
|
async function compositeKeyTables(): Promise<string[]> {
|
|
@@ -91,7 +99,7 @@ describe('migration.platform-statistics-schema', () => {
|
|
|
91
99
|
[getStatisticsDatabaseConfig().DB_DATABASE, 'PRIMARY'],
|
|
92
100
|
{ nestTables: false },
|
|
93
101
|
);
|
|
94
|
-
return (rows as unknown as { tableName: string }[]).map(row => row.tableName).sort();
|
|
102
|
+
return (rows as unknown as { tableName: string }[]).map((row) => row.tableName).sort();
|
|
95
103
|
}
|
|
96
104
|
|
|
97
105
|
/**
|
|
@@ -118,7 +126,10 @@ describe('migration.platform-statistics-schema', () => {
|
|
|
118
126
|
{ nestTables: false },
|
|
119
127
|
);
|
|
120
128
|
|
|
121
|
-
expect((rows as unknown as { columnName: string }[]).map(row => row.columnName)).toEqual([
|
|
129
|
+
expect((rows as unknown as { columnName: string }[]).map((row) => row.columnName)).toEqual([
|
|
130
|
+
'id',
|
|
131
|
+
'periodId',
|
|
132
|
+
]);
|
|
122
133
|
});
|
|
123
134
|
|
|
124
135
|
/**
|
|
@@ -129,7 +140,7 @@ describe('migration.platform-statistics-schema', () => {
|
|
|
129
140
|
it('reconciles deletes only for the tables keyed on their id alone', async () => {
|
|
130
141
|
const composite = new Set(await compositeKeyTables());
|
|
131
142
|
|
|
132
|
-
expect(reconciledTables().filter(table => composite.has(table))).toEqual([]);
|
|
143
|
+
expect(reconciledTables().filter((table) => composite.has(table))).toEqual([]);
|
|
133
144
|
});
|
|
134
145
|
|
|
135
146
|
/**
|
|
@@ -144,12 +155,19 @@ describe('migration.platform-statistics-schema', () => {
|
|
|
144
155
|
);
|
|
145
156
|
const composite = new Set(await compositeKeyTables());
|
|
146
157
|
|
|
147
|
-
expect(
|
|
158
|
+
expect(
|
|
159
|
+
(rows as unknown as { tableName: string }[])
|
|
160
|
+
.map((row) => row.tableName)
|
|
161
|
+
.filter((table) => composite.has(table)),
|
|
162
|
+
).toEqual([]);
|
|
148
163
|
});
|
|
149
164
|
|
|
150
165
|
it('mirrors the column types of the main database', async () => {
|
|
151
166
|
const columns = await getColumns();
|
|
152
|
-
const typeOf = (tableName: string, columnName: string) =>
|
|
167
|
+
const typeOf = (tableName: string, columnName: string) =>
|
|
168
|
+
columns.find(
|
|
169
|
+
(column) => column.tableName === tableName && column.columnName === columnName,
|
|
170
|
+
)?.dataType;
|
|
153
171
|
|
|
154
172
|
expect(typeOf('members', 'id')).toBe('varchar(36)');
|
|
155
173
|
expect(typeOf('registrations', 'memberId')).toBe('varchar(36)');
|
|
@@ -175,7 +193,11 @@ describe('migration.platform-statistics-schema', () => {
|
|
|
175
193
|
*/
|
|
176
194
|
it('records when a locked period stopped being followed, next to the lock itself', async () => {
|
|
177
195
|
const columns = await getColumns();
|
|
178
|
-
const periodColumn = (columnName: string) =>
|
|
196
|
+
const periodColumn = (columnName: string) =>
|
|
197
|
+
columns.find(
|
|
198
|
+
(column) =>
|
|
199
|
+
column.tableName === 'registration_periods' && column.columnName === columnName,
|
|
200
|
+
)?.dataType;
|
|
179
201
|
|
|
180
202
|
expect(periodColumn('locked')).toBe('tinyint');
|
|
181
203
|
expect(periodColumn('lockedAt')).toBe('datetime');
|
|
@@ -187,7 +209,7 @@ describe('migration.platform-statistics-schema', () => {
|
|
|
187
209
|
return false;
|
|
188
210
|
}
|
|
189
211
|
const name = column.columnName.toLowerCase();
|
|
190
|
-
return personalDataColumns.some(forbidden => name.includes(forbidden));
|
|
212
|
+
return personalDataColumns.some((forbidden) => name.includes(forbidden));
|
|
191
213
|
});
|
|
192
214
|
|
|
193
215
|
expect(offending).toEqual([]);
|
|
@@ -195,7 +217,9 @@ describe('migration.platform-statistics-schema', () => {
|
|
|
195
217
|
});
|
|
196
218
|
|
|
197
219
|
describe('migration.postal-code-coordinates', () => {
|
|
198
|
-
async function getPostalCodes(): Promise<
|
|
220
|
+
async function getPostalCodes(): Promise<
|
|
221
|
+
{ postalCode: string; latitude: number; longitude: number }[]
|
|
222
|
+
> {
|
|
199
223
|
// Its own connection: the main database has a `postal_codes` table of its own, which is what
|
|
200
224
|
// the shared one would reach.
|
|
201
225
|
const [rows] = await getStatisticsConnection().select(
|
|
@@ -212,19 +236,24 @@ describe('migration.postal-code-coordinates', () => {
|
|
|
212
236
|
// Belgian postal codes run from 1000 to 9992. A handful are missing from any published list
|
|
213
237
|
// and the count shifts when bpost adds one, so this only guards against a partly loaded table.
|
|
214
238
|
expect(rows.length).toBeGreaterThan(1100);
|
|
215
|
-
expect(rows.every(row => /^\d{4}$/.test(row.postalCode))).toBe(true);
|
|
216
|
-
expect(new Set(rows.map(row => row.postalCode)).size).toBe(rows.length);
|
|
239
|
+
expect(rows.every((row) => /^\d{4}$/.test(row.postalCode))).toBe(true);
|
|
240
|
+
expect(new Set(rows.map((row) => row.postalCode)).size).toBe(rows.length);
|
|
217
241
|
});
|
|
218
242
|
|
|
219
243
|
it('places every point inside Belgium', async () => {
|
|
220
|
-
const outside = (await getPostalCodes()).filter(
|
|
221
|
-
row
|
|
244
|
+
const outside = (await getPostalCodes()).filter(
|
|
245
|
+
(row) =>
|
|
246
|
+
row.latitude < 49.4 ||
|
|
247
|
+
row.latitude > 51.6 ||
|
|
248
|
+
row.longitude < 2.5 ||
|
|
249
|
+
row.longitude > 6.5,
|
|
250
|
+
);
|
|
222
251
|
|
|
223
252
|
expect(outside).toEqual([]);
|
|
224
253
|
});
|
|
225
254
|
|
|
226
255
|
it('puts the largest cities where they belong', async () => {
|
|
227
|
-
const byPostalCode = new Map((await getPostalCodes()).map(row => [row.postalCode, row]));
|
|
256
|
+
const byPostalCode = new Map((await getPostalCodes()).map((row) => [row.postalCode, row]));
|
|
228
257
|
|
|
229
258
|
// Kilometres between the loaded point and where the city actually is. A postal code covers an
|
|
230
259
|
// area, so its centre is only ever near the city, not on it.
|
|
@@ -239,7 +268,7 @@ describe('migration.postal-code-coordinates', () => {
|
|
|
239
268
|
}
|
|
240
269
|
|
|
241
270
|
expect(distanceTo('1000', 50.85, 4.35)).toBeLessThan(5);
|
|
242
|
-
expect(distanceTo('2000', 51.22, 4.
|
|
271
|
+
expect(distanceTo('2000', 51.22, 4.4)).toBeLessThan(5);
|
|
243
272
|
expect(distanceTo('9000', 51.05, 3.72)).toBeLessThan(5);
|
|
244
273
|
expect(distanceTo('8000', 51.21, 3.22)).toBeLessThan(5);
|
|
245
274
|
expect(distanceTo('3500', 50.93, 5.34)).toBeLessThan(5);
|
package/src/schema.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
import { DatabaseInstance, Migration } from '@
|
|
1
|
+
import { DatabaseInstance, Migration } from '@stamhoofd/node-database';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
getStatisticsConnection,
|
|
5
|
+
getStatisticsDatabaseConfig,
|
|
6
|
+
getStatisticsPoolOptions,
|
|
7
|
+
} from './database.js';
|
|
4
8
|
|
|
5
9
|
export function getStatisticsMigrationsPath(): string {
|
|
6
10
|
return path.join(import.meta.dirname, 'migrations');
|
|
@@ -17,12 +21,18 @@ export async function runStatisticsMigrations(): Promise<void> {
|
|
|
17
21
|
const globalDatabase = new DatabaseInstance({ ...getStatisticsPoolOptions(), database: null });
|
|
18
22
|
|
|
19
23
|
try {
|
|
20
|
-
await globalDatabase.statement(
|
|
24
|
+
await globalDatabase.statement(
|
|
25
|
+
`CREATE DATABASE IF NOT EXISTS ${globalDatabase.escapeId(database)} DEFAULT CHARACTER SET = \`utf8mb4\` DEFAULT COLLATE = \`utf8mb4_0900_ai_ci\``,
|
|
26
|
+
);
|
|
21
27
|
} finally {
|
|
22
28
|
await globalDatabase.end();
|
|
23
29
|
}
|
|
24
30
|
|
|
25
|
-
if (
|
|
31
|
+
if (
|
|
32
|
+
!(await Migration.runAll(getStatisticsMigrationsPath(), {
|
|
33
|
+
database: getStatisticsConnection(),
|
|
34
|
+
}))
|
|
35
|
+
) {
|
|
26
36
|
throw new Error('Platform statistics migrations failed');
|
|
27
37
|
}
|
|
28
38
|
}
|
package/src/sync-state.test.ts
CHANGED
|
@@ -7,7 +7,9 @@ describe('nextWatermark', () => {
|
|
|
7
7
|
it('advances to the highest updatedAt it saw', () => {
|
|
8
8
|
const seen = [new Date(2026, 7, 1), new Date(2026, 7, 5), new Date(2026, 7, 3)];
|
|
9
9
|
|
|
10
|
-
expect(nextWatermark(new Date(2026, 6, 1), seen, runStartedAt)).toEqual(
|
|
10
|
+
expect(nextWatermark(new Date(2026, 6, 1), seen, runStartedAt)).toEqual(
|
|
11
|
+
new Date(2026, 7, 5),
|
|
12
|
+
);
|
|
11
13
|
});
|
|
12
14
|
|
|
13
15
|
it('keeps the previous watermark when nothing changed', () => {
|
package/src/sync-state.ts
CHANGED
|
@@ -18,7 +18,11 @@ export type SyncState = {
|
|
|
18
18
|
* be skipped. Capping costs one row that is read again on every run until the clock catches up, and
|
|
19
19
|
* repairs a watermark that a previous run already pushed into the future.
|
|
20
20
|
*/
|
|
21
|
-
export function nextWatermark(
|
|
21
|
+
export function nextWatermark(
|
|
22
|
+
previous: Date | null,
|
|
23
|
+
seen: Date[],
|
|
24
|
+
runStartedAt: Date,
|
|
25
|
+
): Date | null {
|
|
22
26
|
let watermark = previous;
|
|
23
27
|
|
|
24
28
|
for (const date of seen) {
|
|
@@ -42,14 +46,19 @@ export async function readSyncState(tableName: string): Promise<SyncState> {
|
|
|
42
46
|
return row ?? { tableName, watermark: null, lastSucceededAt: null, lastReconciledAt: null };
|
|
43
47
|
}
|
|
44
48
|
|
|
45
|
-
export async function writeSyncState(
|
|
49
|
+
export async function writeSyncState(
|
|
50
|
+
tableName: string,
|
|
51
|
+
state: Partial<Omit<SyncState, 'tableName'>>,
|
|
52
|
+
): Promise<void> {
|
|
46
53
|
const connection = getStatisticsConnection();
|
|
47
54
|
const columns = ['tableName', 'updatedAt', ...Object.keys(state)];
|
|
48
55
|
const values = [tableName, new Date(), ...Object.values(state)];
|
|
49
|
-
const assignments = columns
|
|
56
|
+
const assignments = columns
|
|
57
|
+
.filter((column) => column !== 'tableName')
|
|
58
|
+
.map((column) => `${connection.escapeId(column)} = new.${connection.escapeId(column)}`);
|
|
50
59
|
|
|
51
60
|
await connection.insert(
|
|
52
|
-
`INSERT INTO ${connection.escapeId(syncStateTable)} (${columns.map(column => connection.escapeId(column)).join(', ')}) VALUES (?) AS new ON DUPLICATE KEY UPDATE ${assignments.join(', ')}`,
|
|
61
|
+
`INSERT INTO ${connection.escapeId(syncStateTable)} (${columns.map((column) => connection.escapeId(column)).join(', ')}) VALUES (?) AS new ON DUPLICATE KEY UPDATE ${assignments.join(', ')}`,
|
|
53
62
|
[values],
|
|
54
63
|
);
|
|
55
64
|
}
|
package/src/sync.test.ts
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
import type { Organization, Registration, RegistrationPeriod } from '@stamhoofd/models';
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import {
|
|
3
|
+
GroupFactory,
|
|
4
|
+
Member,
|
|
5
|
+
MemberFactory,
|
|
6
|
+
MemberPlatformMembership,
|
|
7
|
+
OrganizationFactory,
|
|
8
|
+
OrganizationTagFactory,
|
|
9
|
+
Platform,
|
|
10
|
+
RegistrationFactory,
|
|
11
|
+
RegistrationPeriodFactory,
|
|
12
|
+
} from '@stamhoofd/models';
|
|
13
|
+
import {
|
|
14
|
+
BooleanStatus,
|
|
15
|
+
DefaultAgeGroup,
|
|
16
|
+
FinancialSupportSettings,
|
|
17
|
+
Gender,
|
|
18
|
+
PlatformMembershipType,
|
|
19
|
+
} from '@stamhoofd/structures';
|
|
4
20
|
import { getStatisticsConnection } from './database.js';
|
|
5
21
|
import { runStatisticsCycle, syncStatistics, syncStatisticsDeletes } from './sync.js';
|
|
6
22
|
import { readSyncState } from './sync-state.js';
|
|
@@ -33,7 +49,10 @@ describe('statistics sync', () => {
|
|
|
33
49
|
period = await new RegistrationPeriodFactory({}).create();
|
|
34
50
|
organization = await new OrganizationFactory({ period }).create();
|
|
35
51
|
const group = await new GroupFactory({ organization }).create();
|
|
36
|
-
member = await new MemberFactory({
|
|
52
|
+
member = await new MemberFactory({
|
|
53
|
+
organization,
|
|
54
|
+
birthDay: { year: 2011, month: 5, day: 17 },
|
|
55
|
+
}).create();
|
|
37
56
|
registration = await new RegistrationFactory({ member, group }).create();
|
|
38
57
|
|
|
39
58
|
await syncStatistics();
|
|
@@ -44,14 +63,25 @@ describe('statistics sync', () => {
|
|
|
44
63
|
|
|
45
64
|
expect(row).toBeDefined();
|
|
46
65
|
expect(row.birthDate).toEqual(new Date(Date.UTC(2011, 4, 17)));
|
|
47
|
-
expect(Object.keys(row).sort()).toEqual([
|
|
66
|
+
expect(Object.keys(row).sort()).toEqual([
|
|
67
|
+
'birthDate',
|
|
68
|
+
'createdAt',
|
|
69
|
+
'gender',
|
|
70
|
+
'id',
|
|
71
|
+
'lastRegisteredAt',
|
|
72
|
+
'organizationId',
|
|
73
|
+
'periodId',
|
|
74
|
+
'postalCode',
|
|
75
|
+
'source',
|
|
76
|
+
'updatedAt',
|
|
77
|
+
]);
|
|
48
78
|
expect(JSON.stringify(row)).not.toContain(member.details.firstName);
|
|
49
79
|
});
|
|
50
80
|
|
|
51
81
|
it('writes the member against the period their registration is in', async () => {
|
|
52
82
|
const rows = await statisticsRows('members', member.id);
|
|
53
83
|
|
|
54
|
-
expect(rows.map(row => row.periodId)).toContain(registration.periodId);
|
|
84
|
+
expect(rows.map((row) => row.periodId)).toContain(registration.periodId);
|
|
55
85
|
});
|
|
56
86
|
|
|
57
87
|
it('writes no member row for a member without a registration, since nothing counts one', async () => {
|
|
@@ -117,7 +147,11 @@ describe('statistics sync', () => {
|
|
|
117
147
|
*/
|
|
118
148
|
it('writes a tak with several names as words, not as a translation key', async () => {
|
|
119
149
|
const platform = await Platform.getForEditing();
|
|
120
|
-
const combined = DefaultAgeGroup.create({
|
|
150
|
+
const combined = DefaultAgeGroup.create({
|
|
151
|
+
names: ['Kapoenen', 'Welpen'],
|
|
152
|
+
minAge: 6,
|
|
153
|
+
maxAge: 10,
|
|
154
|
+
});
|
|
121
155
|
platform.config.defaultAgeGroups = [...platform.config.defaultAgeGroups, combined];
|
|
122
156
|
await platform.save();
|
|
123
157
|
await Platform.clearCache();
|
|
@@ -138,11 +172,19 @@ describe('statistics sync', () => {
|
|
|
138
172
|
it('writes the platform configuration against every period that is still open', async () => {
|
|
139
173
|
const now = new Date();
|
|
140
174
|
const periods = await statisticsRows('registration_periods');
|
|
141
|
-
const open = new Set(
|
|
142
|
-
|
|
175
|
+
const open = new Set(
|
|
176
|
+
periods
|
|
177
|
+
.filter(
|
|
178
|
+
(row) => (row.cutoffAt === null || row.cutoffAt > now) && row.lockedAt === null,
|
|
179
|
+
)
|
|
180
|
+
.map((row) => row.id),
|
|
181
|
+
);
|
|
182
|
+
const written = new Set(
|
|
183
|
+
(await statisticsRows('default_age_groups', ageGroup.id)).map((row) => row.periodId),
|
|
184
|
+
);
|
|
143
185
|
|
|
144
186
|
expect(open.size).toBeGreaterThan(0);
|
|
145
|
-
expect([...open].filter(periodId => !written.has(periodId))).toEqual([]);
|
|
187
|
+
expect([...open].filter((periodId) => !written.has(periodId))).toEqual([]);
|
|
146
188
|
});
|
|
147
189
|
|
|
148
190
|
it('records a watermark it can resume from', async () => {
|
|
@@ -158,7 +200,9 @@ describe('statistics sync', () => {
|
|
|
158
200
|
|
|
159
201
|
await syncStatistics();
|
|
160
202
|
|
|
161
|
-
expect((await statisticsRows('members', member.id))[0].birthDate).toEqual(
|
|
203
|
+
expect((await statisticsRows('members', member.id))[0].birthDate).toEqual(
|
|
204
|
+
new Date(Date.UTC(2009, 2, 3)),
|
|
205
|
+
);
|
|
162
206
|
});
|
|
163
207
|
|
|
164
208
|
it('writes the same rows again without complaining, so a repeated run is harmless', async () => {
|
|
@@ -188,7 +232,10 @@ describe('statistics sync', () => {
|
|
|
188
232
|
|
|
189
233
|
it('keeps a member whose source row is gone, because deleting one cascades into settled years', async () => {
|
|
190
234
|
const orphan = await new MemberFactory({ organization }).create();
|
|
191
|
-
await new RegistrationFactory({
|
|
235
|
+
await new RegistrationFactory({
|
|
236
|
+
member: orphan,
|
|
237
|
+
group: await new GroupFactory({ organization }).create(),
|
|
238
|
+
}).create();
|
|
192
239
|
await syncStatistics();
|
|
193
240
|
await orphan.delete();
|
|
194
241
|
|
|
@@ -230,11 +277,17 @@ describe('statistics sync', () => {
|
|
|
230
277
|
settledMember.details.gender = Gender.Female;
|
|
231
278
|
await settledMember.save();
|
|
232
279
|
await new RegistrationFactory({ member: settledMember, group }).create();
|
|
233
|
-
await new RegistrationFactory({
|
|
280
|
+
await new RegistrationFactory({
|
|
281
|
+
member: settledMember,
|
|
282
|
+
group: await new GroupFactory({ organization }).create(),
|
|
283
|
+
}).create();
|
|
234
284
|
|
|
235
285
|
await syncStatistics();
|
|
236
286
|
// Settle the period, as an operator would once its numbers are final.
|
|
237
|
-
await getStatisticsConnection().update(
|
|
287
|
+
await getStatisticsConnection().update(
|
|
288
|
+
'UPDATE `registration_periods` SET `cutoffAt` = ? WHERE `id` = ?',
|
|
289
|
+
[new Date(Date.now() - 1000), frozenPeriod.id],
|
|
290
|
+
);
|
|
238
291
|
});
|
|
239
292
|
|
|
240
293
|
/**
|
|
@@ -242,7 +295,13 @@ describe('statistics sync', () => {
|
|
|
242
295
|
* a settled year has to keep the ones it was counted with.
|
|
243
296
|
*/
|
|
244
297
|
it('keeps the member as they were counted, while the year still running follows the source', async () => {
|
|
245
|
-
const genderPerPeriod = async () =>
|
|
298
|
+
const genderPerPeriod = async () =>
|
|
299
|
+
Object.fromEntries(
|
|
300
|
+
(await statisticsRows('members', settledMember.id)).map((row) => [
|
|
301
|
+
row.periodId,
|
|
302
|
+
row.gender,
|
|
303
|
+
]),
|
|
304
|
+
);
|
|
246
305
|
const before = await genderPerPeriod();
|
|
247
306
|
|
|
248
307
|
settledMember.details.gender = Gender.Male;
|
|
@@ -261,22 +320,32 @@ describe('statistics sync', () => {
|
|
|
261
320
|
*/
|
|
262
321
|
it('keeps the unit and the tak named as they were counted, while the year still running follows the source', async () => {
|
|
263
322
|
const nameOf = async (table: string, id: string) =>
|
|
264
|
-
Object.fromEntries(
|
|
323
|
+
Object.fromEntries(
|
|
324
|
+
(await statisticsRows(table, id)).map((row) => [row.periodId, row.name]),
|
|
325
|
+
);
|
|
265
326
|
|
|
266
327
|
const ageGroupId = ageGroup.id;
|
|
267
|
-
const before = {
|
|
328
|
+
const before = {
|
|
329
|
+
unit: await nameOf('organizations', organization.id),
|
|
330
|
+
tak: await nameOf('default_age_groups', ageGroupId),
|
|
331
|
+
};
|
|
268
332
|
|
|
269
333
|
organization.name = 'Hernoemde eenheid';
|
|
270
334
|
await organization.save();
|
|
271
335
|
|
|
272
336
|
const platform = await Platform.getForEditing();
|
|
273
|
-
platform.config.defaultAgeGroups.find(group => group.id === ageGroupId)!.names = [
|
|
337
|
+
platform.config.defaultAgeGroups.find((group) => group.id === ageGroupId)!.names = [
|
|
338
|
+
'Hernoemde tak',
|
|
339
|
+
];
|
|
274
340
|
await platform.save();
|
|
275
341
|
await Platform.clearCache();
|
|
276
342
|
|
|
277
343
|
await syncStatistics();
|
|
278
344
|
|
|
279
|
-
const after = {
|
|
345
|
+
const after = {
|
|
346
|
+
unit: await nameOf('organizations', organization.id),
|
|
347
|
+
tak: await nameOf('default_age_groups', ageGroupId),
|
|
348
|
+
};
|
|
280
349
|
expect(after.unit[frozenPeriod.id]).toBe(before.unit[frozenPeriod.id]);
|
|
281
350
|
expect(after.tak[frozenPeriod.id]).toBe(before.tak[frozenPeriod.id]);
|
|
282
351
|
expect(after.unit[organization.periodId]).toBe('Hernoemde eenheid');
|
|
@@ -288,7 +357,9 @@ describe('statistics sync', () => {
|
|
|
288
357
|
await frozenRegistration.save();
|
|
289
358
|
await syncStatistics();
|
|
290
359
|
|
|
291
|
-
expect(
|
|
360
|
+
expect(
|
|
361
|
+
(await statisticsRows('registrations', frozenRegistration.id))[0].deactivatedAt,
|
|
362
|
+
).toBeNull();
|
|
292
363
|
});
|
|
293
364
|
|
|
294
365
|
it('keeps its rows when the source is deleted, so the settled numbers stay', async () => {
|
|
@@ -318,8 +389,13 @@ describe('statistics sync', () => {
|
|
|
318
389
|
* night it first sees the lock. Ends in a month unless told otherwise, so unlocking it again
|
|
319
390
|
* falls inside the window that reopens a period.
|
|
320
391
|
*/
|
|
321
|
-
async function lockedYear(
|
|
322
|
-
|
|
392
|
+
async function lockedYear(
|
|
393
|
+
endDate = new Date(Date.now() + 30 * day),
|
|
394
|
+
): Promise<{ period: RegistrationPeriod; registration: Registration }> {
|
|
395
|
+
const period = await new RegistrationPeriodFactory({
|
|
396
|
+
startDate: new Date(endDate.getTime() - 365 * day),
|
|
397
|
+
endDate,
|
|
398
|
+
}).create();
|
|
323
399
|
const group = await new GroupFactory({ organization }).create();
|
|
324
400
|
group.periodId = period.id;
|
|
325
401
|
await group.save();
|
|
@@ -440,7 +516,10 @@ describe('statistics sync', () => {
|
|
|
440
516
|
await syncStatistics();
|
|
441
517
|
|
|
442
518
|
const links = await statisticsRows('_organizations_organization_tags');
|
|
443
|
-
const link = links.find(
|
|
519
|
+
const link = links.find(
|
|
520
|
+
(row) =>
|
|
521
|
+
row.organizationsId === organization.id && row.organizationTagsId === tag.id,
|
|
522
|
+
);
|
|
444
523
|
expect(link).toBeDefined();
|
|
445
524
|
expect(link!.periodId).toBe(organization.periodId);
|
|
446
525
|
});
|
|
@@ -458,7 +537,10 @@ describe('statistics sync', () => {
|
|
|
458
537
|
'INSERT INTO `_organizations_organization_tags` (`organizationsId`, `organizationTagsId`, `periodId`, `source`) VALUES (?, ?, ?, ?)',
|
|
459
538
|
[organization.id, tag.id, settledPeriod.id, 'sync'],
|
|
460
539
|
);
|
|
461
|
-
await connection.update(
|
|
540
|
+
await connection.update(
|
|
541
|
+
'UPDATE `registration_periods` SET `cutoffAt` = ? WHERE `id` = ?',
|
|
542
|
+
[new Date(Date.now() - 1000), settledPeriod.id],
|
|
543
|
+
);
|
|
462
544
|
|
|
463
545
|
// The organization moves to another netwerk entirely.
|
|
464
546
|
const moved = await new OrganizationTagFactory({}).create();
|
|
@@ -467,8 +549,18 @@ describe('statistics sync', () => {
|
|
|
467
549
|
await syncStatistics();
|
|
468
550
|
|
|
469
551
|
const links = await statisticsRows('_organizations_organization_tags');
|
|
470
|
-
expect(
|
|
471
|
-
|
|
552
|
+
expect(
|
|
553
|
+
links.some(
|
|
554
|
+
(row) => row.periodId === settledPeriod.id && row.organizationTagsId === tag.id,
|
|
555
|
+
),
|
|
556
|
+
).toBe(true);
|
|
557
|
+
expect(
|
|
558
|
+
links.some(
|
|
559
|
+
(row) =>
|
|
560
|
+
row.periodId === organization.periodId &&
|
|
561
|
+
row.organizationTagsId === moved.id,
|
|
562
|
+
),
|
|
563
|
+
).toBe(true);
|
|
472
564
|
});
|
|
473
565
|
});
|
|
474
566
|
|
|
@@ -479,7 +571,10 @@ describe('statistics sync', () => {
|
|
|
479
571
|
* same status off the member, so a lidgeld carries the tarief it was made at.
|
|
480
572
|
*/
|
|
481
573
|
describe('the tarief of a lidgeld', () => {
|
|
482
|
-
async function membershipOf(
|
|
574
|
+
async function membershipOf(
|
|
575
|
+
member: Member,
|
|
576
|
+
type: PlatformMembershipType,
|
|
577
|
+
): Promise<MemberPlatformMembership> {
|
|
483
578
|
const membership = new MemberPlatformMembership();
|
|
484
579
|
membership.memberId = member.id;
|
|
485
580
|
membership.membershipTypeId = type.id;
|
|
@@ -495,7 +590,9 @@ describe('statistics sync', () => {
|
|
|
495
590
|
const type = PlatformMembershipType.create({ name: 'Volledig scoutsjaar' });
|
|
496
591
|
const platform = await Platform.getForEditing();
|
|
497
592
|
platform.config.membershipTypes = [type];
|
|
498
|
-
platform.config.financialSupport = FinancialSupportSettings.create({
|
|
593
|
+
platform.config.financialSupport = FinancialSupportSettings.create({
|
|
594
|
+
priceName: 'SOMkort',
|
|
595
|
+
});
|
|
499
596
|
await platform.save();
|
|
500
597
|
await Platform.clearCache();
|
|
501
598
|
|
|
@@ -508,8 +605,12 @@ describe('statistics sync', () => {
|
|
|
508
605
|
|
|
509
606
|
await syncStatistics();
|
|
510
607
|
|
|
511
|
-
expect(
|
|
512
|
-
|
|
608
|
+
expect(
|
|
609
|
+
(await statisticsRows('member_platform_memberships', standard.id))[0].reducedPrice,
|
|
610
|
+
).toBe(0);
|
|
611
|
+
expect(
|
|
612
|
+
(await statisticsRows('member_platform_memberships', reduced.id))[0].reducedPrice,
|
|
613
|
+
).toBe(1);
|
|
513
614
|
});
|
|
514
615
|
|
|
515
616
|
/**
|
|
@@ -518,13 +619,17 @@ describe('statistics sync', () => {
|
|
|
518
619
|
*/
|
|
519
620
|
it('writes what the platform calls its verlaagd tarief', async () => {
|
|
520
621
|
const platform = await Platform.getForEditing();
|
|
521
|
-
platform.config.financialSupport = FinancialSupportSettings.create({
|
|
622
|
+
platform.config.financialSupport = FinancialSupportSettings.create({
|
|
623
|
+
priceName: 'SOMkort',
|
|
624
|
+
});
|
|
522
625
|
await platform.save();
|
|
523
626
|
await Platform.clearCache();
|
|
524
627
|
|
|
525
628
|
await syncStatistics();
|
|
526
629
|
|
|
527
|
-
expect((await statisticsRows('platform', platform.id))[0].reducedPriceName).toBe(
|
|
630
|
+
expect((await statisticsRows('platform', platform.id))[0].reducedPriceName).toBe(
|
|
631
|
+
'SOMkort',
|
|
632
|
+
);
|
|
528
633
|
});
|
|
529
634
|
});
|
|
530
635
|
|
|
@@ -534,7 +639,15 @@ describe('statistics sync', () => {
|
|
|
534
639
|
await connection.delete('DELETE FROM `members` WHERE `id` = ?', ['imported-member-1']);
|
|
535
640
|
await connection.insert(
|
|
536
641
|
'INSERT INTO `members` (`id`, `periodId`, `birthDate`, `gender`, `createdAt`, `updatedAt`, `source`) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
|
537
|
-
[
|
|
642
|
+
[
|
|
643
|
+
'imported-member-1',
|
|
644
|
+
period.id,
|
|
645
|
+
new Date(Date.UTC(2004, 0, 15)),
|
|
646
|
+
'Female',
|
|
647
|
+
new Date(),
|
|
648
|
+
new Date(),
|
|
649
|
+
'import',
|
|
650
|
+
],
|
|
538
651
|
);
|
|
539
652
|
|
|
540
653
|
await syncStatisticsDeletes();
|