Skip to content

Commit c320eca

Browse files
authored
fix(controllers): check for enabled flags before coverage check
1 parent 16c562d commit c320eca

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/v3/controllers/public_controller.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,32 @@ export const getFeatureFlags = async (req: Request, res: Response) => {
8787
const result: Record<string, boolean> = {};
8888

8989
// get enabled flags
90-
const enabledFlags = Flags.list.filter((record) => record.status && record.coverage > 0);
90+
const enabledFlags = Flags.list.filter((record) => record.status);
9191

92-
for await (const flag of enabledFlags) {
92+
for (const flag of enabledFlags) {
93+
// don’t provide flag if installation is not provided
9394
if (installation.length === 0) continue;
9495

96+
// target app flag
9597
if (flag.availability === 'app') {
98+
// if the coverage is 100, then the flag is always enabled
9699
if (flag.coverage === 100) {
97100
result[flag.name] = flag.status;
98101
continue;
99102
}
100103

104+
if (flag.coverage === 0) {
105+
continue;
106+
}
107+
101108
const findInstallation = flag.installations.find((rec) => rec.id === installation);
102109

110+
// if installation is included in the flag installations, then the flag is enabled
103111
if (findInstallation) {
104112
result[flag.name] = flag.status;
105113
}
106114

115+
// if the installation is not included in the flag installations, then check the coverage
107116
if (!findInstallation) {
108117
const currentCount = flag.installations.length;
109118
const currentAvg = (currentCount * 100) / installationsCount;
@@ -123,22 +132,27 @@ export const getFeatureFlags = async (req: Request, res: Response) => {
123132
continue;
124133
}
125134

135+
// get user associated with the installation
126136
const findInstallation = Installation.find(installation);
127137
userId = userId || findInstallation?.user;
128138

139+
// target congregation flag
129140
if (flag.availability === 'congregation' && userId) {
130141
const user = UsersList.findById(userId);
131142
const congId = user?.profile.congregation?.id;
132143
const cong = congId ? CongregationsList.findById(congId) : undefined;
133144

145+
// if the user is not associated with a congregation, skip the flag
134146
if (!cong) continue;
135147

136148
const ownFlag = cong.flags.find((record) => record === flag.id);
137149

150+
// if the congregation already has the flag, set it to true
138151
if (ownFlag) {
139152
result[flag.name] = true;
140153
}
141154

155+
// if the congregation does not have the flag, check the coverage
142156
if (!ownFlag) {
143157
if (flag.coverage === 100) {
144158
result[flag.name] = true;
@@ -149,7 +163,7 @@ export const getFeatureFlags = async (req: Request, res: Response) => {
149163
await cong.saveFlags(flags);
150164
}
151165

152-
if (flag.coverage < 100) {
166+
if (flag.coverage > 0 && flag.coverage < 100) {
153167
const currentCount = CongregationsList.list.filter((record) => record.flags.some((f) => f === flag.id)).length;
154168
const currentAvg = (currentCount * 100) / congregationsCount;
155169

@@ -165,17 +179,21 @@ export const getFeatureFlags = async (req: Request, res: Response) => {
165179
}
166180
}
167181

182+
// target user flag
168183
if (flag.availability === 'user' && userId) {
169184
const user = UsersList.findById(userId);
170185

186+
// if the user is not found, skip the flag
171187
if (!user) continue;
172188

173189
const ownFlag = user.flags.find((record) => record === flag.id);
174190

191+
// if the user already has the flag, set it to true
175192
if (ownFlag) {
176193
result[flag.name] = true;
177194
}
178195

196+
// if the user does not have the flag, check the coverage
179197
if (!ownFlag) {
180198
if (flag.coverage === 100) {
181199
result[flag.name] = true;
@@ -186,7 +204,7 @@ export const getFeatureFlags = async (req: Request, res: Response) => {
186204
await user.updateFlags(flags);
187205
}
188206

189-
if (flag.coverage < 100) {
207+
if (flag.coverage > 0 && flag.coverage < 100) {
190208
const currentCount = UsersList.list.filter((record) => record.flags.some((f) => f === flag.id)).length;
191209
const currentAvg = (currentCount * 100) / usersCount;
192210

@@ -206,18 +224,21 @@ export const getFeatureFlags = async (req: Request, res: Response) => {
206224
// update installation
207225
const findInstallation = Installation.find(installation);
208226

227+
// if the installation is not found and userId is provided, link the installation to the user
209228
if (!findInstallation && userId) {
210229
Installation.linked.push({ user: userId, installations: [{ id: installation, registered: new Date().toISOString() }] });
211230
await Installation.save();
212231
}
213232

233+
// if the installation is not found and userId is not provided, add it to pending installations
214234
if (!findInstallation && !userId) {
215235
Installation.pending.push({ id: installation, registered: new Date().toISOString() });
216236
await Installation.save();
217237
}
218238

239+
// if the installation is found and its status is pending and userId is provided, link the installation to the user
219240
if (findInstallation?.status === 'pending' && userId) {
220-
Installation.pending.filter((record) => record.id !== installation);
241+
Installation.pending = Installation.pending.filter((record) => record.id !== installation);
221242
Installation.linked.push({ user: userId, installations: [{ id: installation, registered: new Date().toISOString() }] });
222243
await Installation.save();
223244
}

0 commit comments

Comments
 (0)