-
Notifications
You must be signed in to change notification settings - Fork 3k
/
TeachersUnite.ts
184 lines (167 loc) · 6.44 KB
/
TeachersUnite.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import Onyx from 'react-native-onyx';
import type {OnyxEntry, OnyxUpdate} from 'react-native-onyx';
import * as API from '@libs/API';
import type {AddSchoolPrincipalParams, ReferTeachersUniteVolunteerParams} from '@libs/API/parameters';
import {WRITE_COMMANDS} from '@libs/API/types';
import Navigation from '@libs/Navigation/Navigation';
import * as PhoneNumber from '@libs/PhoneNumber';
import * as ReportUtils from '@libs/ReportUtils';
import type {OptimisticCreatedReportAction} from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {PersonalDetailsList} from '@src/types/onyx';
type CreationData = {
reportID: string;
reportActionID: string;
};
type ReportCreationData = Record<string, CreationData>;
type ExpenseReportActionData = Record<string, OptimisticCreatedReportAction>;
let sessionEmail = '';
let sessionAccountID = 0;
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (value) => {
sessionEmail = value?.email ?? '';
sessionAccountID = value?.accountID ?? 0;
},
});
let allPersonalDetails: OnyxEntry<PersonalDetailsList>;
Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (value) => (allPersonalDetails = value),
});
/**
* @param publicRoomReportID - This is the global reportID for the public room, we'll ignore the optimistic one
*/
function referTeachersUniteVolunteer(partnerUserID: string, firstName: string, lastName: string, policyID: string, publicRoomReportID: string) {
const optimisticPublicRoom = ReportUtils.buildOptimisticChatReport([], CONST.TEACHERS_UNITE.PUBLIC_ROOM_NAME, CONST.REPORT.CHAT_TYPE.POLICY_ROOM, policyID);
const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.REPORT}${publicRoomReportID}`,
value: {
...optimisticPublicRoom,
reportID: publicRoomReportID,
policyName: CONST.TEACHERS_UNITE.POLICY_NAME,
},
},
];
const parameters: ReferTeachersUniteVolunteerParams = {
reportID: publicRoomReportID,
firstName,
lastName,
partnerUserID,
};
API.write(WRITE_COMMANDS.REFER_TEACHERS_UNITE_VOLUNTEER, parameters, {optimisticData});
Navigation.dismissModal(publicRoomReportID);
}
/**
* Optimistically creates a policyExpenseChat for the school principal and passes data to AddSchoolPrincipal
*/
function addSchoolPrincipal(firstName: string, partnerUserID: string, lastName: string, policyID: string) {
const policyName = CONST.TEACHERS_UNITE.POLICY_NAME;
const loggedInEmail = PhoneNumber.addSMSDomainIfPhoneNumber(sessionEmail);
const reportCreationData: ReportCreationData = {};
const expenseChatData = ReportUtils.buildOptimisticChatReport([sessionAccountID], '', CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT, policyID, sessionAccountID, true, policyName);
const expenseChatReportID = expenseChatData.reportID;
const expenseReportCreatedAction = ReportUtils.buildOptimisticCreatedReportAction(sessionEmail);
const expenseReportActionData: ExpenseReportActionData = {
[expenseReportCreatedAction.reportActionID]: expenseReportCreatedAction,
};
reportCreationData[loggedInEmail] = {
reportID: expenseChatReportID,
reportActionID: expenseReportCreatedAction.reportActionID,
};
const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
id: policyID,
isPolicyExpenseChatEnabled: true,
type: CONST.POLICY.TYPE.CORPORATE,
name: policyName,
role: CONST.POLICY.ROLE.USER,
owner: sessionEmail,
outputCurrency: allPersonalDetails?.[sessionAccountID]?.localCurrencyCode ?? CONST.CURRENCY.USD,
employeeList: {
[sessionEmail]: {
role: CONST.POLICY.ROLE.USER,
errors: {},
},
},
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
},
{
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.REPORT}${expenseChatReportID}`,
value: {
pendingFields: {
addWorkspaceRoom: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
...expenseChatData,
},
},
{
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseChatReportID}`,
value: expenseReportActionData,
},
];
const successData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {pendingAction: null},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${expenseChatReportID}`,
value: {
pendingFields: {
addWorkspaceRoom: null,
},
pendingAction: null,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseChatReportID}`,
value: {
[Object.keys(expenseChatData)[0]]: {
pendingAction: null,
},
},
},
];
const failureData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
[sessionEmail]: null,
},
},
{
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.REPORT}${expenseChatReportID}`,
value: null,
},
{
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseChatReportID}`,
value: null,
},
];
const parameters: AddSchoolPrincipalParams = {
firstName,
lastName,
partnerUserID,
policyID,
reportCreationData: JSON.stringify(reportCreationData),
};
API.write(WRITE_COMMANDS.ADD_SCHOOL_PRINCIPAL, parameters, {optimisticData, successData, failureData});
Navigation.dismissModal(expenseChatReportID);
}
export default {referTeachersUniteVolunteer, addSchoolPrincipal};