diff --git a/apps/backend/src/api/routes/integrations.controller.ts b/apps/backend/src/api/routes/integrations.controller.ts index 332de202c..6588ead81 100644 --- a/apps/backend/src/api/routes/integrations.controller.ts +++ b/apps/backend/src/api/routes/integrations.controller.ts @@ -264,6 +264,7 @@ export class IntegrationsController { if (accessToken) { await this._integrationService.createOrUpdateIntegration( + !!integrationProvider.oneTimeToken, getIntegration.organizationId, getIntegration.name, getIntegration.picture!, @@ -345,6 +346,7 @@ export class IntegrationsController { } return this._integrationService.createOrUpdateIntegration( + true, org.id, name, picture, @@ -468,6 +470,7 @@ export class IntegrationsController { } } return this._integrationService.createOrUpdateIntegration( + !!integrationProvider.oneTimeToken, org.id, validName.trim(), picture, 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 79fb06eba..179c00e92 100644 --- a/libraries/nestjs-libraries/src/database/prisma/integrations/integration.repository.ts +++ b/libraries/nestjs-libraries/src/database/prisma/integrations/integration.repository.ts @@ -93,7 +93,8 @@ export class IntegrationRepository { }); } - createOrUpdateIntegration( + async createOrUpdateIntegration( + oneTimeToken: boolean, org: string, name: string, picture: string | undefined, @@ -118,7 +119,7 @@ export class IntegrationRepository { ]), } : {}; - return this._integration.model.integration.upsert({ + const upsert = await this._integration.model.integration.upsert({ where: { organizationId_internalId: { internalId, @@ -141,6 +142,7 @@ export class IntegrationRepository { ...postTimes, organizationId: org, refreshNeeded: false, + rootInternalId: internalId.split('_').pop(), ...(customInstanceDetails ? { customInstanceDetails } : {}), }, update: { @@ -164,6 +166,27 @@ export class IntegrationRepository { refreshNeeded: false, }, }); + + if (oneTimeToken) { + await this._integration.model.integration.updateMany({ + where: { + id: { + not: upsert.id, + }, + organizationId: org, + rootInternalId: internalId.split('_').pop(), + }, + data: { + token, + refreshToken, + ...(expiresIn + ? { tokenExpiration: new Date(Date.now() + expiresIn * 1000) } + : {}), + }, + }); + } + + return upsert; } needsToBeRefreshed() { @@ -497,6 +520,6 @@ export class IntegrationRepository { select: { postingTimes: true, }, - }) + }); } } 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 eb1ff5b4a..5bace62c9 100644 --- a/libraries/nestjs-libraries/src/database/prisma/integrations/integration.service.ts +++ b/libraries/nestjs-libraries/src/database/prisma/integrations/integration.service.ts @@ -42,6 +42,7 @@ export class IntegrationService { } async createOrUpdateIntegration( + oneTimeToken: boolean, org: string, name: string, picture: string | undefined, @@ -61,6 +62,7 @@ export class IntegrationService { ? await this.storage.uploadSimple(picture) : undefined; return this._integrationRepository.createOrUpdateIntegration( + oneTimeToken, org, name, uploadedPicture, @@ -164,6 +166,7 @@ export class IntegrationService { const { refreshToken, accessToken, expiresIn } = data; await this.createOrUpdateIntegration( + !!provider.oneTimeToken, integration.organizationId, integration.name, undefined, @@ -350,6 +353,7 @@ export class IntegrationService { if (accessToken) { await this.createOrUpdateIntegration( + !!integrationProvider.oneTimeToken, getIntegration.organizationId, getIntegration.name, getIntegration.picture!, 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 04961a4dd..13285e243 100644 --- a/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts +++ b/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts @@ -249,6 +249,7 @@ export class PostsService { } await this._integrationService.createOrUpdateIntegration( + !!getIntegration.oneTimeToken, integration.organizationId, integration.name, integration.picture!, diff --git a/libraries/nestjs-libraries/src/database/prisma/schema.prisma b/libraries/nestjs-libraries/src/database/prisma/schema.prisma index cc5b0a1eb..f7c264f14 100644 --- a/libraries/nestjs-libraries/src/database/prisma/schema.prisma +++ b/libraries/nestjs-libraries/src/database/prisma/schema.prisma @@ -287,7 +287,9 @@ model Integration { customer Customer? @relation(fields: [customerId], references: [id]) plugs Plugs[] exisingPlugData ExisingPlugData[] + rootInternalId String? + @@index([rootInternalId]) @@index([updatedAt]) @@index([deletedAt]) @@unique([organizationId, internalId]) diff --git a/libraries/nestjs-libraries/src/integrations/social/linkedin.provider.ts b/libraries/nestjs-libraries/src/integrations/social/linkedin.provider.ts index 3e13c3fbb..3d81fd472 100644 --- a/libraries/nestjs-libraries/src/integrations/social/linkedin.provider.ts +++ b/libraries/nestjs-libraries/src/integrations/social/linkedin.provider.ts @@ -15,6 +15,8 @@ import { PostPlug } from '@gitroom/helpers/decorators/post.plug'; export class LinkedinProvider extends SocialAbstract implements SocialProvider { identifier = 'linkedin'; name = 'LinkedIn'; + oneTimeToken = true; + isBetweenSteps = false; scopes = [ 'openid', diff --git a/libraries/nestjs-libraries/src/integrations/social/social.integrations.interface.ts b/libraries/nestjs-libraries/src/integrations/social/social.integrations.interface.ts index a6d083221..fd99ee445 100644 --- a/libraries/nestjs-libraries/src/integrations/social/social.integrations.interface.ts +++ b/libraries/nestjs-libraries/src/integrations/social/social.integrations.interface.ts @@ -109,6 +109,7 @@ export interface SocialProvider }[] >; name: string; + oneTimeToken?: boolean; isBetweenSteps: boolean; scopes: string[]; externalUrl?: (