fix: add required filters for billing items and validate task project references

- Add buildBillingItemsFilter() to provide required itemDate filter
  BillingItems API requires a filter parameter and returns 500 error without it
- Add validation for task project_id foreign key references
  Tasks with invalid project_id now have the field nullified instead of failing
- Add getValidProjectIds() method to fetch valid project IDs from database

Fixes:
- Billing Items: 'Value cannot be null. Parameter name: filters' error
- Tasks: 'violates foreign key constraint tasks_project_id_fkey' error

Both entities should now sync successfully.
This commit is contained in:
root 2026-01-24 08:09:10 -05:00
parent f85741c993
commit f1037d7964
2 changed files with 72 additions and 0 deletions

View file

@ -332,6 +332,30 @@ export function buildTimeEntriesFilter(yearsBack: number = 2): Array<{ field: st
];
}
/**
* Build special filter for billing items (requires filter)
* @param yearsBack Number of years to look back (default: 2)
* @returns Query filter array for billing items
*/
export function buildBillingItemsFilter(yearsBack: number = 2): Array<{ field: string; op: string; value: any }> {
// BillingItems API requires a filter. Use itemDate to limit the range
// Calculate date from X years ago
const cutoffDate = new Date();
const millisecondsPerYear = 365.25 * 24 * 60 * 60 * 1000;
const millisecondsBack = yearsBack * millisecondsPerYear;
cutoffDate.setTime(cutoffDate.getTime() - millisecondsBack);
console.log(`BillingItems filter: itemDate >= ${cutoffDate.toISOString()} (${yearsBack} years back)`);
return [
{
field: 'itemDate',
op: 'gte',
value: cutoffDate.toISOString(),
},
];
}
/**
* Calculate estimated sync duration based on record count
* @param recordCount Number of records to sync