@@ -82,7 +82,9 @@ import GoogleTagManager from '@libs/GoogleTagManager';
8282// eslint-disable-next-line @typescript-eslint/no-deprecated
8383import { translate , translateLocal } from '@libs/Localize' ;
8484import Log from '@libs/Log' ;
85+ import { buildNextStepNew } from '@libs/NextStepUtils' ;
8586import * as NumberUtils from '@libs/NumberUtils' ;
87+ import Permissions from '@libs/Permissions' ;
8688import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils' ;
8789import * as PhoneNumber from '@libs/PhoneNumber' ;
8890import * as PolicyUtils from '@libs/PolicyUtils' ;
@@ -101,6 +103,7 @@ import type {OnboardingAccounting} from '@src/CONST';
101103import ONYXKEYS from '@src/ONYXKEYS' ;
102104import type {
103105 BankAccountList ,
106+ Beta ,
104107 CardFeeds ,
105108 DuplicateWorkspace ,
106109 IntroSelected ,
@@ -124,6 +127,7 @@ import type {ErrorFields, Errors, PendingAction} from '@src/types/onyx/OnyxCommo
124127import type { Attributes , CompanyAddress , CustomUnit , NetSuiteCustomList , NetSuiteCustomSegment , ProhibitedExpenses , Rate , TaxRate , UberReceiptPartner } from '@src/types/onyx/Policy' ;
125128import type { CustomFieldType } from '@src/types/onyx/PolicyEmployee' ;
126129import type { NotificationPreference } from '@src/types/onyx/Report' ;
130+ import type ReportNextStepDeprecated from '@src/types/onyx/ReportNextStepDeprecated' ;
127131import type { OnyxData } from '@src/types/onyx/Request' ;
128132import { isEmptyObject } from '@src/types/utils/EmptyObject' ;
129133import { buildOptimisticMccGroup , buildOptimisticPolicyCategories , buildOptimisticPolicyWithExistingCategories } from './Category' ;
@@ -208,6 +212,12 @@ type SetWorkspaceReimbursementActionParams = {
208212 shouldUpdateLastPaymentMethod ?: boolean ;
209213} ;
210214
215+ type SetWorkspaceApprovalModeAdditionalData = {
216+ reportNextSteps ?: OnyxCollection < ReportNextStepDeprecated > ;
217+ transactionViolations ?: OnyxCollection < TransactionViolations > ;
218+ betas ?: Beta [ ] ;
219+ } ;
220+
211221const deprecatedAllPolicies : OnyxCollection < Policy > = { } ;
212222Onyx . connect ( {
213223 key : ONYXKEYS . COLLECTION . POLICY ,
@@ -803,7 +813,7 @@ function setWorkspaceAutoReportingMonthlyOffset(policyID: string | undefined, au
803813 API . write ( WRITE_COMMANDS . SET_WORKSPACE_AUTO_REPORTING_MONTHLY_OFFSET , params , { optimisticData, failureData, successData} ) ;
804814}
805815
806- function setWorkspaceApprovalMode ( policyID : string , approver : string , approvalMode : ValueOf < typeof CONST . POLICY . APPROVAL_MODE > ) {
816+ function setWorkspaceApprovalMode ( policyID : string , approver : string , approvalMode : ValueOf < typeof CONST . POLICY . APPROVAL_MODE > , additionalData ?: SetWorkspaceApprovalModeAdditionalData ) {
807817 // This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
808818 // eslint-disable-next-line @typescript-eslint/no-deprecated
809819 const policy = getPolicy ( policyID ) ;
@@ -812,8 +822,63 @@ function setWorkspaceApprovalMode(policyID: string, approver: string, approvalMo
812822 approver,
813823 approvalMode,
814824 } ;
825+ const updatedPolicy = {
826+ ...( policy ?? { } ) ,
827+ ...value ,
828+ } as OnyxEntry < Policy > ;
829+
830+ const currentUserAccountID = deprecatedSessionAccountID ?? CONST . DEFAULT_NUMBER_ID ;
831+ const currentUserEmail = deprecatedSessionEmail ?? '' ;
832+
833+ const nextStepOptimisticData : OnyxUpdate [ ] = [ ] ;
834+ const nextStepFailureData : OnyxUpdate [ ] = [ ] ;
835+ const shouldUpdateNextSteps = additionalData ?. reportNextSteps != null && additionalData ?. transactionViolations != null && additionalData ?. betas != null ;
836+ if ( shouldUpdateNextSteps ) {
837+ const { reportNextSteps, transactionViolations, betas} = additionalData ;
838+ const resolvedTransactionViolations : OnyxCollection < TransactionViolations > = transactionViolations ?? { } ;
839+ const resolvedReportNextSteps : NonNullable < OnyxCollection < ReportNextStepDeprecated > > = reportNextSteps ?? { } ;
840+ const resolvedBetas : Beta [ ] = betas ?? [ ] ;
841+ const isASAPSubmitBetaEnabled = Permissions . isBetaEnabled ( CONST . BETAS . ASAP_SUBMIT , resolvedBetas ) ;
842+ const affectedReports = ReportUtils . getAllPolicyReports ( policyID ) . filter (
843+ ( report ) => ! ! report && ReportUtils . isExpenseReport ( report ) && report ?. statusNum === CONST . REPORT . STATUS_NUM . SUBMITTED ,
844+ ) ;
815845
816- const optimisticData : Array < OnyxUpdate < typeof ONYXKEYS . COLLECTION . POLICY > > = [
846+ for ( const report of affectedReports ) {
847+ const reportID = report ?. reportID ;
848+
849+ if ( ! reportID ) {
850+ continue ;
851+ }
852+
853+ const nextStepKey : `${typeof ONYXKEYS . COLLECTION . NEXT_STEP } ${string } ` = `${ ONYXKEYS . COLLECTION . NEXT_STEP } ${ reportID } ` ;
854+ const currentNextStep : OnyxEntry < ReportNextStepDeprecated > | null = resolvedReportNextSteps [ nextStepKey ] ?? null ;
855+ const hasViolations = ReportUtils . hasViolations ( reportID , resolvedTransactionViolations , currentUserAccountID , currentUserEmail , undefined , undefined , report , updatedPolicy ) ;
856+ // eslint-disable-next-line @typescript-eslint/no-deprecated -- Next step generation uses deprecated "ReportNextStepDeprecated" types (still in active use).
857+ const optimisticNextStep = buildNextStepNew ( {
858+ report,
859+ policy : updatedPolicy ,
860+ currentUserAccountIDParam : currentUserAccountID ,
861+ currentUserEmailParam : currentUserEmail ,
862+ hasViolations,
863+ isASAPSubmitBetaEnabled,
864+ predictedNextStatus : report ?. statusNum ?? CONST . REPORT . STATUS_NUM . SUBMITTED ,
865+ } ) ;
866+
867+ nextStepOptimisticData . push ( {
868+ onyxMethod : Onyx . METHOD . MERGE ,
869+ key : nextStepKey ,
870+ value : optimisticNextStep ,
871+ } ) ;
872+
873+ nextStepFailureData . push ( {
874+ onyxMethod : Onyx . METHOD . MERGE ,
875+ key : nextStepKey ,
876+ value : currentNextStep ?? null ,
877+ } ) ;
878+ }
879+ }
880+
881+ const optimisticData : OnyxUpdate [ ] = [
817882 {
818883 onyxMethod : Onyx . METHOD . MERGE ,
819884 key : `${ ONYXKEYS . COLLECTION . POLICY } ${ policyID } ` ,
@@ -823,8 +888,11 @@ function setWorkspaceApprovalMode(policyID: string, approver: string, approvalMo
823888 } ,
824889 } ,
825890 ] ;
891+ if ( nextStepOptimisticData . length > 0 ) {
892+ optimisticData . push ( ...nextStepOptimisticData ) ;
893+ }
826894
827- const failureData : Array < OnyxUpdate < typeof ONYXKEYS . COLLECTION . POLICY > > = [
895+ const failureData : OnyxUpdate [ ] = [
828896 {
829897 onyxMethod : Onyx . METHOD . MERGE ,
830898 key : `${ ONYXKEYS . COLLECTION . POLICY } ${ policyID } ` ,
@@ -837,6 +905,9 @@ function setWorkspaceApprovalMode(policyID: string, approver: string, approvalMo
837905 } ,
838906 } ,
839907 ] ;
908+ if ( nextStepFailureData . length > 0 ) {
909+ failureData . push ( ...nextStepFailureData ) ;
910+ }
840911
841912 const successData : Array < OnyxUpdate < typeof ONYXKEYS . COLLECTION . POLICY > > = [
842913 {
0 commit comments