Skip to content

Commit fb48fbe

Browse files
authored
Merge pull request Expensify#86918 from Expensify/revert-74570-feat-ORTF-Auto-reporting-frequency
Revert "follow-up: Start & End date does not matches Title format with Trip-based Auto-reporting"
2 parents ffa7513 + dc82257 commit fb48fbe

3 files changed

Lines changed: 13 additions & 117 deletions

File tree

src/libs/Formula.ts

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ function computeAutoReportingInfo(part: FormulaPart, context: FormulaContext, su
330330
return part.definition;
331331
}
332332

333-
const {startDate, endDate} = getAutoReportingDates(policy, report, new Date(), context);
333+
const {startDate, endDate} = getAutoReportingDates(policy, report);
334334

335335
switch (subField.toLowerCase()) {
336336
case 'start':
@@ -658,21 +658,6 @@ function getAllReportTransactionsWithContext(reportID: string, context?: Formula
658658
const transactions = [...getReportTransactions(reportID)];
659659
const contextTransaction = context?.transaction;
660660

661-
// Merge optimistic transactions not yet in Onyx, passed via FormulaContext.allTransactions.
662-
if (context?.allTransactions) {
663-
for (const ctxTransaction of Object.values(context.allTransactions)) {
664-
if (!ctxTransaction?.transactionID || ctxTransaction.reportID !== reportID) {
665-
continue;
666-
}
667-
const existingIndex = transactions.findIndex((t) => t?.transactionID === ctxTransaction.transactionID);
668-
if (existingIndex >= 0) {
669-
transactions[existingIndex] = ctxTransaction;
670-
} else {
671-
transactions.push(ctxTransaction);
672-
}
673-
}
674-
}
675-
676661
if (contextTransaction?.transactionID && contextTransaction.reportID === reportID) {
677662
const transactionIndex = transactions.findIndex((transaction) => transaction?.transactionID === contextTransaction.transactionID);
678663
if (transactionIndex >= 0) {
@@ -784,7 +769,7 @@ function getMonthlyLastBusinessDayPeriod(currentDate: Date): {startDate: Date; e
784769
/**
785770
* Calculate the start and end dates for auto-reporting based on the frequency and current date
786771
*/
787-
function getAutoReportingDates(policy: OnyxEntry<Policy>, report: Report, currentDate = new Date(), context?: FormulaContext): {startDate: Date | undefined; endDate: Date | undefined} {
772+
function getAutoReportingDates(policy: OnyxEntry<Policy>, report: Report, currentDate = new Date()): {startDate: Date | undefined; endDate: Date | undefined} {
788773
const frequency = policy?.autoReportingFrequency;
789774
const offset = policy?.autoReportingOffset;
790775

@@ -837,11 +822,10 @@ function getAutoReportingDates(policy: OnyxEntry<Policy>, report: Report, curren
837822
}
838823

839824
case CONST.POLICY.AUTO_REPORTING_FREQUENCIES.TRIP: {
840-
// For trip-based, use oldest transaction as start and newest transaction as end
841-
const oldestTransactionDateString = getOldestTransactionDate(report.reportID, context);
842-
const newestTransactionDateString = getNewestTransactionDate(report.reportID, context);
825+
// For trip-based, use oldest transaction as start
826+
const oldestTransactionDateString = getOldestTransactionDate(report.reportID);
843827
startDate = oldestTransactionDateString ? new Date(oldestTransactionDateString) : currentDate;
844-
endDate = newestTransactionDateString ? new Date(newestTransactionDateString) : currentDate;
828+
endDate = currentDate;
845829
break;
846830
}
847831

src/libs/actions/IOU/index.ts

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ import {
138138
canEditFieldOfMoneyRequest,
139139
canSubmitAndIsAwaitingForCurrentUser,
140140
canUserPerformWriteAction as canUserPerformWriteActionReportUtils,
141-
computeOptimisticReportName,
142141
findSelfDMReportID,
143142
generateReportID,
144143
getAllHeldTransactions as getAllHeldTransactionsReportUtils,
@@ -189,6 +188,7 @@ import {
189188
isSettled,
190189
isTestTransactionReport,
191190
isTrackExpenseReport,
191+
populateOptimisticReportFormula,
192192
prepareOnboardingOnyxData,
193193
shouldCreateNewMoneyRequestReport as shouldCreateNewMoneyRequestReportReportUtils,
194194
shouldEnableNegative,
@@ -3202,45 +3202,25 @@ function getDeleteTrackExpenseInformation(
32023202
* This is needed when report totals change (e.g., adding expenses or changing reimbursable status)
32033203
* to ensure the report title reflects the updated values like {report:reimbursable}.
32043204
*/
3205-
function recalculateOptimisticReportName(iouReport: OnyxTypes.Report, policy: OnyxEntry<OnyxTypes.Policy>, newTransaction?: OnyxTypes.Transaction): string | undefined {
3205+
function recalculateOptimisticReportName(iouReport: OnyxTypes.Report, policy: OnyxEntry<OnyxTypes.Policy>): string | undefined {
32063206
if (!policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]) {
32073207
return undefined;
32083208
}
32093209
const titleFormula = policy.fieldList[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]?.defaultValue ?? '';
32103210
if (!titleFormula) {
32113211
return undefined;
32123212
}
3213-
3214-
// Gather existing transactions + the optimistic one not yet in Onyx.
3215-
const existingTransactions = getReportTransactions(iouReport.reportID);
3216-
const transactionsRecord: Record<string, OnyxTypes.Transaction> = {};
3217-
for (const transaction of existingTransactions) {
3218-
if (transaction?.transactionID) {
3219-
transactionsRecord[transaction.transactionID] = transaction;
3220-
}
3221-
}
3222-
if (newTransaction?.transactionID) {
3223-
transactionsRecord[newTransaction.transactionID] = newTransaction;
3224-
}
3225-
3226-
const computedName = computeOptimisticReportName(iouReport, policy, iouReport.policyID, transactionsRecord);
3227-
return computedName ?? undefined;
3213+
return populateOptimisticReportFormula(titleFormula, iouReport as Parameters<typeof populateOptimisticReportFormula>[1], policy);
32283214
}
32293215

3230-
function maybeUpdateReportNameForFormulaTitle(iouReport: OnyxTypes.Report, policy: OnyxEntry<OnyxTypes.Policy>, newTransaction?: OnyxTypes.Transaction): OnyxTypes.Report {
3216+
function maybeUpdateReportNameForFormulaTitle(iouReport: OnyxTypes.Report, policy: OnyxEntry<OnyxTypes.Policy>): OnyxTypes.Report {
32313217
const reportNameValuePairs = allReportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${iouReport.reportID}`];
32323218
const titleField = reportNameValuePairs?.expensify_text_title;
3233-
3234-
// Fall back to policy.fieldList when reportNameValuePairs doesn't exist yet (optimistic reports).
3235-
const isFormulaTitle = reportNameValuePairs
3236-
? titleField?.type === CONST.REPORT_FIELD_TYPES.FORMULA
3237-
: policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]?.type === CONST.REPORT_FIELD_TYPES.FORMULA;
3238-
3239-
if (!isFormulaTitle) {
3219+
if (titleField?.type !== CONST.REPORT_FIELD_TYPES.FORMULA) {
32403220
return iouReport;
32413221
}
32423222

3243-
const updatedReportName = recalculateOptimisticReportName(iouReport, policy, newTransaction);
3223+
const updatedReportName = recalculateOptimisticReportName(iouReport, policy);
32443224
if (!updatedReportName) {
32453225
return iouReport;
32463226
}
@@ -3424,6 +3404,8 @@ function getMoneyRequestInformation(moneyRequestInformation: MoneyRequestInforma
34243404
iouReport.nonReimbursableTotal = (iouReport.nonReimbursableTotal ?? 0) - amount;
34253405
}
34263406
}
3407+
3408+
iouReport = maybeUpdateReportNameForFormulaTitle(iouReport, policy);
34273409
}
34283410
if (typeof iouReport.unheldTotal === 'number') {
34293411
// Use newReportTotal in scenarios where the total is based on more than just the current transaction amount, and we need to override it manually
@@ -3525,11 +3507,6 @@ function getMoneyRequestInformation(moneyRequestInformation: MoneyRequestInforma
35253507
}
35263508
}
35273509

3528-
// Recalculate report name after STEP 3 so the optimistic transaction is included in formula computation.
3529-
if (!shouldCreateNewMoneyRequestReport && isPolicyExpenseChat) {
3530-
iouReport = maybeUpdateReportNameForFormulaTitle(iouReport, policy, optimisticTransaction);
3531-
}
3532-
35333510
// STEP 4: Build optimistic reportActions. We need:
35343511
// 1. CREATED action for the chatReport
35353512
// 2. CREATED action for the iouReport

tests/unit/FormulaTest.ts

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -675,71 +675,6 @@ describe('CustomFormula', () => {
675675
const context = createMockContext(policy);
676676

677677
expect(compute('{report:autoreporting:start}', context)).toBe('2025-01-08');
678-
expect(compute('{report:autoreporting:end}', context)).toBe('2025-01-14');
679-
});
680-
681-
test('should use context.transaction for trip end date when adding a new expense to existing report', () => {
682-
// First transaction already in Onyx (oldest expense, dated Jan 8)
683-
mockReportUtils.getReportTransactions.mockReturnValue([
684-
{transactionID: 'existing1', reportID: '123', created: '2025-01-08T12:00:00Z', merchant: 'Hotel', amount: 5000} as Transaction,
685-
]);
686-
687-
const policy = {autoReportingFrequency: CONST.POLICY.AUTO_REPORTING_FREQUENCIES.TRIP} as Policy;
688-
// Second transaction passed via context (newest expense, dated Jan 14 — not in Onyx yet)
689-
const context: FormulaContext = {
690-
report: mockReport,
691-
policy,
692-
transaction: {transactionID: 'optimistic1', reportID: '123', created: '2025-01-14T16:00:00Z', merchant: 'Restaurant', amount: 3000} as Transaction,
693-
};
694-
695-
// Start should be oldest (Jan 8 from Onyx), end should be newest (Jan 14 from context)
696-
expect(compute('{report:autoreporting:start}', context)).toBe('2025-01-08');
697-
expect(compute('{report:autoreporting:end}', context)).toBe('2025-01-14');
698-
});
699-
700-
test('should use allTransactions for trip dates when Onyx is empty (new report optimistic flow)', () => {
701-
mockReportUtils.getReportTransactions.mockReturnValue([]);
702-
703-
const policy = {autoReportingFrequency: CONST.POLICY.AUTO_REPORTING_FREQUENCIES.TRIP} as Policy;
704-
const context: FormulaContext = {
705-
report: mockReport,
706-
policy,
707-
allTransactions: {
708-
trans1: {transactionID: 'trans1', reportID: '123', created: '2025-01-08T12:00:00Z', merchant: 'Hotel', amount: 5000} as Transaction,
709-
},
710-
};
711-
712-
expect(compute('{report:autoreporting:start}', context)).toBe('2025-01-08');
713-
expect(compute('{report:autoreporting:end}', context)).toBe('2025-01-08');
714-
});
715-
716-
test('should use allTransactions to merge Onyx + optimistic transaction for trip date range', () => {
717-
mockReportUtils.getReportTransactions.mockReturnValue([
718-
{transactionID: 'existing1', reportID: '123', created: '2025-01-08T12:00:00Z', merchant: 'Hotel', amount: 5000} as Transaction,
719-
]);
720-
721-
const policy = {autoReportingFrequency: CONST.POLICY.AUTO_REPORTING_FREQUENCIES.TRIP} as Policy;
722-
const context: FormulaContext = {
723-
report: mockReport,
724-
policy,
725-
allTransactions: {
726-
existing1: {transactionID: 'existing1', reportID: '123', created: '2025-01-08T12:00:00Z', merchant: 'Hotel', amount: 5000} as Transaction,
727-
optimistic1: {transactionID: 'optimistic1', reportID: '123', created: '2025-01-14T16:00:00Z', merchant: 'Restaurant', amount: 3000} as Transaction,
728-
},
729-
};
730-
731-
expect(compute('{report:autoreporting:start}', context)).toBe('2025-01-08');
732-
expect(compute('{report:autoreporting:end}', context)).toBe('2025-01-14');
733-
});
734-
735-
test('should fallback to current date for trip frequency when no transactions', () => {
736-
mockReportUtils.getReportTransactions.mockReturnValue([]);
737-
738-
const policy = {autoReportingFrequency: CONST.POLICY.AUTO_REPORTING_FREQUENCIES.TRIP} as Policy;
739-
const context = createMockContext(policy);
740-
741-
// Should fall back to current date (2025-01-19 from jest.setSystemTime)
742-
expect(compute('{report:autoreporting:start}', context)).toBe('2025-01-19');
743678
expect(compute('{report:autoreporting:end}', context)).toBe('2025-01-19');
744679
});
745680

0 commit comments

Comments
 (0)