Skip to content

Commit 730ec86

Browse files
committed
refactor: temporarily use old eligibility logic
the new eligibility map is broken atm
1 parent 0cedd96 commit 730ec86

1 file changed

Lines changed: 37 additions & 56 deletions

File tree

helpers/eligibility.ts

Lines changed: 37 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,41 @@ import { log } from '$helpers/logging';
33
import { fetchTailscaleOffers, getTailcontrolCookie } from '$helpers/offers';
44
import type { Eligibility, EligibilityApiResponse } from '$types/eligibility';
55

6-
const eligibilityMap: Record<string, Eligibility> = {
7-
apiError: {
8-
eligible: false,
9-
reason: 'API error occurred.',
10-
id: 'api-error',
11-
error: 'API error',
12-
},
13-
eligible: {
14-
eligible: true,
15-
reason: 'Eligible for new offers!',
16-
id: 'eligible',
17-
offerType: 'reoffer',
18-
},
19-
'forbidden - not admin': {
20-
eligible: false,
21-
reason:
22-
'User does not have the Admin role in Tailscale, and thus has insufficient privileges.',
23-
id: 'not-admin',
24-
error: 'forbidden - not admin',
25-
},
26-
'forbidden - not logged in': {
27-
eligible: false,
28-
reason: 'User is not logged in to Tailscale.',
29-
id: 'not-logged-in',
30-
error: 'forbidden - not logged in',
31-
},
32-
locked: {
33-
eligible: false,
34-
reason: 'User already used their tailnet name for an HTTPS certificate.',
35-
id: 'custom-tailnet',
36-
offerType: 'locked',
37-
},
38-
unknown: {
39-
eligible: false,
40-
reason: 'Unknown eligibility state.',
41-
id: 'unknown',
42-
},
43-
};
44-
456
export const parseEligibilityResponse = (
467
body: EligibilityApiResponse,
478
): Eligibility => {
489
if (body.error) {
10+
if (body.error === 'forbidden - not admin') {
11+
return {
12+
eligible: false,
13+
reason:
14+
'User does not have the Admin role in Tailscale, and thus has insufficient privileges.',
15+
id: 'not-admin',
16+
error: body.error,
17+
};
18+
}
19+
4920
return {
50-
...eligibilityMap.apiError,
21+
eligible: false,
5122
reason: `API error: ${body.error}`,
23+
id: 'api-error',
5224
error: body.error,
5325
};
5426
}
5527

56-
if (body.error && eligibilityMap[body.error])
57-
return eligibilityMap[body.error];
58-
59-
if (body.data?.offerType === 'locked') return eligibilityMap.locked;
60-
if (body.data?.offerType === 'reoffer') return eligibilityMap.eligible;
28+
if (body.data && body.data.offerType === 'locked') {
29+
return {
30+
eligible: false,
31+
reason: 'User already used their tailnet name for an HTTPS certificate.',
32+
id: 'custom-tailnet',
33+
offerType: body.data.offerType,
34+
};
35+
}
6136

6237
return {
63-
...eligibilityMap.unknown,
38+
eligible: true,
39+
reason: 'Eligible!',
40+
id: 'eligible',
6441
offerType: body.data?.offerType,
6542
};
6643
};
@@ -76,7 +53,11 @@ export const checkEligibility = async (): Promise<Eligibility> => {
7653
'INFO',
7754
'Eligibility',
7855
);
79-
let eligibility: Eligibility = eligibilityMap.unknown;
56+
let eligibility: Eligibility = {
57+
eligible: false,
58+
reason: 'Unknown',
59+
id: 'unknown',
60+
};
8061

8162
const cacheDurationMs = eligibilityCacheInterval * 1000;
8263
const cached = await browser.storage.local.get('eligibility');
@@ -91,28 +72,28 @@ export const checkEligibility = async (): Promise<Eligibility> => {
9172
try {
9273
const cookie = await getTailcontrolCookie();
9374
if (!cookie) {
94-
eligibility = eligibilityMap['forbidden - not logged in'];
75+
eligibility.reason =
76+
'Tailcontrol cookie not found. User must log in to Tailscale.';
77+
eligibility.eligible = false;
78+
eligibility.id = 'not-logged-in';
9579
handleEligibilityResult(eligibility);
9680
return eligibility;
9781
}
9882

9983
const cookieValue = cookie.value;
10084
const response = await fetchTailscaleOffers(cookieValue);
10185
const body = await response.json();
102-
console.log(body);
10386
eligibility = parseEligibilityResponse(body);
10487
handleEligibilityResult(eligibility);
10588

10689
return eligibility;
107-
} catch (e) {
90+
} catch (e: unknown) {
10891
const errorMsg = e instanceof Error ? e.message : 'Unknown error';
10992

110-
eligibility = {
111-
...eligibilityMap.unknown,
112-
reason: errorMsg,
113-
error: errorMsg,
114-
};
115-
93+
eligibility.reason = errorMsg;
94+
eligibility.id = 'unknown-error';
95+
eligibility.eligible = false;
96+
eligibility.error = errorMsg;
11697
handleEligibilityResult(eligibility);
11798

11899
return eligibility;

0 commit comments

Comments
 (0)