Skip to content

Commit

Permalink
Merge pull request #49320 from Expensify/marcaaron-setDefaultAuditRate
Browse files Browse the repository at this point in the history
Use float instead of int for auditRate percentage. Fix incorrect params in `SetPolicyAutoReimbursementLimitParams`
  • Loading branch information
srikarparsi authored Sep 26, 2024
2 parents 4f93277 + 6f58877 commit 70a8a16
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2186,7 +2186,7 @@ const CONST = {
AUTO_REIMBURSEMENT_MAX_LIMIT_CENTS: 2000000,
AUTO_REIMBURSEMENT_DEFAULT_LIMIT_CENTS: 10000,
AUTO_APPROVE_REPORTS_UNDER_DEFAULT_CENTS: 10000,
RANDOM_AUDIT_DEFAULT_PERCENTAGE: 5,
RANDOM_AUDIT_DEFAULT_PERCENTAGE: 0.05,

AUTO_REPORTING_FREQUENCIES: {
INSTANT: 'instant',
Expand Down
2 changes: 1 addition & 1 deletion src/libs/API/parameters/SetPolicyAutoReimbursementLimit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type SetPolicyAutoReimbursementLimitParams = {
policyID: string;
autoReimbursement: {limit: number};
limit: number;
};

export default SetPolicyAutoReimbursementLimitParams;
37 changes: 11 additions & 26 deletions src/libs/actions/Policy/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4160,9 +4160,10 @@ function setPolicyAutomaticApprovalLimit(policyID: string, limit: string) {
function setPolicyAutomaticApprovalRate(policyID: string, auditRate: string) {
const policy = getPolicy(policyID);
const fallbackAuditRate = auditRate === '' ? '0' : auditRate;
const parsedAuditRate = parseInt(fallbackAuditRate, 10);
const parsedAuditRate = parseInt(fallbackAuditRate, 10) / 100;

if (parsedAuditRate === policy?.autoApproval?.auditRate ?? CONST.POLICY.RANDOM_AUDIT_DEFAULT_PERCENTAGE) {
// The auditRate arrives as an int to this method so we will convert it to a float before sending it to the API.
if (parsedAuditRate === (policy?.autoApproval?.auditRate ?? CONST.POLICY.RANDOM_AUDIT_DEFAULT_PERCENTAGE)) {
return;
}

Expand Down Expand Up @@ -4238,17 +4239,8 @@ function enableAutoApprovalOptions(policyID: string, enabled: boolean) {
return;
}

const autoApprovalCleanupValues = !enabled
? {
pendingFields: {
limit: null,
auditRate: null,
},
}
: {};
const autoApprovalValues = !enabled ? {auditRate: CONST.POLICY.RANDOM_AUDIT_DEFAULT_PERCENTAGE, limit: CONST.POLICY.AUTO_APPROVE_REPORTS_UNDER_DEFAULT_CENTS} : {};
const autoApprovalFailureValues = !enabled ? {autoApproval: {limit: policy?.autoApproval?.limit, auditRate: policy?.autoApproval?.auditRate, ...autoApprovalCleanupValues}} : {};

const autoApprovalValues = {auditRate: CONST.POLICY.RANDOM_AUDIT_DEFAULT_PERCENTAGE, limit: CONST.POLICY.AUTO_APPROVE_REPORTS_UNDER_DEFAULT_CENTS};
const autoApprovalFailureValues = {autoApproval: {limit: policy?.autoApproval?.limit, auditRate: policy?.autoApproval?.auditRate, pendingFields: null}};
const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand All @@ -4274,7 +4266,7 @@ function enableAutoApprovalOptions(policyID: string, enabled: boolean) {
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
autoApproval: {...autoApprovalCleanupValues},
autoApproval: {pendingFields: null},
pendingFields: {
shouldShowAutoApprovalOptions: null,
},
Expand Down Expand Up @@ -4367,7 +4359,7 @@ function setPolicyAutoReimbursementLimit(policyID: string, limit: string) {
];

const parameters: SetPolicyAutoReimbursementLimitParams = {
autoReimbursement: {limit: parsedLimit},
limit: parsedLimit,
policyID,
};

Expand All @@ -4380,6 +4372,7 @@ function setPolicyAutoReimbursementLimit(policyID: string, limit: string) {

/**
* Call the API to enable auto-payment for the reports in the given policy
*
* @param policyID - id of the policy to apply the limit to
* @param enabled - whether auto-payment for the reports is enabled in the given policy
*/
Expand All @@ -4390,16 +4383,8 @@ function enablePolicyAutoReimbursementLimit(policyID: string, enabled: boolean)
return;
}

const autoReimbursementCleanupValues = !enabled
? {
pendingFields: {
limit: null,
},
}
: {};
const autoReimbursementFailureValues = !enabled ? {autoReimbursement: {limit: policy?.autoReimbursement?.limit, ...autoReimbursementCleanupValues}} : {};
const autoReimbursementValues = !enabled ? {limit: CONST.POLICY.AUTO_REIMBURSEMENT_DEFAULT_LIMIT_CENTS} : {};

const autoReimbursementFailureValues = {autoReimbursement: {limit: policy?.autoReimbursement?.limit, pendingFields: null}};
const autoReimbursementValues = {limit: CONST.POLICY.AUTO_REIMBURSEMENT_DEFAULT_LIMIT_CENTS};
const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand All @@ -4424,7 +4409,7 @@ function enablePolicyAutoReimbursementLimit(policyID: string, enabled: boolean)
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
autoReimbursement: {...autoReimbursementCleanupValues},
autoReimbursement: {pendingFields: null},
pendingFields: {
shouldShowAutoReimbursementLimitOption: null,
},
Expand Down
15 changes: 4 additions & 11 deletions src/pages/workspace/rules/ExpenseReportRulesSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function ExpenseReportRulesSection({policyID}: ExpenseReportRulesSectionProps) {
);
};

const reportTitlePendingFields = policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]?.pendingFields ?? {};
const optionItems = [
{
title: translate('workspace.rules.expenseReportRules.customReportNamesTitle'),
Expand All @@ -62,11 +63,7 @@ function ExpenseReportRulesSection({policyID}: ExpenseReportRulesSectionProps) {
onToggle: (isEnabled: boolean) => PolicyActions.enablePolicyDefaultReportTitle(policyID, isEnabled),
subMenuItems: [
<OfflineWithFeedback
pendingAction={
!policy?.pendingFields?.shouldShowCustomReportTitleOption && policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE].pendingFields?.defaultValue
? policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE].pendingFields?.defaultValue
: null
}
pendingAction={!policy?.pendingFields?.shouldShowCustomReportTitleOption && reportTitlePendingFields.defaultValue ? reportTitlePendingFields.defaultValue : null}
key="customName"
>
<MenuItemWithTopDescription
Expand All @@ -78,11 +75,7 @@ function ExpenseReportRulesSection({policyID}: ExpenseReportRulesSectionProps) {
/>
</OfflineWithFeedback>,
<ToggleSettingOptionRow
pendingAction={
!policy?.pendingFields?.shouldShowCustomReportTitleOption && policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE].pendingFields?.deletable
? policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE].pendingFields?.deletable
: null
}
pendingAction={!policy?.pendingFields?.shouldShowCustomReportTitleOption && reportTitlePendingFields.deletable ? reportTitlePendingFields.deletable : null}
key="preventMembersFromChangingCustomNames"
title={translate('workspace.rules.expenseReportRules.preventMembersFromChangingCustomNamesTitle')}
switchAccessibilityLabel={translate('workspace.rules.expenseReportRules.preventMembersFromChangingCustomNamesTitle')}
Expand Down Expand Up @@ -142,7 +135,7 @@ function ExpenseReportRulesSection({policyID}: ExpenseReportRulesSectionProps) {
>
<MenuItemWithTopDescription
description={translate('workspace.rules.expenseReportRules.randomReportAuditTitle')}
title={`${policy?.autoApproval?.auditRate ?? CONST.POLICY.RANDOM_AUDIT_DEFAULT_PERCENTAGE}%`}
title={`${Math.round((policy?.autoApproval?.auditRate ?? CONST.POLICY.RANDOM_AUDIT_DEFAULT_PERCENTAGE) * 100)}%`}
shouldShowRightIcon
style={[styles.sectionMenuItemTopDescription, styles.mt6, styles.mbn3]}
onPress={() => Navigation.navigate(ROUTES.RULES_RANDOM_REPORT_AUDIT.getRoute(policyID))}
Expand Down
3 changes: 1 addition & 2 deletions src/pages/workspace/rules/RulesRandomReportAuditPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ function RulesRandomReportAuditPage({route}: RulesRandomReportAuditPageProps) {
const styles = useThemeStyles();

const workflowApprovalsUnavailable = PolicyUtils.getWorkflowApprovalsUnavailable(policy);
const defaultValue = policy?.autoApproval?.auditRate ?? CONST.POLICY.RANDOM_AUDIT_DEFAULT_PERCENTAGE;

const defaultValue = Math.round((policy?.autoApproval?.auditRate ?? CONST.POLICY.RANDOM_AUDIT_DEFAULT_PERCENTAGE) * 100);
return (
<AccessOrNotFoundWrapper
policyID={policyID}
Expand Down

0 comments on commit 70a8a16

Please sign in to comment.