diff --git a/config/eslint/eslint.seatbelt.tsv b/config/eslint/eslint.seatbelt.tsv index 277191eee328..99c9035c6005 100644 --- a/config/eslint/eslint.seatbelt.tsv +++ b/config/eslint/eslint.seatbelt.tsv @@ -556,7 +556,6 @@ "../../src/libs/actions/__mocks__/App.ts" "@typescript-eslint/no-unsafe-type-assertion" 1 "../../src/libs/actions/__mocks__/App.ts" "no-restricted-syntax" 2 "../../src/libs/actions/__mocks__/OnyxUpdates.ts" "no-restricted-syntax" 1 -"../../src/libs/actions/Agent.ts" "rulesdir/no-direct-personal-details-list" 9 "../../src/libs/actions/App.ts" "@typescript-eslint/no-unsafe-type-assertion" 1 "../../src/libs/actions/App.ts" "no-restricted-syntax" 1 "../../src/libs/actions/applyOnyxUpdatesReliably.ts" "no-restricted-syntax" 1 @@ -582,7 +581,6 @@ "../../src/libs/actions/Delegate.ts" "rulesdir/no-direct-personal-details-list" 1 "../../src/libs/actions/Domain.ts" "@typescript-eslint/no-unsafe-type-assertion" 3 "../../src/libs/actions/Domain.ts" "no-restricted-syntax" 1 -"../../src/libs/actions/Domain.ts" "rulesdir/no-direct-personal-details-list" 6 "../../src/libs/actions/ExitSurvey.ts" "no-restricted-syntax" 1 "../../src/libs/actions/getCompanyCardBankConnection/index.tsx" "@typescript-eslint/no-unsafe-type-assertion" 4 "../../src/libs/actions/getCompanyCardBankConnection/index.tsx" "no-restricted-imports" 1 @@ -607,7 +605,6 @@ "../../src/libs/actions/IOU/MoneyRequestBuilder.ts" "@typescript-eslint/no-deprecated/getReportTransactions" 2 "../../src/libs/actions/IOU/MoneyRequestBuilder.ts" "@typescript-eslint/no-deprecated/translateLocal" 1 "../../src/libs/actions/IOU/MoneyRequestBuilder.ts" "@typescript-eslint/no-unsafe-type-assertion" 2 -"../../src/libs/actions/IOU/MoneyRequestBuilder.ts" "rulesdir/no-direct-personal-details-list" 2 "../../src/libs/actions/IOU/PayMoneyRequest.ts" "@typescript-eslint/no-deprecated/getReportTransactions" 2 "../../src/libs/actions/IOU/PayMoneyRequest.ts" "@typescript-eslint/no-unsafe-type-assertion" 2 "../../src/libs/actions/IOU/PayMoneyRequest.ts" "no-restricted-imports" 1 @@ -624,15 +621,12 @@ "../../src/libs/actions/IOU/SearchUpdate.ts" "rulesdir/no-direct-personal-details-list" 2 "../../src/libs/actions/IOU/SendInvoice.ts" "@typescript-eslint/no-unsafe-type-assertion" 1 "../../src/libs/actions/IOU/SendInvoice.ts" "no-restricted-syntax" 1 -"../../src/libs/actions/IOU/SendInvoice.ts" "rulesdir/no-direct-personal-details-list" 2 "../../src/libs/actions/IOU/SendMoney.ts" "@typescript-eslint/no-unsafe-type-assertion" 1 "../../src/libs/actions/IOU/SendMoney.ts" "no-restricted-syntax" 1 -"../../src/libs/actions/IOU/SendMoney.ts" "rulesdir/no-direct-personal-details-list" 2 "../../src/libs/actions/IOU/Split.ts" "@typescript-eslint/no-deprecated/getReportTransactions" 1 "../../src/libs/actions/IOU/Split.ts" "@typescript-eslint/no-deprecated/Localize.translateLocal" 2 "../../src/libs/actions/IOU/Split.ts" "@typescript-eslint/no-unsafe-type-assertion" 2 "../../src/libs/actions/IOU/Split.ts" "no-restricted-syntax" 3 -"../../src/libs/actions/IOU/Split.ts" "rulesdir/no-direct-personal-details-list" 1 "../../src/libs/actions/IOU/SplitExpenseItems.ts" "@typescript-eslint/no-unsafe-type-assertion" 1 "../../src/libs/actions/IOU/SplitTransactionUpdate.ts" "@typescript-eslint/no-unsafe-type-assertion" 20 "../../src/libs/actions/IOU/TrackExpense.ts" "@typescript-eslint/no-deprecated/getReportTransactions" 1 @@ -664,14 +658,13 @@ "../../src/libs/actions/PersonalCards.ts" "@typescript-eslint/no-unsafe-type-assertion" 1 "../../src/libs/actions/PersonalDetails.ts" "@typescript-eslint/no-unsafe-type-assertion" 1 "../../src/libs/actions/PersonalDetails.ts" "no-restricted-syntax" 5 -"../../src/libs/actions/PersonalDetails.ts" "rulesdir/no-direct-personal-details-list" 15 "../../src/libs/actions/Plaid.ts" "no-restricted-syntax" 1 "../../src/libs/actions/Policy/Category.ts" "no-restricted-syntax" 5 "../../src/libs/actions/Policy/DistanceRate.ts" "no-restricted-syntax" 2 "../../src/libs/actions/Policy/Member.ts" "@typescript-eslint/no-unsafe-type-assertion" 2 "../../src/libs/actions/Policy/Member.ts" "no-restricted-imports" 1 "../../src/libs/actions/Policy/Member.ts" "no-restricted-syntax" 7 -"../../src/libs/actions/Policy/Member.ts" "rulesdir/no-direct-personal-details-list" 2 +"../../src/libs/actions/Policy/Member.ts" "rulesdir/no-direct-personal-details-list" 1 "../../src/libs/actions/Policy/Member.ts" "rulesdir/no-onyx-connect" 1 "../../src/libs/actions/Policy/PerDiem.ts" "no-restricted-syntax" 1 "../../src/libs/actions/Policy/Plan.ts" "no-restricted-syntax" 1 @@ -691,13 +684,12 @@ "../../src/libs/actions/QueuedOnyxUpdates.ts" "@typescript-eslint/no-unsafe-type-assertion" 1 "../../src/libs/actions/ReimbursementAccount/resetNonUSDBankAccount.ts" "no-restricted-syntax" 1 "../../src/libs/actions/ReimbursementAccount/resetUSDBankAccount.ts" "no-restricted-syntax" 1 -"../../src/libs/actions/replaceOptimisticAgentWithActualAgent.ts" "rulesdir/no-direct-personal-details-list" 1 "../../src/libs/actions/replaceOptimisticReportWithActualReport.ts" "@typescript-eslint/no-unsafe-type-assertion" 2 "../../src/libs/actions/Report/index.ts" "@typescript-eslint/no-deprecated/getReportTransactions" 3 "../../src/libs/actions/Report/index.ts" "@typescript-eslint/no-deprecated/reportAction.originalMessage" 1 "../../src/libs/actions/Report/index.ts" "@typescript-eslint/no-unsafe-type-assertion" 19 "../../src/libs/actions/Report/index.ts" "no-restricted-syntax" 7 -"../../src/libs/actions/Report/index.ts" "rulesdir/no-direct-personal-details-list" 8 +"../../src/libs/actions/Report/index.ts" "rulesdir/no-direct-personal-details-list" 1 "../../src/libs/actions/Report/index.ts" "rulesdir/no-onyx-connect" 3 "../../src/libs/actions/Report/MarkAllMessageAsRead.tsx" "no-restricted-syntax" 1 "../../src/libs/actions/ReportLayout.ts" "@typescript-eslint/no-unsafe-type-assertion" 1 @@ -708,7 +700,6 @@ "../../src/libs/actions/Search.ts" "@typescript-eslint/no-unsafe-type-assertion" 16 "../../src/libs/actions/Session/index.ts" "@typescript-eslint/no-unsafe-type-assertion" 3 "../../src/libs/actions/Session/index.ts" "no-restricted-syntax" 9 -"../../src/libs/actions/Session/index.ts" "rulesdir/no-direct-personal-details-list" 1 "../../src/libs/actions/Session/index.ts" "rulesdir/no-onyx-connect" 2 "../../src/libs/actions/SplitExpenses.ts" "@typescript-eslint/no-unsafe-type-assertion" 2 "../../src/libs/actions/StatsCounter.ts" "no-restricted-syntax" 2 @@ -720,7 +711,7 @@ "../../src/libs/actions/Task.ts" "@typescript-eslint/no-unsafe-type-assertion" 3 "../../src/libs/actions/Task.ts" "no-restricted-imports" 1 "../../src/libs/actions/Task.ts" "no-restricted-syntax" 5 -"../../src/libs/actions/Task.ts" "rulesdir/no-direct-personal-details-list" 2 +"../../src/libs/actions/Task.ts" "rulesdir/no-direct-personal-details-list" 1 "../../src/libs/actions/TaxRate.ts" "no-restricted-syntax" 1 "../../src/libs/actions/TeachersUnite.ts" "no-restricted-syntax" 1 "../../src/libs/actions/Transaction.ts" "@typescript-eslint/no-deprecated/getReportTransactions" 2 @@ -733,7 +724,6 @@ "../../src/libs/actions/User.ts" "@typescript-eslint/no-unsafe-type-assertion" 3 "../../src/libs/actions/User.ts" "no-restricted-imports" 1 "../../src/libs/actions/User.ts" "no-restricted-syntax" 7 -"../../src/libs/actions/User.ts" "rulesdir/no-direct-personal-details-list" 3 "../../src/libs/actions/VacationDelegate.ts" "no-restricted-syntax" 2 "../../src/libs/actions/Wallet.ts" "@typescript-eslint/no-unsafe-type-assertion" 4 "../../src/libs/actions/Wallet.ts" "no-restricted-syntax" 1 @@ -806,7 +796,7 @@ "../../src/libs/MergeTransactionUtils.ts" "@typescript-eslint/no-unsafe-type-assertion" 5 "../../src/libs/Middleware/HandleUnusedOptimisticID.ts" "@typescript-eslint/no-unsafe-type-assertion" 4 "../../src/libs/Middleware/HandleUnusedOptimisticID.ts" "no-restricted-syntax" 1 -"../../src/libs/Middleware/HandleUnusedOptimisticID.ts" "rulesdir/no-direct-personal-details-list" 4 +"../../src/libs/Middleware/HandleUnusedOptimisticID.ts" "rulesdir/no-direct-personal-details-list" 2 "../../src/libs/Middleware/Logging.ts" "@typescript-eslint/no-unsafe-type-assertion" 1 "../../src/libs/Middleware/Pagination.ts" "@typescript-eslint/no-unsafe-type-assertion" 5 "../../src/libs/Middleware/SaveResponseInOnyx.ts" "no-restricted-syntax" 1 @@ -926,7 +916,7 @@ "../../src/libs/ReportUtils.ts" "@typescript-eslint/no-deprecated/getReportTransactions" 17 "../../src/libs/ReportUtils.ts" "@typescript-eslint/no-deprecated/translateLocal" 8 "../../src/libs/ReportUtils.ts" "@typescript-eslint/no-unsafe-type-assertion" 32 -"../../src/libs/ReportUtils.ts" "rulesdir/no-direct-personal-details-list" 6 +"../../src/libs/ReportUtils.ts" "rulesdir/no-direct-personal-details-list" 1 "../../src/libs/ReportUtils.ts" "rulesdir/no-onyx-connect" 15 "../../src/libs/sanitizeLogParams.ts" "@typescript-eslint/no-unsafe-type-assertion" 2 "../../src/libs/SearchAutocompleteUtils.ts" "@typescript-eslint/no-unsafe-type-assertion" 3 @@ -1457,7 +1447,6 @@ "../../src/stories/DragAndDrop.stories.tsx" "@typescript-eslint/no-unsafe-type-assertion" 1 "../../src/stories/EReceipt.stories.tsx" "@typescript-eslint/no-unsafe-type-assertion" 1 "../../src/stories/Form.stories.tsx" "@typescript-eslint/no-unsafe-type-assertion" 10 -"../../src/stories/MenuItemComparison.stories.tsx" "rulesdir/no-direct-personal-details-list" 1 "../../src/stories/ReportActionItemImages.stories.tsx" "no-restricted-syntax" 1 "../../src/stories/TextInput.stories.tsx" "react-hooks/set-state-in-effect" 1 "../../src/styles/index.ts" "@typescript-eslint/no-unsafe-type-assertion" 1 diff --git a/src/libs/Middleware/HandleUnusedOptimisticID.ts b/src/libs/Middleware/HandleUnusedOptimisticID.ts index 7a33eaf4238d..5e55d4d474ff 100644 --- a/src/libs/Middleware/HandleUnusedOptimisticID.ts +++ b/src/libs/Middleware/HandleUnusedOptimisticID.ts @@ -1,6 +1,7 @@ import {prepareOnyxDataForCleanUpOptimisticParticipants} from '@libs/actions/Report'; import {WRITE_COMMANDS} from '@libs/API/types'; import deepReplaceKeysAndValues from '@libs/deepReplaceKeysAndValues'; +import {buildPersonalDetailsUpdate} from '@libs/PersonalDetailsUtils'; import type {Middleware} from '@libs/Request'; import * as PersistedRequests from '@userActions/PersistedRequests'; @@ -167,11 +168,7 @@ const handleUnusedOptimisticID: Middleware = (requestResponse, request, isFromSe participants: redundantParticipants, }, }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: redundantParticipants, - }, + buildPersonalDetailsUpdate(redundantParticipants), ); } @@ -184,11 +181,7 @@ const handleUnusedOptimisticID: Middleware = (requestResponse, request, isFromSe invitedEmails ?? [], ); if (!isEmptyObject(restoredPersonalDetails)) { - (response.onyxData as AnyOnyxUpdate[]).push({ - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: restoredPersonalDetails, - }); + (response.onyxData as AnyOnyxUpdate[]).push(buildPersonalDetailsUpdate(restoredPersonalDetails)); } } return response; diff --git a/src/libs/PersonalDetailsUtils.ts b/src/libs/PersonalDetailsUtils.ts index 71434f65e7c6..57c429482957 100644 --- a/src/libs/PersonalDetailsUtils.ts +++ b/src/libs/PersonalDetailsUtils.ts @@ -328,6 +328,9 @@ function buildPersonalDetailsUpdate(personalDetails: PersonalDetailsUpdate): Ony }; } +// The Onyx update produced by buildPersonalDetailsUpdate. Use it instead of naming the key in an OnyxUpdate generic. +type PersonalDetailsOnyxUpdate = ReturnType; + /** * Given a list of logins and accountIDs, return Onyx data for users with no existing personal details stored. These users might be brand new or unknown. * They will have an "optimistic" accountID that must be cleaned up later. @@ -357,21 +360,9 @@ function getPersonalDetailsOnyxDataForOptimisticUsers( personalDetailsCleanup[accountID] = null; } - const optimisticData: Array> = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: personalDetailsNew, - }, - ]; + const optimisticData: PersonalDetailsOnyxUpdate[] = [buildPersonalDetailsUpdate(personalDetailsNew)]; - const finallyData: Array> = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: personalDetailsCleanup, - }, - ]; + const finallyData: PersonalDetailsOnyxUpdate[] = [buildPersonalDetailsUpdate(personalDetailsCleanup)]; return { optimisticData, @@ -598,6 +589,8 @@ function areTravelPersonalDetailsMissing(privatePersonalDetails: OnyxEntry>; - successData: Array< - OnyxUpdate - >; - failureData: Array>; + successData: Array | PersonalDetailsOnyxUpdate>; + failureData: Array | PersonalDetailsOnyxUpdate>; optimisticAssigneeAddComment?: OptimisticReportAction; optimisticChatCreatedReportAction?: OptimisticCreatedReportAction; }; @@ -11217,9 +11223,9 @@ function getTaskAssigneeChatOnyxData({ const currentTime = DateUtils.getDBTime(); const optimisticData: Array> = []; const successData: Array< - OnyxUpdate + OnyxUpdate | PersonalDetailsOnyxUpdate > = []; - const failureData: Array> = []; + const failureData: Array | PersonalDetailsOnyxUpdate> = []; // You're able to assign a task to someone you haven't chatted with before - so we need to optimistically create the chat and the chat reportActions // Only add the assignee chat report to onyx if we haven't already set it optimistically @@ -11278,13 +11284,9 @@ function getTaskAssigneeChatOnyxData({ // If assignee is created optimistically, we need to clear the optimistic personal details to prevent duplication with real data sent from BE. if (isOptimisticPersonalDetail(assigneeAccountID)) { successData.push( - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [assigneeAccountID]: null, - }, - }, + buildPersonalDetailsUpdate({ + [assigneeAccountID]: null, + }), { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`, @@ -11308,13 +11310,9 @@ function getTaskAssigneeChatOnyxData({ value: {[optimisticChatCreatedReportAction.reportActionID]: {pendingAction: null}}, }, // If we failed, we want to remove the optimistic personal details as it was likely due to an invalid login - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [assigneeAccountID]: null, - }, - }, + buildPersonalDetailsUpdate({ + [assigneeAccountID]: null, + }), ); } @@ -12347,16 +12345,17 @@ function prepareOnboardingOnyxData({ } else { assignedGuideAccountID = assignedGuideEmail === CONST.EMAIL.QA_GUIDE ? CONST.ACCOUNT_ID.QA_GUIDE : generateAccountID(assignedGuideEmail); isOptimisticAssignedGuide = !assignedGuidePersonalDetail && assignedGuideEmail !== CONST.EMAIL.QA_GUIDE; - // eslint-disable-next-line rulesdir/prefer-actions-set-data - Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, { - [assignedGuideAccountID]: { - accountID: assignedGuideAccountID, - isOptimisticPersonalDetail: isOptimisticAssignedGuide, - login: assignedGuideEmail, - displayName: assignedGuideEmail, - avatar: getDefaultAvatarURL({accountID: assignedGuideAccountID, accountEmail: assignedGuideEmail}), - }, - }); + Onyx.update([ + buildPersonalDetailsUpdate({ + [assignedGuideAccountID]: { + accountID: assignedGuideAccountID, + isOptimisticPersonalDetail: isOptimisticAssignedGuide, + login: assignedGuideEmail, + displayName: assignedGuideEmail, + avatar: getDefaultAvatarURL({accountID: assignedGuideAccountID, accountEmail: assignedGuideEmail}), + }, + }), + ]); } const actorAccountID = shouldPostTasksInAdminsRoom ? assignedGuideAccountID : CONST.ACCOUNT_ID.CONCIERGE; const currentUserEmailToUse = currentUserEmail ?? deprecatedCurrentUserEmail; @@ -12716,9 +12715,8 @@ function prepareOnboardingOnyxData({ }); } - const successData: Array< - TupleToUnion | OnyxUpdate - > = shouldDeferOptimisticTasks ? [] : [...tasksForSuccessData]; + const successData: Array | OnyxUpdate | PersonalDetailsOnyxUpdate> = + shouldDeferOptimisticTasks ? [] : [...tasksForSuccessData]; if (message && !shouldDeferOptimisticTasks) { successData.push({ @@ -12752,7 +12750,8 @@ function prepareOnboardingOnyxData({ const failureData: Array< | TupleToUnion - | OnyxUpdate + | OnyxUpdate + | PersonalDetailsOnyxUpdate > = shouldDeferOptimisticTasks ? [] : [...tasksForFailureData]; failureData.push( { @@ -12946,21 +12945,9 @@ function prepareOnboardingOnyxData({ } if (isOptimisticAssignedGuide) { - successData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [assignedGuideAccountID]: null, - }, - }); + successData.push(buildPersonalDetailsUpdate({[assignedGuideAccountID]: null})); - failureData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [assignedGuideAccountID]: null, - }, - }); + failureData.push(buildPersonalDetailsUpdate({[assignedGuideAccountID]: null})); } return {optimisticData, successData, failureData, guidedSetupData, actorAccountID, selfDMParameters, optimisticConciergeReportActionID}; diff --git a/src/libs/actions/Agent.ts b/src/libs/actions/Agent.ts index 5f59ffd24698..5f3ba73eaa9f 100644 --- a/src/libs/actions/Agent.ts +++ b/src/libs/actions/Agent.ts @@ -7,6 +7,7 @@ import type {CustomRNImageManipulatorResult} from '@libs/cropOrRotateImage/types import {getMicroSecondOnyxErrorWithTranslationKey} from '@libs/ErrorUtils'; import Navigation from '@libs/Navigation/Navigation'; import {rand64} from '@libs/NumberUtils'; +import {buildPersonalDetailsUpdate} from '@libs/PersonalDetailsUtils'; import {buildOptimisticChatReport, buildOptimisticCreatedReportAction, generateReportID} from '@libs/ReportUtils'; import type {AvatarSource} from '@libs/UserAvatarUtils'; @@ -82,11 +83,7 @@ function createAgent( }); const optimisticData: AnyOnyxUpdate[] = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: {[optimisticAccountID]: optimisticPersonalDetail}, - }, + buildPersonalDetailsUpdate({[optimisticAccountID]: optimisticPersonalDetail}), { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.SHARED_NVP_AGENT_PROMPT}${optimisticAccountID}`, @@ -125,11 +122,7 @@ function createAgent( ]; const failureData: AnyOnyxUpdate[] = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: {[optimisticAccountID]: optimisticPersonalDetail}, - }, + buildPersonalDetailsUpdate({[optimisticAccountID]: optimisticPersonalDetail}), { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.SHARED_NVP_AGENT_PROMPT}${optimisticAccountID}`, @@ -179,7 +172,7 @@ function clearNewAgentTemplate() { } function clearAgentError(optimisticAccountID: number) { - Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {[optimisticAccountID]: null}); + Onyx.update([buildPersonalDetailsUpdate({[optimisticAccountID]: null})]); Onyx.set(`${ONYXKEYS.COLLECTION.SHARED_NVP_AGENT_PROMPT}${optimisticAccountID}`, null); } @@ -203,11 +196,7 @@ function updateAgentName(accountID: number, firstName: string, originalFirstName // The caller may still hold an optimistic accountID that was already reconciled while its screen was open. const agentAccountID = resolveAgentAccountID(accountID); const optimisticData: AnyOnyxUpdate[] = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: {[agentAccountID]: {displayName: firstName}}, - }, + buildPersonalDetailsUpdate({[agentAccountID]: {displayName: firstName}}), { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.SHARED_NVP_AGENT_PROMPT}${agentAccountID}`, @@ -224,11 +213,7 @@ function updateAgentName(accountID: number, firstName: string, originalFirstName ]; const failureData: AnyOnyxUpdate[] = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: {[agentAccountID]: {displayName: originalFirstName}}, - }, + buildPersonalDetailsUpdate({[agentAccountID]: {displayName: originalFirstName}}), { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.SHARED_NVP_AGENT_PROMPT}${agentAccountID}`, @@ -284,18 +269,14 @@ function updateAgentAvatar( // The caller may still hold an optimistic accountID that was already reconciled while its screen was open. const agentAccountID = resolveAgentAccountID(accountID); const optimisticData: AnyOnyxUpdate[] = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [agentAccountID]: { - avatar: update.uri, - avatarThumbnail: update.uri, - pendingFields: {avatar: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}, - errorFields: {avatar: null}, - }, + buildPersonalDetailsUpdate({ + [agentAccountID]: { + avatar: update.uri, + avatarThumbnail: update.uri, + pendingFields: {avatar: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}, + errorFields: {avatar: null}, }, - }, + }), { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.SHARED_NVP_AGENT_PROMPT}${agentAccountID}`, @@ -304,16 +285,12 @@ function updateAgentAvatar( ]; const successData: AnyOnyxUpdate[] = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [agentAccountID]: { - pendingFields: {avatar: null}, - errorFields: {avatar: null}, - }, + buildPersonalDetailsUpdate({ + [agentAccountID]: { + pendingFields: {avatar: null}, + errorFields: {avatar: null}, }, - }, + }), { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.SHARED_NVP_AGENT_PROMPT}${agentAccountID}`, @@ -322,18 +299,14 @@ function updateAgentAvatar( ]; const failureData: AnyOnyxUpdate[] = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [agentAccountID]: { - avatar: currentAvatar, - avatarThumbnail: typeof currentAvatar === 'string' ? currentAvatar : undefined, - pendingFields: {avatar: null}, - errorFields: {avatar: null}, - }, + buildPersonalDetailsUpdate({ + [agentAccountID]: { + avatar: currentAvatar, + avatarThumbnail: typeof currentAvatar === 'string' ? currentAvatar : undefined, + pendingFields: {avatar: null}, + errorFields: {avatar: null}, }, - }, + }), { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.SHARED_NVP_AGENT_PROMPT}${agentAccountID}`, @@ -363,11 +336,7 @@ function deleteAgent(accountID: number, agentLogin?: string, allPolicies?: OnyxC key: `${ONYXKEYS.COLLECTION.SHARED_NVP_AGENT_PROMPT}${agentAccountID}`, value: null, }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: {[agentAccountID]: null}, - }, + buildPersonalDetailsUpdate({[agentAccountID]: null}), ]; const failureData: AnyOnyxUpdate[] = [ diff --git a/src/libs/actions/Domain.ts b/src/libs/actions/Domain.ts index 53cdc46dd4e9..b5632af774f9 100644 --- a/src/libs/actions/Domain.ts +++ b/src/libs/actions/Domain.ts @@ -27,6 +27,8 @@ import {USER_AVATARS} from '@libs/Avatars/UserAvatarCatalog'; import {getMicroSecondOnyxErrorWithTranslationKey} from '@libs/ErrorUtils'; import fileDownload from '@libs/fileDownload'; import enhanceParameters from '@libs/Network/enhanceParameters'; +import {buildPersonalDetailsUpdate} from '@libs/PersonalDetailsUtils'; +import type {PersonalDetailsOnyxUpdate} from '@libs/PersonalDetailsUtils'; import {getDefaultAvatarName} from '@libs/UserAvatarUtils'; import {generateAccountID} from '@libs/UserUtils'; @@ -700,13 +702,13 @@ function addAdminToDomain(domainAccountID: number, accountID: number, targetEmai const PERMISSION_KEY = `${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}${accountID}`; const optimisticData: Array< - OnyxUpdate< - | typeof ONYXKEYS.COLLECTION.DOMAIN - | typeof ONYXKEYS.COLLECTION.DOMAIN_PENDING_ACTIONS - | typeof ONYXKEYS.COLLECTION.DOMAIN_ERRORS - | typeof ONYXKEYS.PERSONAL_DETAILS_LIST - | typeof ONYXKEYS.COLLECTION.DOMAIN_HIGHLIGHT_ITEMS - > + | OnyxUpdate< + | typeof ONYXKEYS.COLLECTION.DOMAIN + | typeof ONYXKEYS.COLLECTION.DOMAIN_PENDING_ACTIONS + | typeof ONYXKEYS.COLLECTION.DOMAIN_ERRORS + | typeof ONYXKEYS.COLLECTION.DOMAIN_HIGHLIGHT_ITEMS + > + | PersonalDetailsOnyxUpdate > = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -745,22 +747,20 @@ function addAdminToDomain(domainAccountID: number, accountID: number, targetEmai ]; if (isOptimisticAccount) { - optimisticData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.PERSONAL_DETAILS_LIST}`, - value: { + optimisticData.push( + buildPersonalDetailsUpdate({ [accountID]: { accountID, login: targetEmail, displayName: targetEmail, isOptimisticPersonalDetail: true, }, - }, - }); + }), + ); } const successData: Array< - OnyxUpdate + OnyxUpdate | PersonalDetailsOnyxUpdate > = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -814,13 +814,7 @@ function addAdminToDomain(domainAccountID: number, accountID: number, targetEmai ]; if (isOptimisticAccount) { - successData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.PERSONAL_DETAILS_LIST}`, - value: { - [accountID]: null, - }, - }); + successData.push(buildPersonalDetailsUpdate({[accountID]: null})); } const params: AddAdminToDomainParams = { @@ -983,9 +977,7 @@ function clearAdminError(domainAccountID: number, accountID: number, isOptimisti // The account only ever existed to carry the failed add, so dismissing it takes the placeholder details with it. if (isOptimisticAccount) { - Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, { - [accountID]: null, - }); + Onyx.update([buildPersonalDetailsUpdate({[accountID]: null})]); } Onyx.merge(`${ONYXKEYS.COLLECTION.DOMAIN_ERRORS}${domainAccountID}`, { @@ -1269,13 +1261,13 @@ function addMemberToDomain(domainAccountID: number, email: string, defaultSecuri const optimisticAccountID = generateAccountID(email); const optimisticData: Array< - OnyxUpdate< - | typeof ONYXKEYS.COLLECTION.DOMAIN_PENDING_ACTIONS - | typeof ONYXKEYS.COLLECTION.DOMAIN_ERRORS - | typeof ONYXKEYS.PERSONAL_DETAILS_LIST - | typeof ONYXKEYS.COLLECTION.DOMAIN - | typeof ONYXKEYS.COLLECTION.DOMAIN_HIGHLIGHT_ITEMS - > + | OnyxUpdate< + | typeof ONYXKEYS.COLLECTION.DOMAIN_PENDING_ACTIONS + | typeof ONYXKEYS.COLLECTION.DOMAIN_ERRORS + | typeof ONYXKEYS.COLLECTION.DOMAIN + | typeof ONYXKEYS.COLLECTION.DOMAIN_HIGHLIGHT_ITEMS + > + | PersonalDetailsOnyxUpdate > = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -1288,18 +1280,14 @@ function addMemberToDomain(domainAccountID: number, email: string, defaultSecuri }, } as PrefixedRecord>, }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.PERSONAL_DETAILS_LIST}`, - value: { - [optimisticAccountID]: { - accountID: optimisticAccountID, - login: email, - avatar: USER_AVATARS.getURL(getDefaultAvatarName({accountID: optimisticAccountID, accountEmail: email})) ?? '', - isOptimisticPersonalDetail: true, - }, + buildPersonalDetailsUpdate({ + [optimisticAccountID]: { + accountID: optimisticAccountID, + login: email, + avatar: USER_AVATARS.getURL(getDefaultAvatarName({accountID: optimisticAccountID, accountEmail: email})) ?? '', + isOptimisticPersonalDetail: true, }, - }, + }), { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.DOMAIN_PENDING_ACTIONS}${domainAccountID}`, @@ -1330,7 +1318,7 @@ function addMemberToDomain(domainAccountID: number, email: string, defaultSecuri ]; const successData: Array< - OnyxUpdate + OnyxUpdate | PersonalDetailsOnyxUpdate > = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -1354,13 +1342,7 @@ function addMemberToDomain(domainAccountID: number, email: string, defaultSecuri }, }, }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.PERSONAL_DETAILS_LIST}`, - value: { - [optimisticAccountID]: null, - }, - }, + buildPersonalDetailsUpdate({[optimisticAccountID]: null}), { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.DOMAIN}${domainAccountID}`, @@ -1430,9 +1412,7 @@ function clearDomainMemberError(domainAccountID: number, accountID: number, emai } as PrefixedRecord>); } - Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, { - [accountID]: null, - }); + Onyx.update([buildPersonalDetailsUpdate({[accountID]: null})]); } Onyx.merge(`${ONYXKEYS.COLLECTION.DOMAIN_ERRORS}${domainAccountID}`, { diff --git a/src/libs/actions/IOU/MoneyRequestBuilder.ts b/src/libs/actions/IOU/MoneyRequestBuilder.ts index 74e848842c5e..9742e5129f80 100644 --- a/src/libs/actions/IOU/MoneyRequestBuilder.ts +++ b/src/libs/actions/IOU/MoneyRequestBuilder.ts @@ -10,6 +10,7 @@ import {updateIOUOwnerAndTotal} from '@libs/IOUUtils'; import {translateLocal} from '@libs/Localize'; import {buildOptimisticNextStep} from '@libs/NextStepUtils'; import {rand64} from '@libs/NumberUtils'; +import {buildPersonalDetailsUpdate} from '@libs/PersonalDetailsUtils'; import {addSMSDomainIfPhoneNumber} from '@libs/PhoneNumber'; import {getDistanceRateCustomUnit, hasDependentTags, isGroupPolicy} from '@libs/PolicyUtils'; import {getOriginalMessage, getReportActionHtml, getReportActionText, isReportPreviewAction} from '@libs/ReportActionsUtils'; @@ -801,16 +802,8 @@ function buildOnyxDataForMoneyRequest(moneyRequestParams: BuildOnyxDataForMoneyR redundantParticipants[accountID] = null; } - onyxData.optimisticData?.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: personalDetailListAction, - }); - onyxData.successData?.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: successPersonalDetailListAction, - }); + onyxData.optimisticData?.push(buildPersonalDetailsUpdate(personalDetailListAction)); + onyxData.successData?.push(buildPersonalDetailsUpdate(successPersonalDetailListAction)); } if (!isEmptyObject(nextStep)) { diff --git a/src/libs/actions/IOU/SendInvoice.ts b/src/libs/actions/IOU/SendInvoice.ts index b559eb2ab9f9..c5c3d5fc864a 100644 --- a/src/libs/actions/IOU/SendInvoice.ts +++ b/src/libs/actions/IOU/SendInvoice.ts @@ -9,6 +9,7 @@ import DateUtils from '@libs/DateUtils'; import {deferOrExecuteWrite} from '@libs/deferredLayoutWrite'; import {getMicroSecondOnyxErrorWithTranslationKey} from '@libs/ErrorUtils'; import Log from '@libs/Log'; +import {buildPersonalDetailsUpdate} from '@libs/PersonalDetailsUtils'; import {addSMSDomainIfPhoneNumber} from '@libs/PhoneNumber'; import {resolveCurrentTaxCode} from '@libs/PolicyUtils'; import {getReportActionHtml, getReportActionText} from '@libs/ReportActionsUtils'; @@ -325,16 +326,8 @@ function buildOnyxDataForInvoice( redundantParticipants[accountID] = null; } - optimisticData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: optimisticDataParams.personalDetailListAction, - }); - successData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: successPersonalDetailListAction, - }); + optimisticData.push(buildPersonalDetailsUpdate(optimisticDataParams.personalDetailListAction)); + successData.push(buildPersonalDetailsUpdate(successPersonalDetailListAction)); } successData.push( diff --git a/src/libs/actions/IOU/SendMoney.ts b/src/libs/actions/IOU/SendMoney.ts index c485f862ef50..7a990a340042 100644 --- a/src/libs/actions/IOU/SendMoney.ts +++ b/src/libs/actions/IOU/SendMoney.ts @@ -8,6 +8,8 @@ import {WRITE_COMMANDS} from '@libs/API/types'; import DateUtils from '@libs/DateUtils'; import {deferOrExecuteWrite} from '@libs/deferredLayoutWrite'; import {getMicroSecondOnyxErrorWithTranslationKey} from '@libs/ErrorUtils'; +import {buildPersonalDetailsUpdate} from '@libs/PersonalDetailsUtils'; +import type {PersonalDetailsOnyxUpdate} from '@libs/PersonalDetailsUtils'; import {addSMSDomainIfPhoneNumber} from '@libs/PhoneNumber'; import {getReportActionHtml, getReportActionText} from '@libs/ReportActionsUtils'; import type {OptionData} from '@libs/ReportUtils'; @@ -265,7 +267,7 @@ function getSendMoneyParams({ > = []; // Add optimistic personal details for recipient - let optimisticPersonalDetailListData: OnyxUpdate | null = null; + let optimisticPersonalDetailListData: PersonalDetailsOnyxUpdate | null = null; const optimisticPersonalDetailListAction = isNewChat ? { [recipientAccountID]: { @@ -289,16 +291,8 @@ function getSendMoneyParams({ redundantParticipants[accountID] = null; } - optimisticPersonalDetailListData = { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: optimisticPersonalDetailListAction, - }; - successData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: successPersonalDetailListAction, - }); + optimisticPersonalDetailListData = buildPersonalDetailsUpdate(optimisticPersonalDetailListAction); + successData.push(buildPersonalDetailsUpdate(successPersonalDetailListAction)); } successData.push( diff --git a/src/libs/actions/IOU/Split.ts b/src/libs/actions/IOU/Split.ts index 9ea9f3c65239..df41ecfd3a45 100644 --- a/src/libs/actions/IOU/Split.ts +++ b/src/libs/actions/IOU/Split.ts @@ -16,6 +16,7 @@ import Navigation from '@libs/Navigation/Navigation'; import {roundToTwoDecimalPlaces} from '@libs/NumberUtils'; import * as NumberUtils from '@libs/NumberUtils'; import Parser from '@libs/Parser'; +import {buildPersonalDetailsUpdate} from '@libs/PersonalDetailsUtils'; import {addSMSDomainIfPhoneNumber} from '@libs/PhoneNumber'; import {getReportActionHtml, getReportActionText} from '@libs/ReportActionsUtils'; import { @@ -786,10 +787,8 @@ function startSplitBill({ const participantPersonalDetails = getAllPersonalDetails()[participant?.accountID ?? CONST.DEFAULT_NUMBER_ID]; if (!participantPersonalDetails) { - optimisticData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { + optimisticData.push( + buildPersonalDetailsUpdate({ [accountID]: { accountID, // Disabling this line since participant.displayName can be an empty string @@ -800,8 +799,8 @@ function startSplitBill({ login: participant.login || participant.text, isOptimisticPersonalDetail: true, }, - }, - }); + }), + ); // BE will send different participants. We clear the optimistic ones to avoid duplicated entries redundantParticipants[accountID] = null; } diff --git a/src/libs/actions/PersonalDetails.ts b/src/libs/actions/PersonalDetails.ts index d1a53eb071ea..2e566c882397 100644 --- a/src/libs/actions/PersonalDetails.ts +++ b/src/libs/actions/PersonalDetails.ts @@ -78,15 +78,11 @@ function updatePronouns(pronouns: string, currentUserAccountID: number) { API.write(WRITE_COMMANDS.UPDATE_PRONOUNS, parameters, { optimisticData: [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [currentUserAccountID]: { - pronouns, - }, + PersonalDetailsUtils.buildPersonalDetailsUpdate({ + [currentUserAccountID]: { + pronouns, }, - }, + }), ], }); } @@ -124,9 +120,11 @@ function setDisplayName(firstName: string, lastName: string, formatPhoneNumber: return; } - Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, { - [currentUserPersonalDetails.accountID]: buildOptimisticDisplayNameDetails(firstName, lastName, formatPhoneNumber, currentUserPersonalDetails), - }); + Onyx.update([ + PersonalDetailsUtils.buildPersonalDetailsUpdate({ + [currentUserPersonalDetails.accountID]: buildOptimisticDisplayNameDetails(firstName, lastName, formatPhoneNumber, currentUserPersonalDetails), + }), + ]); } function updateDisplayName(firstName: string, lastName: string, formatPhoneNumber: LocaleContextProps['formatPhoneNumber'], currentUserPersonalDetails: DisplayNamePersonalDetails) { @@ -139,27 +137,19 @@ function updateDisplayName(firstName: string, lastName: string, formatPhoneNumbe API.write(WRITE_COMMANDS.UPDATE_DISPLAY_NAME, parameters, { optimisticData: [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [currentUserPersonalDetails.accountID]: optimisticDetails, - }, - }, + PersonalDetailsUtils.buildPersonalDetailsUpdate({ + [currentUserPersonalDetails.accountID]: optimisticDetails, + }), ], failureData: [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [currentUserPersonalDetails.accountID]: { - firstName: currentUserPersonalDetails.firstName ?? null, - lastName: currentUserPersonalDetails.lastName ?? null, - displayName: currentUserPersonalDetails.displayName ?? null, - ...(optimisticDetails.avatar && {avatar: currentUserPersonalDetails.avatar}), - }, + PersonalDetailsUtils.buildPersonalDetailsUpdate({ + [currentUserPersonalDetails.accountID]: { + firstName: currentUserPersonalDetails.firstName ?? null, + lastName: currentUserPersonalDetails.lastName ?? null, + displayName: currentUserPersonalDetails.displayName ?? null, + ...(optimisticDetails.avatar && {avatar: currentUserPersonalDetails.avatar}), }, - }, + }), ], }); } @@ -172,7 +162,7 @@ function updateLegalName( shouldGoBack = true, ) { const parameters: UpdateLegalNameParams = {legalFirstName, legalLastName}; - const optimisticData: Array> = [ + const optimisticData: Array | PersonalDetailsUtils.PersonalDetailsOnyxUpdate> = [ { onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.PRIVATE_PERSONAL_DETAILS, @@ -184,10 +174,8 @@ function updateLegalName( ]; // In case the user does not have a display name, we will update the display name based on the legal name if (!currentUserPersonalDetail?.firstName && !currentUserPersonalDetail?.lastName) { - optimisticData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { + optimisticData.push( + PersonalDetailsUtils.buildPersonalDetailsUpdate({ [currentUserPersonalDetail.accountID]: { displayName: PersonalDetailsUtils.createDisplayName( currentUserPersonalDetail.email ?? '', @@ -200,8 +188,8 @@ function updateLegalName( firstName: legalFirstName, lastName: legalLastName, }, - }, - }); + }), + ); } API.write(WRITE_COMMANDS.UPDATE_LEGAL_NAME, parameters, { optimisticData, @@ -271,15 +259,11 @@ function updateAutomaticTimezone(timezone: Timezone, currentUserAccountID: numbe API.write(WRITE_COMMANDS.UPDATE_AUTOMATIC_TIMEZONE, parameters, { optimisticData: [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [currentUserAccountID]: { - timezone: formattedTimezone, - }, + PersonalDetailsUtils.buildPersonalDetailsUpdate({ + [currentUserAccountID]: { + timezone: formattedTimezone, }, - }, + }), ], }); } @@ -300,15 +284,11 @@ function updateSelectedTimezone(selectedTimezone: SelectedTimezone, currentUserA if (currentUserAccountID) { API.write(WRITE_COMMANDS.UPDATE_SELECTED_TIMEZONE, parameters, { optimisticData: [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [currentUserAccountID]: { - timezone, - }, + PersonalDetailsUtils.buildPersonalDetailsUpdate({ + [currentUserAccountID]: { + timezone, }, - }, + }), ], }); } @@ -391,54 +371,42 @@ function updateAvatar( return; } - const optimisticData: Array> = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [currentUserPersonalDetails.accountID]: { - avatar: file.uri, - avatarThumbnail: file.uri, - originalFileName: file.name, - errorFields: { - avatar: null, - }, - pendingFields: { - avatar: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, - originalFileName: null, - }, - fallbackIcon: file.uri, + const optimisticData: PersonalDetailsUtils.PersonalDetailsOnyxUpdate[] = [ + PersonalDetailsUtils.buildPersonalDetailsUpdate({ + [currentUserPersonalDetails.accountID]: { + avatar: file.uri, + avatarThumbnail: file.uri, + originalFileName: file.name, + errorFields: { + avatar: null, }, + pendingFields: { + avatar: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, + originalFileName: null, + }, + fallbackIcon: file.uri, }, - }, + }), ]; - const successData: Array> = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [currentUserPersonalDetails.accountID]: { - pendingFields: { - avatar: null, - }, + const successData: PersonalDetailsUtils.PersonalDetailsOnyxUpdate[] = [ + PersonalDetailsUtils.buildPersonalDetailsUpdate({ + [currentUserPersonalDetails.accountID]: { + pendingFields: { + avatar: null, }, }, - }, + }), ]; - const failureData: Array> = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [currentUserPersonalDetails.accountID]: { - avatar: currentUserPersonalDetails?.avatar, - avatarThumbnail: currentUserPersonalDetails?.avatarThumbnail ?? currentUserPersonalDetails?.avatar, - pendingFields: { - avatar: null, - }, - } as OnyxEntry>, - }, - }, + const failureData: PersonalDetailsUtils.PersonalDetailsOnyxUpdate[] = [ + PersonalDetailsUtils.buildPersonalDetailsUpdate({ + [currentUserPersonalDetails.accountID]: { + avatar: currentUserPersonalDetails?.avatar, + avatarThumbnail: currentUserPersonalDetails?.avatarThumbnail ?? currentUserPersonalDetails?.avatar, + pendingFields: { + avatar: null, + }, + } as OnyxEntry>, + }), ]; const parameters: UpdateUserAvatarParams = isDefaultAvatarResult(file) ? {customExpensifyAvatarID: file.customExpensifyAvatarID} : {file}; @@ -465,59 +433,47 @@ function updateAvatarStyle( // The backend clears any stored avatar on a color pick; a generated letter URL is the materialized form of "no stored avatar". const willClearAvatar = !!currentUserPersonalDetails.avatar && !UserAvatarUtils.isGeneratedLetterAvatarURL(currentUserPersonalDetails.avatar); - const optimisticData: Array> = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [currentUserPersonalDetails.accountID]: { - avatarStyle: {color}, - pendingFields: { - avatarStyle: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, - }, - ...(willClearAvatar && { - avatar: UserAvatarUtils.getDefaultAvatarURL({ - accountID: currentUserPersonalDetails.accountID, - accountEmail: currentUserPersonalDetails.email, - firstName: currentUserPersonalDetails.firstName, - lastName: currentUserPersonalDetails.lastName, - }), - fallbackIcon: null, - }), + const optimisticData: PersonalDetailsUtils.PersonalDetailsOnyxUpdate[] = [ + PersonalDetailsUtils.buildPersonalDetailsUpdate({ + [currentUserPersonalDetails.accountID]: { + avatarStyle: {color}, + pendingFields: { + avatarStyle: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, }, + ...(willClearAvatar && { + avatar: UserAvatarUtils.getDefaultAvatarURL({ + accountID: currentUserPersonalDetails.accountID, + accountEmail: currentUserPersonalDetails.email, + firstName: currentUserPersonalDetails.firstName, + lastName: currentUserPersonalDetails.lastName, + }), + fallbackIcon: null, + }), }, - }, + }), ]; - const successData: Array> = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [currentUserPersonalDetails.accountID]: { - pendingFields: { - avatarStyle: null, - }, + const successData: PersonalDetailsUtils.PersonalDetailsOnyxUpdate[] = [ + PersonalDetailsUtils.buildPersonalDetailsUpdate({ + [currentUserPersonalDetails.accountID]: { + pendingFields: { + avatarStyle: null, }, }, - }, + }), ]; - const failureData: Array> = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [currentUserPersonalDetails.accountID]: { - avatarStyle: currentUserPersonalDetails.avatarStyle ?? null, - pendingFields: { - avatarStyle: null, - }, - ...(willClearAvatar && { - avatar: currentUserPersonalDetails.avatar, - fallbackIcon: currentUserPersonalDetails.fallbackIcon, - }), + const failureData: PersonalDetailsUtils.PersonalDetailsOnyxUpdate[] = [ + PersonalDetailsUtils.buildPersonalDetailsUpdate({ + [currentUserPersonalDetails.accountID]: { + avatarStyle: currentUserPersonalDetails.avatarStyle ?? null, + pendingFields: { + avatarStyle: null, }, + ...(willClearAvatar && { + avatar: currentUserPersonalDetails.avatar, + fallbackIcon: currentUserPersonalDetails.fallbackIcon, + }), }, - }, + }), ]; const parameters: UpdateUserAvatarParams = {color}; @@ -545,29 +501,21 @@ function deleteAvatar(currentUserPersonalDetails: Pick> = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [currentUserPersonalDetails.accountID]: { - avatar: defaultAvatar, - fallbackIcon: null, - }, + const optimisticData: PersonalDetailsUtils.PersonalDetailsOnyxUpdate[] = [ + PersonalDetailsUtils.buildPersonalDetailsUpdate({ + [currentUserPersonalDetails.accountID]: { + avatar: defaultAvatar, + fallbackIcon: null, }, - }, + }), ]; - const failureData: Array> = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [currentUserPersonalDetails.accountID]: { - avatar: currentUserPersonalDetails?.avatar, - fallbackIcon: currentUserPersonalDetails?.fallbackIcon, - }, + const failureData: PersonalDetailsUtils.PersonalDetailsOnyxUpdate[] = [ + PersonalDetailsUtils.buildPersonalDetailsUpdate({ + [currentUserPersonalDetails.accountID]: { + avatar: currentUserPersonalDetails?.avatar, + fallbackIcon: currentUserPersonalDetails?.fallbackIcon, }, - }, + }), ]; API.write(WRITE_COMMANDS.DELETE_USER_AVATAR, null, { diff --git a/src/libs/actions/Policy/Member.ts b/src/libs/actions/Policy/Member.ts index 1bc23f365623..f18367b6b1f2 100644 --- a/src/libs/actions/Policy/Member.ts +++ b/src/libs/actions/Policy/Member.ts @@ -18,6 +18,8 @@ import fileDownload from '@libs/fileDownload'; import Log from '@libs/Log'; import enhanceParameters from '@libs/Network/enhanceParameters'; import Parser from '@libs/Parser'; +import {buildPersonalDetailsUpdate} from '@libs/PersonalDetailsUtils'; +import type {PersonalDetailsOnyxUpdate} from '@libs/PersonalDetailsUtils'; import * as PhoneNumber from '@libs/PhoneNumber'; import {getDefaultApprover, isControlPolicy, isPolicyAdmin, isSubmitPolicy} from '@libs/PolicyUtils'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; @@ -895,15 +897,15 @@ function buildAddMembersToWorkspaceOnyxData( } const optimisticData: Array< - OnyxUpdate< - | typeof ONYXKEYS.COLLECTION.POLICY - | typeof ONYXKEYS.PERSONAL_DETAILS_LIST - | typeof ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS - | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS - | typeof ONYXKEYS.COLLECTION.REPORT_METADATA - | typeof ONYXKEYS.COLLECTION.REPORT - | typeof ONYXKEYS.COLLECTION.REPORT_DRAFT - > + | OnyxUpdate< + | typeof ONYXKEYS.COLLECTION.POLICY + | typeof ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS + | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS + | typeof ONYXKEYS.COLLECTION.REPORT_METADATA + | typeof ONYXKEYS.COLLECTION.REPORT + | typeof ONYXKEYS.COLLECTION.REPORT_DRAFT + > + | PersonalDetailsOnyxUpdate > = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -924,13 +926,8 @@ function buildAddMembersToWorkspaceOnyxData( ); const successData: Array< - OnyxUpdate< - | typeof ONYXKEYS.COLLECTION.POLICY - | typeof ONYXKEYS.PERSONAL_DETAILS_LIST - | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS - | typeof ONYXKEYS.COLLECTION.REPORT_METADATA - | typeof ONYXKEYS.COLLECTION.REPORT - > + | OnyxUpdate + | PersonalDetailsOnyxUpdate > = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -949,15 +946,15 @@ function buildAddMembersToWorkspaceOnyxData( ); const failureData: Array< - OnyxUpdate< - | typeof ONYXKEYS.COLLECTION.POLICY - | typeof ONYXKEYS.PERSONAL_DETAILS_LIST - | typeof ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS - | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS - | typeof ONYXKEYS.COLLECTION.REPORT_METADATA - | typeof ONYXKEYS.COLLECTION.RAM_ONLY_REPORT_LOADING_STATE - | typeof ONYXKEYS.COLLECTION.REPORT - > + | OnyxUpdate< + | typeof ONYXKEYS.COLLECTION.POLICY + | typeof ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS + | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS + | typeof ONYXKEYS.COLLECTION.REPORT_METADATA + | typeof ONYXKEYS.COLLECTION.RAM_ONLY_REPORT_LOADING_STATE + | typeof ONYXKEYS.COLLECTION.REPORT + > + | PersonalDetailsOnyxUpdate > = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -1235,9 +1232,7 @@ function clearAddMemberError(policyID: string, login: string, accountID: number) [login]: null, }, }); - Onyx.merge(`${ONYXKEYS.PERSONAL_DETAILS_LIST}`, { - [accountID]: null, - }); + Onyx.update([buildPersonalDetailsUpdate({[accountID]: null})]); } /** diff --git a/src/libs/actions/Report/index.ts b/src/libs/actions/Report/index.ts index 4ffad219f1f0..48eb53bd7374 100644 --- a/src/libs/actions/Report/index.ts +++ b/src/libs/actions/Report/index.ts @@ -1059,13 +1059,8 @@ function addActions({ } const optimisticData: Array< - OnyxUpdate< - | typeof ONYXKEYS.COLLECTION.REPORT - | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS - | typeof ONYXKEYS.COLLECTION.REPORT_METADATA - | typeof ONYXKEYS.PERSONAL_DETAILS_LIST - | typeof ONYXKEYS.COLLECTION.SNAPSHOT - > + | OnyxUpdate + | PersonalDetailsUtils.PersonalDetailsOnyxUpdate > = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -1149,11 +1144,7 @@ function addActions({ if (DateUtils.canUpdateTimezone() && currentUserAccountID) { const timezone = DateUtils.getCurrentTimezone(timezoneParam); parameters.timezone = JSON.stringify(timezone); - optimisticData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: {[currentUserAccountID]: {timezone}}, - }); + optimisticData.push(PersonalDetailsUtils.buildPersonalDetailsUpdate({[currentUserAccountID]: {timezone}})); DateUtils.setTimezoneUpdated(); } @@ -1582,18 +1573,19 @@ type GuidedSetupOptimisticOnyxUpdate = OnyxUpdate< typeof ONYXKEYS.COLLECTION.REPORT | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS | typeof ONYXKEYS.NVP_INTRO_SELECTED | typeof ONYXKEYS.NVP_ONBOARDING | typeof ONYXKEYS.COLLECTION.POLICY >; -type GuidedSetupSuccessOnyxUpdate = OnyxUpdate< - typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS | typeof ONYXKEYS.COLLECTION.POLICY | typeof ONYXKEYS.PERSONAL_DETAILS_LIST | typeof ONYXKEYS.NVP_ONBOARDING ->; +type GuidedSetupSuccessOnyxUpdate = + | OnyxUpdate + | PersonalDetailsUtils.PersonalDetailsOnyxUpdate; -type GuidedSetupFailureOnyxUpdate = OnyxUpdate< - | typeof ONYXKEYS.COLLECTION.REPORT - | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS - | typeof ONYXKEYS.NVP_INTRO_SELECTED - | typeof ONYXKEYS.NVP_ONBOARDING - | typeof ONYXKEYS.COLLECTION.POLICY - | typeof ONYXKEYS.PERSONAL_DETAILS_LIST ->; +type GuidedSetupFailureOnyxUpdate = + | OnyxUpdate< + | typeof ONYXKEYS.COLLECTION.REPORT + | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS + | typeof ONYXKEYS.NVP_INTRO_SELECTED + | typeof ONYXKEYS.NVP_ONBOARDING + | typeof ONYXKEYS.COLLECTION.POLICY + > + | PersonalDetailsUtils.PersonalDetailsOnyxUpdate; type GuidedSetupDataForOpenReport = { optimisticData: GuidedSetupOptimisticOnyxUpdate[]; @@ -1725,18 +1717,18 @@ function openReport(params: OpenReportActionParams) { const optimisticReport: Partial> = hasReportActions || !existingReportName ? {} : {reportName: existingReportName}; const optimisticData: Array< - OnyxUpdate< - | typeof ONYXKEYS.COLLECTION.REPORT_METADATA - | typeof ONYXKEYS.COLLECTION.REPORT - | typeof ONYXKEYS.COLLECTION.TRANSACTION - | typeof ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS - | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS - | typeof ONYXKEYS.NVP_INTRO_SELECTED - | typeof ONYXKEYS.COLLECTION.POLICY - | typeof ONYXKEYS.NVP_ONBOARDING - | typeof ONYXKEYS.PERSONAL_DETAILS_LIST - | typeof ONYXKEYS.COLLECTION.RAM_ONLY_REPORT_LOADING_STATE - > + | OnyxUpdate< + | typeof ONYXKEYS.COLLECTION.REPORT_METADATA + | typeof ONYXKEYS.COLLECTION.REPORT + | typeof ONYXKEYS.COLLECTION.TRANSACTION + | typeof ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS + | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS + | typeof ONYXKEYS.NVP_INTRO_SELECTED + | typeof ONYXKEYS.COLLECTION.POLICY + | typeof ONYXKEYS.NVP_ONBOARDING + | typeof ONYXKEYS.COLLECTION.RAM_ONLY_REPORT_LOADING_STATE + > + | PersonalDetailsUtils.PersonalDetailsOnyxUpdate > = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -1763,15 +1755,15 @@ function openReport(params: OpenReportActionParams) { } const successData: Array< - OnyxUpdate< - | typeof ONYXKEYS.COLLECTION.REPORT - | typeof ONYXKEYS.COLLECTION.REPORT_METADATA - | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS - | typeof ONYXKEYS.COLLECTION.POLICY - | typeof ONYXKEYS.PERSONAL_DETAILS_LIST - | typeof ONYXKEYS.NVP_ONBOARDING - | typeof ONYXKEYS.COLLECTION.RAM_ONLY_REPORT_LOADING_STATE - > + | OnyxUpdate< + | typeof ONYXKEYS.COLLECTION.REPORT + | typeof ONYXKEYS.COLLECTION.REPORT_METADATA + | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS + | typeof ONYXKEYS.COLLECTION.POLICY + | typeof ONYXKEYS.NVP_ONBOARDING + | typeof ONYXKEYS.COLLECTION.RAM_ONLY_REPORT_LOADING_STATE + > + | PersonalDetailsUtils.PersonalDetailsOnyxUpdate > = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -1793,16 +1785,16 @@ function openReport(params: OpenReportActionParams) { ]; const failureData: Array< - OnyxUpdate< - | typeof ONYXKEYS.COLLECTION.REPORT - | typeof ONYXKEYS.COLLECTION.REPORT_METADATA - | typeof ONYXKEYS.NVP_INTRO_SELECTED - | typeof ONYXKEYS.NVP_ONBOARDING - | typeof ONYXKEYS.COLLECTION.POLICY - | typeof ONYXKEYS.PERSONAL_DETAILS_LIST - | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS - | typeof ONYXKEYS.COLLECTION.RAM_ONLY_REPORT_LOADING_STATE - > + | OnyxUpdate< + | typeof ONYXKEYS.COLLECTION.REPORT + | typeof ONYXKEYS.COLLECTION.REPORT_METADATA + | typeof ONYXKEYS.NVP_INTRO_SELECTED + | typeof ONYXKEYS.NVP_ONBOARDING + | typeof ONYXKEYS.COLLECTION.POLICY + | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS + | typeof ONYXKEYS.COLLECTION.RAM_ONLY_REPORT_LOADING_STATE + > + | PersonalDetailsUtils.PersonalDetailsOnyxUpdate > = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -2058,17 +2050,9 @@ function openReport(params: OpenReportActionParams) { }, ); - optimisticData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: optimisticPersonalDetails, - }); + optimisticData.push(PersonalDetailsUtils.buildPersonalDetailsUpdate(optimisticPersonalDetails)); if (!shouldKeepPersonalDetailsOnFailure) { - failureData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: settledPersonalDetails, - }); + failureData.push(PersonalDetailsUtils.buildPersonalDetailsUpdate(settledPersonalDetails)); } if (!isNewThread) { @@ -2188,15 +2172,15 @@ function createGroupChat({ }; const optimisticData: Array< - OnyxUpdate< - | typeof ONYXKEYS.COLLECTION.REPORT_METADATA - | typeof ONYXKEYS.COLLECTION.REPORT - | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS - | typeof ONYXKEYS.PERSONAL_DETAILS_LIST - | typeof ONYXKEYS.NVP_ONBOARDING - | typeof ONYXKEYS.COLLECTION.POLICY - | typeof ONYXKEYS.NVP_INTRO_SELECTED - > + | OnyxUpdate< + | typeof ONYXKEYS.COLLECTION.REPORT_METADATA + | typeof ONYXKEYS.COLLECTION.REPORT + | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS + | typeof ONYXKEYS.NVP_ONBOARDING + | typeof ONYXKEYS.COLLECTION.POLICY + | typeof ONYXKEYS.NVP_INTRO_SELECTED + > + | PersonalDetailsUtils.PersonalDetailsOnyxUpdate > = [ { onyxMethod: Onyx.METHOD.SET, @@ -2220,15 +2204,15 @@ function createGroupChat({ ]; const successData: Array< - OnyxUpdate< - | typeof ONYXKEYS.COLLECTION.REPORT - | typeof ONYXKEYS.COLLECTION.REPORT_METADATA - | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS - | typeof ONYXKEYS.COLLECTION.POLICY - | typeof ONYXKEYS.PERSONAL_DETAILS_LIST - | typeof ONYXKEYS.NVP_ONBOARDING - | typeof ONYXKEYS.COLLECTION.RAM_ONLY_REPORT_LOADING_STATE - > + | OnyxUpdate< + | typeof ONYXKEYS.COLLECTION.REPORT + | typeof ONYXKEYS.COLLECTION.REPORT_METADATA + | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS + | typeof ONYXKEYS.COLLECTION.POLICY + | typeof ONYXKEYS.NVP_ONBOARDING + | typeof ONYXKEYS.COLLECTION.RAM_ONLY_REPORT_LOADING_STATE + > + | PersonalDetailsUtils.PersonalDetailsOnyxUpdate > = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -2262,16 +2246,16 @@ function createGroupChat({ ]; const failureData: Array< - OnyxUpdate< - | typeof ONYXKEYS.COLLECTION.REPORT - | typeof ONYXKEYS.COLLECTION.REPORT_METADATA - | typeof ONYXKEYS.NVP_INTRO_SELECTED - | typeof ONYXKEYS.NVP_ONBOARDING - | typeof ONYXKEYS.COLLECTION.POLICY - | typeof ONYXKEYS.PERSONAL_DETAILS_LIST - | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS - | typeof ONYXKEYS.COLLECTION.RAM_ONLY_REPORT_LOADING_STATE - > + | OnyxUpdate< + | typeof ONYXKEYS.COLLECTION.REPORT + | typeof ONYXKEYS.COLLECTION.REPORT_METADATA + | typeof ONYXKEYS.NVP_INTRO_SELECTED + | typeof ONYXKEYS.NVP_ONBOARDING + | typeof ONYXKEYS.COLLECTION.POLICY + | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS + | typeof ONYXKEYS.COLLECTION.RAM_ONLY_REPORT_LOADING_STATE + > + | PersonalDetailsUtils.PersonalDetailsOnyxUpdate > = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -2318,17 +2302,9 @@ function createGroupChat({ settledPersonalDetails[accountID] = null; } - optimisticData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: optimisticPersonalDetails, - }); + optimisticData.push(PersonalDetailsUtils.buildPersonalDetailsUpdate(optimisticPersonalDetails)); - failureData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: settledPersonalDetails, - }); + failureData.push(PersonalDetailsUtils.buildPersonalDetailsUpdate(settledPersonalDetails)); failureData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, @@ -5030,7 +5006,7 @@ function cleanUpOptimisticPersonalDetailsForFailedChat(report: OnyxEntry return; } - Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, personalDetailsToRemove); + Onyx.update([PersonalDetailsUtils.buildPersonalDetailsUpdate(personalDetailsToRemove)]); } function clearCreateChatError( @@ -5577,7 +5553,7 @@ function buildInviteToRoomOnyxData( return participantCleanUp; }, {}); - const optimisticData: Array> = [ + const optimisticData: Array | PersonalDetailsUtils.PersonalDetailsOnyxUpdate> = [ { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, @@ -5600,7 +5576,7 @@ function buildInviteToRoomOnyxData( (pendingMember) => !(inviteeAccountIDs.includes(Number(pendingMember.accountID)) && pendingMember.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE), ) : null; - const successData: Array> = [ + const successData: Array | PersonalDetailsUtils.PersonalDetailsOnyxUpdate> = [ { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, @@ -5706,9 +5682,7 @@ function clearAddRoomMemberError(reportID: string, invitedAccountID: string) { Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_METADATA}${reportID}`, { pendingChatMembers: reportMetadata?.pendingChatMembers?.filter((pendingChatMember) => pendingChatMember.accountID !== invitedAccountID), }); - Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, { - [invitedAccountID]: null, - }); + Onyx.update([PersonalDetailsUtils.buildPersonalDetailsUpdate({[invitedAccountID]: null})]); } function updateGroupChatMemberRoles(reportID: string, accountIDList: number[], role: ValueOf) { @@ -7494,24 +7468,19 @@ function moveIOUReportToPolicyAndInviteSubmitter( } const optimisticData: Array< - OnyxUpdate< - | typeof ONYXKEYS.COLLECTION.POLICY - | typeof ONYXKEYS.PERSONAL_DETAILS_LIST - | typeof ONYXKEYS.COLLECTION.REPORT - | typeof ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS - | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS - | typeof ONYXKEYS.COLLECTION.REPORT_METADATA - | typeof ONYXKEYS.COLLECTION.TRANSACTION - > + | OnyxUpdate< + | typeof ONYXKEYS.COLLECTION.POLICY + | typeof ONYXKEYS.COLLECTION.REPORT + | typeof ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS + | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS + | typeof ONYXKEYS.COLLECTION.REPORT_METADATA + | typeof ONYXKEYS.COLLECTION.TRANSACTION + > + | PersonalDetailsUtils.PersonalDetailsOnyxUpdate > = []; const successData: Array< - OnyxUpdate< - | typeof ONYXKEYS.COLLECTION.POLICY - | typeof ONYXKEYS.PERSONAL_DETAILS_LIST - | typeof ONYXKEYS.COLLECTION.REPORT - | typeof ONYXKEYS.COLLECTION.REPORT_METADATA - | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS - > + | OnyxUpdate + | PersonalDetailsUtils.PersonalDetailsOnyxUpdate > = []; const failureData: Array< OnyxUpdate< diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 81c76b2a9d06..20a8ea435683 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -34,6 +34,8 @@ import * as MainQueue from '@libs/Network/MainQueue'; import * as NetworkStore from '@libs/Network/NetworkStore'; import {getCurrentUserEmail} from '@libs/Network/NetworkStore'; import * as SequentialQueue from '@libs/Network/SequentialQueue'; +import {buildPersonalDetailsUpdate} from '@libs/PersonalDetailsUtils'; +import type {PersonalDetailsOnyxUpdate} from '@libs/PersonalDetailsUtils'; import clearPrefetchOnAppStart from '@libs/Prefetch/clearPrefetchOnAppStart'; import Pusher from '@libs/Pusher'; import reauthenticate from '@libs/Reauthentication'; @@ -650,16 +652,12 @@ function beginSignIn(email: string) { /** * Create Onyx update to clean up anonymous user data */ -function buildOnyxDataToCleanUpAnonymousUser(): OnyxUpdate { +function buildOnyxDataToCleanUpAnonymousUser(): PersonalDetailsOnyxUpdate { const data: Record = {}; if (deprecatedSession.authTokenType === CONST.AUTH_TOKEN_TYPES.ANONYMOUS && deprecatedSession.accountID) { data[deprecatedSession.accountID] = null; } - return { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: data, - }; + return buildPersonalDetailsUpdate(data); } /** @@ -680,7 +678,7 @@ function signUpUser(login: string | undefined, preferredLocale: Locale | undefin const onyxOperationToCleanUpAnonymousUser = buildOnyxDataToCleanUpAnonymousUser(); - const successData: Array> = [ + const successData: Array | PersonalDetailsOnyxUpdate> = [ { onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.ACCOUNT, @@ -972,7 +970,7 @@ function signIn( const onyxOperationToCleanUpAnonymousUser = buildOnyxDataToCleanUpAnonymousUser(); - const successData: Array> = [ + const successData: Array | PersonalDetailsOnyxUpdate> = [ { onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.ACCOUNT, @@ -1048,7 +1046,7 @@ function signInWithValidateCode(accountID: number, code: string, preferredLocale }, ]; - const successData: Array> = [ + const successData: Array | PersonalDetailsOnyxUpdate> = [ { onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.ACCOUNT, diff --git a/src/libs/actions/Task.ts b/src/libs/actions/Task.ts index b73b8a5d95bc..67699b4b6d50 100644 --- a/src/libs/actions/Task.ts +++ b/src/libs/actions/Task.ts @@ -243,7 +243,8 @@ function createTaskAndNavigate(params: CreateTaskAndNavigateParams) { // FOR TASK REPORT const successData: Array< - OnyxUpdate + | OnyxUpdate + | PersonalDetailsUtils.PersonalDetailsOnyxUpdate > = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -277,7 +278,8 @@ function createTaskAndNavigate(params: CreateTaskAndNavigateParams) { // task report/action on API failure so the task stays visible until the user dismiss the // error from chat. const failureData: Array< - OnyxUpdate + | OnyxUpdate + | PersonalDetailsUtils.PersonalDetailsOnyxUpdate > = []; if (assigneeChatReport && assigneeChatReportID) { @@ -962,7 +964,8 @@ function editTaskAssignee({ ]; const successData: Array< - OnyxUpdate + | OnyxUpdate + | PersonalDetailsUtils.PersonalDetailsOnyxUpdate > = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -976,7 +979,7 @@ function editTaskAssignee({ }, ]; - const failureData: Array> = [ + const failureData: Array | PersonalDetailsUtils.PersonalDetailsOnyxUpdate> = [ { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`, @@ -1131,7 +1134,7 @@ function setNewOptimisticAssignee(currentUserAccountID: number, assigneePersonal login: assigneePersonalDetails?.login, isOptimisticPersonalDetail: assigneePersonalDetails.isOptimisticPersonalDetail, }; - Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {[assigneePersonalDetails.accountID]: optimisticPersonalDetailsListAction}); + Onyx.update([PersonalDetailsUtils.buildPersonalDetailsUpdate({[assigneePersonalDetails.accountID]: optimisticPersonalDetailsListAction})]); return {assignee: optimisticPersonalDetailsListAction, assigneeReport: report}; } diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index b03c801ce809..3017b920b0f9 100644 --- a/src/libs/actions/User.ts +++ b/src/libs/actions/User.ts @@ -1039,7 +1039,7 @@ function setContactMethodAsDefault( ]; // Pattern C: apply all actual data changes only after server confirms success - const successData: Array> = [ + const successData: Array | PersonalDetailsUtils.PersonalDetailsOnyxUpdate> = [ { onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.ACCOUNT, @@ -1065,16 +1065,12 @@ function setContactMethodAsDefault( }, }, }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [currentUserPersonalDetails.accountID]: { - login: newDefaultContactMethod, - displayName: PersonalDetailsUtils.createDisplayName(newDefaultContactMethod, currentUserPersonalDetails, formatPhoneNumber), - }, + PersonalDetailsUtils.buildPersonalDetailsUpdate({ + [currentUserPersonalDetails.accountID]: { + login: newDefaultContactMethod, + displayName: PersonalDetailsUtils.createDisplayName(newDefaultContactMethod, currentUserPersonalDetails, formatPhoneNumber), }, - }, + }), ]; const failureData: Array> = [ @@ -1143,16 +1139,12 @@ function setHighContrastIntent(hasIntent: boolean | null) { * Sets a custom status */ function updateCustomStatus(currentUserAccountID: number, status: Status) { - const optimisticData: Array> = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [currentUserAccountID]: { - status, - }, + const optimisticData: PersonalDetailsUtils.PersonalDetailsOnyxUpdate[] = [ + PersonalDetailsUtils.buildPersonalDetailsUpdate({ + [currentUserAccountID]: { + status, }, - }, + }), ]; const parameters: UpdateStatusParams = {text: status.text, emojiCode: status.emojiCode, clearAfter: status.clearAfter}; @@ -1166,16 +1158,12 @@ function updateCustomStatus(currentUserAccountID: number, status: Status) { * Clears the custom status */ function clearCustomStatus(currentUserAccountID: number) { - const optimisticData: Array> = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - value: { - [currentUserAccountID]: { - status: null, // Clearing the field - }, + const optimisticData: PersonalDetailsUtils.PersonalDetailsOnyxUpdate[] = [ + PersonalDetailsUtils.buildPersonalDetailsUpdate({ + [currentUserAccountID]: { + status: null, // Clearing the field }, - }, + }), ]; API.write(WRITE_COMMANDS.CLEAR_STATUS, null, {optimisticData}); } diff --git a/src/libs/actions/replaceOptimisticAgentWithActualAgent.ts b/src/libs/actions/replaceOptimisticAgentWithActualAgent.ts index 97475426bd62..bcb75c6b7944 100644 --- a/src/libs/actions/replaceOptimisticAgentWithActualAgent.ts +++ b/src/libs/actions/replaceOptimisticAgentWithActualAgent.ts @@ -1,6 +1,7 @@ import {registerAgentAccountIDMapping} from '@libs/AgentAccountIDMapping'; import Navigation, {navigationRef} from '@libs/Navigation/Navigation'; import TransitionTracker from '@libs/Navigation/TransitionTracker'; +import {buildPersonalDetailsUpdate} from '@libs/PersonalDetailsUtils'; import ONYXKEYS from '@src/ONYXKEYS'; import SCREENS from '@src/SCREENS'; @@ -117,7 +118,7 @@ function replaceOptimisticAgentWithActualAgent(optimisticAccountID: number, real remapReportParticipants(optimisticAccountID, realAccountID); - Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {[optimisticAccountID]: null}); + Onyx.update([buildPersonalDetailsUpdate({[optimisticAccountID]: null})]); Onyx.merge(`${ONYXKEYS.COLLECTION.SHARED_NVP_AGENT_PROMPT}${optimisticAccountID}`, null); Onyx.merge(ONYXKEYS.OPTIMISTIC_AGENT_ACCOUNT_ID_MAPPING, {[optimisticAccountID]: null}); }, diff --git a/src/stories/MenuItemComparison.stories.tsx b/src/stories/MenuItemComparison.stories.tsx index edbe0fa34ff0..31accca86da9 100644 --- a/src/stories/MenuItemComparison.stories.tsx +++ b/src/stories/MenuItemComparison.stories.tsx @@ -18,6 +18,8 @@ import Text from '@components/Text'; import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset'; import useThemeStyles from '@hooks/useThemeStyles'; +import {buildPersonalDetailsUpdate} from '@libs/PersonalDetailsUtils'; + import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; @@ -50,13 +52,15 @@ const STORY_POLICY_ID = 'menuItemComparisonStoryPolicy'; /** Seeds the personal details, policy and report the ID-driven avatar cases read from */ async function seedStoryOnyxData() { await Promise.all([ - Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, { - [STORY_ACCOUNT_ID]: { - accountID: STORY_ACCOUNT_ID, - displayName: 'John Doe', - login: 'john@example.com', - }, - }), + Onyx.update([ + buildPersonalDetailsUpdate({ + [STORY_ACCOUNT_ID]: { + accountID: STORY_ACCOUNT_ID, + displayName: 'John Doe', + login: 'john@example.com', + }, + }), + ]), Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${STORY_POLICY_ID}`, { id: STORY_POLICY_ID, name: 'Expensify Inc',