Skip to content

Commit

Permalink
feat: fix linkedin connect for multiple accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
nevo-david committed Dec 30, 2024
1 parent 1a3b52a commit 4806b82
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 3 deletions.
3 changes: 3 additions & 0 deletions apps/backend/src/api/routes/integrations.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ export class IntegrationsController {

if (accessToken) {
await this._integrationService.createOrUpdateIntegration(
!!integrationProvider.oneTimeToken,
getIntegration.organizationId,
getIntegration.name,
getIntegration.picture!,
Expand Down Expand Up @@ -345,6 +346,7 @@ export class IntegrationsController {
}

return this._integrationService.createOrUpdateIntegration(
true,
org.id,
name,
picture,
Expand Down Expand Up @@ -468,6 +470,7 @@ export class IntegrationsController {
}
}
return this._integrationService.createOrUpdateIntegration(
!!integrationProvider.oneTimeToken,
org.id,
validName.trim(),
picture,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ export class IntegrationRepository {
});
}

createOrUpdateIntegration(
async createOrUpdateIntegration(
oneTimeToken: boolean,
org: string,
name: string,
picture: string | undefined,
Expand All @@ -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,
Expand All @@ -141,6 +142,7 @@ export class IntegrationRepository {
...postTimes,
organizationId: org,
refreshNeeded: false,
rootInternalId: internalId.split('_').pop(),
...(customInstanceDetails ? { customInstanceDetails } : {}),
},
update: {
Expand All @@ -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() {
Expand Down Expand Up @@ -497,6 +520,6 @@ export class IntegrationRepository {
select: {
postingTimes: true,
},
})
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class IntegrationService {
}

async createOrUpdateIntegration(
oneTimeToken: boolean,
org: string,
name: string,
picture: string | undefined,
Expand All @@ -61,6 +62,7 @@ export class IntegrationService {
? await this.storage.uploadSimple(picture)
: undefined;
return this._integrationRepository.createOrUpdateIntegration(
oneTimeToken,
org,
name,
uploadedPicture,
Expand Down Expand Up @@ -164,6 +166,7 @@ export class IntegrationService {
const { refreshToken, accessToken, expiresIn } = data;

await this.createOrUpdateIntegration(
!!provider.oneTimeToken,
integration.organizationId,
integration.name,
undefined,
Expand Down Expand Up @@ -350,6 +353,7 @@ export class IntegrationService {

if (accessToken) {
await this.createOrUpdateIntegration(
!!integrationProvider.oneTimeToken,
getIntegration.organizationId,
getIntegration.name,
getIntegration.picture!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ export class PostsService {
}

await this._integrationService.createOrUpdateIntegration(
!!getIntegration.oneTimeToken,
integration.organizationId,
integration.name,
integration.picture!,
Expand Down
2 changes: 2 additions & 0 deletions libraries/nestjs-libraries/src/database/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export interface SocialProvider
}[]
>;
name: string;
oneTimeToken?: boolean;
isBetweenSteps: boolean;
scopes: string[];
externalUrl?: (
Expand Down

0 comments on commit 4806b82

Please sign in to comment.