From 85866366f9a4173bc89082e0930b78521d1f5c92 Mon Sep 17 00:00:00 2001 From: Gaurav Oberoi Date: Mon, 10 Jun 2024 11:50:42 +0530 Subject: [PATCH] feat: added sage accounting oauth endpoints --- .../src/connections/sageAccounting/init.ts | 40 +++++++++++++++++ .../src/connections/sageAccounting/refresh.ts | 45 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 integrationos-platform-oauth/src/connections/sageAccounting/init.ts create mode 100644 integrationos-platform-oauth/src/connections/sageAccounting/refresh.ts diff --git a/integrationos-platform-oauth/src/connections/sageAccounting/init.ts b/integrationos-platform-oauth/src/connections/sageAccounting/init.ts new file mode 100644 index 00000000..f048a0eb --- /dev/null +++ b/integrationos-platform-oauth/src/connections/sageAccounting/init.ts @@ -0,0 +1,40 @@ +import axios from "axios"; +import { DataObject, OAuthResponse } from "../../lib/types"; + +export const init = async ({ body }: DataObject): Promise => { + try { + const requestBody = { + grant_type: "authorization_code", + code: body.metadata?.code, + client_id: body.clientId, + client_secret: body.clientSecret, + redirect_uri: body.metadata?.redirectUri, + }; + + const response = await axios.post( + `https://oauth.accounting.sage.com/token`, + requestBody + ); + + const { + data: { + access_token: accessToken, + refresh_token: refreshToken, + expires_in: expiresIn, + requested_by_id: requestedById, + }, + } = response; + + return { + accessToken, + refreshToken, + expiresIn, + tokenType: '', + meta: { + requestedById, + }, + }; + } catch (error) { + throw new Error(`Error fetching access token for SageAccounting: ${error}`); + } +}; diff --git a/integrationos-platform-oauth/src/connections/sageAccounting/refresh.ts b/integrationos-platform-oauth/src/connections/sageAccounting/refresh.ts new file mode 100644 index 00000000..dcc12935 --- /dev/null +++ b/integrationos-platform-oauth/src/connections/sageAccounting/refresh.ts @@ -0,0 +1,45 @@ +import axios from "axios"; + +import { DataObject, OAuthResponse } from "../../lib/types"; +export const refresh = async ({ body }: DataObject): Promise => { + try { + const { + OAUTH_CLIENT_ID: client_id, + OAUTH_CLIENT_SECRET: client_secret, + OAUTH_REFRESH_TOKEN: refresh_token, + } = body; + + const requestBody = { + grant_type: "refresh_token", + client_id, + client_secret, + refresh_token, + }; + + const response = await axios.post( + `https://oauth.accounting.sage.com/token`, + requestBody + ); + + const { + data: { + access_token: accessToken, + refresh_token: refreshToken, + expires_in: expiresIn, + requested_by_id: requestedById, + }, + } = response; + + return { + accessToken, + refreshToken, + expiresIn, + tokenType: "", + meta: { + requestedById, + }, + }; + } catch (error) { + throw new Error(`Error fetching access token for SageAccounting: ${error}`); + } +};