Skip to content

Commit

Permalink
fix(core): Fix multi-node query method and rare billing email assignm…
Browse files Browse the repository at this point in the history
…ent. (#2599)
  • Loading branch information
betimshahini authored Aug 11, 2023
1 parent 763bf25 commit 7b8ef26
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
23 changes: 14 additions & 9 deletions platform/address/src/jsonrpc/methods/getAddressURNForEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,24 @@ export const getAddressURNForEmailMethod = async ({
}): Promise<GetAddressURNForEmailResult> => {
const caller = router.createCaller(ctx)

let node = await caller.edges.findNode({
qc: {
alias: input,
let node = await caller.edges.findNodeBatch([
{
qc: {
alias: input,
},
},
})
])

if (!node) {
node = await caller.edges.findNode({
qc: {
alias: input.toLowerCase(),
node = await caller.edges.findNodeBatch([
{
qc: {
alias: input.toLowerCase(),
},
},
})
])
}

return node?.baseUrn as AddressURN
//We return first urn
return node.length > 0 ? (node[0]?.baseUrn as AddressURN) : undefined
}
26 changes: 18 additions & 8 deletions platform/edges/src/db/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type {
EdgeQueryResults,
NodeFilter,
} from './types'
import { InternalServerError } from '@proofzero/errors'

enum compType {
SRCQ = 'SRCQ',
Expand Down Expand Up @@ -132,7 +133,7 @@ export async function nodeBatch(
): Promise<(Node | undefined)[]> {
const prepStatements = filters.map((f) => getPrepStatementForNodeFilter(g, f))
const batchResults = await g.db.batch(prepStatements)
const results = batchResults.map((r) => getNodeFromResultSet(r))
const results = batchResults.flatMap((r) => getNodeFromResultSet(r))
return results
}

Expand All @@ -142,23 +143,31 @@ export async function node(
): Promise<Node | undefined> {
const prepStatement = getPrepStatementForNodeFilter(g, filter)
const resultSet = await prepStatement.all()
const node = getNodeFromResultSet(resultSet)
return node
const nodes = getNodeFromResultSet(resultSet)
if (nodes.length > 1)
throw new InternalServerError({
message: 'Mode than one node returned for a single-node query',
})
return nodes[0]
}

function getNodeFromResultSet(resultSet: D1Result): Node | undefined {
function getNodeFromResultSet(resultSet: D1Result): Node[] {
type resultRec = {
urn: AnyURN
compType: compType
k: string
v: string
}

let node: Node | undefined = undefined
let results: Node[] = []
let node = undefined

for (const result of (resultSet.results as resultRec[]) || []) {
if (node && node.baseUrn !== result.urn) {
results.push(node)
node = undefined
}
if (!node) node = { baseUrn: result.urn, qc: {}, rc: {} }
if (node.baseUrn !== result.urn)
throw new Error('More than one node found for given criteria.')

if (result.compType === compType.SRCQ) {
const compRec = { [result.k]: result.v }
Expand All @@ -169,8 +178,9 @@ function getNodeFromResultSet(resultSet: D1Result): Node | undefined {
Object.assign(node.rc, compRec)
}
}
if (node) results.push(node)

return node
return results
}

function getPrepStatementForNodeFilter(
Expand Down

0 comments on commit 7b8ef26

Please sign in to comment.