-
Notifications
You must be signed in to change notification settings - Fork 9
Update sync code for unified-accounting #290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import type {QBO_ENTITY_NAME} from '@openint/connector-qbo' | ||
import {type QBOSDKTypes} from '@openint/connector-qbo' | ||
import {snakeCase, type z} from '@openint/util' | ||
import {R, snakeCase, type z} from '@openint/util' | ||
import {mapper, zCast} from '@openint/vdk' | ||
import * as unified from '../../unifiedModels' | ||
|
||
|
@@ -36,6 +36,10 @@ const tranactionMappers = { | |
})) | ||
}, | ||
bank_category: () => 'Purchase', | ||
vendor_id: (t) => | ||
t.EntityRef?.type === 'Vendor' && t.EntityRef?.value | ||
? makeQboId('Vendor', t.EntityRef.value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No null check on t.EntityRef.value before using it in makeQboId. If value is undefined/null but type is 'Vendor', this will create an invalid ID. Should add null check for value property.
|
||
: '', // for some reason `null` causes vendor_id to be quoted, probably encoded as json, does not add up but... | ||
created_at: 'MetaData.CreateTime', | ||
updated_at: 'MetaData.LastUpdatedTime', | ||
}), | ||
|
@@ -46,17 +50,33 @@ const tranactionMappers = { | |
currency: 'CurrencyRef.value', | ||
memo: 'PrivateNote', | ||
amount: 'TotalAmt', | ||
lines: (p) => | ||
p.Line.map((line) => ({ | ||
id: line.Id, | ||
memo: line.Description, | ||
amount: -1 * line.Amount, | ||
currency: p.CurrencyRef.value, | ||
account_id: makeQboId( | ||
'Account', | ||
line.DepositLineDetail?.AccountRef?.value ?? '', | ||
), | ||
})), | ||
lines: (_t) => { | ||
const t = _t as typeof _t & { | ||
CashBack?: { | ||
Amount: number | ||
Memo: string | ||
AccountRef: {value: string; name: string} | ||
} | ||
} | ||
return R.compact([ | ||
...t.Line.map((line) => ({ | ||
id: line.Id, | ||
memo: line.Description, | ||
amount: -1 * line.Amount, | ||
currency: t.CurrencyRef.value, | ||
account_id: makeQboId( | ||
'Account', | ||
line.DepositLineDetail?.AccountRef?.value ?? '', | ||
), | ||
})), | ||
t.CashBack && { | ||
amount: t.CashBack.Amount, | ||
currency: t.CurrencyRef.value, | ||
memo: t.CashBack.Memo, | ||
account_id: makeQboId('Account', t.CashBack.AccountRef.value), | ||
}, | ||
]) | ||
}, | ||
bank_category: () => 'Deposit', | ||
created_at: 'MetaData.CreateTime', | ||
updated_at: 'MetaData.LastUpdatedTime', | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -46,6 +46,7 @@ export function unifiedAccountingLink(ctx: { | |||||
// TODO: Should build this into the mapper itself | ||||||
const stream = | ||||||
{ | ||||||
// QBO | ||||||
Purchase: 'transaction', | ||||||
JournalEntry: 'transaction', | ||||||
Deposit: 'transaction', | ||||||
|
@@ -55,25 +56,40 @@ export function unifiedAccountingLink(ctx: { | |||||
Vendor: 'vendor', | ||||||
Customer: 'customer', | ||||||
Attachable: 'attachment', | ||||||
// Plaid | ||||||
merchant: 'vendor', | ||||||
}[op.data.entityName] ?? op.data.entityName | ||||||
|
||||||
const mapped = applyMapper(mapper, op.data.entity, { | ||||||
remote_data_key: 'remote_data', | ||||||
}) | ||||||
if ('lines' in mapped) { | ||||||
const txn = mapped as { | ||||||
amount?: number | ||||||
lines: {amount: number}[] | ||||||
id: string | ||||||
} | ||||||
const trialBalance = txn.lines?.reduce((acc, line) => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The optional chaining operator (?.) on txn.lines could lead to undefined being passed to reduce(), which would cause a runtime error. Should add a null check or provide default empty array: txn.lines?.reduce(...) ?? 0
|
||||||
return acc + line.amount | ||||||
}, txn.amount ?? 0) | ||||||
if (trialBalance !== 0) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The trial balance check uses strict equality to zero, which may be error‐prone due to floating‐point rounding. Consider comparing with a small epsilon tolerance.
Suggested change
|
||||||
console.warn(`Transaction does not balance: ${trialBalance} ${txn.id}`) | ||||||
} | ||||||
} | ||||||
|
||||||
return rxjs.of({ | ||||||
...op, | ||||||
data: { | ||||||
stream, | ||||||
data: { | ||||||
...mapped, | ||||||
connection_id: ctx.source.id, // Should this be the default somehow? | ||||||
_openint_connection_id: ctx.source.id, // Should this be the default somehow? | ||||||
// Denormalize customer_id onto entities for now, though may be better | ||||||
// to just sync connection also? | ||||||
customer_id: ctx.source.customerId, | ||||||
_openint_customer_id: ctx.source.customerId, | ||||||
}, | ||||||
upsert: { | ||||||
key_columns: ['connection_id', 'id'], | ||||||
key_columns: ['_openint_connection_id', 'id'], | ||||||
insert_only_columns: ['created_at'], | ||||||
no_diff_columns: ['updated_at'], | ||||||
}, | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider stricter validation (e.g.
.strict()
) for the 'merchant' entity if extra props aren’t allowed.