Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/libs/API/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ use(Pagination);
// middlewares after this, because the SequentialQueue depends on the result of this middleware to pause the queue (if needed) to bring the app to an up-to-date state.
use(SaveResponseInOnyx);

let requestIndex = 0;

type OnyxData = {
optimisticData?: OnyxUpdate[];
successData?: OnyxUpdate[];
Expand Down Expand Up @@ -78,6 +80,7 @@ function prepareRequest<TCommand extends ApiCommand>(
command,
data,
initiatedOffline: isOffline(),
requestID: requestIndex++,

@suneox suneox Mar 2, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should update this to use a unique timestamp with Date.now() for this variable

...onyxDataWithoutOptimisticData,
...conflictResolver,
};
Expand Down
5 changes: 4 additions & 1 deletion src/libs/actions/Policy/Category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import enhanceParameters from '@libs/Network/enhanceParameters';
import {hasEnabledOptions} from '@libs/OptionsListUtils';
import {getPolicy, goBackWhenEnableFeature} from '@libs/PolicyUtils';
import {getAllPolicyReports} from '@libs/ReportUtils';
import {resolveEnableFeatureConflicts} from '@userActions/RequestConflictUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy, PolicyCategories, PolicyCategory, RecentlyUsedCategories, Report} from '@src/types/onyx';
Expand Down Expand Up @@ -1107,7 +1108,9 @@ function enablePolicyCategories(policyID: string, enabled: boolean, shouldGoBack

const parameters: EnablePolicyCategoriesParams = {policyID, enabled};

API.write(WRITE_COMMANDS.ENABLE_POLICY_CATEGORIES, parameters, onyxData);
API.write(WRITE_COMMANDS.ENABLE_POLICY_CATEGORIES, parameters, onyxData, {
checkAndFixConflictingRequest: (persistedRequests) => resolveEnableFeatureConflicts(WRITE_COMMANDS.ENABLE_POLICY_CATEGORIES, persistedRequests, parameters),
});

if (enabled && getIsNarrowLayout() && shouldGoBack) {
goBackWhenEnableFeature(policyID);
Expand Down
5 changes: 4 additions & 1 deletion src/libs/actions/Policy/DistanceRate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import * as ErrorUtils from '@libs/ErrorUtils';
import getIsNarrowLayout from '@libs/getIsNarrowLayout';
import {getDistanceRateCustomUnit, goBackWhenEnableFeature, removePendingFieldsFromCustomUnit} from '@libs/PolicyUtils';
import * as ReportUtils from '@libs/ReportUtils';
import {resolveEnableFeatureConflicts} from '@userActions/RequestConflictUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy, Report} from '@src/types/onyx';
Expand Down Expand Up @@ -163,7 +164,9 @@ function enablePolicyDistanceRates(policyID: string, enabled: boolean) {

const parameters: EnablePolicyDistanceRatesParams = {policyID, enabled};

API.write(WRITE_COMMANDS.ENABLE_POLICY_DISTANCE_RATES, parameters, onyxData);
API.write(WRITE_COMMANDS.ENABLE_POLICY_DISTANCE_RATES, parameters, onyxData, {
checkAndFixConflictingRequest: (persistedRequests) => resolveEnableFeatureConflicts(WRITE_COMMANDS.ENABLE_POLICY_DISTANCE_RATES, persistedRequests, parameters),
});
Comment on lines +167 to +169

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check caused inconsistencies in data updates #68739


if (enabled && getIsNarrowLayout()) {
goBackWhenEnableFeature(policyID);
Expand Down
33 changes: 25 additions & 8 deletions src/libs/actions/Policy/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ import * as ReportUtils from '@libs/ReportUtils';
import type {PolicySelector} from '@pages/home/sidebar/FloatingActionButtonAndPopover';
import * as PaymentMethods from '@userActions/PaymentMethods';
import * as PersistedRequests from '@userActions/PersistedRequests';
import {resolveEnableFeatureConflicts} from '@userActions/RequestConflictUtils';
import type {OnboardingPurpose} from '@src/CONST';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -2859,7 +2860,9 @@ function enablePolicyConnections(policyID: string, enabled: boolean) {

const parameters: EnablePolicyConnectionsParams = {policyID, enabled};

API.write(WRITE_COMMANDS.ENABLE_POLICY_CONNECTIONS, parameters, onyxData);
API.write(WRITE_COMMANDS.ENABLE_POLICY_CONNECTIONS, parameters, onyxData, {
checkAndFixConflictingRequest: (persistedRequests) => resolveEnableFeatureConflicts(WRITE_COMMANDS.ENABLE_POLICY_CONNECTIONS, persistedRequests, parameters),
});

if (enabled && getIsNarrowLayout()) {
goBackWhenEnableFeature(policyID);
Expand Down Expand Up @@ -2916,7 +2919,9 @@ function enableExpensifyCard(policyID: string, enabled: boolean, shouldNavigateT

const parameters: EnablePolicyExpensifyCardsParams = {authToken, policyID, enabled};

API.write(WRITE_COMMANDS.ENABLE_POLICY_EXPENSIFY_CARDS, parameters, onyxData);
API.write(WRITE_COMMANDS.ENABLE_POLICY_EXPENSIFY_CARDS, parameters, onyxData, {
checkAndFixConflictingRequest: (persistedRequests) => resolveEnableFeatureConflicts(WRITE_COMMANDS.ENABLE_POLICY_EXPENSIFY_CARDS, persistedRequests, parameters),
});

if (enabled && shouldNavigateToExpensifyCardPage) {
navigateToExpensifyCardPage(policyID);
Expand Down Expand Up @@ -2971,7 +2976,9 @@ function enableCompanyCards(policyID: string, enabled: boolean, shouldGoBack = t

const parameters: EnablePolicyCompanyCardsParams = {authToken, policyID, enabled};

API.write(WRITE_COMMANDS.ENABLE_POLICY_COMPANY_CARDS, parameters, onyxData);
API.write(WRITE_COMMANDS.ENABLE_POLICY_COMPANY_CARDS, parameters, onyxData, {
checkAndFixConflictingRequest: (persistedRequests) => resolveEnableFeatureConflicts(WRITE_COMMANDS.ENABLE_POLICY_COMPANY_CARDS, persistedRequests, parameters),
});

if (enabled && getIsNarrowLayout() && shouldGoBack) {
goBackWhenEnableFeature(policyID);
Expand Down Expand Up @@ -3019,7 +3026,9 @@ function enablePolicyReportFields(policyID: string, enabled: boolean, shouldGoBa

const parameters: EnablePolicyReportFieldsParams = {policyID, enabled};

API.write(WRITE_COMMANDS.ENABLE_POLICY_REPORT_FIELDS, parameters, onyxData);
API.write(WRITE_COMMANDS.ENABLE_POLICY_REPORT_FIELDS, parameters, onyxData, {
checkAndFixConflictingRequest: (persistedRequests) => resolveEnableFeatureConflicts(WRITE_COMMANDS.ENABLE_POLICY_REPORT_FIELDS, persistedRequests, parameters),
});

if (enabled && getIsNarrowLayout() && shouldGoBack) {
goBackWhenEnableFeature(policyID);
Expand Down Expand Up @@ -3133,7 +3142,9 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) {
if (shouldAddDefaultTaxRatesData) {
parameters.taxFields = JSON.stringify(defaultTaxRates);
}
API.write(WRITE_COMMANDS.ENABLE_POLICY_TAXES, parameters, onyxData);
API.write(WRITE_COMMANDS.ENABLE_POLICY_TAXES, parameters, onyxData, {
checkAndFixConflictingRequest: (persistedRequests) => resolveEnableFeatureConflicts(WRITE_COMMANDS.ENABLE_POLICY_TAXES, persistedRequests, parameters),
});

if (enabled && getIsNarrowLayout()) {
goBackWhenEnableFeature(policyID);
Expand Down Expand Up @@ -3233,7 +3244,9 @@ function enablePolicyWorkflows(policyID: string, enabled: boolean) {
setWorkspaceAutoReportingFrequency(policyID, CONST.POLICY.AUTO_REPORTING_FREQUENCIES.INSTANT);
}

API.write(WRITE_COMMANDS.ENABLE_POLICY_WORKFLOWS, parameters, onyxData);
API.write(WRITE_COMMANDS.ENABLE_POLICY_WORKFLOWS, parameters, onyxData, {
checkAndFixConflictingRequest: (persistedRequests) => resolveEnableFeatureConflicts(WRITE_COMMANDS.ENABLE_POLICY_WORKFLOWS, persistedRequests, parameters),
});

if (enabled && getIsNarrowLayout()) {
goBackWhenEnableFeature(policyID);
Expand Down Expand Up @@ -3297,7 +3310,9 @@ function enablePolicyRules(policyID: string, enabled: boolean, shouldGoBack = tr
};

const parameters: SetPolicyRulesEnabledParams = {policyID, enabled};
API.write(WRITE_COMMANDS.SET_POLICY_RULES_ENABLED, parameters, onyxData);
API.write(WRITE_COMMANDS.SET_POLICY_RULES_ENABLED, parameters, onyxData, {
checkAndFixConflictingRequest: (persistedRequests) => resolveEnableFeatureConflicts(WRITE_COMMANDS.SET_POLICY_RULES_ENABLED, persistedRequests, parameters),
});

if (enabled && getIsNarrowLayout() && shouldGoBack) {
goBackWhenEnableFeature(policyID);
Expand Down Expand Up @@ -3408,7 +3423,9 @@ function enablePolicyInvoicing(policyID: string, enabled: boolean) {

const parameters: EnablePolicyInvoicingParams = {policyID, enabled};

API.write(WRITE_COMMANDS.ENABLE_POLICY_INVOICING, parameters, onyxData);
API.write(WRITE_COMMANDS.ENABLE_POLICY_INVOICING, parameters, onyxData, {
checkAndFixConflictingRequest: (persistedRequests) => resolveEnableFeatureConflicts(WRITE_COMMANDS.ENABLE_POLICY_INVOICING, persistedRequests, parameters),
});

if (enabled && getIsNarrowLayout()) {
goBackWhenEnableFeature(policyID);
Expand Down
5 changes: 4 additions & 1 deletion src/libs/actions/Policy/Tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {goBackWhenEnableFeature} from '@libs/PolicyUtils';
import * as ReportUtils from '@libs/ReportUtils';
import {getTagArrayFromName} from '@libs/TransactionUtils';
import type {PolicyTagList} from '@pages/workspace/tags/types';
import {resolveEnableFeatureConflicts} from '@userActions/RequestConflictUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy, PolicyTag, PolicyTagLists, PolicyTags, RecentlyUsedTags, Report} from '@src/types/onyx';
Expand Down Expand Up @@ -689,7 +690,9 @@ function enablePolicyTags(policyID: string, enabled: boolean) {

const parameters: EnablePolicyTagsParams = {policyID, enabled};

API.write(WRITE_COMMANDS.ENABLE_POLICY_TAGS, parameters, onyxData);
API.write(WRITE_COMMANDS.ENABLE_POLICY_TAGS, parameters, onyxData, {
checkAndFixConflictingRequest: (persistedRequests) => resolveEnableFeatureConflicts(WRITE_COMMANDS.ENABLE_POLICY_TAGS, persistedRequests, parameters),
});

if (enabled && getIsNarrowLayout()) {
goBackWhenEnableFeature(policyID);
Expand Down
46 changes: 46 additions & 0 deletions src/libs/actions/RequestConflictUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type {OnyxUpdate} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import type {TupleToUnion} from 'type-fest';
import type {OpenReportParams, UpdateCommentParams} from '@libs/API/parameters';
import {WRITE_COMMANDS} from '@libs/API/types';
import type {ApiRequestCommandParameters} from '@libs/API/types';
import ONYXKEYS from '@src/ONYXKEYS';
import type OnyxRequest from '@src/types/onyx/Request';
import type {ConflictActionData} from '@src/types/onyx/Request';
Expand All @@ -19,6 +21,22 @@ const commentsToBeDeleted = new Set<string>([
WRITE_COMMANDS.REMOVE_EMOJI_REACTION,
]);

const enablePolicyFeatureCommand = [
WRITE_COMMANDS.ENABLE_POLICY_DISTANCE_RATES,
WRITE_COMMANDS.ENABLE_POLICY_EXPENSIFY_CARDS,
WRITE_COMMANDS.ENABLE_POLICY_COMPANY_CARDS,
WRITE_COMMANDS.ENABLE_POLICY_CONNECTIONS,
WRITE_COMMANDS.ENABLE_POLICY_CATEGORIES,
WRITE_COMMANDS.ENABLE_POLICY_TAGS,
WRITE_COMMANDS.ENABLE_POLICY_TAXES,
WRITE_COMMANDS.ENABLE_POLICY_REPORT_FIELDS,
WRITE_COMMANDS.ENABLE_POLICY_WORKFLOWS,
WRITE_COMMANDS.SET_POLICY_RULES_ENABLED,
WRITE_COMMANDS.ENABLE_POLICY_INVOICING,
] as const;

type EnablePolicyFeatureCommand = TupleToUnion<typeof enablePolicyFeatureCommand>;

function createUpdateCommentMatcher(reportActionID: string) {
return function (request: OnyxRequest) {
return request.command === WRITE_COMMANDS.UPDATE_COMMENT && request.data?.reportActionID === reportActionID;
Expand Down Expand Up @@ -175,10 +193,38 @@ function resolveEditCommentWithNewAddCommentRequest(persistedRequests: OnyxReque
} as ConflictActionData;
}

function resolveEnableFeatureConflicts(
command: EnablePolicyFeatureCommand,
persistedRequests: OnyxRequest[],
parameters: ApiRequestCommandParameters[EnablePolicyFeatureCommand],
): ConflictActionData {
const deleteRequestIndex = persistedRequests.findIndex(
(request) => request.command === command && request.data?.policyID === parameters.policyID && request.data?.enabled !== parameters.enabled,
);

if (deleteRequestIndex === -1) {
return {
conflictAction: {
type: 'push',
},
};
}

return {
conflictAction: {
type: 'delete',
indices: [deleteRequestIndex],
pushNewRequest: false,
},
};
}

export {
resolveDuplicationConflictAction,
resolveOpenReportDuplicationConflictAction,
resolveCommentDeletionConflicts,
resolveEditCommentWithNewAddCommentRequest,
createUpdateCommentMatcher,
resolveEnableFeatureConflicts,
enablePolicyFeatureCommand,
};
3 changes: 3 additions & 0 deletions src/types/onyx/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ type RequestData = {
* More info https://github.com/Expensify/App/issues/51761
*/
initiatedOffline?: boolean;

/** The unique ID of the request */
requestID?: number;
};

/**
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/APITest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,33 @@ describe('APITests', () => {
});
});

test('All write requests are in the queue should be called even some of them are the same', () => {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should update this to use a unique timestamp with Date.now() for this variable

@suneox This test fails when I use Date.now() because three APIs are called almost the same time.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you try to mockImplementation for Date.now with a manually increasing time value?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@suneox I think we can't because the implementation is executed when the mock is called, while the request is initialized when we call API.write. I updated the description for requestID.

Screenshot 2025-03-04 at 11 07 16

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I got it. Thanks for the explanation

// Given offline state where all requests will eventualy succeed without issue
const xhr = jest.spyOn(HttpUtils, 'xhr').mockResolvedValue({jsonCode: CONST.JSON_CODE.SUCCESS});
return Onyx.multiSet({
[ONYXKEYS.SESSION]: {authToken: 'anyToken'},
[ONYXKEYS.NETWORK]: {isOffline: true},
[ONYXKEYS.CREDENTIALS]: {autoGeneratedLogin: 'test_user', autoGeneratedPassword: 'psswd'},
})
.then(() => {
// When we queue 3 persistable commands and two of them are the same
API.write<WriteCommand>('MockCommand' as WriteCommand, {content: 'value1'} as ApiRequestCommandParameters[WriteCommand]);
API.write<WriteCommand>('MockCommand' as WriteCommand, {content: 'value2'} as ApiRequestCommandParameters[WriteCommand]);
API.write<WriteCommand>('MockCommand' as WriteCommand, {content: 'value1'} as ApiRequestCommandParameters[WriteCommand]);

return waitForBatchedUpdates();
})
.then(() => Onyx.set(ONYXKEYS.NETWORK, {isOffline: false}))
.then(waitForBatchedUpdates)
.then(() => {
// Then expect all 3 calls to have been made and for the Writes to be made in the order that we made them
expect(xhr.mock.calls.length).toBe(3);
expect(xhr.mock.calls.at(0)?.[1].content).toBe('value1');
expect(xhr.mock.calls.at(1)?.[1].content).toBe('value2');
expect(xhr.mock.calls.at(2)?.[1].content).toBe('value1');
});
});

test('Read request should not stuck when SequentialQueue is paused and resumed', async () => {
// Given 2 WRITE requests and 1 READ request where the first write request pauses the SequentialQueue
const xhr = jest.spyOn(HttpUtils, 'xhr').mockResolvedValueOnce({previousUpdateID: 1});
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/PersistedRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const request: Request = {
command: 'OpenReport',
successData: [{key: 'reportMetadata_1', onyxMethod: 'merge', value: {}}],
failureData: [{key: 'reportMetadata_2', onyxMethod: 'merge', value: {}}],
requestID: 1,
};

beforeAll(() =>
Expand Down Expand Up @@ -52,6 +53,7 @@ describe('PersistedRequests', () => {
command: 'AddComment',
successData: [{key: 'reportMetadata_3', onyxMethod: 'merge', value: {}}],
failureData: [{key: 'reportMetadata_4', onyxMethod: 'merge', value: {}}],
requestID: 2,
};
PersistedRequests.save(request2);
PersistedRequests.processNextRequest();
Expand All @@ -66,6 +68,7 @@ describe('PersistedRequests', () => {
command: 'OpenReport',
successData: [{key: 'reportMetadata_1', onyxMethod: 'set', value: {}}],
failureData: [{key: 'reportMetadata_2', onyxMethod: 'set', value: {}}],
requestID: 3,
};
PersistedRequests.update(0, newRequest);
expect(PersistedRequests.getAll().at(0)).toEqual(newRequest);
Expand All @@ -76,6 +79,7 @@ describe('PersistedRequests', () => {
command: 'OpenReport',
successData: [{key: 'reportMetadata_1', onyxMethod: 'set', value: {}}],
failureData: [{key: 'reportMetadata_2', onyxMethod: 'set', value: {}}],
requestID: 4,
};
PersistedRequests.updateOngoingRequest(newRequest);
expect(PersistedRequests.getOngoingRequest()).toEqual(newRequest);
Expand Down
28 changes: 27 additions & 1 deletion tests/unit/RequestConflictUtilsTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import Onyx from 'react-native-onyx';
import {resolveCommentDeletionConflicts, resolveDuplicationConflictAction, resolveEditCommentWithNewAddCommentRequest} from '@libs/actions/RequestConflictUtils';
import {
enablePolicyFeatureCommand,
resolveCommentDeletionConflicts,
resolveDuplicationConflictAction,
resolveEditCommentWithNewAddCommentRequest,
resolveEnableFeatureConflicts,
} from '@libs/actions/RequestConflictUtils';
import type {WriteCommand} from '@libs/API/types';

describe('RequestConflictUtils', () => {
Expand Down Expand Up @@ -134,4 +140,24 @@ describe('RequestConflictUtils', () => {
},
});
});

it.each(enablePolicyFeatureCommand)('resolveEnableFeatureConflicts should return push when the same enable feature API is not found', (commandName) => {
const persistedRequests = [{command: commandName, data: {policyID: '1', enabled: true}}];
const parameters = {policyID: '2', enabled: false};
const result = resolveEnableFeatureConflicts(commandName, persistedRequests, parameters);
expect(result).toEqual({conflictAction: {type: 'push'}});
});

it.each(enablePolicyFeatureCommand)('resolveEnableFeatureConflicts should return delete when the same enable feature API is found', (commandName) => {
const persistedRequests = [{command: commandName, data: {policyID: '1', enabled: true}}];
const parameters = {policyID: '1', enabled: false};
const result = resolveEnableFeatureConflicts(commandName, persistedRequests, parameters);
expect(result).toEqual({
conflictAction: {
type: 'delete',
indices: [0],
pushNewRequest: false,
},
});
});
});