-
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.
Merge pull request #214 from panoratech/feat/zendesk-front-integration
✨ Integrations for ticket WIP
- Loading branch information
Showing
174 changed files
with
10,650 additions
and
1,020 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
VITE_BACKEND_DOMAIN= | ||
VITE_BACKEND_DOMAIN= | ||
VITE_ML_FRONTEND_URL= |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -6,19 +6,21 @@ const prisma = new PrismaClient(); | |
async function main() { | ||
const org = await prisma.organizations.create({ | ||
data: { | ||
id_organization: uuidv4(), | ||
id_organization: `55222419-795d-4183-8478-361626363e58`, | ||
name: `Acme Inc`, | ||
stripe_customer_id: `cust_stripe_acme_${uuidv4()}`, | ||
stripe_customer_id: `cust_stripe_acme_56604f75-7bf8-4541-9ab4-5928aade4bb8`, | ||
}, | ||
}); | ||
|
||
await prisma.users.create({ | ||
data: { | ||
id_user: uuidv4(), | ||
id_user: `0ce39030-2901-4c56-8db0-5e326182ec6b`, | ||
email: '[email protected]', | ||
password_hash: 'password_hashed_her', | ||
password_hash: | ||
'$2b$10$Nxcp3x0yDaCrMrhZQ6IiNeqk0BxxDTnfn9iGG2UK5nWMh/UB6LgZu', | ||
first_name: 'audrey', | ||
last_name: 'aubry', | ||
id_organization: '55222419-795d-4183-8478-361626363e58', | ||
}, | ||
}); | ||
|
||
|
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
151 changes: 151 additions & 0 deletions
151
packages/api/src/@core/connections/ticketing/services/front/front.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,151 @@ | ||
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, | ||
ITicketingConnectionService, | ||
FrontOAuthResponse, | ||
} from '../../types'; | ||
import { ServiceRegistry } from '../registry.service'; | ||
|
||
@Injectable() | ||
export class FrontConnectionService implements ITicketingConnectionService { | ||
constructor( | ||
private prisma: PrismaService, | ||
private logger: LoggerService, | ||
private env: EnvironmentService, | ||
private cryptoService: EncryptionService, | ||
private registry: ServiceRegistry, | ||
) { | ||
this.logger.setContext(FrontConnectionService.name); | ||
this.registry.registerService('front', this); | ||
} | ||
|
||
async handleCallback(opts: CallbackParams) { | ||
try { | ||
const { linkedUserId, projectId, code } = opts; | ||
const isNotUnique = await this.prisma.connections.findFirst({ | ||
where: { | ||
id_linked_user: linkedUserId, | ||
provider_slug: 'front', //TODO | ||
}, | ||
}); | ||
|
||
//reconstruct the redirect URI that was passed in the frontend it must be the same | ||
const REDIRECT_URI = `${this.env.getOAuthRredirectBaseUrl()}/connections/oauth/callback`; | ||
|
||
const formData = new URLSearchParams({ | ||
grant_type: 'authorization_code', | ||
redirect_uri: REDIRECT_URI, | ||
code: code, | ||
}); | ||
const res = await axios.post( | ||
`https://app.frontapp.com/oauth/token`, | ||
formData.toString(), | ||
{ | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8', | ||
Authorization: `Basic ${Buffer.from( | ||
`${this.env.getFrontSecret().CLIENT_ID}:${ | ||
this.env.getFrontSecret().CLIENT_SECRET | ||
}`, | ||
).toString('base64')}`, | ||
}, | ||
}, | ||
); | ||
const data: FrontOAuthResponse = res.data; | ||
this.logger.log( | ||
'OAuth credentials : front ticketing ' + JSON.stringify(data), | ||
); | ||
|
||
let db_res; | ||
|
||
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), | ||
expiration_timestamp: new Date( | ||
new Date().getTime() + Number(data.expires_at) * 1000, | ||
), | ||
status: 'valid', | ||
created_at: new Date(), | ||
}, | ||
}); | ||
} else { | ||
db_res = await this.prisma.connections.create({ | ||
data: { | ||
id_connection: uuidv4(), | ||
provider_slug: 'front', | ||
token_type: 'oauth', | ||
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_at) * 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, 'front', Action.oauthCallback); | ||
} | ||
} | ||
|
||
async handleTokenRefresh(opts: RefreshParams) { | ||
try { | ||
const { connectionId, refreshToken } = opts; | ||
const formData = new URLSearchParams({ | ||
grant_type: 'refresh_token', | ||
refresh_token: this.cryptoService.decrypt(refreshToken), | ||
}); | ||
const res = await axios.post( | ||
`https://app.frontapp.com/oauth/token`, | ||
formData.toString(), | ||
{ | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8', | ||
Authorization: `Basic ${Buffer.from( | ||
`${this.env.getFrontSecret().CLIENT_ID}:${ | ||
this.env.getFrontSecret().CLIENT_SECRET | ||
}`, | ||
).toString('base64')}`, | ||
}, | ||
}, | ||
); | ||
const data: FrontOAuthResponse = 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_at) * 1000, | ||
), | ||
}, | ||
}); | ||
this.logger.log('OAuth credentials updated : front '); | ||
} catch (error) { | ||
handleServiceError(error, this.logger, 'front', Action.oauthRefresh); | ||
} | ||
} | ||
} |
Oops, something went wrong.