Skip to content

Commit

Permalink
feat(identity): introduce merge-preview procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
szkl committed Feb 29, 2024
1 parent f653535 commit 190eb81
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 0 deletions.
135 changes: 135 additions & 0 deletions platform/identity/src/jsonrpc/methods/merge-preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { z } from 'zod'

import { InternalServerError } from '@proofzero/errors'

import { router } from '@proofzero/platform.core'
import { EDGE_ACCOUNT } from '@proofzero/platform.account/src/constants'
import { EDGE_AUTHORIZES } from '@proofzero/platform.authorization/src/constants'
import { IdentityURNInput } from '@proofzero/platform-middleware/inputValidators'

import { Context } from '../../context'
import { initIdentityNodeByName } from '../../nodes'

export const MergePreviewInput = z.object({
source: IdentityURNInput,
target: IdentityURNInput,
})
type MergePreviewInput = z.infer<typeof MergePreviewInput>

const IdentitySummary = z.object({
avatar: z.string(),
displayName: z.string(),
primaryAccountAlias: z.string(),
accounts: z.number(),
applications: z.number(),
})

export const MergePreviewOutput = z.object({
source: IdentitySummary.optional(),
target: IdentitySummary.optional(),
})
type MergePreviewOutput = z.infer<typeof MergePreviewOutput>

type MergePreviewParams = {
input: MergePreviewInput
ctx: Context
}

type MergePreviewResult = MergePreviewOutput

interface MergePreviewMethod {
(params: MergePreviewParams): Promise<MergePreviewResult>
}

export const mergePreviewMethod: MergePreviewMethod = async ({
input,
ctx,
}) => {
const { source, target } = input

const sourceIdentityNode = initIdentityNodeByName(source, ctx.env.Identity)
const sourceIdentityProfile = await sourceIdentityNode.class.getProfile()
const targetIdentityNode = initIdentityNodeByName(target, ctx.env.Identity)
const targetIdentityProfile = await targetIdentityNode.class.getProfile()

if (!sourceIdentityProfile)
throw new InternalServerError({
message: 'missing source identity profile',
})

if (!targetIdentityProfile)
throw new InternalServerError({
message: 'missing target identity profile',
})

const caller = router.createCaller(ctx)

let sourceIdentityPrimaryAccountProfile
if (sourceIdentityProfile.primaryAccountURN) {
;[sourceIdentityPrimaryAccountProfile] =
await caller.account.getAccountProfileBatch([
sourceIdentityProfile.primaryAccountURN,
])
}

let targetIdentityPrimaryAccountProfile
if (targetIdentityProfile.primaryAccountURN) {
;[targetIdentityPrimaryAccountProfile] =
await caller.account.getAccountProfileBatch([
targetIdentityProfile.primaryAccountURN,
])
}

const { edges: sourceIdentityAccountEdges } = await caller.edges.getEdges({
query: {
src: { baseUrn: source },
tag: EDGE_ACCOUNT,
},
})

const { edges: sourceIdentityApplicationEdges } = await caller.edges.getEdges(
{
query: {
src: { baseUrn: source },
tag: EDGE_AUTHORIZES,
},
}
)

const { edges: targetIdentityAccountEdges } = await caller.edges.getEdges({
query: {
src: { baseUrn: target },
tag: EDGE_ACCOUNT,
},
})

const { edges: targetIdentityApplicationEdges } = await caller.edges.getEdges(
{
query: {
src: { baseUrn: target },
tag: EDGE_AUTHORIZES,
},
}
)

return {
source: sourceIdentityProfile
? {
avatar: sourceIdentityProfile.pfp?.image || '',
displayName: sourceIdentityProfile.displayName,
primaryAccountAlias: sourceIdentityPrimaryAccountProfile?.title || '',
accounts: sourceIdentityAccountEdges.length,
applications: sourceIdentityApplicationEdges.length,
}
: undefined,
target: targetIdentityProfile
? {
avatar: targetIdentityProfile.pfp?.image || '',
displayName: targetIdentityProfile.displayName,
primaryAccountAlias: targetIdentityPrimaryAccountProfile?.title || '',
accounts: targetIdentityAccountEdges.length,
applications: targetIdentityApplicationEdges.length,
}
: undefined,
}
}
15 changes: 15 additions & 0 deletions platform/identity/src/jsonrpc/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ import {
} from './methods/patchProfileFields'
import { resetProfileFieldsMethod } from './methods/resetProfileFields'
import { mergeMethod, MergeInput, MergeOutput } from './methods/merge'
import {
mergePreviewMethod,
MergePreviewInput,
MergePreviewOutput,
} from './methods/merge-preview'

const t = initTRPC.context<Context>().create({ errorFormatter })

Expand Down Expand Up @@ -320,4 +325,14 @@ export const appRouter = t.router({
.input(MergeInput)
.output(MergeOutput)
.mutation(mergeMethod),
mergePreview: t.procedure
.use(LogUsage)
.use(Analytics)
.use(AuthorizationTokenFromHeader)
.use(ValidateJWT)
.use(RequireIdentity)
.use(injectIdentityNode)
.input(MergePreviewInput)
.output(MergePreviewOutput)
.query(mergePreviewMethod),
})

0 comments on commit 190eb81

Please sign in to comment.