Skip to content

Commit

Permalink
Merge pull request #47545 from VickyStash/feature/UpdateExpensifyCard…
Browse files Browse the repository at this point in the history
…LimitType-api-call

[No QA] Implement UpdateExpensifyCardLimitType api call
  • Loading branch information
MariaHCD authored Aug 19, 2024
2 parents 74f095d + 2a45c56 commit e903ec9
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/libs/API/parameters/UpdateExpensifyCardLimitTypeParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type UpdateExpensifyCardLimitTypeParams = {
authToken: string;
cardID: number;
limitType: string;
};

export default UpdateExpensifyCardLimitTypeParams;
1 change: 1 addition & 0 deletions src/libs/API/parameters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,4 @@ export type {default as CreateExpensifyCardParams} from './CreateExpensifyCardPa
export type {default as UpdateExpensifyCardTitleParams} from './UpdateExpensifyCardTitleParams';
export type {default as OpenCardDetailsPageParams} from './OpenCardDetailsPageParams';
export type {default as ToggleCardContinuousReconciliationParams} from './ToggleCardContinuousReconciliationParams';
export type {default as UpdateExpensifyCardLimitTypeParams} from './UpdateExpensifyCardLimitTypeParams';
2 changes: 2 additions & 0 deletions src/libs/API/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const WRITE_COMMANDS = {
ACTIVATE_PHYSICAL_EXPENSIFY_CARD: 'ActivatePhysicalExpensifyCard',
UPDATE_EXPENSIFY_CARD_LIMIT: 'UpdateExpensifyCardLimit',
UPDATE_EXPENSIFY_CARD_TITLE: 'UpdateExpensifyCardTitle',
UPDATE_EXPENSIFY_CARD_LIMIT_TYPE: 'UpdateExpensifyCardLimitType',
CHRONOS_REMOVE_OOO_EVENT: 'Chronos_RemoveOOOEvent',
MAKE_DEFAULT_PAYMENT_METHOD: 'MakeDefaultPaymentMethod',
ADD_PAYMENT_CARD: 'AddPaymentCard',
Expand Down Expand Up @@ -357,6 +358,7 @@ type WriteCommandParameters = {
[WRITE_COMMANDS.ACTIVATE_PHYSICAL_EXPENSIFY_CARD]: Parameters.ActivatePhysicalExpensifyCardParams;
[WRITE_COMMANDS.UPDATE_EXPENSIFY_CARD_LIMIT]: Parameters.UpdateExpensifyCardLimitParams;
[WRITE_COMMANDS.UPDATE_EXPENSIFY_CARD_TITLE]: Parameters.UpdateExpensifyCardTitleParams;
[WRITE_COMMANDS.UPDATE_EXPENSIFY_CARD_LIMIT_TYPE]: Parameters.UpdateExpensifyCardLimitTypeParams;
[WRITE_COMMANDS.MAKE_DEFAULT_PAYMENT_METHOD]: Parameters.MakeDefaultPaymentMethodParams;
[WRITE_COMMANDS.ADD_PAYMENT_CARD]: Parameters.AddPaymentCardParams;
[WRITE_COMMANDS.ADD_PAYMENT_CARD_GBP]: Parameters.AddPaymentCardParams;
Expand Down
64 changes: 63 additions & 1 deletion src/libs/actions/Card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import type {
RevealExpensifyCardDetailsParams,
StartIssueNewCardFlowParams,
UpdateExpensifyCardLimitParams,
UpdateExpensifyCardLimitTypeParams,
UpdateExpensifyCardTitleParams,
} from '@libs/API/parameters';
import {READ_COMMANDS, SIDE_EFFECT_REQUEST_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
import * as ErrorUtils from '@libs/ErrorUtils';
import * as NetworkStore from '@libs/Network/NetworkStore';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {ExpensifyCardDetails, IssueNewCardData, IssueNewCardStep} from '@src/types/onyx/Card';
import type {CardLimitType, ExpensifyCardDetails, IssueNewCardData, IssueNewCardStep} from '@src/types/onyx/Card';
import type {ConnectionName} from '@src/types/onyx/Policy';

type ReplacementReason = 'damaged' | 'stolen';
Expand Down Expand Up @@ -438,6 +439,66 @@ function updateExpensifyCardTitle(workspaceAccountID: number, cardID: number, ne
API.write(WRITE_COMMANDS.UPDATE_EXPENSIFY_CARD_TITLE, parameters, {optimisticData, successData, failureData});
}

function updateExpensifyCardLimitType(workspaceAccountID: number, cardID: number, newLimitType: CardLimitType, oldLimitType?: CardLimitType) {
const authToken = NetworkStore.getAuthToken();

if (!authToken) {
return;
}

const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${workspaceAccountID}_${CONST.EXPENSIFY_CARD.BANK}`,
value: {
[cardID]: {
nameValuePairs: {
limitType: newLimitType,
},
isLoading: true,
errors: null,
},
},
},
];

const successData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${workspaceAccountID}_${CONST.EXPENSIFY_CARD.BANK}`,
value: {
[cardID]: {
isLoading: false,
},
},
},
];

const failureData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${workspaceAccountID}_${CONST.EXPENSIFY_CARD.BANK}`,
value: {
[cardID]: {
nameValuePairs: {
limitType: oldLimitType,
},
isLoading: false,
errors: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('common.genericErrorMessage'),
},
},
},
];

const parameters: UpdateExpensifyCardLimitTypeParams = {
authToken,
cardID,
limitType: newLimitType,
};

API.write(WRITE_COMMANDS.UPDATE_EXPENSIFY_CARD_LIMIT_TYPE, parameters, {optimisticData, successData, failureData});
}

function startIssueNewCardFlow(policyID: string) {
const parameters: StartIssueNewCardFlowParams = {
policyID,
Expand Down Expand Up @@ -573,5 +634,6 @@ export {
issueExpensifyCard,
openCardDetailsPage,
toggleContinuousReconciliation,
updateExpensifyCardLimitType,
};
export type {ReplacementReason};
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@ function WorkspaceEditCardLimitPage({route}: WorkspaceEditCardLimitPageProps) {
return newLimit - currentSpend;
};

const goBack = useCallback(() => Navigation.goBack(ROUTES.WORKSPACE_EXPENSIFY_CARD_DETAILS.getRoute(policyID, cardID)), [policyID, cardID]);

const updateCardLimit = (newLimit: number) => {
const newAvailableSpend = getNewAvailableSpend(newLimit);

setIsConfirmModalVisible(false);

Card.updateExpensifyCardLimit(workspaceAccountID, Number(cardID), newLimit, newAvailableSpend, card?.nameValuePairs?.unapprovedExpenseLimit, card?.availableSpend);

Navigation.goBack();
goBack();
};

const submit = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.EDIT_EXPENSIFY_CARD_LIMIT_FORM>) => {
Expand Down Expand Up @@ -108,7 +110,7 @@ function WorkspaceEditCardLimitPage({route}: WorkspaceEditCardLimitPageProps) {
>
<HeaderWithBackButton
title={translate('workspace.expensifyCard.cardLimit')}
onBackButtonPress={() => Navigation.goBack(ROUTES.WORKSPACE_EXPENSIFY_CARD_DETAILS.getRoute(policyID, cardID))}
onBackButtonPress={goBack}
/>
<FormProvider
formID={ONYXKEYS.FORMS.EDIT_EXPENSIFY_CARD_LIMIT_FORM}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as PolicyUtils from '@libs/PolicyUtils';
import Navigation from '@navigation/Navigation';
import type {SettingsNavigatorParamList} from '@navigation/types';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import * as Card from '@userActions/Card';
import * as Policy from '@userActions/Policy/Policy';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -48,14 +49,20 @@ function WorkspaceEditCardLimitTypePage({route}: WorkspaceEditCardLimitTypePageP
const [typeSelected, setTypeSelected] = useState(initialLimitType);
const [isConfirmModalVisible, setIsConfirmModalVisible] = useState(false);

const goBack = useCallback(() => Navigation.goBack(ROUTES.WORKSPACE_EXPENSIFY_CARD_DETAILS.getRoute(policyID, cardID)), [policyID, cardID]);

const fetchCardLimitTypeData = useCallback(() => {
Policy.openPolicyEditCardLimitTypePage(policyID, Number(cardID));
}, [policyID, cardID]);

useFocusEffect(fetchCardLimitTypeData);

const updateCardLimitType = () => {
// TODO: add API call when it's supported https://github.com/Expensify/Expensify/issues/407833
setIsConfirmModalVisible(false);

Card.updateExpensifyCardLimitType(workspaceAccountID, Number(cardID), typeSelected, card?.nameValuePairs?.limitType);

goBack();
};

const submit = () => {
Expand Down Expand Up @@ -146,7 +153,7 @@ function WorkspaceEditCardLimitTypePage({route}: WorkspaceEditCardLimitTypePageP
>
<HeaderWithBackButton
title={translate('workspace.card.issueNewCard.limitType')}
onBackButtonPress={() => Navigation.goBack(ROUTES.WORKSPACE_EXPENSIFY_CARD_DETAILS.getRoute(policyID, cardID))}
onBackButtonPress={goBack}
/>
<FullPageOfflineBlockingView>
<SelectionList
Expand Down
7 changes: 5 additions & 2 deletions src/types/onyx/Card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ type CardList = Record<string, Card>;
/** Issue new card flow steps */
type IssueNewCardStep = ValueOf<typeof CONST.EXPENSIFY_CARD.STEP>;

/** Card spending limit type */
type CardLimitType = ValueOf<typeof CONST.EXPENSIFY_CARD.LIMIT_TYPES>;

/** Data required to be sent to issue a new card */
type IssueNewCardData = {
/** The email address of the cardholder */
Expand All @@ -104,7 +107,7 @@ type IssueNewCardData = {
cardType: ValueOf<typeof CONST.EXPENSIFY_CARD.CARD_TYPE>;

/** Card spending limit type */
limitType: ValueOf<typeof CONST.EXPENSIFY_CARD.LIMIT_TYPES>;
limitType: CardLimitType;

/** Card spending limit */
limit: number;
Expand All @@ -129,4 +132,4 @@ type IssueNewCard = {
type WorkspaceCardsList = Record<string, Card>;

export default Card;
export type {ExpensifyCardDetails, CardList, IssueNewCard, IssueNewCardStep, IssueNewCardData, WorkspaceCardsList};
export type {ExpensifyCardDetails, CardList, IssueNewCard, IssueNewCardStep, IssueNewCardData, WorkspaceCardsList, CardLimitType};

0 comments on commit e903ec9

Please sign in to comment.