-
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.
✨ Connections for BigCommerce & SquareSpace in Ecommerce
- Loading branch information
Showing
10 changed files
with
536 additions
and
25 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
147 changes: 147 additions & 0 deletions
147
packages/api/src/@core/connections/ecommerce/services/bigcommerce/bigcommerce.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,147 @@ | ||
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 BigcommerceConnectionService 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(BigcommerceConnectionService.name); | ||
this.registry.registerService('bigcommerce', this); | ||
this.type = providerToType('bigcommerce', 'ecommerce', 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, | ||
}, | ||
}); | ||
|
||
const access_token = JSON.parse( | ||
this.cryptoService.decrypt(connection.access_token), | ||
); | ||
config.headers = { | ||
...config.headers, | ||
...headers, | ||
'X-Auth-Token': access_token, | ||
}; | ||
|
||
return await this.retryService.makeRequest( | ||
{ | ||
method: config.method, | ||
url: config.url, | ||
data: config.data, | ||
headers: config.headers, | ||
}, | ||
'ecommerce.bigcommerce.passthrough', | ||
config.linkedUserId, | ||
); | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
|
||
async handleCallback(opts: OAuthCallbackParams) { | ||
try { | ||
const { linkedUserId, projectId, body } = opts; | ||
const { api_key, store_hash } = body; | ||
const isNotUnique = await this.prisma.connections.findFirst({ | ||
where: { | ||
id_linked_user: linkedUserId, | ||
provider_slug: 'bigcommerce', | ||
vertical: 'ecommerce', | ||
}, | ||
}); | ||
|
||
let db_res; | ||
const connection_token = uuidv4(); | ||
const BASE_API_URL = ( | ||
CONNECTORS_METADATA['ecommerce']['bigcommerce'].urls | ||
.apiUrl as DynamicApiUrl | ||
)(store_hash); | ||
|
||
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: 'bigcommerce', | ||
vertical: 'ecommerce', | ||
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
Oops, something went wrong.