-
Notifications
You must be signed in to change notification settings - Fork 195
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
110 changed files
with
2,363 additions
and
907 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
41 changes: 41 additions & 0 deletions
41
packages/api/src/@core/connections/accounting/accounting.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,41 @@ | ||
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 { ServiceRegistry } from './services/registry.service'; | ||
import { AccountingConnectionsService } from './services/accounting.connection.service'; | ||
import { PennylaneConnectionService } from './services/pennylane/pennylane.service'; | ||
import { FreeagentConnectionService } from './services/freeagent/freeagent.service'; | ||
import { FreshbooksConnectionService } from './services/freshbooks/freshbooks.service'; | ||
import { MoneybirdConnectionService } from './services/moneybird/moneybird.service'; | ||
import { QuickbooksConnectionService } from './services/quickbooks/quickbooks.service'; | ||
import { SageConnectionService } from './services/sage/sage.service'; | ||
import { WaveFinancialConnectionService } from './services/wave_financial/wave_financial.service'; | ||
|
||
@Module({ | ||
imports: [WebhookModule], | ||
providers: [ | ||
AccountingConnectionsService, | ||
PrismaService, | ||
LoggerService, | ||
WebhookService, | ||
EnvironmentService, | ||
EncryptionService, | ||
ServiceRegistry, | ||
ConnectionsStrategiesService, | ||
//PROVIDERS SERVICES, | ||
PennylaneConnectionService, | ||
FreeagentConnectionService, | ||
FreshbooksConnectionService, | ||
MoneybirdConnectionService, | ||
QuickbooksConnectionService, | ||
SageConnectionService, | ||
WaveFinancialConnectionService, | ||
], | ||
exports: [AccountingConnectionsService], | ||
}) | ||
export class AccountingConnectionModule {} |
104 changes: 104 additions & 0 deletions
104
packages/api/src/@core/connections/accounting/services/accounting.connection.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,104 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { NotFoundError, handleServiceError } from '@@core/utils/errors'; | ||
import { LoggerService } from '@@core/logger/logger.service'; | ||
import { WebhookService } from '@@core/webhook/webhook.service'; | ||
import { connections as Connection } from '@prisma/client'; | ||
import { PrismaService } from '@@core/prisma/prisma.service'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { CallbackParams, RefreshParams } from '../types'; | ||
import { ServiceRegistry } from './registry.service'; | ||
|
||
@Injectable() | ||
export class AccountingConnectionsService { | ||
constructor( | ||
private serviceRegistry: ServiceRegistry, | ||
private webhook: WebhookService, | ||
private logger: LoggerService, | ||
private prisma: PrismaService, | ||
) { | ||
this.logger.setContext(AccountingConnectionsService.name); | ||
} | ||
//STEP 1:[FRONTEND STEP] | ||
//create a frontend SDK snippet in which an authorization embedded link is set up so when users click | ||
// on it to grant access => they grant US the access and then when confirmed | ||
/*const authUrl = | ||
'https://app.hubspot.com/oauth/authorize' + | ||
`?client_id=${encodeURIComponent(CLIENT_ID)}` + | ||
`&scope=${encodeURIComponent(SCOPES)}` + | ||
`&redirect_uri=${encodeURIComponent(REDIRECT_URI)}`;*/ //oauth/callback | ||
|
||
// oauth server calls this redirect callback | ||
// WE WOULD HAVE CREATED A DEV ACCOUNT IN THE 5 CRMs (Panora dev account) | ||
// we catch the tmp token and swap it against oauth2 server for access/refresh tokens | ||
// to perform actions on his behalf | ||
// this call pass 1. integrationID 2. CustomerId 3. Panora Api Key | ||
async handleAccountingCallBack( | ||
projectId: string, | ||
linkedUserId: string, | ||
providerName: string, | ||
code: string, | ||
) { | ||
try { | ||
const serviceName = providerName.toLowerCase(); | ||
const service = this.serviceRegistry.getService(serviceName); | ||
|
||
if (!service) { | ||
throw new NotFoundError(`Unknown provider, found ${providerName}`); | ||
} | ||
const callbackOpts: CallbackParams = { | ||
linkedUserId: linkedUserId, | ||
projectId: projectId, | ||
code: code, | ||
}; | ||
const data: Connection = await service.handleCallback(callbackOpts); | ||
|
||
const event = await this.prisma.events.create({ | ||
data: { | ||
id_event: uuidv4(), | ||
status: 'success', | ||
type: 'connection.created', | ||
method: 'GET', | ||
url: '/oauth/callback', | ||
provider: providerName.toLowerCase(), | ||
direction: '0', | ||
timestamp: new Date(), | ||
id_linked_user: linkedUserId, | ||
}, | ||
}); | ||
//directly send the webhook | ||
await this.webhook.handlePriorityWebhook( | ||
data, | ||
'connection.created', | ||
projectId, | ||
event.id_event, | ||
); | ||
} catch (error) { | ||
handleServiceError(error, this.logger); | ||
} | ||
} | ||
|
||
async handleAccountingTokensRefresh( | ||
connectionId: string, | ||
providerName: string, | ||
refresh_token: string, | ||
id_project: string, | ||
account_url?: string, | ||
) { | ||
try { | ||
const serviceName = providerName.toLowerCase(); | ||
const service = this.serviceRegistry.getService(serviceName); | ||
if (!service) { | ||
throw new NotFoundError(`Unknown provider, found ${providerName}`); | ||
} | ||
const refreshOpts: RefreshParams = { | ||
connectionId: connectionId, | ||
refreshToken: refresh_token, | ||
account_url: account_url, | ||
projectId: id_project, | ||
}; | ||
const data = await service.handleTokenRefresh(refreshOpts); | ||
} catch (error) { | ||
handleServiceError(error, this.logger); | ||
} | ||
} | ||
} |
Oops, something went wrong.