Skip to content

πŸ“¨ The ultimate agentic social media scheduling tool πŸ€–

fix(account): cancel the subscription and remove team members on account deletion

Gilad Resisi41f23e9d523a
diff --git a/apps/backend/src/api/routes/users.controller.ts b/apps/backend/src/api/routes/users.controller.ts
index 3e51f0973..8571c02f5 100644
--- a/apps/backend/src/api/routes/users.controller.ts
+++ b/apps/backend/src/api/routes/users.controller.ts
@@ -332,6 +332,11 @@ export class UsersController {
response.status(200).send();
}
+ @Get('/delete-account-info')
+ getDeleteAccountInfo(@GetUserFromRequest() user: User) {
+ return this._userService.getDeleteAccountInfo(user.id);
+ }
+
@Post('/delete-account')
async deleteAccount(
@GetUserFromRequest() user: User,
diff --git a/apps/frontend/src/components/settings/delete-account.component.tsx b/apps/frontend/src/components/settings/delete-account.component.tsx
index 8d6bf939c..2d518e064 100644
--- a/apps/frontend/src/components/settings/delete-account.component.tsx
+++ b/apps/frontend/src/components/settings/delete-account.component.tsx
@@ -1,6 +1,7 @@
'use client';
import React, { FC, useCallback, useState } from 'react';
+import useSWR from 'swr';
import { useFetch } from '@gitroom/helpers/utils/custom.fetch';
import { deleteDialog } from '@gitroom/react/helpers/delete.dialog';
import { Button } from '@gitroom/react/form/button';
@@ -10,20 +11,62 @@ import { useToaster } from '@gitroom/react/toaster/toaster';
import { useT } from '@gitroom/react/translation/get.transation.service.client';
import { TrashIcon } from '@gitroom/frontend/components/ui/icons';
+interface DeleteAccountInfo {
+ hasSubscription: boolean;
+ hasAdditionalMembers: boolean;
+}
+
+export const useDeleteAccountInfo = () => {
+ const fetch = useFetch();
+
+ const load = useCallback(async () => {
+ return (await fetch('/user/delete-account-info')).json();
+ }, []);
+
+ return useSWR<DeleteAccountInfo>('delete-account-info', load, {
+ revalidateOnFocus: false,
+ revalidateOnReconnect: false,
+ revalidateIfStale: false,
+ revalidateOnMount: true,
+ refreshWhenHidden: false,
+ refreshWhenOffline: false,
+ });
+};
+
const DeleteAccountComponent: FC<{ isLink?: boolean }> = ({ isLink }) => {
const t = useT();
const fetch = useFetch();
const toaster = useToaster();
const { isSecured } = useVariables();
const [loading, setLoading] = useState(false);
+ const { mutate } = useDeleteAccountInfo();
const deleteAccount = useCallback(async () => {
+ const info = await mutate();
+ const message =
+ info?.hasSubscription && info?.hasAdditionalMembers
+ ? t(
+ 'delete_account_confirm_subscription_members',
+ 'Your account, organizations, channels and posts will be deleted, your subscription will be cancelled and the additional team members will be removed from your organization. This action cannot be undone, are you sure?'
+ )
+ : info?.hasSubscription
+ ? t(
+ 'delete_account_confirm_subscription',
+ 'Your account, organizations, channels and posts will be deleted and your subscription will be cancelled. This action cannot be undone, are you sure?'
+ )
+ : info?.hasAdditionalMembers
+ ? t(
+ 'delete_account_confirm_members',
+ 'Your account, organizations, channels and posts will be deleted and the additional team members will be removed from your organization. This action cannot be undone, are you sure?'
+ )
+ : t(
+ 'delete_account_confirm',
+ 'Your account, organizations, channels and posts will be deleted. This action cannot be undone, are you sure?'
+ );
+
if (
!(await deleteDialog(
- t(
- 'delete_account_confirm',
- 'Your account, organizations, channels and posts will be deleted. This action cannot be undone, are you sure?'
- ),
+ message,
t('yes_delete_my_account', 'Yes, delete my account')
))
) {
diff --git a/libraries/nestjs-libraries/src/database/prisma/organizations/organization.repository.ts b/libraries/nestjs-libraries/src/database/prisma/organizations/organization.repository.ts
index a27f25713..f70f71c9d 100644
--- a/libraries/nestjs-libraries/src/database/prisma/organizations/organization.repository.ts
+++ b/libraries/nestjs-libraries/src/database/prisma/organizations/organization.repository.ts
@@ -563,7 +563,6 @@ export class OrganizationRepository {
users: {
select: {
role: true,
- disabled: true,
user: {
select: {
email: true,
diff --git a/libraries/nestjs-libraries/src/database/prisma/users/users.service.ts b/libraries/nestjs-libraries/src/database/prisma/users/users.service.ts
index c11276d82..b162eeb83 100644
--- a/libraries/nestjs-libraries/src/database/prisma/users/users.service.ts
+++ b/libraries/nestjs-libraries/src/database/prisma/users/users.service.ts
@@ -1,4 +1,4 @@
-import { HttpException, Injectable, Logger } from '@nestjs/common';
+import { Injectable, Logger } from '@nestjs/common';
import { UsersRepository } from '@gitroom/nestjs-libraries/database/prisma/users/users.repository';
import { Provider, Role } from '@prisma/client';
import { UserDetailDto } from '@gitroom/nestjs-libraries/dtos/users/user.details.dto';
@@ -90,21 +90,31 @@ export class UsersService {
(org) => org.users[0].role === Role.SUPERADMIN
);
- for (const org of ownedOrgs) {
- const team = await this._organizationRepository.getTeam(org.id);
- if (
- team?.users?.some(
- (member) => member.user.id !== userId && !member.disabled
+ const teams = await Promise.all(
+ ownedOrgs.map((org) => this._organizationRepository.getTeam(org.id))
+ );
+
+ return ownedOrgs.filter(
+ (org, index) =>
+ !teams[index]?.users?.some(
+ (member) =>
+ member.user.id !== userId && member.role === Role.SUPERADMIN
)
- ) {
- throw new HttpException(
- 'Please remove your team members before deleting your account',
- 400
- );
- }
- }
+ );
+ }
+
+ async getDeleteAccountInfo(userId: string) {
+ const ownedOrgs = await this.getOrgsToDeleteForAccount(userId);
+ const teams = await Promise.all(
+ ownedOrgs.map((org) => this._organizationRepository.getTeam(org.id))
+ );
- return ownedOrgs;
+ return {
+ hasSubscription: ownedOrgs.some((org) => !!org.subscription),
+ hasAdditionalMembers: teams.some((team) =>
+ team?.users?.some((member) => member.user.id !== userId)
+ ),
+ };
}
async deleteAccount(userId: string) {
@@ -112,7 +122,17 @@ export class UsersService {
const orgs = await this._organizationRepository.getOrgsByUserId(userId);
for (const org of orgs) {
- if (org.users[0].role === Role.SUPERADMIN) {
+ if (deletedOrgs.some((deletedOrg) => deletedOrg.id === org.id)) {
+ const team = await this._organizationRepository.getTeam(org.id);
+ for (const member of team?.users || []) {
+ if (member.user.id !== userId) {
+ await this._organizationRepository.deleteTeamMember(
+ org.id,
+ member.user.id
+ );
+ }
+ }
+
await this._integrationRepository.deleteIntegrationsForAccount(org.id);
await this._organizationRepository.deleteOrganization(org.id);
} else {