@@ -1058,10 +1058,12 @@ function renamePolicyTagList(policyID: string, policyTagListName: {oldName: stri
10581058 API . write ( WRITE_COMMANDS . RENAME_POLICY_TAG_LIST , parameters , onyxData ) ;
10591059}
10601060
1061- function setPolicyRequiresTag ( policyData : PolicyData , requiresTag : boolean ) {
1061+ /** extraPolicyUpdate folds a caller's same-save requiresCategory change into this action's single violation recompute. */
1062+ function setPolicyRequiresTag ( policyData : PolicyData , requiresTag : boolean , extraPolicyUpdate : Partial < Policy > = { } ) {
10621063 const policyID = policyData . policy ?. id ;
10631064
10641065 const policyOptimisticData : Partial < Policy > = {
1066+ ...extraPolicyUpdate ,
10651067 requiresTag,
10661068 // A manual toggle is explicit, so any pending switch-level restore intent is no longer needed.
10671069 pendingRequiresTagRestore : null ,
@@ -1266,7 +1268,7 @@ function setPolicyTagsRequired(policyData: PolicyData, requiresTag: boolean, tag
12661268 * key, the last request would overwrite the others — e.g. requiring Region while clearing Department would drop the
12671269 * Missing Region violation the first half just added.
12681270 */
1269- function setPolicyTagLevelsRequired ( policyData : PolicyData , requiredByOrderWeight : Record < number , boolean > ) {
1271+ function setPolicyTagLevelsRequired ( policyData : PolicyData , requiredByOrderWeight : Record < number , boolean > , extraPolicyUpdate : Partial < Policy > = { } ) {
12701272 const policyID = policyData . policy ?. id ;
12711273 if ( ! policyID || ! policyData . tags ) {
12721274 return ;
@@ -1282,6 +1284,11 @@ function setPolicyTagLevelsRequired(policyData: PolicyData, requiredByOrderWeigh
12821284 combinedTagsUpdate [ tagList . name ] = { required : requiredByOrderWeight [ tagList . orderWeight ] } ;
12831285 }
12841286
1287+ // ViolationsUtils gates every tag violation behind policy.requiresTag, so mirror what the backend derives from the
1288+ // levels instead of waiting for the next policy refresh.
1289+ const requiresTag = Object . values ( policyData . tags ) . some ( ( tagList ) => requiredByOrderWeight [ tagList . orderWeight ] ?? ! ! tagList . required ) ;
1290+ const policyUpdate : Partial < Policy > = { ...extraPolicyUpdate , requiresTag} ;
1291+
12851292 const buildOnyxData = ( tagLists : Array < PolicyTagLists [ string ] > , isRequired : boolean , shouldRecomputeViolations : boolean ) => {
12861293 const optimisticValue : Record < string , Partial < PolicyTagLists [ string ] > > = { } ;
12871294 const successValue : Record < string , Partial < PolicyTagLists [ string ] > > = { } ;
@@ -1301,14 +1308,21 @@ function setPolicyTagLevelsRequired(policyData: PolicyData, requiredByOrderWeigh
13011308 } ;
13021309 }
13031310
1304- const onyxData : OnyxData < typeof ONYXKEYS . COLLECTION . POLICY_TAGS > = {
1311+ const onyxData : OnyxData < typeof ONYXKEYS . COLLECTION . POLICY | typeof ONYXKEYS . COLLECTION . POLICY_TAGS > = {
13051312 optimisticData : [ { onyxMethod : Onyx . METHOD . MERGE , key : `${ ONYXKEYS . COLLECTION . POLICY_TAGS } ${ policyID } ` , value : optimisticValue } ] ,
13061313 successData : [ { onyxMethod : Onyx . METHOD . MERGE , key : `${ ONYXKEYS . COLLECTION . POLICY_TAGS } ${ policyID } ` , value : successValue } ] ,
13071314 failureData : [ { onyxMethod : Onyx . METHOD . MERGE , key : `${ ONYXKEYS . COLLECTION . POLICY_TAGS } ${ policyID } ` , value : failureValue } ] ,
13081315 } ;
13091316
1317+ // Only the request that owns the recompute writes the derived policy flags, so the other can't revert them.
13101318 if ( shouldRecomputeViolations ) {
1311- pushTransactionViolationsOnyxData ( onyxData , policyData , { } , { } , combinedTagsUpdate ) ;
1319+ onyxData . optimisticData ?. push ( { onyxMethod : Onyx . METHOD . MERGE , key : `${ ONYXKEYS . COLLECTION . POLICY } ${ policyID } ` , value : policyUpdate } ) ;
1320+ onyxData . failureData ?. push ( {
1321+ onyxMethod : Onyx . METHOD . MERGE ,
1322+ key : `${ ONYXKEYS . COLLECTION . POLICY } ${ policyID } ` ,
1323+ value : { requiresTag : policyData . policy ?. requiresTag ?? false } ,
1324+ } ) ;
1325+ pushTransactionViolationsOnyxData ( onyxData , policyData , policyUpdate , { } , combinedTagsUpdate ) ;
13121326 }
13131327
13141328 return onyxData ;
0 commit comments