From 02ffca06c4928007662d3f29cf67cfbbec042fdd Mon Sep 17 00:00:00 2001 From: Nevo David Date: Fri, 12 Jul 2024 11:44:13 +0700 Subject: [PATCH] feat: delete channel --- .../src/api/routes/integrations.controller.ts | 3 +-- .../integrations/integration.repository.ts | 5 +++-- .../integrations/integration.service.ts | 20 +++++++++++-------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/apps/backend/src/api/routes/integrations.controller.ts b/apps/backend/src/api/routes/integrations.controller.ts index e509aba3..d3058a99 100644 --- a/apps/backend/src/api/routes/integrations.controller.ts +++ b/apps/backend/src/api/routes/integrations.controller.ts @@ -31,7 +31,7 @@ import { NotEnoughScopesFilter } from '@gitroom/nestjs-libraries/integrations/in export class IntegrationsController { constructor( private _integrationManager: IntegrationManager, - private _integrationService: IntegrationService + private _integrationService: IntegrationService, ) {} @Get('/') getIntegration() { @@ -293,7 +293,6 @@ export class IntegrationsController { @GetOrgFromRequest() org: Organization, @Body('id') id: string ) { - // @ts-ignore return this._integrationService.deleteChannel(org.id, id); } } diff --git a/libraries/nestjs-libraries/src/database/prisma/integrations/integration.repository.ts b/libraries/nestjs-libraries/src/database/prisma/integrations/integration.repository.ts index 4c9677b6..4762cb85 100644 --- a/libraries/nestjs-libraries/src/database/prisma/integrations/integration.repository.ts +++ b/libraries/nestjs-libraries/src/database/prisma/integrations/integration.repository.ts @@ -187,8 +187,9 @@ export class IntegrationRepository { }); } - countPostsForChannel(org: string, id: string) { - return this._posts.model.post.count({ + getPostsForChannel(org: string, id: string) { + return this._posts.model.post.groupBy({ + by: ['group'], where: { organizationId: org, integrationId: id, diff --git a/libraries/nestjs-libraries/src/database/prisma/integrations/integration.service.ts b/libraries/nestjs-libraries/src/database/prisma/integrations/integration.service.ts index 2f201b73..f21c09ce 100644 --- a/libraries/nestjs-libraries/src/database/prisma/integrations/integration.service.ts +++ b/libraries/nestjs-libraries/src/database/prisma/integrations/integration.service.ts @@ -7,13 +7,15 @@ import { SocialProvider } from '@gitroom/nestjs-libraries/integrations/social/so import { Integration } from '@prisma/client'; import { NotificationService } from '@gitroom/nestjs-libraries/database/prisma/notifications/notification.service'; import { LinkedinPageProvider } from '@gitroom/nestjs-libraries/integrations/social/linkedin.page.provider'; +import { PostsService } from '@gitroom/nestjs-libraries/database/prisma/posts/posts.service'; @Injectable() export class IntegrationService { constructor( private _integrationRepository: IntegrationRepository, private _integrationManager: IntegrationManager, - private _notificationService: NotificationService + private _notificationService: NotificationService, + private _postsService: PostsService ) {} createOrUpdateIntegration( org: string, @@ -143,15 +145,14 @@ export class IntegrationService { } async deleteChannel(org: string, id: string) { - const isTherePosts = await this._integrationRepository.countPostsForChannel( + const isTherePosts = await this._integrationRepository.getPostsForChannel( org, id ); - if (isTherePosts) { - throw new HttpException( - 'There are posts for this channel', - HttpStatus.NOT_ACCEPTABLE - ); + if (isTherePosts.length) { + for (const post of isTherePosts) { + await this._postsService.deletePost(org, post.group); + } } return this._integrationRepository.deleteChannel(org, id); @@ -217,7 +218,10 @@ export class IntegrationService { page ); - await this.checkForDeletedOnceAndUpdate(org, String(getIntegrationInformation.id)); + await this.checkForDeletedOnceAndUpdate( + org, + String(getIntegrationInformation.id) + ); await this._integrationRepository.updateIntegration(String(id), { picture: getIntegrationInformation.picture,