-
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
65 changed files
with
3,764 additions
and
128 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
108 changes: 108 additions & 0 deletions
108
packages/api/src/@core/connections/ticketing/services/clickup/clickup.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,108 @@ | ||
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, | ||
ClickupOAuthResponse, | ||
} from '../../types'; | ||
import { ServiceRegistry } from '../registry.service'; | ||
|
||
@Injectable() | ||
export class ClickupConnectionService implements ITicketingConnectionService { | ||
constructor( | ||
private prisma: PrismaService, | ||
private logger: LoggerService, | ||
private env: EnvironmentService, | ||
private cryptoService: EncryptionService, | ||
private registry: ServiceRegistry, | ||
) { | ||
this.logger.setContext(ClickupConnectionService.name); | ||
this.registry.registerService('clickup', 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: 'clickup', | ||
}, | ||
}); | ||
|
||
//reconstruct the redirect URI that was passed in the githubend it must be the same | ||
//const REDIRECT_URI = `${this.env.getOAuthRredirectBaseUrl()}/connections/oauth/callback`; | ||
|
||
const formData = new URLSearchParams({ | ||
client_id: this.env.getClickupSecret().CLIENT_ID, | ||
client_secret: this.env.getClickupSecret().CLIENT_SECRET, | ||
code: code, | ||
}); | ||
const res = await axios.post( | ||
`https://api.clickup.com/api/v2/oauth/token`, | ||
formData.toString(), | ||
{ | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8', | ||
}, | ||
}, | ||
); | ||
const data: ClickupOAuthResponse = res.data; | ||
this.logger.log( | ||
'OAuth credentials : clickup ticketing ' + JSON.stringify(data), | ||
); | ||
|
||
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(data.access_token), | ||
refresh_token: '', | ||
expiration_timestamp: '', | ||
status: 'valid', | ||
created_at: new Date(), | ||
}, | ||
}); | ||
} else { | ||
db_res = await this.prisma.connections.create({ | ||
data: { | ||
id_connection: uuidv4(), | ||
connection_token: connection_token, | ||
provider_slug: 'clickup', | ||
token_type: 'oauth', | ||
access_token: this.cryptoService.encrypt(data.access_token), | ||
refresh_token: '', | ||
expiration_timestamp: '', | ||
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, 'clickup', Action.oauthCallback); | ||
} | ||
} | ||
|
||
async handleTokenRefresh(opts: RefreshParams) { | ||
return; | ||
} | ||
} |
Oops, something went wrong.