-
Notifications
You must be signed in to change notification settings - Fork 989
web: delete org #1338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
web: delete org #1338
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bb3451c
delete org support
ameer2468 00145f9
Update _journal.json
ameer2468 c2edd0b
update api for desktop
ameer2468 aba39b1
cleanup effect
ameer2468 e79cd6c
Update _journal.json
ameer2468 daa99c1
wrap in a transaction
ameer2468 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
apps/web/app/(org)/dashboard/settings/organization/components/DeleteOrg.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { Button, Card, CardDescription, CardHeader, CardTitle } from "@cap/ui"; | ||
| import { useState } from "react"; | ||
| import { useDashboardContext } from "../../../Contexts"; | ||
| import DeleteOrgDialog from "./DeleteOrgDialog"; | ||
|
|
||
| const DeleteOrg = () => { | ||
| const [toggleDeleteDialog, setToggleDeleteDialog] = useState(false); | ||
| const { organizationData, user } = useDashboardContext(); | ||
|
|
||
| return ( | ||
| <> | ||
| <DeleteOrgDialog | ||
| open={toggleDeleteDialog} | ||
| onOpenChange={setToggleDeleteDialog} | ||
| /> | ||
| <Card className="flex flex-wrap gap-6 justify-between items-center w-full"> | ||
| <CardHeader> | ||
| <CardTitle>Delete Organization</CardTitle> | ||
| <CardDescription> | ||
| Delete your organization and all associated data.{" "} | ||
| </CardDescription> | ||
| </CardHeader> | ||
| <Button | ||
| variant="destructive" | ||
| disabled={organizationData?.length === 1} | ||
| size="sm" | ||
| onClick={(e) => { | ||
| e.stopPropagation(); | ||
| e.preventDefault(); | ||
| setToggleDeleteDialog(true); | ||
| }} | ||
| > | ||
| Delete Organization | ||
| </Button> | ||
| </Card> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| export default DeleteOrg; | ||
92 changes: 92 additions & 0 deletions
92
apps/web/app/(org)/dashboard/settings/organization/components/DeleteOrgDialog.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import { | ||
| Button, | ||
| Dialog, | ||
| DialogContent, | ||
| DialogFooter, | ||
| DialogHeader, | ||
| DialogTitle, | ||
| Input, | ||
| } from "@cap/ui"; | ||
| import type { Organisation } from "@cap/web-domain"; | ||
| import { faTrashCan } from "@fortawesome/free-solid-svg-icons"; | ||
| import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; | ||
| import { Effect } from "effect"; | ||
| import { useRouter } from "next/navigation"; | ||
| import { startTransition, useId, useState } from "react"; | ||
| import { toast } from "sonner"; | ||
| import { useEffectMutation, useRpcClient } from "@/lib/EffectRuntime"; | ||
| import { useDashboardContext } from "../../../Contexts"; | ||
|
|
||
| interface DeleteOrgDialogProps { | ||
| open: boolean; | ||
| onOpenChange: (open: boolean) => void; | ||
| } | ||
|
|
||
| const DeleteOrgDialog = ({ open, onOpenChange }: DeleteOrgDialogProps) => { | ||
| const { activeOrganization, organizationData } = useDashboardContext(); | ||
| const [organizationName, setOrganizationName] = useState(""); | ||
| const rpc = useRpcClient(); | ||
| const inputId = useId(); | ||
| const router = useRouter(); | ||
| const deleteOrg = useEffectMutation({ | ||
| mutationFn: Effect.fn(function* () { | ||
| if (!activeOrganization) return; | ||
| yield* rpc.OrganisationDelete({ | ||
| id: activeOrganization.organization.id, | ||
| }); | ||
| }), | ||
| onSuccess: () => { | ||
| toast.success("Organization deleted successfully"); | ||
| onOpenChange(false); | ||
| startTransition(() => { | ||
| router.push("/dashboard/caps"); | ||
| router.refresh(); | ||
| }); | ||
| }, | ||
| onError: (error) => { | ||
| console.error(error); | ||
| toast.error("Failed to delete organization"); | ||
| }, | ||
| }); | ||
|
|
||
| return ( | ||
| <Dialog open={open} onOpenChange={onOpenChange}> | ||
| <DialogContent> | ||
| <DialogHeader | ||
| icon={<FontAwesomeIcon className="size-3.5" icon={faTrashCan} />} | ||
| description="Removing your organization will delete all associated data, including videos, and cannot be undone." | ||
| > | ||
| <DialogTitle>Delete Organization</DialogTitle> | ||
| </DialogHeader> | ||
| <div className="p-5"> | ||
| <Input | ||
| id={inputId} | ||
| value={organizationName} | ||
| onChange={(e) => setOrganizationName(e.target.value)} | ||
| placeholder="Organization name" | ||
| /> | ||
| </div> | ||
| <DialogFooter> | ||
| <Button size="sm" variant="gray" onClick={() => onOpenChange(false)}> | ||
| Cancel | ||
| </Button> | ||
| <Button | ||
| size="sm" | ||
| variant="destructive" | ||
| onClick={() => deleteOrg.mutate()} | ||
| spinner={deleteOrg.isPending} | ||
| disabled={ | ||
| organizationData?.length === 1 || | ||
| organizationName !== activeOrganization?.organization.name || | ||
| deleteOrg.isPending | ||
| } | ||
| > | ||
| {deleteOrg.isPending ? "Deleting..." : "Delete"} | ||
| </Button> | ||
| </DialogFooter> | ||
| </DialogContent> | ||
| </Dialog> | ||
| ); | ||
| }; | ||
|
|
||
| export default DeleteOrgDialog; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,7 @@ | ||
| import * as Db from "@cap/database/schema"; | ||
| import { type ImageUpload, Organisation, Policy } from "@cap/web-domain"; | ||
| import { CurrentUser, Organisation, Policy } from "@cap/web-domain"; | ||
| import * as Dz from "drizzle-orm"; | ||
| import { Array, Effect, Option } from "effect"; | ||
|
|
||
| import { Database } from "../Database"; | ||
| import { ImageUploads } from "../ImageUploads"; | ||
| import { S3Buckets } from "../S3Buckets"; | ||
|
|
@@ -49,7 +48,48 @@ export class Organisations extends Effect.Service<Organisations>()( | |
| } | ||
| }); | ||
|
|
||
| return { update }; | ||
| const deleteOrg = Effect.fn("Organisations.deleteOrg")(function* ( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this would be better named |
||
| id: Organisation.OrganisationId, | ||
| ) { | ||
| const user = yield* CurrentUser; | ||
|
|
||
| yield* Policy.withPolicy(policy.isOwner(id))(Effect.void); | ||
|
|
||
| // Perform tombstone, find other org, and update user in a single transaction | ||
| yield* db.use((db) => | ||
| db.transaction(async (tx) => { | ||
| await tx | ||
| .update(Db.organizations) | ||
| .set({ tombstoneAt: new Date() }) | ||
| .where(Dz.eq(Db.organizations.id, id)); | ||
|
|
||
| // Find another active organization owned by the user | ||
| const [otherOrg] = await tx | ||
| .select({ id: Db.organizations.id }) | ||
| .from(Db.organizations) | ||
| .where( | ||
| Dz.and( | ||
| Dz.ne(Db.organizations.id, id), | ||
| Dz.isNull(Db.organizations.tombstoneAt), | ||
| Dz.eq(Db.organizations.ownerId, user.id), | ||
| ), | ||
| ) | ||
| .orderBy(Dz.asc(Db.organizations.createdAt)) | ||
| .limit(1); | ||
|
|
||
| if (otherOrg) { | ||
| await tx | ||
| .update(Db.users) | ||
| .set({ | ||
| activeOrganizationId: otherOrg.id, | ||
| defaultOrgId: otherOrg.id, | ||
| }) | ||
| .where(Dz.eq(Db.users.id, user.id)); | ||
| } | ||
| }), | ||
| ); | ||
| }); | ||
| return { update, deleteOrg }; | ||
| }), | ||
| dependencies: [ | ||
| ImageUploads.Default, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.