+37
-56
helpers/eligibility.ts
+37
-56
helpers/eligibility.ts
···
3
3
import { fetchTailscaleOffers, getTailcontrolCookie } from '$helpers/offers';
4
4
import type { Eligibility, EligibilityApiResponse } from '$types/eligibility';
5
5
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
-
45
6
export const parseEligibilityResponse = (
46
7
body: EligibilityApiResponse,
47
8
): Eligibility => {
48
9
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
+
49
20
return {
50
-
...eligibilityMap.apiError,
21
+
eligible: false,
51
22
reason: `API error: ${body.error}`,
23
+
id: 'api-error',
52
24
error: body.error,
53
25
};
54
26
}
55
27
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
+
}
61
36
62
37
return {
63
-
...eligibilityMap.unknown,
38
+
eligible: true,
39
+
reason: 'Eligible!',
40
+
id: 'eligible',
64
41
offerType: body.data?.offerType,
65
42
};
66
43
};
···
76
53
'INFO',
77
54
'Eligibility',
78
55
);
79
-
let eligibility: Eligibility = eligibilityMap.unknown;
56
+
let eligibility: Eligibility = {
57
+
eligible: false,
58
+
reason: 'Unknown',
59
+
id: 'unknown',
60
+
};
80
61
81
62
const cacheDurationMs = eligibilityCacheInterval * 1000;
82
63
const cached = await browser.storage.local.get('eligibility');
···
91
72
try {
92
73
const cookie = await getTailcontrolCookie();
93
74
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';
95
79
handleEligibilityResult(eligibility);
96
80
return eligibility;
97
81
}
···
99
83
const cookieValue = cookie.value;
100
84
const response = await fetchTailscaleOffers(cookieValue);
101
85
const body = await response.json();
102
-
console.log(body);
103
86
eligibility = parseEligibilityResponse(body);
104
87
handleEligibilityResult(eligibility);
105
88
106
89
return eligibility;
107
-
} catch (e) {
90
+
} catch (e: unknown) {
108
91
const errorMsg = e instanceof Error ? e.message : 'Unknown error';
109
92
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;
116
97
handleEligibilityResult(eligibility);
117
98
118
99
return eligibility;