Skip to content

Commit

Permalink
chore(console): Revise IdentityGroups to map to Identities (Accounts) (
Browse files Browse the repository at this point in the history
  • Loading branch information
Cosmin-Parvulescu authored Aug 11, 2023
1 parent 7b8ef26 commit 5573296
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 134 deletions.
69 changes: 36 additions & 33 deletions apps/console/app/routes/__layout/spuorg.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import createCoreClient from '@proofzero/platform-clients/core'
import { getAuthzHeaderConditionallyFromToken } from '@proofzero/utils'
import { PlatformAddressURNHeader } from '@proofzero/types/headers'
import { NO_OP_ADDRESS_PLACEHOLDER } from '@proofzero/platform/address/src/constants'
import { AddressURN } from '@proofzero/urns/address'
import { Outlet, useLoaderData } from '@remix-run/react'
import { AddressURN, AddressURNSpace } from '@proofzero/urns/address'
import { Outlet, useLoaderData, useOutletContext } from '@remix-run/react'
import { Toaster } from '@proofzero/design-system/src/atoms/toast'

type GroupMemberModel = {
URN: AddressURN
URN: AccountURN
iconURL: string
title: string
address: string
address?: string
joinTimestamp: number
}

Expand All @@ -29,16 +29,15 @@ type GroupRootLoaderData = {
groups: GroupModel[]
CONSOLE_URL: string
PASSPORT_URL: string
ownAddressURNList: AddressURN[]
}

export type GroupRootContextData = GroupRootLoaderData
export type GroupRootContextData = GroupRootLoaderData & {
accountURN: AccountURN
}

export const loader: LoaderFunction = getRollupReqFunctionErrorWrapper(
async ({ request, context }) => {
const jwt = await requireJWT(request, context.env)
const parsedJwt = parseJwt(jwt!)
const accountURN = parsedJwt.sub as AccountURN

const traceHeader = generateTraceContextHeaders(context.traceSpan)

Expand All @@ -48,34 +47,37 @@ export const loader: LoaderFunction = getRollupReqFunctionErrorWrapper(
...traceHeader,
})

const groups = await coreClient.account.listIdentityGroups.query({
accountURN,
})
const groups = await coreClient.account.listIdentityGroups.query()

const mappedGroups = await Promise.all(
groups.map(async (group) => {
const memberMap = group.members
.filter((m) => m.joinTimestamp != null)
.reduce(
(acc, curr) => ({ ...acc, [curr.URN]: curr }),
{} as Record<AddressURN, { URN: AddressURN; joinTimestamp: number }>
)

const memberProfiles =
await coreClient.address.getAddressProfileBatch.query(
group.members.map((m) => m.URN)
{} as Record<AccountURN, { URN: AccountURN; joinTimestamp: number }>
)

const memberModels: GroupMemberModel[] = memberProfiles.map(
(profile) => ({
URN: profile.id,
iconURL: profile.icon!,
title: profile.title,
address: profile.address,
joinTimestamp: memberMap[profile.id].joinTimestamp,
})
const memberProfiles = await coreClient.account.getProfileBatch.query(
group.members.map((m) => m.URN)
)

const memberModels: GroupMemberModel[] = memberProfiles
.filter((mp) => Boolean(mp.profile))
.map(({ profile, URN }) => ({
URN: URN,
iconURL: profile!.pfp!.image,
title: profile!.displayName,
address: profile!.primaryAddressURN
? profile!.addresses.find(
(a) =>
a.baseUrn ===
AddressURNSpace.getBaseURN(profile!.primaryAddressURN!)
)?.qc.alias
: undefined,
joinTimestamp: memberMap[URN].joinTimestamp,
}))

return {
URN: group.URN,
name: group.name,
Expand All @@ -84,28 +86,29 @@ export const loader: LoaderFunction = getRollupReqFunctionErrorWrapper(
})
)

const ownAddresses = await coreClient.account.getOwnAddresses.query({
account: accountURN,
})
const ownAddressURNList =
ownAddresses?.map((a) => a.baseUrn as AddressURN) ?? []

return json<GroupRootLoaderData>({
groups: mappedGroups,
CONSOLE_URL: context.env.CONSOLE_URL,
PASSPORT_URL: context.env.PASSPORT_URL,
ownAddressURNList,
})
}
)

export default () => {
const data = useLoaderData<GroupRootLoaderData>()
const { accountURN } = useOutletContext<{
accountURN: AccountURN
}>()

return (
<>
<Toaster position="top-right" />
<Outlet context={data} />
<Outlet
context={{
accountURN,
...data,
}}
/>
</>
)
}
4 changes: 2 additions & 2 deletions apps/console/app/routes/__layout/spuorg/$groupID/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ const InviteMemberModal = ({
}

export default () => {
const { groups, PASSPORT_URL, ownAddressURNList } =
const { groups, PASSPORT_URL, accountURN } =
useOutletContext<GroupRootContextData>()
const { URN, groupID, invitations } = useLoaderData<LoaderData>()

Expand Down Expand Up @@ -539,7 +539,7 @@ export default () => {
{item.val.title}
</Text>

{ownAddressURNList.includes(item.key) && (
{accountURN === item.key && (
<Pill className="bg-indigo-50 rounded-lg !pr-2">
<Text
size="xs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export const action: ActionFunction = getRollupReqFunctionErrorWrapper(

const { inviteCode } =
await coreClient.account.inviteIdentityGroupMember.mutate({
inviterAccountURN: accountURN,
identifier,
addressType: addressType,
identityGroupURN: groupURN,
Expand Down
7 changes: 1 addition & 6 deletions apps/console/app/routes/__layout/spuorg/create.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { generateTraceContextHeaders } from '@proofzero/platform-middleware/trace'
import { AccountURN } from '@proofzero/urns/account'
import { getRollupReqFunctionErrorWrapper } from '@proofzero/utils/errors'
import { ActionFunction, redirect } from '@remix-run/cloudflare'
import { parseJwt, requireJWT } from '~/utilities/session.server'
import { requireJWT } from '~/utilities/session.server'
import createCoreClient from '@proofzero/platform-clients/core'
import { getAuthzHeaderConditionallyFromToken } from '@proofzero/utils'
import { BadRequestError } from '@proofzero/errors'

export const action: ActionFunction = getRollupReqFunctionErrorWrapper(
async ({ request, context }) => {
const jwt = await requireJWT(request, context.env)
const parsedJwt = parseJwt(jwt!)
const accountURN = parsedJwt.sub as AccountURN

const traceHeader = generateTraceContextHeaders(context.traceSpan)

const coreClient = createCoreClient(context.env.Core, {
Expand All @@ -29,7 +25,6 @@ export const action: ActionFunction = getRollupReqFunctionErrorWrapper(
}

await coreClient.account.createIdentityGroup.mutate({
accountURN,
name: name as string,
})

Expand Down
44 changes: 38 additions & 6 deletions platform/account/src/jsonrpc/methods/getProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { appRouter } from '../router'
import { ProfileSchema } from '../validators/profile'
import { Node } from '../../../../edges/src/jsonrpc/validators/node'
import { AddressURN } from '@proofzero/urns/address'
import { AccountURN } from '@proofzero/urns/account'
import { AccountURNInput } from '@proofzero/platform-middleware/inputValidators'

export const GetProfileInput = z.object({
account: inputValidators.AccountURNInput,
Expand All @@ -31,15 +33,11 @@ export const getProfileMethod = async ({
const node = await initAccountNodeByName(input.account, ctx.Account)
const caller = appRouter.createCaller(ctx)

const getAddressesCall =
ctx.accountURN === input.account
? caller.getOwnAddresses
: caller.getPublicAddresses

const [profile, addresses] = await Promise.all([
node.class.getProfile(),
getAddressesCall({ account: input.account }),
caller.getPublicAddresses({ account: input.account }),
])

if (!profile) return null
if (!profile.primaryAddressURN) {
const caller = appRouter.createCaller(ctx)
Expand All @@ -55,3 +53,37 @@ export const getProfileMethod = async ({

return { ...profile, addresses }
}

export const GetProfileBatchInput = z.array(inputValidators.AccountURNInput)
export const GetProfileBatchOutput = z.array(
z.object({
profile: ProfileSchema.merge(
z.object({
addresses: z.array(Node),
})
).nullable(),
URN: AccountURNInput,
})
)

export type GetProfileBatchOutputParams = z.infer<typeof GetProfileBatchOutput>
export type GetProfileBatchParams = z.infer<typeof GetProfileBatchInput>

export const getProfileBatchMethod = async ({
input,
ctx,
}: {
input: GetProfileBatchParams
ctx: Context
}): Promise<GetProfileBatchOutputParams> =>
Promise.all(
input.map(async (accountURN) => ({
profile: await getProfileMethod({
input: {
account: accountURN,
},
ctx,
}),
URN: accountURN as AccountURN,
}))
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { initIdentityGroupNodeByName } from '../../../nodes'
import { InternalServerError } from '@proofzero/errors'
import { router } from '@proofzero/platform.core'
import { EDGE_MEMBER_OF_IDENTITY_GROUP } from '@proofzero/types/graph'
import { AccountURN } from '@proofzero/urns/account'

export const AcceptIdentityGroupMemberInvitationInputSchema = z.object({
identityGroupURN: IdentityGroupURNValidator,
Expand Down Expand Up @@ -72,7 +73,7 @@ export const acceptIdentityGroupMemberInvitation = async ({
})

await caller.edges.makeEdge({
src: targetAddress.baseUrn,
src: ctx.accountURN as AccountURN,
tag: EDGE_MEMBER_OF_IDENTITY_GROUP,
dst: identityGroupURN,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { z } from 'zod'
import { router } from '@proofzero/platform.core'
import { AccountURNInput } from '@proofzero/platform-middleware/inputValidators'
import { hexlify } from '@ethersproject/bytes'
import { randomBytes } from '@ethersproject/random'
import { IDENTITY_GROUP_OPTIONS } from '../../../constants'
import { IdentityGroupURNSpace } from '@proofzero/urns/identity-group'
import { EDGE_MEMBER_OF_IDENTITY_GROUP } from '@proofzero/types/graph'
import { RollupError } from '@proofzero/errors'
import { AddressURN, AddressURNSpace } from '@proofzero/urns/address'

import { Context } from '../../../context'
import { AccountURN } from '@proofzero/urns/account'

export const CreateIdentityGroupInputSchema = z.object({
accountURN: AccountURNInput,
name: z.string(),
})

Expand All @@ -33,24 +30,12 @@ export const createIdentityGroup = async ({

const caller = router.createCaller(ctx)

const accountNode = await caller.edges.findNode({
baseUrn: input.accountURN,
})
if (!accountNode) {
throw new RollupError({
message: 'Account node not found',
})
}

await caller.edges.updateNode({
urnOfNode: groupURN,
})

const primaryAddressURN = AddressURNSpace.getBaseURN(
accountNode.qc.primaryAddressURN as AddressURN
)
await caller.edges.makeEdge({
src: primaryAddressURN,
src: ctx.accountURN as AccountURN,
tag: EDGE_MEMBER_OF_IDENTITY_GROUP,
dst: baseGroupURN,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,15 @@ import {
EmailAddressType,
OAuthAddressType,
} from '@proofzero/types/address'
import {
AccountURNInput,
IdentityGroupURNValidator,
} from '@proofzero/platform-middleware/inputValidators'
import { IdentityGroupURNValidator } from '@proofzero/platform-middleware/inputValidators'
import { initIdentityGroupNodeByName } from '../../../nodes'
import { hexlify } from '@ethersproject/bytes'
import { randomBytes } from '@ethersproject/random'
import { IDENTITY_GROUP_OPTIONS } from '../../../constants'
import { router } from '@proofzero/platform.core'
import { AddressURN, AddressURNSpace } from '@proofzero/urns/address'
import generateRandomString from '@proofzero/utils/generateRandomString'
import { AccountURN } from '@proofzero/urns/account'

export const InviteIdentityGroupMemberInputSchema = z.object({
inviterAccountURN: AccountURNInput,
identityGroupURN: IdentityGroupURNValidator,
identifier: z.string(),
addressType: z.union([
Expand Down Expand Up @@ -48,7 +43,8 @@ export const inviteIdentityGroupMember = async ({
input: InviteIdentityGroupMemberInput
ctx: Context
}): Promise<InviteIdentityGroupMemberOutput> => {
const { inviterAccountURN, identityGroupURN, identifier, addressType } = input
const { identityGroupURN, identifier, addressType } = input
const inviterAccountURN = ctx.accountURN as AccountURN

const node = await initIdentityGroupNodeByName(
identityGroupURN,
Expand Down
Loading

0 comments on commit 5573296

Please sign in to comment.