-
Notifications
You must be signed in to change notification settings - Fork 180
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
37 changed files
with
1,846 additions
and
20 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
143 changes: 143 additions & 0 deletions
143
packages/api/src/@core/connections/hris/services/sage/sage.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,143 @@ | ||
import { EncryptionService } from '@@core/@core-services/encryption/encryption.service'; | ||
import { LoggerService } from '@@core/@core-services/logger/logger.service'; | ||
import { PrismaService } from '@@core/@core-services/prisma/prisma.service'; | ||
import { RetryHandler } from '@@core/@core-services/request-retry/retry.handler'; | ||
import { ConnectionsStrategiesService } from '@@core/connections-strategies/connections-strategies.service'; | ||
import { ConnectionUtils } from '@@core/connections/@utils'; | ||
import { | ||
AbstractBaseConnectionService, | ||
OAuthCallbackParams, | ||
PassthroughInput, | ||
RefreshParams, | ||
} from '@@core/connections/@utils/types'; | ||
import { PassthroughResponse } from '@@core/passthrough/types'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { | ||
AuthStrategy, | ||
CONNECTORS_METADATA, | ||
DynamicApiUrl, | ||
providerToType, | ||
} from '@panora/shared'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { ServiceRegistry } from '../registry.service'; | ||
|
||
@Injectable() | ||
export class SageConnectionService extends AbstractBaseConnectionService { | ||
private readonly type: string; | ||
|
||
constructor( | ||
protected prisma: PrismaService, | ||
private logger: LoggerService, | ||
protected cryptoService: EncryptionService, | ||
private registry: ServiceRegistry, | ||
private connectionUtils: ConnectionUtils, | ||
private cService: ConnectionsStrategiesService, | ||
private retryService: RetryHandler, | ||
) { | ||
super(prisma, cryptoService); | ||
this.logger.setContext(SageConnectionService.name); | ||
this.registry.registerService('sage', this); | ||
this.type = providerToType('sage', 'hris', AuthStrategy.oauth2); | ||
} | ||
|
||
async passthrough( | ||
input: PassthroughInput, | ||
connectionId: string, | ||
): Promise<PassthroughResponse> { | ||
try { | ||
const { headers } = input; | ||
const config = await this.constructPassthrough(input, connectionId); | ||
|
||
const connection = await this.prisma.connections.findUnique({ | ||
where: { | ||
id_connection: connectionId, | ||
}, | ||
}); | ||
|
||
config.headers = { | ||
...config.headers, | ||
...headers, | ||
'X-Auth-Token': this.cryptoService.decrypt(connection.access_token), | ||
}; | ||
|
||
return await this.retryService.makeRequest( | ||
{ | ||
method: config.method, | ||
url: config.url, | ||
data: config.data, | ||
headers: config.headers, | ||
}, | ||
'hris.sage.passthrough', | ||
config.linkedUserId, | ||
); | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
|
||
async handleCallback(opts: OAuthCallbackParams) { | ||
try { | ||
const { linkedUserId, projectId, body } = opts; | ||
const { api_key, subdomain } = body; | ||
const isNotUnique = await this.prisma.connections.findFirst({ | ||
where: { | ||
id_linked_user: linkedUserId, | ||
provider_slug: 'sage', | ||
vertical: 'hris', | ||
}, | ||
}); | ||
|
||
let db_res; | ||
const connection_token = uuidv4(); | ||
const BASE_API_URL = ( | ||
CONNECTORS_METADATA['hris']['sage'].urls.apiUrl as DynamicApiUrl | ||
)(subdomain); | ||
|
||
if (isNotUnique) { | ||
db_res = await this.prisma.connections.update({ | ||
where: { | ||
id_connection: isNotUnique.id_connection, | ||
}, | ||
data: { | ||
access_token: this.cryptoService.encrypt(api_key), | ||
account_url: BASE_API_URL, | ||
status: 'valid', | ||
created_at: new Date(), | ||
}, | ||
}); | ||
} else { | ||
db_res = await this.prisma.connections.create({ | ||
data: { | ||
id_connection: uuidv4(), | ||
connection_token: connection_token, | ||
provider_slug: 'sage', | ||
vertical: 'hris', | ||
token_type: 'basic', | ||
account_url: BASE_API_URL, | ||
access_token: this.cryptoService.encrypt(api_key), | ||
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; | ||
} | ||
} | ||
|
||
handleTokenRefresh?(opts: RefreshParams): Promise<any> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { EncryptionService } from '@@core/@core-services/encryption/encryption.service'; | ||
import { EnvironmentService } from '@@core/@core-services/environment/environment.service'; | ||
import { LoggerService } from '@@core/@core-services/logger/logger.service'; | ||
import { PrismaService } from '@@core/@core-services/prisma/prisma.service'; | ||
import { ApiResponse } from '@@core/utils/types'; | ||
import { SyncParam } from '@@core/utils/types/interface'; | ||
import { HrisObject } from '@hris/@lib/@types'; | ||
import { ICompanyService } from '@hris/company/types'; | ||
import { Injectable } from '@nestjs/common'; | ||
import axios from 'axios'; | ||
import { ServiceRegistry } from '../registry.service'; | ||
import { DeelCompanyOutput } from './types'; | ||
import { DesunifyReturnType } from '@@core/utils/types/desunify.input'; | ||
import { OriginalCompanyOutput } from '@@core/utils/types/original/original.hris'; | ||
|
||
@Injectable() | ||
export class DeelService implements ICompanyService { | ||
constructor( | ||
private prisma: PrismaService, | ||
private logger: LoggerService, | ||
private cryptoService: EncryptionService, | ||
private env: EnvironmentService, | ||
private registry: ServiceRegistry, | ||
) { | ||
this.logger.setContext( | ||
HrisObject.company.toUpperCase() + ':' + DeelService.name, | ||
); | ||
this.registry.registerService('deel', this); | ||
} | ||
|
||
addCompany( | ||
companyData: DesunifyReturnType, | ||
linkedUserId: string, | ||
): Promise<ApiResponse<OriginalCompanyOutput>> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
async sync(data: SyncParam): Promise<ApiResponse<DeelCompanyOutput[]>> { | ||
try { | ||
const { linkedUserId } = data; | ||
|
||
const connection = await this.prisma.connections.findFirst({ | ||
where: { | ||
id_linked_user: linkedUserId, | ||
provider_slug: 'deel', | ||
vertical: 'hris', | ||
}, | ||
}); | ||
|
||
const resp = await axios.get( | ||
`${connection.account_url}/rest/v2/legal-entities`, | ||
{ | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${this.cryptoService.decrypt( | ||
connection.access_token, | ||
)}`, | ||
}, | ||
}, | ||
); | ||
this.logger.log(`Synced deel companys !`); | ||
|
||
return { | ||
data: resp.data.data, | ||
message: 'Deel companys retrieved', | ||
statusCode: 200, | ||
}; | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
} |
Oops, something went wrong.