From c51b46e3cd6beeece07ed2f4dce1aa23811f1fdf Mon Sep 17 00:00:00 2001 From: elraphty Date: Wed, 15 Nov 2023 20:31:00 +0100 Subject: [PATCH] added delete org by admin functionality --- db/db.go | 12 ++++- db/structs.go | 1 + .../widgetViews/OrganizationDetails.tsx | 13 ++++-- .../people/widgetViews/OrganizationView.tsx | 1 + .../widgetViews/organization/EditOrgModal.tsx | 7 ++- .../widgetViews/organization/RolesModal.tsx | 4 +- .../widgetViews/organization/interface.ts | 1 + frontend/app/src/store/main.ts | 20 ++++++++ handlers/organizations.go | 46 +++++++++++++++---- routes/organizations.go | 1 + 10 files changed, 89 insertions(+), 17 deletions(-) diff --git a/db/db.go b/db/db.go index 56afae34f..2fe75033f 100644 --- a/db/db.go +++ b/db/db.go @@ -1005,7 +1005,7 @@ func (db database) GetOrganizations(r *http.Request) []Organization { offset, limit, sortBy, direction, search := utils.GetPaginationParams(r) // return if like owner_alias, unique_name, or equals pubkey - db.db.Offset(offset).Limit(limit).Order(sortBy+" "+direction+" ").Where("LOWER(name) LIKE ?", "%"+search+"%").Find(&ms) + db.db.Offset(offset).Limit(limit).Order(sortBy+" "+direction+" ").Where("LOWER(name) LIKE ?", "%"+search+"%").Where("deleted != ?", false).Find(&ms) return ms } @@ -1105,7 +1105,7 @@ func (db database) GetUserRoles(uuid string, pubkey string) []UserRoles { func (db database) GetUserCreatedOrganizations(pubkey string) []Organization { ms := []Organization{} - db.db.Where("owner_pub_key = ?", pubkey).Find(&ms) + db.db.Where("owner_pub_key = ?", pubkey).Where("deleted != ?", true).Find(&ms) return ms } @@ -1300,3 +1300,11 @@ func (db database) DeleteUserInvoiceData(payment_request string) UserInvoiceData db.db.Where("payment_request = ?", payment_request).Delete(&ms) return ms } + +func (db database) ChangeOrganizationDeleteStatus(org_uuid string, status bool) Organization { + ms := Organization{} + db.db.Model(&ms).Where("uuid", org_uuid).Updates(map[string]interface{}{ + "deleted": status, + }) + return ms +} diff --git a/db/structs.go b/db/structs.go index f292095fb..d50e794aa 100644 --- a/db/structs.go +++ b/db/structs.go @@ -432,6 +432,7 @@ type Organization struct { Created *time.Time `json:"created"` Updated *time.Time `json:"updated"` Show bool `json:"show"` + Deleted bool `gorm:"default:false" json:"deleted"` BountyCount int64 `json:"bounty_count,omitempty"` Budget uint `json:"budget,omitempty"` } diff --git a/frontend/app/src/people/widgetViews/OrganizationDetails.tsx b/frontend/app/src/people/widgetViews/OrganizationDetails.tsx index 4877ec6d9..bff810df1 100644 --- a/frontend/app/src/people/widgetViews/OrganizationDetails.tsx +++ b/frontend/app/src/people/widgetViews/OrganizationDetails.tsx @@ -47,6 +47,7 @@ const OrganizationDetails = (props: { close: () => void; org: Organization | undefined; resetOrg: (Organization) => void; + getOrganizations: () => Promise; }) => { const { main, ui } = useStores(); @@ -79,7 +80,7 @@ const OrganizationDetails = (props: { const addWithdrawDisabled = !isOrganizationAdmin && !userHasRole(main.bountyRoles, userRoles, 'WITHDRAW BUDGET'); - const { org } = props; + const { org, close, getOrganizations } = props; const uuid = org?.uuid || ''; function addToast(title: string, color: 'danger' | 'success') { @@ -237,11 +238,15 @@ const OrganizationDetails = (props: { }; const onDeleteOrg = async () => { - const res = { status: 200 }; + const res = await main.organizationDelete(uuid); if (res.status === 200) { - addToast('Still need to implement this', 'danger'); + addToast('Deleted organization', 'success'); + if (ui.meInfo) { + getOrganizations(); + close(); + } } else { - addToast('Error: could not create organization', 'danger'); + addToast('Error: could not delete organization', 'danger'); } }; diff --git a/frontend/app/src/people/widgetViews/OrganizationView.tsx b/frontend/app/src/people/widgetViews/OrganizationView.tsx index 3e5797389..443e45a59 100644 --- a/frontend/app/src/people/widgetViews/OrganizationView.tsx +++ b/frontend/app/src/people/widgetViews/OrganizationView.tsx @@ -274,6 +274,7 @@ const Organizations = (props: { person: Person }) => { close={closeDetails} org={organization} resetOrg={(newOrg: Organization) => setOrganization(newOrg)} + getOrganizations={getUserOrganizations} /> )} {!detailsOpen && ( diff --git a/frontend/app/src/people/widgetViews/organization/EditOrgModal.tsx b/frontend/app/src/people/widgetViews/organization/EditOrgModal.tsx index 8671dd6a1..b2d9662f0 100644 --- a/frontend/app/src/people/widgetViews/organization/EditOrgModal.tsx +++ b/frontend/app/src/people/widgetViews/organization/EditOrgModal.tsx @@ -7,6 +7,7 @@ import { Formik } from 'formik'; import { validator } from 'components/form/utils'; import { widgetConfigs } from 'people/utils/Constants'; import { FormField } from 'components/form/utils'; +import { useStores } from 'store'; import Input from '../../../components/form/inputs'; import { Button, Modal } from '../../../components/common'; import { colors } from '../../../config/colors'; @@ -138,6 +139,8 @@ const HLine = styled.div` `; const EditOrgModal = (props: EditOrgModalProps) => { + const { ui } = useStores(); + const isMobile = useIsMobile(); const { isOpen, close, onSubmit, onDelete, org } = props; const [showDeleteModal, setShowDeleteModal] = useState(false); @@ -154,6 +157,8 @@ const EditOrgModal = (props: EditOrgModalProps) => { const [selectedImage, setSelectedImage] = useState(org?.img || ''); const fileInputRef = useRef(null); + const isOrganizationAdmin = props.org?.owner_pubkey === ui.meInfo?.owner_pubkey; + const handleFileInputChange = (e: ChangeEvent) => { const file = e.target.files && e.target.files[0]; if (file) { @@ -312,7 +317,7 @@ const EditOrgModal = (props: EditOrgModalProps) => {