Skip to content

Commit

Permalink
Add image deletion in setProfileMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
Cosmin-Parvulescu committed Dec 1, 2023
1 parent 9278166 commit 795c075
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions platform/identity/src/jsonrpc/methods/setProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { inputValidators } from '@proofzero/platform-middleware'
import { Context } from '../../context'
import { ProfileSchema } from '../validators/profile'
import { IdentityURNSpace } from '@proofzero/urns/identity'
import { InternalServerError } from '@proofzero/errors'
import createImageClient from '@proofzero/platform-clients/image'
import { generateTraceContextHeaders } from '@proofzero/platform-middleware/trace'

export const SetProfileInput = z.object({
name: inputValidators.IdentityURNInput,
Expand All @@ -18,10 +21,23 @@ export const setProfileMethod = async ({
input: z.infer<typeof SetProfileInput>
ctx: Context
}): Promise<void> => {
if (!ctx.identityNode) {
throw new InternalServerError({
message: 'Identity node not found',
})
}

const caller = router.createCaller(ctx)

const existingProfile = await ctx.identityNode.class.getProfile()
const identityGraphNode = await caller.edges.findNode({
baseUrn: ctx.identityURN,
})

// if user is calling this method with the same identityURN in jwt
// TODO: validate JWT in "ValidateJWT" middleware
if (ctx.identityURN === input.name) {
await ctx.identityNode?.class.setProfile(input.profile)
await ctx.identityNode.class.setProfile(input.profile)
}

const qcomps = {
Expand All @@ -36,8 +52,37 @@ export const setProfileMethod = async ({
qcomps
)

const caller = router.createCaller(ctx)
await caller.edges.updateNode({ urnOfNode: enhancedUrn })
}
return

if (existingProfile && identityGraphNode) {
if (
existingProfile.primaryAccountURN !==
identityGraphNode.qc.primaryAccountURN
) {
throw new InternalServerError({
message: 'Primary account URN mismatch',
})
}

const accountNodeProfiles = await caller.account.getAccountProfileBatch([
existingProfile.primaryAccountURN,
])
if (!accountNodeProfiles || accountNodeProfiles.length === 0) {
throw new InternalServerError({
message: 'Primary account node not found',
})
}

if (
existingProfile.pfp?.image &&
existingProfile.pfp?.image !== accountNodeProfiles[0].icon
) {
const imageClient = createImageClient(ctx.env.Images, {
headers: generateTraceContextHeaders(ctx.traceSpan),
})

ctx.waitUntil(imageClient.delete.mutate(existingProfile.pfp?.image))
}
}
}

0 comments on commit 795c075

Please sign in to comment.