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
76 changes: 16 additions & 60 deletions src/libs/actions/Policy/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
CustomUnit,
NetSuiteCustomList,
NetSuiteCustomSegment,
PolicyReportField,
ProhibitedExpenses,
Rate,
TaxRate,
Expand Down Expand Up @@ -251,7 +252,7 @@
};

const deprecatedAllPolicies: OnyxCollection<Policy> = {};
Onyx.connect({

Check warning on line 255 in src/libs/actions/Policy/Policy.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.POLICY,
callback: (val, key) => {
if (!key) {
Expand All @@ -267,7 +268,7 @@
});

let deprecatedAllReportActions: OnyxCollection<ReportActions>;
Onyx.connect({

Check warning on line 271 in src/libs/actions/Policy/Policy.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
waitForCollectionCallback: true,
callback: (actions) => {
Expand All @@ -277,7 +278,7 @@

let deprecatedSessionEmail = '';
let deprecatedSessionAccountID = 0;
Onyx.connect({

Check warning on line 281 in src/libs/actions/Policy/Policy.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.SESSION,
callback: (val) => {
deprecatedSessionEmail = val?.email ?? '';
Expand All @@ -286,7 +287,7 @@
});

let deprecatedAllPersonalDetails: OnyxEntry<PersonalDetailsList>;
Onyx.connect({

Check warning on line 290 in src/libs/actions/Policy/Policy.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (val) => (deprecatedAllPersonalDetails = val),
});
Expand All @@ -313,17 +314,6 @@
return CONST.DIRECT_REIMBURSEMENT_CURRENCIES.includes(currency);
}

/**
* Returns the policy of the report
* @deprecated Get the data straight from Onyx - This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
*/
function getPolicy(policyID: string | undefined): OnyxEntry<Policy> {
if (!deprecatedAllPolicies || !policyID) {
return undefined;
}
return deprecatedAllPolicies[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
}

/** Check if the policy has invoicing company details */
function hasInvoicingDetails(policy: OnyxEntry<Policy>): boolean {
return !!policy?.invoice?.companyName && !!policy?.invoice?.companyWebsite;
Expand Down Expand Up @@ -3836,14 +3826,10 @@
API.write(WRITE_COMMANDS.REQUEST_EXPENSIFY_CARD_LIMIT_INCREASE, params);
}

function updateMemberCustomField(policyID: string, login: string, customFieldType: CustomFieldType, value: string) {
function updateMemberCustomField(policyID: string, login: string, customFieldType: CustomFieldType, value: string, currentValue: string | undefined) {
const customFieldKey = CONST.CUSTOM_FIELD_KEYS[customFieldType];
// This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
// eslint-disable-next-line @typescript-eslint/no-deprecated
const policy = getPolicy(policyID);
const previousValue = policy?.employeeList?.[login]?.[customFieldKey];

if (value === (previousValue ?? '')) {
if (value === (currentValue ?? '')) {
return;
}

Expand All @@ -3870,7 +3856,7 @@
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
onyxMethod: Onyx.METHOD.MERGE,
value: {
employeeList: {[login]: {[customFieldKey]: previousValue, pendingFields: {[customFieldKey]: null}}},
employeeList: {[login]: {[customFieldKey]: currentValue, pendingFields: {[customFieldKey]: null}}},
},
},
];
Expand Down Expand Up @@ -5425,16 +5411,7 @@
API.write(WRITE_COMMANDS.DOWNGRADE_TO_TEAM, parameters, {optimisticData, successData, failureData});
}

function setWorkspaceDefaultSpendCategory(policyID: string, groupID: string, category: string) {
// This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
// eslint-disable-next-line @typescript-eslint/no-deprecated
const policy = getPolicy(policyID);
if (!policy) {
return;
}

const {mccGroup} = policy;

function setWorkspaceDefaultSpendCategory(policyID: string, groupID: string, category: string, mccGroup: Policy['mccGroup']) {
const optimisticData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.POLICY>> = mccGroup
? [
{
Expand Down Expand Up @@ -5768,13 +5745,9 @@
* @param policyID - id of the policy to set the max expense age
* @param customRules - the custom rules description in natural language
*/
function updateCustomRules(policyID: string, customRules: string) {
// This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
// eslint-disable-next-line @typescript-eslint/no-deprecated
const policy = getPolicy(policyID);
const originalCustomRules = policy?.customRules;
function updateCustomRules(policyID: string, customRules: string, currentCustomRules: string | undefined) {
const parsedCustomRules = ReportUtils.getParsedComment(customRules);
if (parsedCustomRules === originalCustomRules) {
if (parsedCustomRules === currentCustomRules) {
return;
}

Expand Down Expand Up @@ -5809,7 +5782,7 @@
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
customRules: originalCustomRules,
customRules: currentCustomRules,
pendingFields: {customRules: null},
errorFields: {customRules: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('common.genericErrorMessage')},
// TODO
Expand Down Expand Up @@ -5988,11 +5961,6 @@
* @param policyID - id of the policy to enable or disable the billable mode
*/
function disableWorkspaceBillableExpenses(policyID: string) {
// This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
// eslint-disable-next-line @typescript-eslint/no-deprecated
const policy = getPolicy(policyID);
const originalDefaultBillableDisabled = policy?.disabledFields?.defaultBillable;

const onyxData: OnyxData<typeof ONYXKEYS.COLLECTION.POLICY> = {
optimisticData: [
{
Expand Down Expand Up @@ -6025,7 +5993,7 @@
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
pendingFields: {disabledFields: null},
disabledFields: {defaultBillable: originalDefaultBillableDisabled},
disabledFields: {defaultBillable: false},
},
},
],
Expand Down Expand Up @@ -6197,17 +6165,11 @@
* @param policyID - id of the policy to apply the naming pattern to
* @param customName - name pattern to be used for the reports
*/
function setPolicyDefaultReportTitle(policyID: string, customName: string) {
// This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
// eslint-disable-next-line @typescript-eslint/no-deprecated
const policy = getPolicy(policyID);

if (customName === policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]?.defaultValue) {
function setPolicyDefaultReportTitle(policyID: string, customName: string, currentReportTitleField: PolicyReportField | undefined) {
if (customName === currentReportTitleField?.defaultValue) {
return;
}

const previousReportTitleField = policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE] ?? {};

const optimisticData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.POLICY>> = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand Down Expand Up @@ -6242,7 +6204,7 @@
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
fieldList: {
[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]: {...previousReportTitleField, pendingFields: {defaultValue: null}},
[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]: {...currentReportTitleField, pendingFields: {defaultValue: null}},
},
errorFields: {
fieldList: {
Expand Down Expand Up @@ -6272,26 +6234,20 @@
* @param policyID - id of the policy to apply the naming pattern to
* @param enforced - flag whether to enforce policy name
*/
function setPolicyPreventMemberCreatedTitle(policyID: string, enforced: boolean) {
// This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
// eslint-disable-next-line @typescript-eslint/no-deprecated
const policy = getPolicy(policyID);

function setPolicyPreventMemberCreatedTitle(policyID: string, enforced: boolean, currentReportTitleField: PolicyReportField | undefined) {
// When fieldList is empty, deletable is undefined. We treat undefined as true (not enforced) to match OldDot's fallback behavior.
const currentDeletable = policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]?.deletable ?? true;
const currentDeletable = currentReportTitleField?.deletable ?? true;
if (!enforced === currentDeletable) {
return;
}

const previousReportTitleField = policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE] ?? {};

const optimisticData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.POLICY>> = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
fieldList: {
[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]: {...previousReportTitleField, deletable: !enforced, pendingFields: {deletable: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}},
[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]: {...currentReportTitleField, deletable: !enforced, pendingFields: {deletable: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}},
},
},
},
Expand All @@ -6316,7 +6272,7 @@
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
fieldList: {
[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]: {...previousReportTitleField, pendingFields: {deletable: null}},
[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]: {...currentReportTitleField, pendingFields: {deletable: null}},
},
errorFields: {
fieldList: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('common.genericErrorMessage'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function WorkspaceCategoriesSettingsPage({policy, route}: WorkspaceCategoriesSet
return;
}
if (categoryID !== selectedCategory.keyForList) {
setWorkspaceDefaultSpendCategory(policyID, currentGroupID, selectedCategory.keyForList);
setWorkspaceDefaultSpendCategory(policyID, currentGroupID, selectedCategory.keyForList, policy?.mccGroup);
}

Keyboard.dismiss();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function WorkspaceMemberCustomFieldPage({policy, route, personalDetails}: Worksp
enabledWhenOffline
submitButtonText={translate('common.save')}
onSubmit={() => {
updateMemberCustomField(params.policyID, memberLogin, customFieldType, customField.trim());
updateMemberCustomField(params.policyID, memberLogin, customFieldType, customField.trim(), member?.[customFieldKey]);
goBack();
}}
>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/workspace/reports/ReportsDefaultTitle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function ReportsDefaultTitlePage({route}: RulesCustomNamePageProps) {
});

const submitForm = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.REPORTS_DEFAULT_TITLE_MODAL_FORM>) => {
setPolicyDefaultReportTitle(policyID, values.defaultTitle);
setPolicyDefaultReportTitle(policyID, values.defaultTitle, policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]);
Navigation.goBack();
};

Expand Down
2 changes: 1 addition & 1 deletion src/pages/workspace/reports/WorkspaceReportsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ function WorkspaceReportFieldsPage({
return;
}

setPolicyPreventMemberCreatedTitle(policyID, isEnabled);
setPolicyPreventMemberCreatedTitle(policyID, isEnabled, policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]);
}}
/>
</Section>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/workspace/rules/RulesCustomPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function RulesCustomPage({
style={[styles.flexGrow1, styles.ph5]}
formID={ONYXKEYS.FORMS.RULES_CUSTOM_FORM}
onSubmit={({customRules}) => {
updateCustomRules(policyID, customRules);
updateCustomRules(policyID, customRules, policy?.customRules);
Navigation.setNavigationActionToMicrotaskQueue(Navigation.goBack);
}}
submitButtonText={translate('workspace.editor.save')}
Expand Down
2 changes: 1 addition & 1 deletion src/pages/workspace/upgrade/WorkspaceUpgradePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ function WorkspaceUpgradePage({route}: WorkspaceUpgradePageProps) {
}
if (!feature) {
if (featureNameAlias === CONST.UPGRADE_FEATURE_INTRO_MAPPING.policyPreventMemberChangingTitle.alias) {
setPolicyPreventMemberCreatedTitle(policyID, true);
setPolicyPreventMemberCreatedTitle(policyID, true, policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]);
}
return;
}
Expand Down
Loading
Loading