fix: name on customer if custom data is filled

This commit is contained in:
Rafael Taranto 2025-05-23 22:17:05 +01:00
parent f0bf26d394
commit b252333702

View file

@ -1,9 +1,13 @@
import db from './db.js' import db from './db.js'
import { ExpressionBuilder, sql } from 'kysely' import { ExpressionBuilder } from 'kysely'
import { Customers, DB } from './types/types.js' import { Customers, DB, EditedCustomerData } from './types/types.js'
import { jsonObjectFrom } from 'kysely/helpers/postgres' import { jsonObjectFrom } from 'kysely/helpers/postgres'
type CustomerEB = ExpressionBuilder<DB & { c: Customers }, 'c'> type CustomerEB = ExpressionBuilder<DB & { c: Customers }, 'c'>
type CustomerWithEditedEB = ExpressionBuilder<
DB & { c: Customers } & { e: EditedCustomerData | null },
'c' | 'e'
>
const TX_PASSTHROUGH_ERROR_CODES = [ const TX_PASSTHROUGH_ERROR_CODES = [
'operatorCancel', 'operatorCancel',
@ -87,6 +91,23 @@ function joinTxsTotals(eb: CustomerEB) {
.as('txStats') .as('txStats')
} }
function selectNewestIdCardData(eb: CustomerWithEditedEB, ref: any) {
return eb
.case()
.when(
eb.and([
eb(ref('e.idCardDataAt'), 'is not', null),
eb.or([
eb(ref('c.idCardDataAt'), 'is', null),
eb(ref('e.idCardDataAt'), '>', ref('c.idCardDataAt')),
]),
]),
)
.then(ref('e.idCardData'))
.else(ref('c.idCardData'))
.end()
}
interface GetCustomerListOptions { interface GetCustomerListOptions {
withCustomInfoRequest: boolean withCustomInfoRequest: boolean
} }
@ -101,22 +122,23 @@ function getCustomerList(
): Promise<any[]> { ): Promise<any[]> {
return db return db
.selectFrom('customers as c') .selectFrom('customers as c')
.leftJoin('editedCustomerData as e', 'e.customerId', 'c.id')
.leftJoinLateral(joinTxsTotals, join => join.onTrue()) .leftJoinLateral(joinTxsTotals, join => join.onTrue())
.leftJoinLateral(joinLatestTx, join => join.onTrue()) .leftJoinLateral(joinLatestTx, join => join.onTrue())
.select(({ eb, fn, val, ref }) => [ .select(({ eb, fn, val, ref }) => [
'id', 'c.id',
'authorizedOverride', 'c.authorizedOverride',
'frontCameraPath', 'c.frontCameraPath',
'frontCameraOverride', 'c.frontCameraOverride',
'idCardPhotoPath', 'c.idCardPhotoPath',
'idCardPhotoOverride', 'c.idCardPhotoOverride',
'idCardData', selectNewestIdCardData(eb, ref).as('idCardData'),
'idCardDataOverride', 'c.idCardDataOverride',
'email', 'c.email',
'usSsn', 'c.usSsn',
'usSsnOverride', 'c.usSsnOverride',
'sanctions', 'c.sanctions',
'sanctionsOverride', 'c.sanctionsOverride',
'txStats.totalSpent', 'txStats.totalSpent',
'txStats.totalTxs', 'txStats.totalTxs',
ref('lastTx.fiatCode').as('lastTxFiatCode'), ref('lastTx.fiatCode').as('lastTxFiatCode'),