diff --git a/src/pages/groups/[groupId].tsx b/src/pages/groups/[groupId].tsx index d68f023..8a04219 100644 --- a/src/pages/groups/[groupId].tsx +++ b/src/pages/groups/[groupId].tsx @@ -6,7 +6,7 @@ import { Button } from '~/components/ui/button'; import { SplitType, type User } from '@prisma/client'; import { api } from '~/utils/api'; import { useRouter } from 'next/router'; -import { Check, ChevronLeft, Share, UserPlus } from 'lucide-react'; +import { Check, ChevronLeft, DoorOpen, LogOut, Share, Trash2, UserPlus } from 'lucide-react'; import { InformationCircleIcon } from '@heroicons/react/24/outline'; import { AppDrawer } from '~/components/ui/drawer'; import { UserAvatar } from '~/components/ui/avatar'; @@ -157,14 +157,6 @@ const BalancePage: NextPageWithUser = ({ user }) => {
{isAdmin ? ( <> - - @@ -175,30 +167,35 @@ const BalancePage: NextPageWithUser = ({ user }) => { - Are you absolutely sure? + + {canDelete ? 'Are you absolutely sure?' : ''} + - This action cannot be reversed + {canDelete + ? 'This action cannot be reversed' + : "Can't delete the group until everyone settles up the balance"} Cancel - + {canDelete ? ( + + ) : null} @@ -215,30 +212,35 @@ const BalancePage: NextPageWithUser = ({ user }) => { - Are you absolutely sure? + + {canLeave ? 'Are you absolutely sure?' : ''} + - This action cannot be reversed + {canLeave + ? 'This action cannot be reversed' + : "Can't leave the group until your outstanding balance is settled"} Cancel - + {canLeave ? ( + + ) : null} diff --git a/src/server/api/routers/group.ts b/src/server/api/routers/group.ts index 28b6a64..43baabe 100644 --- a/src/server/api/routers/group.ts +++ b/src/server/api/routers/group.ts @@ -269,12 +269,24 @@ export const groupRouter = createTRPCRouter({ where: { id: input.groupId, }, + include: { + groupBalances: true, + }, }); if (group?.userId !== ctx.session.user.id) { throw new TRPCError({ code: 'UNAUTHORIZED', message: 'Only creator can delete the group' }); } + const balanceWithNonZero = group?.groupBalances.find((b) => b.amount !== 0); + + if (balanceWithNonZero) { + throw new TRPCError({ + code: 'BAD_REQUEST', + message: 'You have a non-zero balance in this group', + }); + } + await ctx.db.group.delete({ where: { id: input.groupId,