diff --git a/apps/backend/src/api/routes/integrations.controller.ts b/apps/backend/src/api/routes/integrations.controller.ts index 18770649..e19e8668 100644 --- a/apps/backend/src/api/routes/integrations.controller.ts +++ b/apps/backend/src/api/routes/integrations.controller.ts @@ -1,5 +1,12 @@ import { - Body, Controller, Delete, Get, Param, Post, Query, UseFilters + Body, + Controller, + Delete, + Get, + Param, + Post, + Query, + UseFilters, } from '@nestjs/common'; import { ioRedis } from '@gitroom/nestjs-libraries/redis/redis.service'; import { ConnectIntegrationDto } from '@gitroom/nestjs-libraries/dtos/integrations/connect.integration.dto'; @@ -22,7 +29,11 @@ import { PostsService } from '@gitroom/nestjs-libraries/database/prisma/posts/po import { IntegrationTimeDto } from '@gitroom/nestjs-libraries/dtos/integrations/integration.time.dto'; import { AuthService } from '@gitroom/helpers/auth/auth.service'; import { AuthTokenDetails } from '@gitroom/nestjs-libraries/integrations/social/social.integrations.interface'; -import { NotEnoughScopes } from '@gitroom/nestjs-libraries/integrations/social.abstract'; +import { + NotEnoughScopes, + RefreshToken, +} from '@gitroom/nestjs-libraries/integrations/social.abstract'; +import { timer } from '@gitroom/helpers/utils/timer'; @ApiTags('Integrations') @Controller('/integrations') @@ -201,11 +212,51 @@ export class IntegrationsController { } if (integrationProvider[body.name]) { - return integrationProvider[body.name]( - getIntegration.token, - body.data, - getIntegration.internalId - ); + try { + const load = await integrationProvider[body.name]( + getIntegration.token, + body.data, + getIntegration.internalId + ); + + return load; + } catch (err) { + if (err instanceof RefreshToken) { + const { accessToken, refreshToken, expiresIn } = + await integrationProvider.refreshToken( + getIntegration.refreshToken + ); + + if (accessToken) { + await this._integrationService.createOrUpdateIntegration( + getIntegration.organizationId, + getIntegration.name, + getIntegration.picture!, + 'social', + getIntegration.internalId, + getIntegration.providerIdentifier, + accessToken, + refreshToken, + expiresIn + ); + + getIntegration.token = accessToken; + + if (integrationProvider.refreshWait) { + await timer(10000); + } + return this.functionIntegration(org, body); + } else { + await this._integrationService.disconnectChannel( + org.id, + getIntegration + ); + return false; + } + } + + return false; + } } throw new Error('Function not found'); } @@ -352,7 +403,9 @@ export class IntegrationsController { } if (refresh && id !== refresh) { - throw new NotEnoughScopes('Please refresh the channel that needs to be refreshed'); + throw new NotEnoughScopes( + 'Please refresh the channel that needs to be refreshed' + ); } return this._integrationService.createOrUpdateIntegration(