-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ModifiedExpenseMessage.ts
226 lines (205 loc) · 9.14 KB
/
ModifiedExpenseMessage.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
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
218
219
220
221
222
223
224
225
226
import Onyx from 'react-native-onyx';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {PolicyTags, ReportAction} from '@src/types/onyx';
import * as CurrencyUtils from './CurrencyUtils';
import DateUtils from './DateUtils';
import * as Localize from './Localize';
import * as PolicyUtils from './PolicyUtils';
import * as ReportUtils from './ReportUtils';
import type {ExpenseOriginalMessage} from './ReportUtils';
let allPolicyTags: Record<string, PolicyTags | null> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY_TAGS,
waitForCollectionCallback: true,
callback: (value) => {
if (!value) {
allPolicyTags = {};
return;
}
allPolicyTags = value;
},
});
/**
* Builds the partial message fragment for a modified field on the expense.
*/
function buildMessageFragmentForValue(
newValue: string,
oldValue: string,
valueName: string,
valueInQuotes: boolean,
setFragments: string[],
removalFragments: string[],
changeFragments: string[],
shouldConvertToLowercase = true,
) {
const newValueToDisplay = valueInQuotes ? `"${newValue}"` : newValue;
const oldValueToDisplay = valueInQuotes ? `"${oldValue}"` : oldValue;
const displayValueName = shouldConvertToLowercase ? valueName.toLowerCase() : valueName;
const isOldValuePartialMerchant = valueName === Localize.translateLocal('common.merchant') && oldValue === CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT;
// In case of a partial merchant value, we want to avoid user seeing the "(none)" value in the message.
if (!oldValue || isOldValuePartialMerchant) {
const fragment = Localize.translateLocal('iou.setTheRequest', {valueName: displayValueName, newValueToDisplay});
setFragments.push(fragment);
} else if (!newValue) {
const fragment = Localize.translateLocal('iou.removedTheRequest', {valueName: displayValueName, oldValueToDisplay});
removalFragments.push(fragment);
} else {
const fragment = Localize.translateLocal('iou.updatedTheRequest', {valueName: displayValueName, newValueToDisplay, oldValueToDisplay});
changeFragments.push(fragment);
}
}
/**
* Get the message line for a modified expense.
*/
function getMessageLine(prefix: string, messageFragments: string[]): string {
if (messageFragments.length === 0) {
return '';
}
return messageFragments.reduce((acc, value, index) => {
if (index === messageFragments.length - 1) {
if (messageFragments.length === 1) {
return `${acc} ${value}.`;
}
if (messageFragments.length === 2) {
return `${acc} ${Localize.translateLocal('common.and')} ${value}.`;
}
return `${acc}, ${Localize.translateLocal('common.and')} ${value}.`;
}
if (index === 0) {
return `${acc} ${value}`;
}
return `${acc}, ${value}`;
}, prefix);
}
function getForDistanceRequest(newDistance: string, oldDistance: string, newAmount: string, oldAmount: string): string {
if (!oldDistance) {
return Localize.translateLocal('iou.setTheDistance', {newDistanceToDisplay: newDistance, newAmountToDisplay: newAmount});
}
return Localize.translateLocal('iou.updatedTheDistance', {
newDistanceToDisplay: newDistance,
oldDistanceToDisplay: oldDistance,
newAmountToDisplay: newAmount,
oldAmountToDisplay: oldAmount,
});
}
/**
* Get the report action message when expense has been modified.
*
* ModifiedExpense::getNewDotComment in Web-Expensify should match this.
* If we change this function be sure to update the backend as well.
*/
function getForReportAction(reportAction: ReportAction): string {
if (reportAction.actionName !== CONST.REPORT.ACTIONS.TYPE.MODIFIEDEXPENSE) {
return '';
}
const reportActionOriginalMessage = reportAction.originalMessage as ExpenseOriginalMessage | undefined;
const policyID = ReportUtils.getReportPolicyID(reportAction.reportID) ?? '';
const policyTags = allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`] ?? {};
const policyTagListName = PolicyUtils.getTagListName(policyTags) || Localize.translateLocal('common.tag');
const removalFragments: string[] = [];
const setFragments: string[] = [];
const changeFragments: string[] = [];
const hasModifiedAmount =
reportActionOriginalMessage &&
'oldAmount' in reportActionOriginalMessage &&
'oldCurrency' in reportActionOriginalMessage &&
'amount' in reportActionOriginalMessage &&
'currency' in reportActionOriginalMessage;
const hasModifiedMerchant = reportActionOriginalMessage && 'oldMerchant' in reportActionOriginalMessage && 'merchant' in reportActionOriginalMessage;
if (hasModifiedAmount) {
const oldCurrency = reportActionOriginalMessage?.oldCurrency ?? '';
const oldAmount = CurrencyUtils.convertToDisplayString(reportActionOriginalMessage?.oldAmount ?? 0, oldCurrency);
const currency = reportActionOriginalMessage?.currency ?? '';
const amount = CurrencyUtils.convertToDisplayString(reportActionOriginalMessage?.amount ?? 0, currency);
// Only Distance edits should modify amount and merchant (which stores distance) in a single transaction.
// We check the merchant is in distance format (includes @) as a sanity check
if (hasModifiedMerchant && (reportActionOriginalMessage?.merchant ?? '').includes('@')) {
return getForDistanceRequest(reportActionOriginalMessage?.merchant ?? '', reportActionOriginalMessage?.oldMerchant ?? '', amount, oldAmount);
}
buildMessageFragmentForValue(amount, oldAmount, Localize.translateLocal('iou.amount'), false, setFragments, removalFragments, changeFragments);
}
const hasModifiedComment = reportActionOriginalMessage && 'oldComment' in reportActionOriginalMessage && 'newComment' in reportActionOriginalMessage;
if (hasModifiedComment) {
buildMessageFragmentForValue(
reportActionOriginalMessage?.newComment ?? '',
reportActionOriginalMessage?.oldComment ?? '',
Localize.translateLocal('common.description'),
true,
setFragments,
removalFragments,
changeFragments,
);
}
if (reportActionOriginalMessage?.oldCreated && reportActionOriginalMessage?.created) {
const formattedOldCreated = DateUtils.formatWithUTCTimeZone(reportActionOriginalMessage.oldCreated, CONST.DATE.FNS_FORMAT_STRING);
buildMessageFragmentForValue(
reportActionOriginalMessage.created,
formattedOldCreated,
Localize.translateLocal('common.date'),
false,
setFragments,
removalFragments,
changeFragments,
);
}
if (hasModifiedMerchant) {
buildMessageFragmentForValue(
reportActionOriginalMessage?.merchant ?? '',
reportActionOriginalMessage?.oldMerchant ?? '',
Localize.translateLocal('common.merchant'),
true,
setFragments,
removalFragments,
changeFragments,
);
}
const hasModifiedCategory = reportActionOriginalMessage && 'oldCategory' in reportActionOriginalMessage && 'category' in reportActionOriginalMessage;
if (hasModifiedCategory) {
buildMessageFragmentForValue(
reportActionOriginalMessage?.category ?? '',
reportActionOriginalMessage?.oldCategory ?? '',
Localize.translateLocal('common.category'),
true,
setFragments,
removalFragments,
changeFragments,
);
}
const hasModifiedTag = reportActionOriginalMessage && 'oldTag' in reportActionOriginalMessage && 'tag' in reportActionOriginalMessage;
if (hasModifiedTag) {
buildMessageFragmentForValue(
reportActionOriginalMessage?.tag ?? '',
reportActionOriginalMessage?.oldTag ?? '',
policyTagListName,
true,
setFragments,
removalFragments,
changeFragments,
policyTagListName === Localize.translateLocal('common.tag'),
);
}
const hasModifiedBillable = reportActionOriginalMessage && 'oldBillable' in reportActionOriginalMessage && 'billable' in reportActionOriginalMessage;
if (hasModifiedBillable) {
buildMessageFragmentForValue(
reportActionOriginalMessage?.billable ?? '',
reportActionOriginalMessage?.oldBillable ?? '',
Localize.translateLocal('iou.request'),
true,
setFragments,
removalFragments,
changeFragments,
);
}
const message =
getMessageLine(`\n${Localize.translateLocal('iou.changed')}`, changeFragments) +
getMessageLine(`\n${Localize.translateLocal('iou.set')}`, setFragments) +
getMessageLine(`\n${Localize.translateLocal('iou.removed')}`, removalFragments);
if (message === '') {
return Localize.translateLocal('iou.changedTheRequest');
}
return `${message.substring(1, message.length)}`;
}
export default {
getForReportAction,
};