2022-03-10 06:59:48 +00:00
|
|
|
import { QueryError, QueryResult, QueryService, OAuthUnauthorizedClientError } from '@tooljet-plugins/common';
|
|
|
|
|
import { readData, appendData, deleteData, batchUpdateToSheet } from './operations';
|
|
|
|
|
import got, { Headers } from 'got';
|
|
|
|
|
import { SourceOptions, QueryOptions } from './types';
|
2021-07-24 12:06:56 +00:00
|
|
|
|
|
|
|
|
export default class GooglesheetsQueryService implements QueryService {
|
|
|
|
|
authUrl(): string {
|
2023-11-01 09:39:41 +00:00
|
|
|
const host = process.env.TOOLJET_HOST;
|
|
|
|
|
const subpath = process.env.SUB_PATH;
|
|
|
|
|
const fullUrl = `${host}${subpath ? subpath : '/'}`;
|
2022-03-20 02:43:58 +00:00
|
|
|
const clientId = process.env.GOOGLE_CLIENT_ID;
|
|
|
|
|
const clientSecret = process.env.GOOGLE_CLIENT_SECRET;
|
2022-03-21 04:45:31 +00:00
|
|
|
|
|
|
|
|
if (!clientId || !clientSecret) {
|
2022-03-20 02:43:58 +00:00
|
|
|
throw Error('You need to define Google OAuth environment variables');
|
|
|
|
|
}
|
2022-03-21 04:45:31 +00:00
|
|
|
|
2022-02-05 01:39:40 +00:00
|
|
|
return (
|
2022-03-10 06:59:48 +00:00
|
|
|
'https://accounts.google.com/o/oauth2/v2/auth' +
|
2022-02-05 01:39:40 +00:00
|
|
|
`?response_type=code&client_id=${clientId}` +
|
2023-11-01 09:39:41 +00:00
|
|
|
`&redirect_uri=${fullUrl}oauth2/authorize`
|
2022-02-05 01:39:40 +00:00
|
|
|
);
|
2021-07-24 12:06:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async accessDetailsFrom(authCode: string): Promise<object> {
|
2022-03-10 06:59:48 +00:00
|
|
|
const accessTokenUrl = 'https://oauth2.googleapis.com/token';
|
2021-07-24 12:06:56 +00:00
|
|
|
const clientId = process.env.GOOGLE_CLIENT_ID;
|
|
|
|
|
const clientSecret = process.env.GOOGLE_CLIENT_SECRET;
|
2023-11-01 09:39:41 +00:00
|
|
|
const host = process.env.TOOLJET_HOST;
|
|
|
|
|
const subpath = process.env.SUB_PATH;
|
|
|
|
|
const fullUrl = `${host}${subpath ? subpath : '/'}`;
|
|
|
|
|
const redirectUri = `${fullUrl}oauth2/authorize`;
|
2022-03-10 06:59:48 +00:00
|
|
|
const grantType = 'authorization_code';
|
|
|
|
|
const customParams = { prompt: 'consent', access_type: 'offline' };
|
2021-07-24 12:06:56 +00:00
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
const data = {
|
|
|
|
|
code: authCode,
|
2021-07-24 12:06:56 +00:00
|
|
|
client_id: clientId,
|
|
|
|
|
client_secret: clientSecret,
|
|
|
|
|
grant_type: grantType,
|
|
|
|
|
redirect_uri: redirectUri,
|
2021-09-21 13:48:28 +00:00
|
|
|
...customParams,
|
|
|
|
|
};
|
2021-07-24 12:06:56 +00:00
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
const authDetails = [];
|
2021-07-24 12:06:56 +00:00
|
|
|
|
|
|
|
|
try {
|
2021-08-03 13:32:11 +00:00
|
|
|
const response = await got(accessTokenUrl, {
|
2022-03-10 06:59:48 +00:00
|
|
|
method: 'post',
|
2021-07-24 12:06:56 +00:00
|
|
|
json: data,
|
2022-03-10 06:59:48 +00:00
|
|
|
headers: { 'Content-Type': 'application/json' },
|
2021-07-24 12:06:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const result = JSON.parse(response.body);
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
if (response.statusCode !== 200) {
|
2022-03-10 06:59:48 +00:00
|
|
|
throw Error('could not connect to Googlesheets');
|
2021-07-24 12:06:56 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-10 06:59:48 +00:00
|
|
|
if (result['access_token']) {
|
|
|
|
|
authDetails.push(['access_token', result['access_token']]);
|
2021-07-24 12:06:56 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-10 06:59:48 +00:00
|
|
|
if (result['refresh_token']) {
|
|
|
|
|
authDetails.push(['refresh_token', result['refresh_token']]);
|
2021-07-24 12:06:56 +00:00
|
|
|
}
|
2021-09-21 13:48:28 +00:00
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error.response.body);
|
2022-03-10 06:59:48 +00:00
|
|
|
throw Error('could not connect to Googlesheets');
|
2021-09-21 13:48:28 +00:00
|
|
|
}
|
2022-02-05 01:39:40 +00:00
|
|
|
|
2021-07-24 12:06:56 +00:00
|
|
|
return authDetails;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-17 07:08:17 +00:00
|
|
|
authHeader(token: string): Headers {
|
2022-02-05 01:39:40 +00:00
|
|
|
return {
|
|
|
|
|
Authorization: `Bearer ${token}`,
|
2022-03-10 06:59:48 +00:00
|
|
|
'Content-Type': 'application/json',
|
2022-02-05 01:39:40 +00:00
|
|
|
};
|
2021-07-24 12:06:56 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-10 06:59:48 +00:00
|
|
|
async run(sourceOptions: SourceOptions, queryOptions: QueryOptions, dataSourceId: string): Promise<QueryResult> {
|
2021-09-21 13:48:28 +00:00
|
|
|
let result = {};
|
2021-07-24 12:06:56 +00:00
|
|
|
let response = null;
|
|
|
|
|
const operation = queryOptions.operation;
|
2022-01-24 13:59:21 +00:00
|
|
|
const spreadsheetId = queryOptions.spreadsheet_id;
|
2022-03-10 06:59:48 +00:00
|
|
|
const spreadsheetRange = queryOptions.spreadsheet_range ? queryOptions.spreadsheet_range : 'A1:Z500';
|
|
|
|
|
const accessToken = sourceOptions['access_token'];
|
2022-02-05 01:39:40 +00:00
|
|
|
const queryOptionFilter = {
|
|
|
|
|
key: queryOptions.where_field,
|
|
|
|
|
value: queryOptions.where_value,
|
|
|
|
|
};
|
2021-07-24 12:06:56 +00:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
switch (operation) {
|
2022-03-10 06:59:48 +00:00
|
|
|
case 'info':
|
|
|
|
|
response = await got(`https://sheets.googleapis.com/v4/spreadsheets/${spreadsheetId}`, {
|
|
|
|
|
method: 'get',
|
|
|
|
|
headers: this.authHeader(accessToken),
|
|
|
|
|
});
|
2021-08-03 13:32:11 +00:00
|
|
|
|
2021-07-24 12:06:56 +00:00
|
|
|
result = JSON.parse(response.body);
|
|
|
|
|
break;
|
2021-08-03 13:32:11 +00:00
|
|
|
|
2022-03-10 06:59:48 +00:00
|
|
|
case 'read':
|
|
|
|
|
result = await readData(spreadsheetId, spreadsheetRange, queryOptions.sheet, this.authHeader(accessToken));
|
2021-08-03 13:32:11 +00:00
|
|
|
break;
|
|
|
|
|
|
2022-03-10 06:59:48 +00:00
|
|
|
case 'append':
|
|
|
|
|
result = await appendData(spreadsheetId, queryOptions.sheet, queryOptions.rows, this.authHeader(accessToken));
|
2021-08-03 13:32:11 +00:00
|
|
|
break;
|
|
|
|
|
|
2022-03-10 06:59:48 +00:00
|
|
|
case 'update':
|
2021-12-02 06:55:40 +00:00
|
|
|
result = await batchUpdateToSheet(
|
|
|
|
|
spreadsheetId,
|
2022-03-07 08:48:30 +00:00
|
|
|
spreadsheetRange,
|
2022-02-16 18:42:20 +00:00
|
|
|
queryOptions.sheet,
|
2022-01-24 13:59:21 +00:00
|
|
|
queryOptions.body,
|
2021-12-02 06:55:40 +00:00
|
|
|
queryOptionFilter,
|
2022-01-24 13:59:21 +00:00
|
|
|
queryOptions.where_operation,
|
2021-12-02 06:55:40 +00:00
|
|
|
this.authHeader(accessToken)
|
|
|
|
|
);
|
|
|
|
|
break;
|
|
|
|
|
|
2022-03-10 06:59:48 +00:00
|
|
|
case 'delete_row':
|
2021-08-03 13:32:11 +00:00
|
|
|
result = await deleteData(
|
|
|
|
|
spreadsheetId,
|
2022-01-24 13:59:21 +00:00
|
|
|
queryOptions.sheet,
|
|
|
|
|
queryOptions.row_index,
|
2021-09-21 13:48:28 +00:00
|
|
|
this.authHeader(accessToken)
|
2021-08-03 13:32:11 +00:00
|
|
|
);
|
|
|
|
|
break;
|
2021-07-24 12:06:56 +00:00
|
|
|
}
|
|
|
|
|
} catch (error) {
|
2022-10-19 14:34:57 +00:00
|
|
|
console.error({ statusCode: error?.response?.statusCode, message: error?.response?.body });
|
2022-02-05 01:39:40 +00:00
|
|
|
|
2022-05-11 05:22:55 +00:00
|
|
|
if (error?.response?.statusCode === 401) {
|
2022-03-10 06:59:48 +00:00
|
|
|
throw new OAuthUnauthorizedClientError('Query could not be completed', error.message, { ...error });
|
2022-02-05 01:39:40 +00:00
|
|
|
}
|
2022-03-10 06:59:48 +00:00
|
|
|
throw new QueryError('Query could not be completed', error.message, {});
|
2021-07-24 12:06:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
2022-03-10 06:59:48 +00:00
|
|
|
status: 'ok',
|
2021-09-21 13:48:28 +00:00
|
|
|
data: result,
|
|
|
|
|
};
|
2021-07-24 12:06:56 +00:00
|
|
|
}
|
2022-02-05 01:39:40 +00:00
|
|
|
|
2022-10-19 14:34:57 +00:00
|
|
|
async refreshToken(sourceOptions) {
|
2022-03-10 06:59:48 +00:00
|
|
|
if (!sourceOptions['refresh_token']) {
|
2022-10-19 14:34:57 +00:00
|
|
|
throw new QueryError('Query could not be completed', 'Refresh token empty', {});
|
2022-02-05 01:39:40 +00:00
|
|
|
}
|
2022-03-10 06:59:48 +00:00
|
|
|
const accessTokenUrl = 'https://oauth2.googleapis.com/token';
|
2022-02-05 01:39:40 +00:00
|
|
|
const clientId = process.env.GOOGLE_CLIENT_ID;
|
|
|
|
|
const clientSecret = process.env.GOOGLE_CLIENT_SECRET;
|
2022-03-10 06:59:48 +00:00
|
|
|
const grantType = 'refresh_token';
|
2022-02-05 01:39:40 +00:00
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
|
client_id: clientId,
|
|
|
|
|
client_secret: clientSecret,
|
|
|
|
|
grant_type: grantType,
|
2022-03-10 06:59:48 +00:00
|
|
|
refresh_token: sourceOptions['refresh_token'],
|
2022-02-05 01:39:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const accessTokenDetails = {};
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await got(accessTokenUrl, {
|
2022-03-10 06:59:48 +00:00
|
|
|
method: 'post',
|
2022-02-05 01:39:40 +00:00
|
|
|
json: data,
|
2022-03-10 06:59:48 +00:00
|
|
|
headers: { 'Content-Type': 'application/json' },
|
2022-02-05 01:39:40 +00:00
|
|
|
});
|
|
|
|
|
const result = JSON.parse(response.body);
|
|
|
|
|
|
|
|
|
|
if (!(response.statusCode >= 200 || response.statusCode < 300)) {
|
2022-10-19 14:34:57 +00:00
|
|
|
throw new QueryError(
|
|
|
|
|
'could not connect to Googlesheets',
|
|
|
|
|
JSON.stringify({ statusCode: response?.statusCode, message: response?.body }),
|
|
|
|
|
{}
|
|
|
|
|
);
|
2022-02-05 01:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-10 06:59:48 +00:00
|
|
|
if (result['access_token']) {
|
|
|
|
|
accessTokenDetails['access_token'] = result['access_token'];
|
2022-10-19 14:34:57 +00:00
|
|
|
accessTokenDetails['refresh_token'] = result['refresh_token'];
|
|
|
|
|
} else {
|
|
|
|
|
throw new QueryError(
|
|
|
|
|
'access_token not found in the response',
|
|
|
|
|
{},
|
|
|
|
|
{
|
|
|
|
|
responseObject: {
|
|
|
|
|
statusCode: response.statusCode,
|
|
|
|
|
responseBody: response.body,
|
|
|
|
|
},
|
|
|
|
|
responseHeaders: response.headers,
|
|
|
|
|
}
|
|
|
|
|
);
|
2022-02-05 01:39:40 +00:00
|
|
|
}
|
|
|
|
|
} catch (error) {
|
2022-10-19 14:34:57 +00:00
|
|
|
console.error(
|
|
|
|
|
`Error while REST API refresh token call. Status code : ${error.response?.statusCode}, Message : ${error.response?.body}`
|
|
|
|
|
);
|
|
|
|
|
throw new QueryError(
|
|
|
|
|
'could not connect to Googlesheets',
|
|
|
|
|
JSON.stringify({ statusCode: error.response?.statusCode, message: error.response?.body }),
|
|
|
|
|
{}
|
|
|
|
|
);
|
2022-02-05 01:39:40 +00:00
|
|
|
}
|
|
|
|
|
return accessTokenDetails;
|
|
|
|
|
}
|
2021-07-24 12:06:56 +00:00
|
|
|
}
|