Skip to content

Commit

Permalink
feat(console|passport): Delete group, delete member and identity of g…
Browse files Browse the repository at this point in the history
…roup member (#2597)
  • Loading branch information
Cosmin-Parvulescu authored Aug 15, 2023
1 parent d0da01e commit ace4ca9
Show file tree
Hide file tree
Showing 12 changed files with 742 additions and 58 deletions.
56 changes: 46 additions & 10 deletions apps/console/app/routes/__layout/spuorg.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import { generateTraceContextHeaders } from '@proofzero/platform-middleware/trac
import { AccountURN } from '@proofzero/urns/account'
import { getRollupReqFunctionErrorWrapper } from '@proofzero/utils/errors'
import { LoaderFunction, json } from '@remix-run/cloudflare'
import { parseJwt, requireJWT } from '~/utilities/session.server'
import { commitFlashSession, requireJWT } from '~/utilities/session.server'
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, AddressURNSpace } from '@proofzero/urns/address'
import { AddressURNSpace } from '@proofzero/urns/address'
import { Outlet, useLoaderData, useOutletContext } from '@remix-run/react'
import { Toaster } from '@proofzero/design-system/src/atoms/toast'
import { Toaster, toast } from '@proofzero/design-system/src/atoms/toast'
import { ToastModel, getToastsAndFlashSession } from '~/utils/toast.server'
import { useEffect } from 'react'

type GroupMemberModel = {
URN: AccountURN
Expand All @@ -19,7 +21,7 @@ type GroupMemberModel = {
joinTimestamp: number
}

type GroupModel = {
export type GroupModel = {
URN: string
name: string
members: GroupMemberModel[]
Expand Down Expand Up @@ -86,20 +88,54 @@ export const loader: LoaderFunction = getRollupReqFunctionErrorWrapper(
})
)

return json<GroupRootLoaderData>({
groups: mappedGroups,
CONSOLE_URL: context.env.CONSOLE_URL,
PASSPORT_URL: context.env.PASSPORT_URL,
})
const { flashSession, toasts } = await getToastsAndFlashSession(
request,
context.env
)

return json<
GroupRootLoaderData & {
toasts: ToastModel[]
}
>(
{
groups: mappedGroups,
CONSOLE_URL: context.env.CONSOLE_URL,
PASSPORT_URL: context.env.PASSPORT_URL,
toasts,
},
{
headers: {
'Set-Cookie': await commitFlashSession(flashSession, context.env),
},
}
)
}
)

export default () => {
const data = useLoaderData<GroupRootLoaderData>()
const data = useLoaderData<
GroupRootLoaderData & {
toasts: ToastModel[]
}
>()

const { accountURN } = useOutletContext<{
accountURN: AccountURN
}>()

useEffect(() => {
const toasts = data.toasts

if (!toasts || !toasts.length) return

for (const { type, message } of toasts) {
toast(type, {
message: message,
})
}
}, [data.toasts])

return (
<>
<Toaster position="top-right" />
Expand Down
59 changes: 59 additions & 0 deletions apps/console/app/routes/__layout/spuorg/$groupID/delete.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { generateTraceContextHeaders } from '@proofzero/platform-middleware/trace'
import { getRollupReqFunctionErrorWrapper } from '@proofzero/utils/errors'
import { ActionFunction, redirect } from '@remix-run/cloudflare'
import { commitFlashSession, requireJWT } from '~/utilities/session.server'
import createCoreClient from '@proofzero/platform-clients/core'
import { getAuthzHeaderConditionallyFromToken } from '@proofzero/utils'
import {
IdentityGroupURN,
IdentityGroupURNSpace,
} from '@proofzero/urns/identity-group'
import { appendToastToFlashSession } from '~/utils/toast.server'
import { ToastType } from '@proofzero/design-system/src/atoms/toast'

export const action: ActionFunction = getRollupReqFunctionErrorWrapper(
async ({ request, context, params }) => {
const groupID = params.groupID as string
const groupURN = IdentityGroupURNSpace.urn(
groupID as string
) as IdentityGroupURN

const jwt = await requireJWT(request, context.env)
const traceHeader = generateTraceContextHeaders(context.traceSpan)

const coreClient = createCoreClient(context.env.Core, {
...getAuthzHeaderConditionallyFromToken(jwt),
...traceHeader,
})

let toastSession

try {
await coreClient.account.deleteIdentityGroup.mutate(groupURN)

toastSession = await appendToastToFlashSession(
request,
{
message: `Succesfully deleted group`,
type: ToastType.Success,
},
context.env
)
} catch (e) {
toastSession = await appendToastToFlashSession(
request,
{
message: `There was an error deleting the group`,
type: ToastType.Error,
},
context.env
)
}

return redirect('/spuorg', {
headers: {
'Set-Cookie': await commitFlashSession(toastSession, context.env),
},
})
}
)
Loading

0 comments on commit ace4ca9

Please sign in to comment.