deleting groups
This commit is contained in:
parent
4afcb84e08
commit
48f55ff164
2 changed files with 50 additions and 0 deletions
|
|
@ -105,6 +105,20 @@
|
|||
|
||||
<Button type="submit" class="w-full">Save Changes</Button>
|
||||
</form>
|
||||
|
||||
{#if (currentEntity as OverviewGroup).members === 1}
|
||||
<form
|
||||
method="POST"
|
||||
action="?/deleteGroup"
|
||||
class="border-t border-red-500/30 pt-4"
|
||||
>
|
||||
<input type="hidden" name="groupId" value={currentEntityId} />
|
||||
|
||||
<Button type="submit" variant="destructive" class="w-full">
|
||||
Delete group
|
||||
</Button>
|
||||
</form>
|
||||
{/if}
|
||||
</Tabs.Content>
|
||||
{/if}
|
||||
<Tabs.Content value="users">
|
||||
|
|
|
|||
|
|
@ -452,6 +452,42 @@ export const actions = {
|
|||
|
||||
return { success: true };
|
||||
},
|
||||
deleteGroup: async ({ request, locals }) => {
|
||||
const data = await request.formData();
|
||||
const groupId = data.get('groupId');
|
||||
|
||||
if (typeof groupId !== 'string') {
|
||||
return fail(400, { error: 'Invalid group ID' });
|
||||
}
|
||||
|
||||
const group = await db.select().from(table.group).where(eq(table.group.id, groupId)).limit(1);
|
||||
|
||||
if (!group.length) {
|
||||
return fail(404, { error: 'Group not found' });
|
||||
}
|
||||
|
||||
const g = group[0];
|
||||
|
||||
if (g.owner !== locals.user!.id) {
|
||||
return fail(403, { error: 'Not allowed' });
|
||||
}
|
||||
|
||||
if ((g.members as string[]).length !== 1 || (g.members as string[])[0] !== locals.user!.id) {
|
||||
return fail(400, { error: 'Group still has members' });
|
||||
}
|
||||
|
||||
await db.transaction(async (tx) => {
|
||||
await tx
|
||||
.update(table.user)
|
||||
.set({
|
||||
groups: locals.user!.groups.map((z) => z.id).filter((id) => id !== groupId)
|
||||
})
|
||||
.where(eq(table.user.id, locals.user!.id));
|
||||
|
||||
await tx.delete(table.group).where(eq(table.group.id, groupId));
|
||||
});
|
||||
_sendToUser(locals.user!.id, { type: 'group', status: 'removed-from-group' });
|
||||
},
|
||||
|
||||
createServer: async ({ request, locals }) => {
|
||||
const data = await request.formData();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue