@@ -408,19 +408,52 @@ const QUICKBOOKS_TRANSACTION_LIST_VALUES = {
408408 } ,
409409} as const
410410
411+ function getQuickBooksTransactionListControl (
412+ value : unknown ,
413+ values : Record < string , string > ,
414+ field : string
415+ ) : string | undefined {
416+ if ( value === undefined || value === 'default' ) return undefined
417+ if ( typeof value !== 'string' || ! Object . hasOwn ( values , value ) || ! values [ value ] ) {
418+ throw new Error ( `Unsupported QuickBooks ${ field } : ${ String ( value ) } ` )
419+ }
420+ return values [ value ]
421+ }
422+
411423function addQuickBooksTransactionListFilters (
412424 url : URL ,
413425 params : QuickBooksRunFinancialReportParams
414426) : void {
415- const transactionType = params . transactionType === 'default' ? undefined : params . transactionType
416- const groupBy = params . groupBy === 'default' ? undefined : params . groupBy
417- const accountsPayablePaid =
418- params . accountsPayablePaid === 'default' ? undefined : params . accountsPayablePaid
419- const accountsReceivablePaid =
420- params . accountsReceivablePaid === 'default' ? undefined : params . accountsReceivablePaid
421- const clearedStatus = params . clearedStatus === 'default' ? undefined : params . clearedStatus
422- const sourceAccountType =
423- params . sourceAccountType === 'default' ? undefined : params . sourceAccountType
427+ const transactionType = getQuickBooksTransactionListControl (
428+ params . transactionType ,
429+ QUICKBOOKS_TRANSACTION_LIST_VALUES . transactionType ,
430+ 'transactionType'
431+ )
432+ const groupBy = getQuickBooksTransactionListControl (
433+ params . groupBy ,
434+ QUICKBOOKS_TRANSACTION_LIST_VALUES . groupBy ,
435+ 'groupBy'
436+ )
437+ const accountsPayablePaid = getQuickBooksTransactionListControl (
438+ params . accountsPayablePaid ,
439+ QUICKBOOKS_TRANSACTION_LIST_VALUES . paidStatus ,
440+ 'accountsPayablePaid'
441+ )
442+ const accountsReceivablePaid = getQuickBooksTransactionListControl (
443+ params . accountsReceivablePaid ,
444+ QUICKBOOKS_TRANSACTION_LIST_VALUES . paidStatus ,
445+ 'accountsReceivablePaid'
446+ )
447+ const clearedStatus = getQuickBooksTransactionListControl (
448+ params . clearedStatus ,
449+ QUICKBOOKS_TRANSACTION_LIST_VALUES . clearedStatus ,
450+ 'clearedStatus'
451+ )
452+ const sourceAccountType = getQuickBooksTransactionListControl (
453+ params . sourceAccountType ,
454+ QUICKBOOKS_TRANSACTION_LIST_VALUES . sourceAccountType ,
455+ 'sourceAccountType'
456+ )
424457 const controls = {
425458 transaction_type : transactionType ,
426459 group_by : groupBy ,
@@ -436,37 +469,13 @@ function addQuickBooksTransactionListFilters(
436469 return
437470 }
438471
439- if ( transactionType ) {
440- url . searchParams . set (
441- 'transaction_type' ,
442- QUICKBOOKS_TRANSACTION_LIST_VALUES . transactionType [ transactionType ]
443- )
444- }
445- if ( groupBy ) {
446- url . searchParams . set ( 'group_by' , QUICKBOOKS_TRANSACTION_LIST_VALUES . groupBy [ groupBy ] )
447- }
448- if ( accountsPayablePaid ) {
449- url . searchParams . set (
450- 'appaid' ,
451- QUICKBOOKS_TRANSACTION_LIST_VALUES . paidStatus [ accountsPayablePaid ]
452- )
453- }
454- if ( accountsReceivablePaid ) {
455- url . searchParams . set (
456- 'arpaid' ,
457- QUICKBOOKS_TRANSACTION_LIST_VALUES . paidStatus [ accountsReceivablePaid ]
458- )
459- }
460- if ( clearedStatus ) {
461- url . searchParams . set ( 'cleared' , QUICKBOOKS_TRANSACTION_LIST_VALUES . clearedStatus [ clearedStatus ] )
462- }
472+ if ( transactionType ) url . searchParams . set ( 'transaction_type' , transactionType )
473+ if ( groupBy ) url . searchParams . set ( 'group_by' , groupBy )
474+ if ( accountsPayablePaid ) url . searchParams . set ( 'appaid' , accountsPayablePaid )
475+ if ( accountsReceivablePaid ) url . searchParams . set ( 'arpaid' , accountsReceivablePaid )
476+ if ( clearedStatus ) url . searchParams . set ( 'cleared' , clearedStatus )
463477 if ( controls . docnum ) url . searchParams . set ( 'docnum' , controls . docnum )
464- if ( sourceAccountType ) {
465- url . searchParams . set (
466- 'source_account_type' ,
467- QUICKBOOKS_TRANSACTION_LIST_VALUES . sourceAccountType [ sourceAccountType ]
468- )
469- }
478+ if ( sourceAccountType ) url . searchParams . set ( 'source_account_type' , sourceAccountType )
470479}
471480
472481export function buildQuickBooksReportUrl ( params : QuickBooksRunFinancialReportParams ) : URL {
0 commit comments