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
12 changes: 6 additions & 6 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@
};

let conciergeReportIDOnyxConnect: OnyxEntry<string>;
Onyx.connect({

Check warning on line 1059 in src/libs/ReportUtils.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.CONCIERGE_REPORT_ID,
callback: (value) => {
conciergeReportIDOnyxConnect = value;
Expand All @@ -1064,7 +1064,7 @@
});

const defaultAvatarBuildingIconTestID = 'SvgDefaultAvatarBuilding Icon';
Onyx.connect({

Check warning on line 1067 in src/libs/ReportUtils.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: (value) => {
// When signed out, val is undefined
Expand All @@ -1082,7 +1082,7 @@
let allPersonalDetails: OnyxEntry<PersonalDetailsList>;
let allPersonalDetailLogins: string[];
let currentUserPersonalDetails: OnyxEntry<PersonalDetails>;
Onyx.connect({

Check warning on line 1085 in src/libs/ReportUtils.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: (value) => {
if (deprecatedCurrentUserAccountID) {
Expand All @@ -1094,7 +1094,7 @@
});

let allReportsDraft: OnyxCollection<Report>;
Onyx.connect({

Check warning on line 1097 in src/libs/ReportUtils.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_DRAFT,
waitForCollectionCallback: true,
callback: (value) => (allReportsDraft = value),
Expand All @@ -1102,7 +1102,7 @@

let allPolicies: OnyxCollection<Policy>;
let policiesArray: Policy[] = [];
Onyx.connect({

Check warning on line 1105 in src/libs/ReportUtils.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,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -1112,7 +1112,7 @@
});

let allPolicyDrafts: OnyxCollection<Policy>;
Onyx.connect({

Check warning on line 1115 in src/libs/ReportUtils.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_DRAFTS,
waitForCollectionCallback: true,
callback: (value) => (allPolicyDrafts = value),
Expand All @@ -1120,7 +1120,7 @@

let allReports: OnyxCollection<Report>;
let reportsByPolicyID: ReportByPolicyMap;
Onyx.connect({

Check warning on line 1123 in src/libs/ReportUtils.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,
waitForCollectionCallback: true,
callback: (value) => {
Expand Down Expand Up @@ -1156,14 +1156,14 @@
});

let betaConfiguration: OnyxEntry<BetaConfiguration> = {};
Onyx.connect({

Check warning on line 1159 in src/libs/ReportUtils.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.BETA_CONFIGURATION,
callback: (value) => (betaConfiguration = value ?? {}),
});

let deprecatedAllTransactions: OnyxCollection<Transaction> = {};
let deprecatedReportsTransactions: Record<string, Transaction[]> = {};
Onyx.connect({

Check warning on line 1166 in src/libs/ReportUtils.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.TRANSACTION,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -1189,7 +1189,7 @@
});

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

Check warning on line 1192 in src/libs/ReportUtils.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 Down Expand Up @@ -4168,10 +4168,10 @@
* Builds an optimistic REIMBURSEMENT_DEQUEUED report action with a randomly generated reportActionID.
*
*/
function buildOptimisticCancelPaymentReportAction(expenseReportID: string, amount: number, currency: string): OptimisticCancelPaymentReportAction {
function buildOptimisticCancelPaymentReportAction(expenseReportID: string, amount: number, currency: string, currentUserAccountID: number): OptimisticCancelPaymentReportAction {
return {
actionName: CONST.REPORT.ACTIONS.TYPE.REIMBURSEMENT_DEQUEUED,
actorAccountID: deprecatedCurrentUserAccountID,
actorAccountID: currentUserAccountID,
message: [
{
cancellationReason: CONST.REPORT.CANCEL_PAYMENT_REASONS.ADMIN,
Expand Down Expand Up @@ -7266,7 +7266,7 @@
/**
* Builds an optimistic APPROVED report action with a randomly generated reportActionID.
*/
function buildOptimisticApprovedReportAction(amount: number, currency: string, expenseReportID: string): OptimisticApprovedReportAction {
function buildOptimisticApprovedReportAction(amount: number, currency: string, expenseReportID: string, currentUserAccountID: number): OptimisticApprovedReportAction {
const originalMessage = {
amount,
currency,
Expand All @@ -7276,7 +7276,7 @@

return {
actionName: CONST.REPORT.ACTIONS.TYPE.APPROVED,
actorAccountID: deprecatedCurrentUserAccountID,
actorAccountID: currentUserAccountID,
automatic: false,
avatar: getCurrentUserAvatar(),
isAttachmentOnly: false,
Expand Down Expand Up @@ -8300,10 +8300,10 @@
};
}

function buildOptimisticCardAssignedReportAction(assigneeAccountID: number): OptimisticCardAssignedReportAction {
function buildOptimisticCardAssignedReportAction(assigneeAccountID: number, currentUserAccountID: number): OptimisticCardAssignedReportAction {
return {
actionName: CONST.REPORT.ACTIONS.TYPE.CARD_ASSIGNED,
actorAccountID: deprecatedCurrentUserAccountID,
actorAccountID: currentUserAccountID,
avatar: getCurrentUserAvatar(),
created: DateUtils.getDBTime(),
originalMessage: {assigneeAccountID, cardID: -1},
Expand Down
10 changes: 8 additions & 2 deletions src/libs/actions/CompanyCards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,19 @@ function deleteWorkspaceCompanyCardFeed(
API.write(WRITE_COMMANDS.DELETE_COMPANY_CARD_FEED, parameters, {optimisticData, successData, failureData});
}

function assignWorkspaceCompanyCard(policy: OnyxEntry<Policy>, domainOrWorkspaceAccountID: number, translate: LocaleContextProps['translate'], data: Partial<AssignCardData>) {
function assignWorkspaceCompanyCard(
policy: OnyxEntry<Policy>,
domainOrWorkspaceAccountID: number,
translate: LocaleContextProps['translate'],
data: Partial<AssignCardData>,
currentUserAccountID: number,
) {
if (!policy?.id) {
return;
}
const {bankName, email = '', encryptedCardNumber = '', startDate = '', customCardName = ''} = data;
const assigneeDetails = PersonalDetailsUtils.getPersonalDetailByEmail(email);
const optimisticCardAssignedReportAction = ReportUtils.buildOptimisticCardAssignedReportAction(assigneeDetails?.accountID ?? CONST.DEFAULT_NUMBER_ID);
const optimisticCardAssignedReportAction = ReportUtils.buildOptimisticCardAssignedReportAction(assigneeDetails?.accountID ?? CONST.DEFAULT_NUMBER_ID, currentUserAccountID);

const parameters: AssignCompanyCardParams = {
domainAccountID: domainOrWorkspaceAccountID,
Expand Down
3 changes: 2 additions & 1 deletion src/libs/actions/IOU/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9921,7 +9921,7 @@ function approveMoneyRequest(params: ApproveMoneyRequestFunctionParams) {
if (hasHeldExpenses && !full && !!expenseReport.unheldTotal) {
total = expenseReport.unheldTotal;
}
const optimisticApprovedReportAction = buildOptimisticApprovedReportAction(total, expenseReport.currency ?? '', expenseReport.reportID);
const optimisticApprovedReportAction = buildOptimisticApprovedReportAction(total, expenseReport.currency ?? '', expenseReport.reportID, currentUserAccountIDParam);

const isDEWPolicy = hasDynamicExternalWorkflow(policy);
const shouldAddOptimisticApproveAction = !isDEWPolicy || isOffline();
Expand Down Expand Up @@ -11079,6 +11079,7 @@ function cancelPayment(
expenseReport.reportID,
-((expenseReport.total ?? 0) - (expenseReport?.nonReimbursableTotal ?? 0)),
expenseReport.currency ?? '',
currentUserAccountIDParam,
);
const approvalMode = policy?.approvalMode ?? CONST.POLICY.APPROVAL_MODE.BASIC;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ScrollView from '@components/ScrollView';
import Text from '@components/Text';
import useCardFeeds from '@hooks/useCardFeeds';
import {useCurrencyListState} from '@hooks/useCurrencyList';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useOnyx from '@hooks/useOnyx';
Expand Down Expand Up @@ -69,6 +70,9 @@ function ConfirmationStep({route}: ConfirmationStepProps) {
const cardholderEmail = Str.removeSMSDomain(cardToAssign?.email ?? '');
const cardholderAccountID = cardholder?.accountID;

const currentUserPersonalDetails = useCurrentUserPersonalDetails();
const currentUserAccountID = currentUserPersonalDetails.accountID;

useEffect(() => {
if (!assignCard?.isAssignmentFinished) {
return;
Expand Down Expand Up @@ -112,7 +116,7 @@ function ConfirmationStep({route}: ConfirmationStepProps) {
return;
}

assignWorkspaceCompanyCard(policy, domainOrWorkspaceAccountID, translate, {...cardToAssign, cardholder, bankName});
assignWorkspaceCompanyCard(policy, domainOrWorkspaceAccountID, translate, {...cardToAssign, cardholder, bankName}, currentUserAccountID);
};

const editStep = (step: string) => {
Expand Down
115 changes: 115 additions & 0 deletions tests/unit/ReportUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ import {getOriginalMessage, getReportAction, isWhisperAction} from '@libs/Report
import {buildReportNameFromParticipantNames, computeReportName as computeReportNameOriginal, getGroupChatName, getPolicyExpenseChatName, getReportName} from '@libs/ReportNameUtils';
import type {OptionData} from '@libs/ReportUtils';
import {
buildOptimisticApprovedReportAction,
buildOptimisticCancelPaymentReportAction,
buildOptimisticCardAssignedReportAction,
buildOptimisticChatReport,
buildOptimisticCreatedReportAction,
buildOptimisticCreatedReportForUnapprovedAction,
Expand Down Expand Up @@ -14881,4 +14884,116 @@ describe('ReportUtils', () => {
expect(details?.taxValue).toBe('10%');
});
});

describe('buildOptimisticApprovedReportAction', () => {
it('should set actorAccountID to the provided currentUserAccountID', () => {
const customAccountID = 99;
const action = buildOptimisticApprovedReportAction(500, 'USD', 'expenseReport1', customAccountID);

expect(action.actorAccountID).toBe(customAccountID);
});

it('should set actionName to APPROVED', () => {
const action = buildOptimisticApprovedReportAction(500, 'USD', 'expenseReport1', currentUserAccountID);

expect(action.actionName).toBe(CONST.REPORT.ACTIONS.TYPE.APPROVED);
});

it('should set originalMessage with the provided amount, currency, and expenseReportID', () => {
const amount = 1200;
const currency = 'EUR';
const expenseReportID = 'report42';
const action = buildOptimisticApprovedReportAction(amount, currency, expenseReportID, currentUserAccountID);

expect(getOriginalMessage(action as ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.APPROVED>)).toMatchObject({amount, currency, expenseReportID});
});

it('should set pendingAction to ADD', () => {
const action = buildOptimisticApprovedReportAction(500, 'USD', 'expenseReport1', currentUserAccountID);

expect(action.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD);
});

it('should generate a non-empty reportActionID', () => {
const action = buildOptimisticApprovedReportAction(500, 'USD', 'expenseReport1', currentUserAccountID);

expect(action.reportActionID).toBeTruthy();
});
});

describe('buildOptimisticCancelPaymentReportAction', () => {
it('should set actorAccountID to the provided currentUserAccountID', () => {
const customAccountID = 77;
const action = buildOptimisticCancelPaymentReportAction('expenseReport2', 300, 'USD', customAccountID);

expect(action.actorAccountID).toBe(customAccountID);
});

it('should set actionName to REIMBURSEMENT_DEQUEUED', () => {
const action = buildOptimisticCancelPaymentReportAction('expenseReport2', 300, 'USD', currentUserAccountID);

expect(action.actionName).toBe(CONST.REPORT.ACTIONS.TYPE.REIMBURSEMENT_DEQUEUED);
});

it('should set originalMessage with the provided expenseReportID, amount, currency, and cancellationReason', () => {
const expenseReportID = 'report55';
const amount = 750;
const currency = 'GBP';
const action = buildOptimisticCancelPaymentReportAction(expenseReportID, amount, currency, currentUserAccountID);

expect(getOriginalMessage(action as ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.REIMBURSEMENT_DEQUEUED>)).toMatchObject({
expenseReportID,
amount,
currency,
cancellationReason: CONST.REPORT.CANCEL_PAYMENT_REASONS.ADMIN,
});
});

it('should set pendingAction to ADD', () => {
const action = buildOptimisticCancelPaymentReportAction('expenseReport2', 300, 'USD', currentUserAccountID);

expect(action.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD);
});

it('should generate a non-empty reportActionID', () => {
const action = buildOptimisticCancelPaymentReportAction('expenseReport2', 300, 'USD', currentUserAccountID);

expect(action.reportActionID).toBeTruthy();
});
});

describe('buildOptimisticCardAssignedReportAction', () => {
it('should set actorAccountID to the provided currentUserAccountID', () => {
const assigneeAccountID = 10;
const customAccountID = 88;
const action = buildOptimisticCardAssignedReportAction(assigneeAccountID, customAccountID);

expect(action.actorAccountID).toBe(customAccountID);
});

it('should set actionName to CARD_ASSIGNED', () => {
const action = buildOptimisticCardAssignedReportAction(10, currentUserAccountID);

expect(action.actionName).toBe(CONST.REPORT.ACTIONS.TYPE.CARD_ASSIGNED);
});

it('should set originalMessage with the provided assigneeAccountID', () => {
const assigneeAccountID = 42;
const action = buildOptimisticCardAssignedReportAction(assigneeAccountID, currentUserAccountID);

expect(getOriginalMessage(action as ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.CARD_ASSIGNED>)).toMatchObject({assigneeAccountID});
});

it('should set pendingAction to ADD', () => {
const action = buildOptimisticCardAssignedReportAction(10, currentUserAccountID);

expect(action.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD);
});

it('should generate a non-empty reportActionID', () => {
const action = buildOptimisticCardAssignedReportAction(10, currentUserAccountID);

expect(action.reportActionID).toBeTruthy();
});
});
});
Loading