Skip to content

Commit 07c8b77

Browse files
authored
Merge pull request Expensify#86295 from dukenv0307/fix/66412-part-4
refactor deletePolicy, buildOptimisticChangeFieldAction, buildOptimisticChangePolicyReportAction, buildOptimisticChangedTaskAssigneeReportAction to use accountID from useOnyx
2 parents f20e7e9 + 86c0f84 commit 07c8b77

9 files changed

Lines changed: 89 additions & 11 deletions

File tree

src/libs/ReportUtils.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4090,10 +4090,10 @@ function getReimbursementDeQueuedOrCanceledActionMessage(
40904090
* Builds an optimistic REIMBURSEMENT_DEQUEUED report action with a randomly generated reportActionID.
40914091
*
40924092
*/
4093-
function buildOptimisticChangeFieldAction(reportField: PolicyReportField, previousReportField: PolicyReportField): OptimisticChangeFieldAction {
4093+
function buildOptimisticChangeFieldAction(reportField: PolicyReportField, previousReportField: PolicyReportField, currentUserAccountID: number): OptimisticChangeFieldAction {
40944094
return {
40954095
actionName: CONST.REPORT.ACTIONS.TYPE.CHANGE_FIELD,
4096-
actorAccountID: deprecatedCurrentUserAccountID,
4096+
actorAccountID: currentUserAccountID,
40974097
message: [
40984098
{
40994099
type: 'TEXT',
@@ -7399,7 +7399,7 @@ function buildOptimisticMovedReportAction(
73997399
* Builds an optimistic CHANGE_POLICY report action with a randomly generated reportActionID.
74007400
* This action is used when we change the workspace of a report.
74017401
*/
7402-
function buildOptimisticChangePolicyReportAction(fromPolicyID: string | undefined, toPolicyID: string, automaticAction = false): ReportAction {
7402+
function buildOptimisticChangePolicyReportAction(fromPolicyID: string | undefined, toPolicyID: string, currentUserAccountID: number, automaticAction = false): ReportAction {
74037403
const originalMessage = {
74047404
fromPolicy: fromPolicyID,
74057405
toPolicy: toPolicyID,
@@ -7430,7 +7430,7 @@ function buildOptimisticChangePolicyReportAction(fromPolicyID: string | undefine
74307430

74317431
return {
74327432
actionName: CONST.REPORT.ACTIONS.TYPE.CHANGE_POLICY,
7433-
actorAccountID: deprecatedCurrentUserAccountID,
7433+
actorAccountID: currentUserAccountID,
74347434
avatar: getCurrentUserAvatar(),
74357435
created: DateUtils.getDBTime(),
74367436
originalMessage,
@@ -8301,14 +8301,14 @@ function buildOptimisticCardAssignedReportAction(assigneeAccountID: number, curr
83018301
};
83028302
}
83038303

8304-
function buildOptimisticChangedTaskAssigneeReportAction(assigneeAccountID: number): OptimisticEditedTaskReportAction {
8304+
function buildOptimisticChangedTaskAssigneeReportAction(assigneeAccountID: number, currentUserAccountID: number): OptimisticEditedTaskReportAction {
83058305
const delegateAccountDetails = getPersonalDetailByEmail(delegateEmail);
83068306

83078307
return {
83088308
reportActionID: rand64(),
83098309
actionName: CONST.REPORT.ACTIONS.TYPE.TASK_EDITED,
83108310
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
8311-
actorAccountID: deprecatedCurrentUserAccountID,
8311+
actorAccountID: currentUserAccountID,
83128312
message: [
83138313
{
83148314
type: CONST.REPORT.MESSAGE.TYPE.COMMENT,
@@ -8339,11 +8339,12 @@ function buildOptimisticChangedTaskAssigneeReportAction(assigneeAccountID: numbe
83398339
function buildOptimisticClosedReportAction(
83408340
emailClosingReport: string,
83418341
policyName: string,
8342+
currentUserAccountID: number,
83428343
reason: ValueOf<typeof CONST.REPORT.ARCHIVE_REASON> = CONST.REPORT.ARCHIVE_REASON.DEFAULT,
83438344
): OptimisticClosedReportAction {
83448345
return {
83458346
actionName: CONST.REPORT.ACTIONS.TYPE.CLOSED,
8346-
actorAccountID: deprecatedCurrentUserAccountID,
8347+
actorAccountID: currentUserAccountID,
83478348
automatic: false,
83488349
avatar: getCurrentUserAvatar(),
83498350
created: DateUtils.getDBTime(),

src/libs/actions/Policy/Policy.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ type DeleteWorkspaceActionParams = {
382382
reimbursementAccountError: Errors | undefined;
383383
lastUsedPaymentMethods?: LastPaymentMethod;
384384
localeCompare: LocaleContextProps['localeCompare'];
385+
currentUserAccountID: number;
385386
};
386387

387388
/**
@@ -401,6 +402,7 @@ function deleteWorkspace(params: DeleteWorkspaceActionParams) {
401402
lastUsedPaymentMethods,
402403
localeCompare,
403404
personalPolicyID,
405+
currentUserAccountID,
404406
} = params;
405407

406408
const policy = policies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
@@ -545,7 +547,7 @@ function deleteWorkspace(params: DeleteWorkspaceActionParams) {
545547
if (!!ownerAccountID && ownerAccountID !== CONST.POLICY.OWNER_ACCOUNT_ID_FAKE) {
546548
emailClosingReport = deprecatedAllPersonalDetails?.[ownerAccountID]?.login ?? '';
547549
}
548-
const optimisticClosedReportAction = ReportUtils.buildOptimisticClosedReportAction(emailClosingReport, policyName, CONST.REPORT.ARCHIVE_REASON.POLICY_DELETED);
550+
const optimisticClosedReportAction = ReportUtils.buildOptimisticClosedReportAction(emailClosingReport, policyName, currentUserAccountID, CONST.REPORT.ARCHIVE_REASON.POLICY_DELETED);
549551
optimisticData.push({
550552
onyxMethod: Onyx.METHOD.MERGE,
551553
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,

src/libs/actions/Report/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3100,7 +3100,7 @@ function updateReportField({
31003100
const fieldKey = getReportFieldKey(reportField.fieldID);
31013101
const recentlyUsedValues = recentlyUsedReportFields?.[fieldKey] ?? [];
31023102

3103-
const optimisticChangeFieldAction = buildOptimisticChangeFieldAction(reportField, previousReportField);
3103+
const optimisticChangeFieldAction = buildOptimisticChangeFieldAction(reportField, previousReportField, accountID);
31043104
const predictedNextStatus = policy?.reimbursementChoice === CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_NO ? CONST.REPORT.STATUS_NUM.CLOSED : CONST.REPORT.STATUS_NUM.OPEN;
31053105

31063106
// buildOptimisticNextStep is used in parallel
@@ -6813,7 +6813,7 @@ function buildOptimisticChangePolicyData(
68136813
});
68146814

68156815
// 4. Optimistically create a CHANGE_POLICY reportAction on the report using the reportActionID
6816-
const optimisticMovedReportAction = buildOptimisticChangePolicyReportAction(report.policyID, policy.id);
6816+
const optimisticMovedReportAction = buildOptimisticChangePolicyReportAction(report.policyID, policy.id, currentUserAccountID);
68176817
optimisticData.push({
68186818
onyxMethod: Onyx.METHOD.MERGE,
68196819
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,

src/libs/actions/Task.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ function editTaskAssignee(
730730
isOptimisticReport?: boolean,
731731
) {
732732
// Create the EditedReportAction on the task
733-
const editTaskReportAction = ReportUtils.buildOptimisticChangedTaskAssigneeReportAction(assigneeAccountID ?? CONST.DEFAULT_NUMBER_ID);
733+
const editTaskReportAction = ReportUtils.buildOptimisticChangedTaskAssigneeReportAction(assigneeAccountID ?? CONST.DEFAULT_NUMBER_ID, currentUserAccountID);
734734
const reportName = report.reportName?.trim();
735735

736736
let assigneeChatReportOnyxData;

src/pages/workspace/WorkspaceOverviewPage.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ function WorkspaceOverviewPage({policyDraft, policy: policyProp, route}: Workspa
263263
lastUsedPaymentMethods: lastPaymentMethod,
264264
localeCompare,
265265
personalPolicyID,
266+
currentUserAccountID: accountID,
266267
});
267268
if (isOffline) {
268269
setIsDeleteModalOpen(false);

src/pages/workspace/WorkspacesListPage.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ function WorkspacesListPage() {
243243
lastUsedPaymentMethods: lastPaymentMethod,
244244
localeCompare,
245245
personalPolicyID,
246+
currentUserAccountID: currentUserPersonalDetails.accountID,
246247
});
247248
if (isOffline) {
248249
setIsDeleteModalOpen(false);

tests/actions/IOUTest.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9915,6 +9915,7 @@ describe('actions/IOU', () => {
99159915
reimbursementAccountError: undefined,
99169916
lastUsedPaymentMethods: undefined,
99179917
localeCompare,
9918+
currentUserAccountID: CARLOS_ACCOUNT_ID,
99189919
});
99199920
}
99209921
return waitForBatchedUpdates();

tests/actions/PolicyTest.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2755,6 +2755,7 @@ describe('actions/Policy', () => {
27552755
reimbursementAccountError: {},
27562756
lastUsedPaymentMethods: undefined,
27572757
localeCompare: TestHelper.localeCompare,
2758+
currentUserAccountID: ESH_ACCOUNT_ID,
27582759
});
27592760

27602761
await waitForBatchedUpdates();
@@ -2853,6 +2854,7 @@ describe('actions/Policy', () => {
28532854
reimbursementAccountError: undefined,
28542855
lastUsedPaymentMethods: undefined,
28552856
localeCompare: TestHelper.localeCompare,
2857+
currentUserAccountID: ESH_ACCOUNT_ID,
28562858
});
28572859

28582860
await waitForBatchedUpdates();
@@ -2909,6 +2911,7 @@ describe('actions/Policy', () => {
29092911
reimbursementAccountError: undefined,
29102912
lastUsedPaymentMethods: undefined,
29112913
localeCompare: TestHelper.localeCompare,
2914+
currentUserAccountID: ESH_ACCOUNT_ID,
29122915
});
29132916
await waitForBatchedUpdates();
29142917

@@ -2946,6 +2949,7 @@ describe('actions/Policy', () => {
29462949
reimbursementAccountError: undefined,
29472950
lastUsedPaymentMethods: undefined,
29482951
localeCompare: TestHelper.localeCompare,
2952+
currentUserAccountID: ESH_ACCOUNT_ID,
29492953
});
29502954
await waitForBatchedUpdates();
29512955

@@ -2985,6 +2989,7 @@ describe('actions/Policy', () => {
29852989
reimbursementAccountError: undefined,
29862990
lastUsedPaymentMethods: undefined,
29872991
localeCompare: TestHelper.localeCompare,
2992+
currentUserAccountID: ESH_ACCOUNT_ID,
29882993
});
29892994
await waitForBatchedUpdates();
29902995

tests/unit/ReportUtilsTest.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
buildOptimisticCancelPaymentReportAction,
3737
buildOptimisticCardAssignedReportAction,
3838
buildOptimisticChatReport,
39+
buildOptimisticClosedReportAction,
3940
buildOptimisticCreatedReportAction,
4041
buildOptimisticCreatedReportForUnapprovedAction,
4142
buildOptimisticEmptyReport,
@@ -15188,4 +15189,70 @@ describe('ReportUtils', () => {
1518815189
expect(action.reportActionID).toBeTruthy();
1518915190
});
1519015191
});
15192+
15193+
describe('buildOptimisticClosedReportAction', () => {
15194+
it('should set actorAccountID to the provided currentUserAccountID', () => {
15195+
const customAccountID = 99;
15196+
const action = buildOptimisticClosedReportAction('user@example.com', 'Test Policy', customAccountID);
15197+
15198+
expect(action.actorAccountID).toBe(customAccountID);
15199+
});
15200+
15201+
it('should set actionName to CLOSED', () => {
15202+
const action = buildOptimisticClosedReportAction('user@example.com', 'Test Policy', currentUserAccountID);
15203+
15204+
expect(action.actionName).toBe(CONST.REPORT.ACTIONS.TYPE.CLOSED);
15205+
});
15206+
15207+
it('should set originalMessage with the provided policyName and default reason', () => {
15208+
const policyName = 'My Workspace';
15209+
const action = buildOptimisticClosedReportAction('user@example.com', policyName, currentUserAccountID);
15210+
15211+
expect(getOriginalMessage(action as ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.CLOSED>)).toMatchObject({
15212+
policyName,
15213+
reason: CONST.REPORT.ARCHIVE_REASON.DEFAULT,
15214+
});
15215+
});
15216+
15217+
it('should set originalMessage with the provided reason when specified', () => {
15218+
const policyName = 'My Workspace';
15219+
const reason = CONST.REPORT.ARCHIVE_REASON.POLICY_DELETED;
15220+
const action = buildOptimisticClosedReportAction('user@example.com', policyName, currentUserAccountID, reason);
15221+
15222+
expect(getOriginalMessage(action as ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.CLOSED>)).toMatchObject({
15223+
policyName,
15224+
reason,
15225+
});
15226+
});
15227+
15228+
it('should set message with the emailClosingReport as the first text', () => {
15229+
const emailClosingReport = 'admin@company.com';
15230+
const action = buildOptimisticClosedReportAction(emailClosingReport, 'Test Policy', currentUserAccountID);
15231+
const messages = action.message as Array<{type: string; style: string; text: string}>;
15232+
15233+
expect(messages.at(0)).toMatchObject({
15234+
type: CONST.REPORT.MESSAGE.TYPE.TEXT,
15235+
style: 'strong',
15236+
text: emailClosingReport,
15237+
});
15238+
});
15239+
15240+
it('should set pendingAction to ADD', () => {
15241+
const action = buildOptimisticClosedReportAction('user@example.com', 'Test Policy', currentUserAccountID);
15242+
15243+
expect(action.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD);
15244+
});
15245+
15246+
it('should set shouldShow to true', () => {
15247+
const action = buildOptimisticClosedReportAction('user@example.com', 'Test Policy', currentUserAccountID);
15248+
15249+
expect(action.shouldShow).toBe(true);
15250+
});
15251+
15252+
it('should generate a non-empty reportActionID', () => {
15253+
const action = buildOptimisticClosedReportAction('user@example.com', 'Test Policy', currentUserAccountID);
15254+
15255+
expect(action.reportActionID).toBeTruthy();
15256+
});
15257+
});
1519115258
});

0 commit comments

Comments
 (0)