From 795c07596f1bd6f2464f2590152892d997215188 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cosmin=20P=C3=A2rvulescu?= Date: Fri, 1 Dec 2023 14:12:47 +0200 Subject: [PATCH] Add image deletion in setProfileMethod --- .../src/jsonrpc/methods/setProfile.ts | 51 +++++++++++++++++-- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/platform/identity/src/jsonrpc/methods/setProfile.ts b/platform/identity/src/jsonrpc/methods/setProfile.ts index 4d273ecf59..193b309c16 100644 --- a/platform/identity/src/jsonrpc/methods/setProfile.ts +++ b/platform/identity/src/jsonrpc/methods/setProfile.ts @@ -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, @@ -18,10 +21,23 @@ export const setProfileMethod = async ({ input: z.infer ctx: Context }): Promise => { + 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 = { @@ -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)) + } + } }