From a3c326e5af119eb81c3fa292ab7eb90dbc9428d0 Mon Sep 17 00:00:00 2001 From: Edward Alvarado Date: Mon, 18 Dec 2023 12:38:47 -0300 Subject: [PATCH] getRelatedWallets --- .../node-sdk/paima-db/src/delegate-wallet.ts | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/node-sdk/paima-db/src/delegate-wallet.ts b/packages/node-sdk/paima-db/src/delegate-wallet.ts index da07e5e4e..77dfcab9a 100644 --- a/packages/node-sdk/paima-db/src/delegate-wallet.ts +++ b/packages/node-sdk/paima-db/src/delegate-wallet.ts @@ -56,10 +56,25 @@ export async function getRelatedWallets( ): Promise<{ from: IGetDelegationsFromWithAddressResult[]; to: IGetDelegationsToWithAddressResult[]; + id: number; }> { const address = _address.toLocaleLowerCase(); const [addressResult] = await getAddressFromAddress.run({ address }, DBConn); - const from = await getDelegationsFromWithAddress.run({ from_id: addressResult.id }, DBConn); - const to = await getDelegationsToWithAddress.run({ to_id: addressResult.id }, DBConn); - return { from, to }; + if (!addressResult) { + return { from: [], to: [], id: NO_USER_ID }; + } + let to: IGetDelegationsToWithAddressResult[] = []; + let from: IGetDelegationsFromWithAddressResult[] = []; + + to = await getDelegationsToWithAddress.run({ to_id: addressResult.id }, DBConn); + if (!to.length) { + // cannot be both from and to. + from = await getDelegationsFromWithAddress.run({ from_id: addressResult.id }, DBConn); + } + + return { + from, + to, + id: to.length ? to[0].id : addressResult.id, + }; }