-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
747 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,3 +111,4 @@ | |
"testEnvironment": "node" | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
.../api/src/@core/connections/marketing_automation/marketing_automation.connection.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { PrismaService } from '@@core/prisma/prisma.service'; | ||
import { LoggerService } from '@@core/logger/logger.service'; | ||
import { WebhookService } from '@@core/webhook/webhook.service'; | ||
import { WebhookModule } from '@@core/webhook/webhook.module'; | ||
import { EnvironmentService } from '@@core/environment/environment.service'; | ||
import { EncryptionService } from '@@core/encryption/encryption.service'; | ||
import { ConnectionsStrategiesService } from '@@core/connections-strategies/connections-strategies.service'; | ||
import { MarketingAutomationConnectionsService } from './services/marketing_automation.connection.service'; | ||
import { ServiceRegistry } from './services/registry.service'; | ||
|
||
@Module({ | ||
imports: [WebhookModule], | ||
providers: [ | ||
MarketingAutomationConnectionsService, | ||
PrismaService, | ||
LoggerService, | ||
WebhookService, | ||
EnvironmentService, | ||
EncryptionService, | ||
ServiceRegistry, | ||
ConnectionsStrategiesService, | ||
//PROVIDERS SERVICES | ||
], | ||
exports: [MarketingAutomationConnectionsService], | ||
}) | ||
export class MarketingAutomationConnectionsModule {} |
194 changes: 194 additions & 0 deletions
194
...pi/src/@core/connections/marketing_automation/services/getresponse/getresponse.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import axios from 'axios'; | ||
import { PrismaService } from '@@core/prisma/prisma.service'; | ||
import { Action, handleServiceError } from '@@core/utils/errors'; | ||
import { LoggerService } from '@@core/logger/logger.service'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { EnvironmentService } from '@@core/environment/environment.service'; | ||
import { EncryptionService } from '@@core/encryption/encryption.service'; | ||
import { | ||
CallbackParams, | ||
RefreshParams, | ||
IMarketingAutomationConnectionService, | ||
} from '../../types'; | ||
import { ServiceRegistry } from '../registry.service'; | ||
import { AuthStrategy, providersConfig } from '@panora/shared'; | ||
import { OAuth2AuthData, providerToType } from '@panora/shared'; | ||
import { ConnectionsStrategiesService } from '@@core/connections-strategies/connections-strategies.service'; | ||
|
||
export type GetresponseOAuthResponse = { | ||
access_token: string; | ||
refresh_token: string; | ||
expires_in: string; | ||
token_type: string; | ||
scope: any; | ||
}; | ||
|
||
@Injectable() | ||
export class GetresponseConnectionService | ||
implements IMarketingAutomationConnectionService | ||
{ | ||
private readonly type: string; | ||
|
||
constructor( | ||
private prisma: PrismaService, | ||
private logger: LoggerService, | ||
private env: EnvironmentService, | ||
private cryptoService: EncryptionService, | ||
private registry: ServiceRegistry, | ||
private cService: ConnectionsStrategiesService, | ||
) { | ||
this.logger.setContext(GetresponseConnectionService.name); | ||
this.registry.registerService('getresponse', this); | ||
this.type = providerToType( | ||
'getresponse', | ||
'marketing_automation', | ||
AuthStrategy.oauth2, | ||
); | ||
} | ||
|
||
async handleCallback(opts: CallbackParams) { | ||
try { | ||
const { linkedUserId, projectId, code } = opts; | ||
const isNotUnique = await this.prisma.connections.findFirst({ | ||
where: { | ||
id_linked_user: linkedUserId, | ||
provider_slug: 'getresponse', | ||
vertical: 'marketing_automation', | ||
}, | ||
}); | ||
|
||
//reconstruct the redirect URI that was passed in the githubend it must be the same | ||
const REDIRECT_URI = `${this.env.getOAuthRredirectBaseUrl()}/connections/oauth/callback`; | ||
const CREDENTIALS = (await this.cService.getCredentials( | ||
projectId, | ||
this.type, | ||
)) as OAuth2AuthData; | ||
|
||
const formData = new URLSearchParams({ | ||
code: code, | ||
grant_type: 'authorization_code', | ||
}); | ||
const res = await axios.post( | ||
'https://api.getresponse.com/v3/token', | ||
formData.toString(), | ||
{ | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8', | ||
Authorization: `Basic ${Buffer.from( | ||
`${CREDENTIALS.CLIENT_ID}:${CREDENTIALS.CLIENT_SECRET}`, | ||
).toString('base64')}`, | ||
}, | ||
}, | ||
); | ||
const data: GetresponseOAuthResponse = res.data; | ||
this.logger.log( | ||
'OAuth credentials : getresponse ticketing ' + JSON.stringify(data), | ||
); | ||
|
||
let db_res; | ||
const connection_token = uuidv4(); | ||
|
||
if (isNotUnique) { | ||
db_res = await this.prisma.connections.update({ | ||
where: { | ||
id_connection: isNotUnique.id_connection, | ||
}, | ||
data: { | ||
access_token: this.cryptoService.encrypt(data.access_token), | ||
refresh_token: this.cryptoService.encrypt(data.refresh_token), | ||
account_url: | ||
providersConfig['marketing_automation']['getresponse'].urls | ||
.apiUrl, | ||
expiration_timestamp: new Date( | ||
new Date().getTime() + Number(data.expires_in) * 1000, | ||
), | ||
status: 'valid', | ||
created_at: new Date(), | ||
}, | ||
}); | ||
} else { | ||
db_res = await this.prisma.connections.create({ | ||
data: { | ||
id_connection: uuidv4(), | ||
connection_token: connection_token, | ||
provider_slug: 'getresponse', | ||
vertical: 'marketing_automation', | ||
token_type: 'oauth', | ||
account_url: | ||
providersConfig['marketing_automation']['getresponse'].urls | ||
.apiUrl, | ||
access_token: this.cryptoService.encrypt(data.access_token), | ||
refresh_token: this.cryptoService.encrypt(data.refresh_token), | ||
expiration_timestamp: new Date( | ||
new Date().getTime() + Number(data.expires_in) * 1000, | ||
), | ||
status: 'valid', | ||
created_at: new Date(), | ||
projects: { | ||
connect: { id_project: projectId }, | ||
}, | ||
linked_users: { | ||
connect: { id_linked_user: linkedUserId }, | ||
}, | ||
}, | ||
}); | ||
} | ||
return db_res; | ||
} catch (error) { | ||
handleServiceError( | ||
error, | ||
this.logger, | ||
'getresponse', | ||
Action.oauthCallback, | ||
); | ||
} | ||
} | ||
|
||
async handleTokenRefresh(opts: RefreshParams) { | ||
try { | ||
const { connectionId, refreshToken, projectId } = opts; | ||
const CREDENTIALS = (await this.cService.getCredentials( | ||
projectId, | ||
this.type, | ||
)) as OAuth2AuthData; | ||
const formData = new URLSearchParams({ | ||
grant_type: 'refresh_token', | ||
refresh_token: this.cryptoService.decrypt(refreshToken), | ||
}); | ||
const res = await axios.post( | ||
'https://api.getresponse.com/v3/token', | ||
formData.toString(), | ||
{ | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8', | ||
Authorization: `Basic ${Buffer.from( | ||
`${CREDENTIALS.CLIENT_ID}:${CREDENTIALS.CLIENT_SECRET}`, | ||
).toString('base64')}`, | ||
}, | ||
}, | ||
); | ||
const data: GetresponseOAuthResponse = res.data; | ||
await this.prisma.connections.update({ | ||
where: { | ||
id_connection: connectionId, | ||
}, | ||
data: { | ||
access_token: this.cryptoService.encrypt(data.access_token), | ||
refresh_token: this.cryptoService.encrypt(data.refresh_token), | ||
expiration_timestamp: new Date( | ||
new Date().getTime() + Number(data.expires_in) * 1000, | ||
), | ||
}, | ||
}); | ||
this.logger.log('OAuth credentials updated : getresponse '); | ||
} catch (error) { | ||
handleServiceError( | ||
error, | ||
this.logger, | ||
'getresponse', | ||
Action.oauthRefresh, | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.