-
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
4 changed files
with
92 additions
and
4 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
82 changes: 82 additions & 0 deletions
82
packages/api/src/@core/connections/crm/services/affinity/affinity.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,82 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { PrismaService } from '@@core/prisma/prisma.service'; | ||
import { LoggerService } from '@@core/logger/logger.service'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { EncryptionService } from '@@core/encryption/encryption.service'; | ||
import { ICrmConnectionService } from '../../types'; | ||
import { ServiceRegistry } from '../registry.service'; | ||
import { CONNECTORS_METADATA } from '@panora/shared'; | ||
import { ConnectionUtils } from '@@core/connections/@utils'; | ||
import { APIKeyCallbackParams } from '@@core/connections/@utils/types'; | ||
|
||
@Injectable() | ||
export class AffinityConnectionService implements ICrmConnectionService { | ||
constructor( | ||
private prisma: PrismaService, | ||
private logger: LoggerService, | ||
private cryptoService: EncryptionService, | ||
private registry: ServiceRegistry, | ||
private connectionUtils: ConnectionUtils, | ||
) { | ||
this.logger.setContext(AffinityConnectionService.name); | ||
this.registry.registerService('affinity', this); | ||
} | ||
|
||
async handleCallback(opts: APIKeyCallbackParams) { | ||
try { | ||
const { linkedUserId, projectId } = opts; | ||
const isNotUnique = await this.prisma.connections.findFirst({ | ||
where: { | ||
id_linked_user: linkedUserId, | ||
provider_slug: 'affinity', | ||
vertical: 'crm', | ||
}, | ||
}); | ||
|
||
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(opts.apikey), | ||
account_url: CONNECTORS_METADATA['crm']['affinity'].urls.apiUrl, | ||
status: 'valid', | ||
created_at: new Date(), | ||
}, | ||
}); | ||
} else { | ||
db_res = await this.prisma.connections.create({ | ||
data: { | ||
id_connection: uuidv4(), | ||
connection_token: connection_token, | ||
provider_slug: 'affinity', | ||
vertical: 'crm', | ||
token_type: 'api_key', | ||
account_url: CONNECTORS_METADATA['crm']['affinity'].urls.apiUrl, | ||
access_token: this.cryptoService.encrypt(opts.apikey), | ||
status: 'valid', | ||
created_at: new Date(), | ||
projects: { | ||
connect: { id_project: projectId }, | ||
}, | ||
linked_users: { | ||
connect: { | ||
id_linked_user: await this.connectionUtils.getLinkedUserId( | ||
projectId, | ||
linkedUserId, | ||
), | ||
}, | ||
}, | ||
}, | ||
}); | ||
} | ||
return db_res; | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
} |
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