From 44a0a77615b415f73c450367288d97d2ca936dc7 Mon Sep 17 00:00:00 2001 From: Nevo David Date: Sun, 10 Mar 2024 21:18:17 +0700 Subject: [PATCH] feat: prevent posts for disabled accounts --- .../src/api/routes/users.controller.ts | 2 +- .../src/services/auth/auth.middleware.ts | 2 +- .../billing/no.billing.component.tsx | 27 +++++++++++++++++-- .../organizations/organization.repository.ts | 1 + .../database/prisma/posts/posts.service.ts | 12 ++++++++- 5 files changed, 39 insertions(+), 5 deletions(-) diff --git a/apps/backend/src/api/routes/users.controller.ts b/apps/backend/src/api/routes/users.controller.ts index 04493e6b..37f04a6a 100644 --- a/apps/backend/src/api/routes/users.controller.ts +++ b/apps/backend/src/api/routes/users.controller.ts @@ -95,7 +95,7 @@ export class UsersController { @Get('/organizations') async getOrgs(@GetUserFromRequest() user: User) { - return this._orgService.getOrgsByUserId(user.id); + return (await this._orgService.getOrgsByUserId(user.id)).filter(f => !f.users[0].disabled); } @Post('/change-org') diff --git a/apps/backend/src/services/auth/auth.middleware.ts b/apps/backend/src/services/auth/auth.middleware.ts index 726eb24a..d97f9396 100644 --- a/apps/backend/src/services/auth/auth.middleware.ts +++ b/apps/backend/src/services/auth/auth.middleware.ts @@ -24,7 +24,7 @@ export class AuthMiddleware implements NestMiddleware { } delete user.password; - const organization = await this._organizationService.getOrgsByUserId(user.id); + const organization = (await this._organizationService.getOrgsByUserId(user.id)).filter(f => !f.users[0].disabled); const setOrg = organization.find((org) => org.id === orgHeader) || organization[0]; diff --git a/apps/frontend/src/components/billing/no.billing.component.tsx b/apps/frontend/src/components/billing/no.billing.component.tsx index d31d0451..02cf4974 100644 --- a/apps/frontend/src/components/billing/no.billing.component.tsx +++ b/apps/frontend/src/components/billing/no.billing.component.tsx @@ -222,11 +222,29 @@ export const NoBillingComponent: FC<{ const moveToCheckout = useCallback( (billing: 'STANDARD' | 'PRO' | 'FREE') => async () => { + const messages = []; + const beforeTotalChannels = pricing[billing].channel || initialChannels; + + if (totalChannels < beforeTotalChannels) { + messages.push( + `Some of the channels will be disabled` + ); + } + + if ( + !pricing[billing].team_members && + pricing[subscription?.subscriptionTier!]?.team_members + ) { + messages.push( + `Your team members will be removed from your organization` + ); + } + if (billing === 'FREE') { if ( subscription?.cancelAt || (await deleteDialog( - 'Are you sure you want to cancel your subscription?', + `Are you sure you want to cancel your subscription? ${messages.join(', ')}`, 'Yes, cancel', 'Cancel Subscription' )) @@ -247,6 +265,11 @@ export const NoBillingComponent: FC<{ } return; } + + if (messages.length && !await deleteDialog(messages.join(', '), 'Yes, continue')) { + return ; + } + setLoading(true); const { url, portal } = await ( await fetch('/billing/subscribe', { @@ -299,7 +322,7 @@ export const NoBillingComponent: FC<{ setLoading(false); }, - [monthlyOrYearly, totalChannels] + [monthlyOrYearly, totalChannels, subscription, user] ); return ( 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 f3628e49..fd07844a 100644 --- a/libraries/nestjs-libraries/src/database/prisma/organizations/organization.repository.ts +++ b/libraries/nestjs-libraries/src/database/prisma/organizations/organization.repository.ts @@ -27,6 +27,7 @@ export class OrganizationRepository { userId, }, select: { + disabled: true, role: true, }, }, diff --git a/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts b/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts index d035353a..8cf07362 100644 --- a/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts +++ b/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts @@ -67,6 +67,16 @@ export class PostsService { return; } + if (firstPost.integration?.disabled) { + await this._notificationService.inAppNotification( + firstPost.organizationId, + `We couldn't post to ${firstPost.integration?.providerIdentifier} for ${firstPost?.integration?.name}`, + `We couldn't post to ${firstPost.integration?.providerIdentifier} for ${firstPost?.integration?.name} because it's disabled. Please enable it and try again.`, + true + ); + return; + } + try { if (firstPost.integration?.type === 'article') { await this.postArticle(firstPost.integration!, [ @@ -74,7 +84,7 @@ export class PostsService { ...morePosts, ]); - return ; + return; } await this.postSocial(firstPost.integration!, [firstPost, ...morePosts]);