-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsService.js
More file actions
210 lines (191 loc) · 6.61 KB
/
Copy pathSettingsService.js
File metadata and controls
210 lines (191 loc) · 6.61 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
* Application service for user defaults and ride settings snapshots.
*/
export class SettingsService {
static PARTICIPATION_NOTIFICATION_LEVELS = Object.freeze({
ALL: 'all',
MEMBERSHIP: 'membership'
});
/**
* @param {import('../storage/interface.js').StorageInterface} storage
*/
constructor(storage) {
this.storage = storage;
}
/**
* @returns {{notifyParticipation: boolean, allowReposts: boolean}}
*/
static getSystemRideDefaults() {
return {
notifyParticipation: true,
allowReposts: false
};
}
/**
* @param {Object} [baseSettings={}]
* @param {Object} [overrideSettings={}]
* @returns {{notifyParticipation: boolean, allowReposts: boolean}}
*/
static buildRideSettingsSnapshot(baseSettings = {}, overrideSettings = {}) {
return {
...SettingsService.getSystemRideDefaults(),
...(baseSettings || {}),
...(overrideSettings || {})
};
}
/**
* @param {Object|null} user
* @returns {{notifyParticipation: boolean, allowReposts: boolean}}
*/
static getEffectiveUserRideDefaults(user) {
return SettingsService.buildRideSettingsSnapshot(user?.settings?.rideDefaults);
}
/**
* @param {string|undefined} value
* @returns {'all'|'membership'}
*/
static resolveParticipationNotificationLevel(value) {
return value === SettingsService.PARTICIPATION_NOTIFICATION_LEVELS.MEMBERSHIP
? SettingsService.PARTICIPATION_NOTIFICATION_LEVELS.MEMBERSHIP
: SettingsService.PARTICIPATION_NOTIFICATION_LEVELS.ALL;
}
/**
* @param {Object} [input={}]
* @returns {{notifyParticipation?: boolean, allowReposts?: boolean}}
*/
static extractExplicitRideSettings(input = {}) {
return { ...(input.settings || {}) };
}
/**
* Resolve effective ride settings from a ride-like object.
*
* @param {Object} [ride={}]
* @returns {{notifyParticipation: boolean, allowReposts: boolean}}
*/
static getRideSettingsSnapshot(ride = {}) {
const explicitSettings = SettingsService.extractExplicitRideSettings(ride);
return SettingsService.buildRideSettingsSnapshot({}, explicitSettings);
}
/**
* Resolve the next persisted ride settings snapshot for an update operation.
*
* @param {Object} currentRide
* @param {Object} [updates={}]
* @returns {{notifyParticipation: boolean, allowReposts: boolean}}
*/
static resolveUpdatedRideSettings(currentRide, updates = {}) {
return SettingsService.buildRideSettingsSnapshot(
SettingsService.getRideSettingsSnapshot(currentRide),
SettingsService.extractExplicitRideSettings(updates)
);
}
/**
* @param {number} userId
* @returns {Promise<{notifyParticipation: boolean, allowReposts: boolean}>}
*/
async getUserRideDefaults(userId) {
const existingUser = await this.storage.getUser(userId);
return SettingsService.getEffectiveUserRideDefaults(existingUser);
}
/**
* Read the live participation notification preference without creating a user.
* @param {number} userId
* @returns {Promise<'all'|'membership'>}
*/
async getParticipationNotificationLevel(userId) {
const user = await this.storage.getUser(userId);
return SettingsService.resolveParticipationNotificationLevel(
user?.settings?.participationNotificationLevel
);
}
/**
* @param {import('../models/UserProfile.js').UserProfile} userProfile
* @param {'all'|'membership'} level
* @returns {Promise<import('../storage/interface.js').UserEntity>}
*/
async updateParticipationNotificationLevel(userProfile, level) {
const resolvedLevel = SettingsService.resolveParticipationNotificationLevel(level);
if (resolvedLevel !== level) {
throw new Error(`Unsupported participation notification level: ${level}`);
}
const existingUser = await this.storage.getUser(userProfile.userId);
return this.storage.upsertUser({
userId: userProfile.userId,
username: userProfile.username,
firstName: userProfile.firstName,
lastName: userProfile.lastName,
settings: {
...(existingUser?.settings || {}),
participationNotificationLevel: level
}
});
}
/**
* Ensure the user exists with persisted defaults.
*
* @param {import('../models/UserProfile.js').UserProfile} userProfile
* @returns {Promise<import('../storage/interface.js').UserEntity>}
*/
async ensureUserWithRideDefaults(userProfile) {
const existingUser = await this.storage.getUser(userProfile.userId);
if (existingUser?.settings?.rideDefaults) {
return existingUser;
}
return this.storage.upsertUser({
userId: userProfile.userId,
username: userProfile.username,
firstName: userProfile.firstName,
lastName: userProfile.lastName,
settings: {
...(existingUser?.settings || {}),
rideDefaults: SettingsService.getEffectiveUserRideDefaults(existingUser)
}
});
}
/**
* Update persisted user ride defaults, creating the user record if needed.
*
* @param {import('../models/UserProfile.js').UserProfile} userProfile
* @param {Object} rideDefaultsPatch
* @returns {Promise<import('../storage/interface.js').UserEntity>}
*/
async updateUserRideDefaults(userProfile, rideDefaultsPatch) {
const existingUser = await this.storage.getUser(userProfile.userId);
const mergedRideDefaults = SettingsService.buildRideSettingsSnapshot(
existingUser?.settings?.rideDefaults,
rideDefaultsPatch
);
return this.storage.upsertUser({
userId: userProfile.userId,
username: userProfile.username,
firstName: userProfile.firstName,
lastName: userProfile.lastName,
settings: {
...(existingUser?.settings || {}),
rideDefaults: mergedRideDefaults
}
});
}
/**
* Resolve explicit settings for a new ride snapshot and materialize the user when required.
*
* @param {Object} params
* @param {import('../models/UserProfile.js').UserProfile|null} [params.creatorProfile]
* @param {Object} [params.input]
* @returns {Promise<{notifyParticipation: boolean, allowReposts: boolean}>}
*/
async resolveCreateRideSettings({ creatorProfile = null, input = {} } = {}) {
const explicitRideSettings = SettingsService.extractExplicitRideSettings(input);
if (!creatorProfile) {
return SettingsService.buildRideSettingsSnapshot(
SettingsService.getSystemRideDefaults(),
explicitRideSettings
);
}
const creatorUser = await this.ensureUserWithRideDefaults(creatorProfile);
return SettingsService.buildRideSettingsSnapshot(
SettingsService.getEffectiveUserRideDefaults(creatorUser),
explicitRideSettings
);
}
}