Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix: an ability to unassign project from organisation

Blue Mouse53777b5020b0
diff --git a/backend/apps/cloud/src/organisation/organisation.service.ts b/backend/apps/cloud/src/organisation/organisation.service.ts
index 5f7b7f5bc..d35100acc 100644
--- a/backend/apps/cloud/src/organisation/organisation.service.ts
+++ b/backend/apps/cloud/src/organisation/organisation.service.ts
@@ -71,7 +71,7 @@ export class OrganisationService {
await this.membershipRepository.delete(id)
}
- async validateManageAccess(organisation: Organisation, userId: string) {
+ validateManageAccess(organisation: Organisation, userId: string) {
const membership = _find(
organisation.members,
member => member.user?.id === userId,
diff --git a/backend/apps/cloud/src/project/dto/update-project.dto.ts b/backend/apps/cloud/src/project/dto/update-project.dto.ts
index 5a39ff1b7..1489a5880 100644
--- a/backend/apps/cloud/src/project/dto/update-project.dto.ts
+++ b/backend/apps/cloud/src/project/dto/update-project.dto.ts
@@ -3,12 +3,10 @@ import { IntersectionType, PartialType } from '@nestjs/mapped-types'
import { ApiProperty } from '@nestjs/swagger'
import { ProjectDTO } from './project.dto'
import { ProjectPasswordDto } from './project-password.dto'
-import { ProjectOrganisationDto } from './project-organisation.dto'
export class UpdateProjectDto extends IntersectionType(
PartialType(ProjectDTO),
ProjectPasswordDto,
- ProjectOrganisationDto,
) {
@ApiProperty({
required: false,
diff --git a/backend/apps/cloud/src/project/project.controller.ts b/backend/apps/cloud/src/project/project.controller.ts
index 9eb4fc8e2..8345f2212 100644
--- a/backend/apps/cloud/src/project/project.controller.ts
+++ b/backend/apps/cloud/src/project/project.controller.ts
@@ -113,6 +113,7 @@ import { BulkAddUsersDto } from './dto/bulk-add-users.dto'
import { BulkAddUsersResponse } from './interfaces/bulk-add-users'
import { OrganisationService } from '../organisation/organisation.service'
import { Organisation } from '../organisation/entity/organisation.entity'
+import { ProjectOrganisationDto } from './dto/project-organisation.dto'
const PROJECTS_MAXIMUM = 50
@@ -1763,6 +1764,47 @@ export class ProjectController {
res.send(html)
}
+ @ApiBearerAuth()
+ @Patch('/:id/organisation')
+ @HttpCode(200)
+ @Auth([], true)
+ async updateOrganisation(
+ @Param('id') id: string,
+ @Body() body: ProjectOrganisationDto,
+ @CurrentUserId() uid: string,
+ ) {
+ this.logger.log({ body }, 'PATCH /project/:id/organisation')
+
+ if (!uid) {
+ throw new UnauthorizedException('Please auth first')
+ }
+
+ const project = await this.projectService.getFullProject(id)
+
+ if (_isEmpty(project)) {
+ throw new NotFoundException()
+ }
+
+ this.projectService.allowedToManage(project, uid)
+
+ if (body.organisationId) {
+ const canManage = await this.organisationService.canManageOrganisation(
+ body.organisationId,
+ uid,
+ )
+
+ if (!canManage) {
+ throw new ForbiddenException(
+ 'You do not have permission to manage this organisation',
+ )
+ }
+ }
+
+ return this.projectService.update({ id }, {
+ organisation: { id: body.organisationId || null },
+ } as Project)
+ }
+
@ApiBearerAuth()
@Put('/:id')
@HttpCode(200)
@@ -1824,12 +1866,6 @@ export class ProjectController {
project.name = _trim(projectDTO.name)
}
- if (projectDTO.organisationId) {
- project.organisation = {
- id: projectDTO.organisationId,
- } as Organisation
- }
-
if (_isBoolean(projectDTO.isPasswordProtected)) {
if (projectDTO.isPasswordProtected) {
if (projectDTO.password) {
diff --git a/web/app/api/index.ts b/web/app/api/index.ts
index e9b8f94cd..6459f0e0d 100644
--- a/web/app/api/index.ts
+++ b/web/app/api/index.ts
@@ -298,6 +298,14 @@ export const deleteOrganisation = (id: string) =>
throw _isEmpty(error.response.data?.message) ? error.response.data : error.response.data.message
})
+export const assignProjectToOrganisation = (projectId: string, organisationId?: string) =>
+ api
+ .patch(`/project/${projectId}/organisation`, { organisationId })
+ .then((response) => response.data)
+ .catch((error) => {
+ throw _isEmpty(error.response.data?.message) ? error.response.data : error.response.data.message
+ })
+
export const createOrganisation = (name: string) =>
api
.post('/organisation', { name })
diff --git a/web/app/pages/Organisations/Settings/Projects.tsx b/web/app/pages/Organisations/Settings/Projects.tsx
index b99bac475..e3d6b1cca 100644
--- a/web/app/pages/Organisations/Settings/Projects.tsx
+++ b/web/app/pages/Organisations/Settings/Projects.tsx
@@ -113,6 +113,7 @@ const SelectAProject = ({ onSelect }: SelectAProjectProps) => {
pageAmount={pageAmount}
setPage={setCurrentPage}
total={total}
+ pageSize={PAGINATION_ENTRIES}
/>
) : null}
</div>
diff --git a/web/app/pages/Project/Settings/ProjectSettings.tsx b/web/app/pages/Project/Settings/ProjectSettings.tsx
index 0469d5120..d476c0ecd 100644
--- a/web/app/pages/Project/Settings/ProjectSettings.tsx
+++ b/web/app/pages/Project/Settings/ProjectSettings.tsx
@@ -29,6 +29,7 @@ import {
getFilters,
resetFilters,
getProject,
+ assignProjectToOrganisation,
} from 'api'
import Input from 'ui/Input'
import Button from 'ui/Button'
@@ -325,6 +326,15 @@ const ProjectSettings = () => {
[user.organisationMemberships, t],
)
+ const assignOrganisation = async (organisationId?: string) => {
+ try {
+ await assignProjectToOrganisation(id, organisationId)
+ toast.success(t('apiNotifications.projectAssigned'))
+ } catch (reason: any) {
+ toast.error(typeof reason === 'string' ? reason : t('apiNotifications.projectAssignError'))
+ }
+ }
+
const sharableLink = useMemo(() => {
const origin = requestOrigin || isBrowser ? window.location.origin : 'https://swetrix.com'
@@ -740,7 +750,8 @@ const ProjectSettings = () => {
return item.name
}}
- onSelect={(item) => {
+ onSelect={async (item) => {
+ await assignOrganisation(item.id)
setForm((oldForm) => ({
...oldForm,
organisationId: item.id,
diff --git a/web/app/ui/Pagination.tsx b/web/app/ui/Pagination.tsx
index 454626d1d..b9b8106a7 100644
--- a/web/app/ui/Pagination.tsx
+++ b/web/app/ui/Pagination.tsx
@@ -11,17 +11,12 @@ interface IPagination {
pageAmount: number
total: number
className?: string
+ pageSize?: number
}
-const Pagination = ({ page, setPage, pageAmount, total, className }: IPagination): JSX.Element => {
+const Pagination = ({ page, setPage, pageAmount, total, className, pageSize }: IPagination): JSX.Element => {
const { t } = useTranslation('common')
- const paginationRange = usePagination(total, page)
-
- console.log('total:', total)
- console.log('page:', page)
- console.log('pageAmount:', pageAmount)
-
- console.log('paginationRange:', paginationRange)
+ const paginationRange = usePagination(total, page, 1, pageSize)
return (
<nav className={cx('flex items-center justify-between border-t-0 border-gray-200 px-4 sm:px-0', className)}>
diff --git a/web/public/locales/en.json b/web/public/locales/en.json
index 18f000297..5be9388d3 100644
--- a/web/public/locales/en.json
+++ b/web/public/locales/en.json
@@ -59,7 +59,9 @@
"projectAddError": "Error occurred while adding the project to the organisation",
"orgUserRemoved": "The user has been removed from the organisation",
"orgUserRemoveError": "Error occurred while removing the user from the organisation",
- "orgSettingsUpdated": "Organisation settings have been updated"
+ "orgSettingsUpdated": "Organisation settings have been updated",
+ "projectAssigned": "The project has been assigned to the organisation",
+ "projectAssignError": "Failed to assign the project to the organisation"
},
"common": {
"features": "Features",