@@ -87,23 +87,32 @@ export const getFeatureFlags = async (req: Request, res: Response) => {
87
87
const result : Record < string , boolean > = { } ;
88
88
89
89
// get enabled flags
90
- const enabledFlags = Flags . list . filter ( ( record ) => record . status && record . coverage > 0 ) ;
90
+ const enabledFlags = Flags . list . filter ( ( record ) => record . status ) ;
91
91
92
- for await ( const flag of enabledFlags ) {
92
+ for ( const flag of enabledFlags ) {
93
+ // don’t provide flag if installation is not provided
93
94
if ( installation . length === 0 ) continue ;
94
95
96
+ // target app flag
95
97
if ( flag . availability === 'app' ) {
98
+ // if the coverage is 100, then the flag is always enabled
96
99
if ( flag . coverage === 100 ) {
97
100
result [ flag . name ] = flag . status ;
98
101
continue ;
99
102
}
100
103
104
+ if ( flag . coverage === 0 ) {
105
+ continue ;
106
+ }
107
+
101
108
const findInstallation = flag . installations . find ( ( rec ) => rec . id === installation ) ;
102
109
110
+ // if installation is included in the flag installations, then the flag is enabled
103
111
if ( findInstallation ) {
104
112
result [ flag . name ] = flag . status ;
105
113
}
106
114
115
+ // if the installation is not included in the flag installations, then check the coverage
107
116
if ( ! findInstallation ) {
108
117
const currentCount = flag . installations . length ;
109
118
const currentAvg = ( currentCount * 100 ) / installationsCount ;
@@ -123,22 +132,27 @@ export const getFeatureFlags = async (req: Request, res: Response) => {
123
132
continue ;
124
133
}
125
134
135
+ // get user associated with the installation
126
136
const findInstallation = Installation . find ( installation ) ;
127
137
userId = userId || findInstallation ?. user ;
128
138
139
+ // target congregation flag
129
140
if ( flag . availability === 'congregation' && userId ) {
130
141
const user = UsersList . findById ( userId ) ;
131
142
const congId = user ?. profile . congregation ?. id ;
132
143
const cong = congId ? CongregationsList . findById ( congId ) : undefined ;
133
144
145
+ // if the user is not associated with a congregation, skip the flag
134
146
if ( ! cong ) continue ;
135
147
136
148
const ownFlag = cong . flags . find ( ( record ) => record === flag . id ) ;
137
149
150
+ // if the congregation already has the flag, set it to true
138
151
if ( ownFlag ) {
139
152
result [ flag . name ] = true ;
140
153
}
141
154
155
+ // if the congregation does not have the flag, check the coverage
142
156
if ( ! ownFlag ) {
143
157
if ( flag . coverage === 100 ) {
144
158
result [ flag . name ] = true ;
@@ -149,7 +163,7 @@ export const getFeatureFlags = async (req: Request, res: Response) => {
149
163
await cong . saveFlags ( flags ) ;
150
164
}
151
165
152
- if ( flag . coverage < 100 ) {
166
+ if ( flag . coverage > 0 && flag . coverage < 100 ) {
153
167
const currentCount = CongregationsList . list . filter ( ( record ) => record . flags . some ( ( f ) => f === flag . id ) ) . length ;
154
168
const currentAvg = ( currentCount * 100 ) / congregationsCount ;
155
169
@@ -165,17 +179,21 @@ export const getFeatureFlags = async (req: Request, res: Response) => {
165
179
}
166
180
}
167
181
182
+ // target user flag
168
183
if ( flag . availability === 'user' && userId ) {
169
184
const user = UsersList . findById ( userId ) ;
170
185
186
+ // if the user is not found, skip the flag
171
187
if ( ! user ) continue ;
172
188
173
189
const ownFlag = user . flags . find ( ( record ) => record === flag . id ) ;
174
190
191
+ // if the user already has the flag, set it to true
175
192
if ( ownFlag ) {
176
193
result [ flag . name ] = true ;
177
194
}
178
195
196
+ // if the user does not have the flag, check the coverage
179
197
if ( ! ownFlag ) {
180
198
if ( flag . coverage === 100 ) {
181
199
result [ flag . name ] = true ;
@@ -186,7 +204,7 @@ export const getFeatureFlags = async (req: Request, res: Response) => {
186
204
await user . updateFlags ( flags ) ;
187
205
}
188
206
189
- if ( flag . coverage < 100 ) {
207
+ if ( flag . coverage > 0 && flag . coverage < 100 ) {
190
208
const currentCount = UsersList . list . filter ( ( record ) => record . flags . some ( ( f ) => f === flag . id ) ) . length ;
191
209
const currentAvg = ( currentCount * 100 ) / usersCount ;
192
210
@@ -206,18 +224,21 @@ export const getFeatureFlags = async (req: Request, res: Response) => {
206
224
// update installation
207
225
const findInstallation = Installation . find ( installation ) ;
208
226
227
+ // if the installation is not found and userId is provided, link the installation to the user
209
228
if ( ! findInstallation && userId ) {
210
229
Installation . linked . push ( { user : userId , installations : [ { id : installation , registered : new Date ( ) . toISOString ( ) } ] } ) ;
211
230
await Installation . save ( ) ;
212
231
}
213
232
233
+ // if the installation is not found and userId is not provided, add it to pending installations
214
234
if ( ! findInstallation && ! userId ) {
215
235
Installation . pending . push ( { id : installation , registered : new Date ( ) . toISOString ( ) } ) ;
216
236
await Installation . save ( ) ;
217
237
}
218
238
239
+ // if the installation is found and its status is pending and userId is provided, link the installation to the user
219
240
if ( findInstallation ?. status === 'pending' && userId ) {
220
- Installation . pending . filter ( ( record ) => record . id !== installation ) ;
241
+ Installation . pending = Installation . pending . filter ( ( record ) => record . id !== installation ) ;
221
242
Installation . linked . push ( { user : userId , installations : [ { id : installation , registered : new Date ( ) . toISOString ( ) } ] } ) ;
222
243
await Installation . save ( ) ;
223
244
}
0 commit comments