import { Transaction } from 'knex' ; import knex from '../db/knex' ; import { Account , Address , AddressInformation } from '../types/database' ; import logger from '../utils/logger' ; param payId returns export async function checkUserExistence ( payId ) { const result = await knex . select ( knex . raw ( 'exists(select 1 from account where pay_id = ?)' , payId ) ) ; return result [ 0 ] . exists ; } param payId param addresses param identityKey returns export async function insertUser ( payId , addresses , identityKey ? ) { return knex . transaction ( async ( transaction ) => { const insertedAddresses = await knex . insert ( { payId , identityKey , } ) . into < Account > ( 'account' ) . transacting ( transaction ) . returning ( 'id' ) . then ( async ( ids ) => { const accountId = ids [ 0 ] ; const mappedAddresses = addAccountIdToAddresses ( addresses , accountId ) ; return insertAddresses ( mappedAddresses , transaction ) ; } ) . then ( transaction . commit ) . catch ( transaction . rollback ) ; return insertedAddresses ; } ) ; } param oldPayId param newPayId returns export async function replaceUserPayId ( oldPayId , newPayId ) { const account = await knex < Account > ( 'account' ) . where ( 'payId' , oldPayId ) . update ( { payId : newPayId } ) . returning ( '*' ) ; if ( account . length === 1 ) { return account [ 0 ] ; } return null ; } param oldPayId param newPayId param addresses param identityKey returns export async function replaceUser ( oldPayId , newPayId , addresses , identityKey ? ) { return knex . transaction ( async ( transaction ) => { const updatedAddresses = await knex < Account > ( 'account' ) . where ( 'payId' , oldPayId ) . update ( { payId : newPayId , identityKey } ) . transacting ( transaction ) . returning ( 'id' ) . then ( async ( ids ) => { const accountId = ids [ 0 ] ; if ( accountId === undefined ) { return null ; } await knex < Address > ( 'address' ) . delete ( ) . where ( 'accountId' , accountId ) . transacting ( transaction ) ; const mappedAddresses = addAccountIdToAddresses ( addresses , accountId ) ; return insertAddresses ( mappedAddresses , transaction ) ; } ) . then ( transaction . commit ) . catch ( transaction . rollback ) ; return updatedAddresses ; } ) ; } param payId export async function removeUser ( payId ) { await knex < Account > ( 'account' ) . delete ( ) . where ( 'payId' , payId ) . then ( ( count ) => { if ( count > 1 ) { logger . fatal ( `We deleted ${ count } accounts with the PayID ${ payId }, which should be impossible due to our unique constraint.` ) ; process . exit ( 1 ) ; } } ) ; } interface DatabaseAddress extends AddressInformation { readonly accountId : string ; readonly identityKeySignature ? : string ; } param addresses param accountId returns function addAccountIdToAddresses ( addresses , accountId ) { return addresses . map ( ( address ) => ( { accountId , paymentNetwork : address . paymentNetwork . toUpperCase ( ) , environment : address . environment ?. toUpperCase ( ) , details : address . details , identityKeySignature : address . identityKeySignature , } ) ) ; } param addresses param transaction returns async function insertAddresses ( addresses , transaction ) { if ( addresses . length === 0 ) { return [ ] ; } return knex . insert ( addresses ) . into < Address > ( 'address' ) . transacting ( transaction ) . returning ( [ 'paymentNetwork' , 'environment' , 'details' , 'identityKeySignature' , ] ) ; }