feat(xero): add xero integration

This commit is contained in:
Rıdvan Akca
2023-11-08 14:46:44 +03:00
parent 373d29eeab
commit 9200e1011b
13 changed files with 242 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
import { IField, IGlobalVariable } from '@automatisch/types';
import getCurrentUser from '../common/get-current-user';
import { URLSearchParams } from 'url';
const verifyCredentials = async ($: IGlobalVariable) => {
const oauthRedirectUrlField = $.app.auth.fields.find(
(field: IField) => field.key == 'oAuthRedirectUrl'
);
const redirectUri = oauthRedirectUrlField.value as string;
const headers = {
Authorization: `Basic ${Buffer.from(
$.auth.data.clientId + ':' + $.auth.data.clientSecret
).toString('base64')}`,
'Content-Type': 'application/x-www-form-urlencoded',
};
const params = new URLSearchParams({
grant_type: 'authorization_code',
code: $.auth.data.code as string,
redirect_uri: redirectUri,
});
const { data } = await $.http.post(
'https://identity.xero.com/connect/token',
params.toString(),
{
headers,
}
);
await $.auth.set({
accessToken: data.access_token,
tokenType: data.token_type,
idToken: data.id_token,
});
const currentUser = await getCurrentUser($);
const screenName = [currentUser.tenantName, currentUser.tenantType]
.filter(Boolean)
.join(' @ ');
await $.auth.set({
clientId: $.auth.data.clientId,
clientSecret: $.auth.data.clientSecret,
scope: $.auth.data.scope,
expiresIn: data.expires_in,
refreshToken: data.refresh_token,
tenantId: currentUser.tenantId,
screenName,
});
};
export default verifyCredentials;