-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Delegate.ts
159 lines (143 loc) · 5.71 KB
/
Delegate.ts
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
import Onyx from 'react-native-onyx';
import type {OnyxUpdate} from 'react-native-onyx';
import * as API from '@libs/API';
import {SIDE_EFFECT_REQUEST_COMMANDS} from '@libs/API/types';
import Log from '@libs/Log';
import * as NetworkStore from '@libs/Network/NetworkStore';
import * as SequentialQueue from '@libs/Network/SequentialQueue';
import ONYXKEYS from '@src/ONYXKEYS';
import type {DelegatedAccess} from '@src/types/onyx/Account';
import {confirmReadyToOpenApp, openApp} from './App';
import updateSessionAuthTokens from './Session/updateSessionAuthTokens';
let delegatedAccess: DelegatedAccess;
Onyx.connect({
key: ONYXKEYS.ACCOUNT,
callback: (val) => {
delegatedAccess = val?.delegatedAccess ?? {};
},
});
const KEYS_TO_PRESERVE_DELEGATE_ACCESS = [ONYXKEYS.NVP_TRY_FOCUS_MODE, ONYXKEYS.PREFERRED_THEME, ONYXKEYS.NVP_PREFERRED_LOCALE, ONYXKEYS.SESSION];
function connect(email: string) {
if (!delegatedAccess?.delegators) {
return;
}
const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
delegatedAccess: {
delegators: delegatedAccess.delegators.map((delegator) => (delegator.email === email ? {...delegator, error: undefined} : delegator)),
},
},
},
];
const successData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
delegatedAccess: {
delegators: delegatedAccess.delegators.map((delegator) => (delegator.email === email ? {...delegator, error: undefined} : delegator)),
},
},
},
];
const failureData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
delegatedAccess: {
delegators: delegatedAccess.delegators.map((delegator) => (delegator.email === email ? {...delegator, error: 'delegate.genericError'} : delegator)),
},
},
},
];
// We need to access the authToken directly from the response to update the session
// eslint-disable-next-line rulesdir/no-api-side-effects-method
API.makeRequestWithSideEffects(SIDE_EFFECT_REQUEST_COMMANDS.CONNECT_AS_DELEGATE, {to: email}, {optimisticData, successData, failureData})
.then((response) => {
if (!response?.restrictedToken || !response?.encryptedAuthToken) {
Log.alert('[Delegate] No auth token returned while connecting as a delegate');
Onyx.update(failureData);
return;
}
return SequentialQueue.waitForIdle()
.then(() => Onyx.clear(KEYS_TO_PRESERVE_DELEGATE_ACCESS))
.then(() => {
// Update authToken in Onyx and in our local variables so that API requests will use the new authToken
updateSessionAuthTokens(response?.restrictedToken, response?.encryptedAuthToken);
NetworkStore.setAuthToken(response?.restrictedToken ?? null);
confirmReadyToOpenApp();
openApp();
});
})
.catch((error) => {
Log.alert('[Delegate] Error connecting as delegate', {error});
Onyx.update(failureData);
});
}
function disconnect() {
const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
delegatedAccess: {
error: null,
},
},
},
];
const successData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
delegatedAccess: {
error: null,
},
},
},
];
const failureData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
delegatedAccess: {
error: 'delegate.genericError',
},
},
},
];
// We need to access the authToken directly from the response to update the session
// eslint-disable-next-line rulesdir/no-api-side-effects-method
API.makeRequestWithSideEffects(SIDE_EFFECT_REQUEST_COMMANDS.DISCONNECT_AS_DELEGATE, {}, {optimisticData, successData, failureData})
.then((response) => {
if (!response?.authToken || !response?.encryptedAuthToken) {
Log.alert('[Delegate] No auth token returned while disconnecting as a delegate');
return;
}
return SequentialQueue.waitForIdle()
.then(() => Onyx.clear(KEYS_TO_PRESERVE_DELEGATE_ACCESS))
.then(() => {
// Update authToken in Onyx and in our local variables so that API requests will use the new authToken
updateSessionAuthTokens(response?.authToken, response?.encryptedAuthToken);
NetworkStore.setAuthToken(response?.authToken ?? null);
confirmReadyToOpenApp();
openApp();
});
})
.catch((error) => {
Log.alert('[Delegate] Error disconnecting as a delegate', {error});
});
}
function clearDelegatorErrors() {
if (!delegatedAccess?.delegators) {
return;
}
Onyx.merge(ONYXKEYS.ACCOUNT, {delegatedAccess: {delegators: delegatedAccess.delegators.map((delegator) => ({...delegator, error: undefined}))}});
}
export {connect, clearDelegatorErrors, disconnect};