-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathreportUtils.js
217 lines (199 loc) · 6.5 KB
/
reportUtils.js
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import _ from 'underscore';
import Str from 'expensify-common/lib/str';
import lodashGet from 'lodash/get';
import Onyx from 'react-native-onyx';
import moment from 'moment';
import ONYXKEYS from '../ONYXKEYS';
import CONST from '../CONST';
let sessionEmail;
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: val => sessionEmail = val ? val.email : null,
});
/**
* Returns the concatenated title for the PrimaryLogins of a report
*
* @param {Array} logins
* @returns {string}
*/
function getReportParticipantsTitle(logins) {
return _.map(logins, login => Str.removeSMSDomain(login)).join(', ');
}
/**
* Check whether a report action is Attachment is not.
*
* @param {Object} reportMessageText report action's message as text
* @returns {Boolean}
*/
function isReportMessageAttachment(reportMessageText) {
return reportMessageText === '[Attachment]';
}
/**
* Given a collection of reports returns them sorted by last visited
*
* @param {Object} reports
* @returns {Array}
*/
function sortReportsByLastVisited(reports) {
return _.chain(reports)
.toArray()
.filter(report => report && report.reportID)
.sortBy('lastVisitedTimestamp')
.value();
}
/**
* Can only edit if it's an ADDCOMMENT that is not an attachment,
* the author is this user and it's not an optimistic response.
* If it's an optimistic response comment it will not have a reportActionID,
* and we should wait until it does before we show the actions
*
* @param {Object} reportAction
* @param {String} sessionEmail
* @returns {Boolean}
*/
function canEditReportAction(reportAction) {
return reportAction.actorEmail === sessionEmail
&& reportAction.reportActionID
&& reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT
&& !isReportMessageAttachment(lodashGet(reportAction, ['message', 0, 'text'], ''));
}
/**
* Can only delete if it's an ADDCOMMENT, the author is this user and it's not an optimistic response.
* If it's an optimistic response comment it will not have a reportActionID,
* and we should wait until it does before we show the actions
*
* @param {Object} reportAction
* @param {String} sessionEmail
* @returns {Boolean}
*/
function canDeleteReportAction(reportAction) {
return reportAction.actorEmail === sessionEmail
&& reportAction.reportActionID
&& reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT;
}
/**
* Whether the provided report is a default room
* @param {Object} report
* @param {String} report.chatType
* @returns {Boolean}
*/
function isDefaultRoom(report) {
return _.contains([
CONST.REPORT.CHAT_TYPE.POLICY_ADMINS,
CONST.REPORT.CHAT_TYPE.POLICY_ANNOUNCE,
CONST.REPORT.CHAT_TYPE.DOMAIN_ALL,
], lodashGet(report, ['chatType'], ''));
}
/**
* Whether the provided report is a policy room
* @param {Object} report
* @param {String} report.chatType
* @returns {Boolean}
*/
function isPolicyRoom(report) {
return lodashGet(report, ['chatType'], '') === CONST.REPORT.CHAT_TYPE.POLICY_ROOM;
}
/**
* Given a collection of reports returns the most recently accessed one
*
* @param {Record<String, {lastVisitedTimestamp, reportID}>|Array<{lastVisitedTimestamp, reportID}>} reports
* @param {Boolean} [ignoreDefaultRooms]
* @returns {Object}
*/
function findLastAccessedReport(reports, ignoreDefaultRooms) {
let sortedReports = sortReportsByLastVisited(reports);
if (ignoreDefaultRooms) {
sortedReports = _.filter(sortedReports, report => !isDefaultRoom(report));
}
return _.last(sortedReports);
}
/**
* Whether the provided report is an archived room
* @param {Object} report
* @param {Number} report.stateNum
* @param {Number} report.statusNum
* @returns {Boolean}
*/
function isArchivedRoom(report) {
if (!isDefaultRoom(report)) {
return false;
}
return report.statusNum === 2 && report.stateNum === 2;
}
/**
* Get either the policyName or domainName the chat is tied to
* @param {Object} report
* @param {Object} policiesMap must have onyxkey prefix (i.e 'policy_') for keys
* @returns {String}
*/
function getDefaultRoomSubtitle(report, policiesMap) {
if (!isDefaultRoom(report)) {
return '';
}
if (report.chatType === CONST.REPORT.CHAT_TYPE.DOMAIN_ALL) {
// The domainAll rooms are just #domainName, so we ignore the prefix '#' to get the domainName
return report.reportName.substring(1);
}
if (isArchivedRoom(report)) {
return report.oldPolicyName;
}
return lodashGet(
policiesMap,
[`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`, 'name'],
'Unknown Policy',
);
}
/**
* Only returns true if this is our main 1:1 DM report with Concierge
*
* @param {Object} report
* @returns {Boolean}
*/
function isConciergeChatReport(report) {
return lodashGet(report, 'participants', []).length === 1
&& report.participants[0] === CONST.EMAIL.CONCIERGE;
}
/**
* Returns true if there is any automated expensify account in emails
* @param {Array} emails
* @returns {Boolean}
*/
function hasExpensifyEmails(emails) {
return _.intersection(emails, CONST.EXPENSIFY_EMAILS).length > 0;
}
/**
* Whether the time row should be shown for a report.
* @param {Array<Object>} personalDetails
* @param {Object} myPersonalDetails
* @param {Object} report
* @return {Boolean}
*/
function canShowReportRecipientLocalTime(personalDetails, myPersonalDetails, report) {
const reportParticipants = lodashGet(report, 'participants', []);
const hasMultipleParticipants = reportParticipants.length > 1;
const reportRecipient = personalDetails[reportParticipants[0]];
const currentUserTimezone = lodashGet(myPersonalDetails, 'timezone', CONST.DEFAULT_TIME_ZONE);
const reportRecipientTimezone = lodashGet(reportRecipient, 'timezone', CONST.DEFAULT_TIME_ZONE);
return !hasExpensifyEmails(reportParticipants)
&& !hasMultipleParticipants
&& reportRecipient
&& reportRecipientTimezone
&& currentUserTimezone.selected
&& reportRecipientTimezone.selected
&& moment().tz(currentUserTimezone.selected).utcOffset() !== moment().tz(reportRecipientTimezone.selected).utcOffset();
}
export {
getReportParticipantsTitle,
isReportMessageAttachment,
findLastAccessedReport,
canEditReportAction,
canDeleteReportAction,
sortReportsByLastVisited,
isDefaultRoom,
isPolicyRoom,
getDefaultRoomSubtitle,
isArchivedRoom,
isConciergeChatReport,
hasExpensifyEmails,
canShowReportRecipientLocalTime,
};