-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsync-service.ts
106 lines (94 loc) · 3.01 KB
/
sync-service.ts
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* SudoSOS back-end API service.
* Copyright (C) 2024 Study association GEWIS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @license
*/
/**
* This is the module page of the abstract sync-service.
*
* @module internal/sync-service
*/
import WithManager from '../../database/with-manager';
export interface SyncResult {
skipped: boolean;
result: boolean;
}
/**
* SyncService interface.
*
* SyncService is the abstract class which is used to sync entity data.
* This can be used to integrate external data sources into the SudoSOS back-end.
*/
export abstract class SyncService<T> extends WithManager {
/**
* Guard determines whether the entity should be synced using this sync service.
*
* Not passing the guard will result in the user being skipped.
* A skipped sync does not count as a failure.
*
* @param entity The entity to check.
* @returns {Promise<boolean>} True if the entity should be synced, false otherwise.
*/
abstract guard(entity: T): Promise<boolean>;
/**
* Up is a wrapper around `sync` that handles the guard.
*
* @param entity
*
* @returns {Promise<SyncResult>} The result of the sync.
*/
async up(entity: T): Promise<SyncResult> {
const guardResult = await this.guard(entity);
if (!guardResult) return { skipped: true, result: false };
const result = await this.sync(entity);
return { skipped: false, result };
}
/**
* Synchronizes the user data with the external data source.
*
* @param entity The user to synchronize.
* @returns {Promise<boolean>} True if the user was synchronized, false otherwise.
*/
protected abstract sync(entity: T): Promise<boolean>;
/**
* Fetches the user data from the external data source.
* `sync` can be seen as a `push` and `fetch` as a `pull`.
*
*/
abstract fetch(): Promise<void>;
/**
* Down is called when the SyncService decides that the entity is no longer connected to this sync service be removed.
* This can be used to remove the entity from the database or clean up entities.
*
* This should be revertible and idempotent!
*
* @param entity
*/
abstract down(entity: T): Promise<void>;
/**
* Called before a sync batch is started.
*/
pre(): Promise<void> {
return Promise.resolve();
}
/**
* Called after a sync batch is finished.
*/
post(): Promise<void> {
return Promise.resolve();
}
}