2023-03-09 09:13:15 -08:00
|
|
|
import type {
|
|
|
|
IExecuteFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IDataObject,
|
|
|
|
JsonObject,
|
2024-02-14 07:29:09 -08:00
|
|
|
IHttpRequestMethods,
|
|
|
|
IRequestOptions,
|
2023-03-09 09:13:15 -08:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2020-11-18 22:47:26 -08:00
|
|
|
|
2022-04-08 14:32:08 -07:00
|
|
|
import moment from 'moment-timezone';
|
2024-07-08 04:40:52 -07:00
|
|
|
import { getGoogleAccessToken } from '../../GenericFunctions';
|
2021-05-12 12:21:56 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function googleApiRequest(
|
2023-08-16 06:52:41 -07:00
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: string,
|
|
|
|
body: any = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
uri: string | null = null,
|
|
|
|
): Promise<any> {
|
2024-02-14 07:29:09 -08:00
|
|
|
const options: IRequestOptions = {
|
2020-11-18 22:47:26 -08:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
qsStringifyOptions: {
|
|
|
|
arrayFormat: 'repeat',
|
|
|
|
},
|
2023-01-19 04:37:19 -08:00
|
|
|
uri: uri || `https://firestore.googleapis.com/v1/projects${resource}`,
|
2020-11-18 22:47:26 -08:00
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
try {
|
2023-02-27 19:39:43 -08:00
|
|
|
if (Object.keys(body as IDataObject).length === 0) {
|
2020-11-18 22:47:26 -08:00
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
2024-07-08 04:40:52 -07:00
|
|
|
let credentialType = 'googleFirebaseCloudFirestoreOAuth2Api';
|
|
|
|
const authentication = this.getNodeParameter('authentication', 0) as string;
|
|
|
|
|
|
|
|
if (authentication === 'serviceAccount') {
|
|
|
|
const credentials = await this.getCredentials('googleApi');
|
|
|
|
credentialType = 'googleApi';
|
|
|
|
|
|
|
|
const { access_token } = await getGoogleAccessToken.call(this, credentials, 'firestore');
|
|
|
|
|
|
|
|
(options.headers as IDataObject).Authorization = `Bearer ${access_token}`;
|
|
|
|
}
|
|
|
|
|
2020-11-18 22:47:26 -08:00
|
|
|
//@ts-ignore
|
2024-07-08 04:40:52 -07:00
|
|
|
return await this.helpers.requestWithAuthentication.call(this, credentialType, options);
|
2020-11-18 22:47:26 -08:00
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2020-11-18 22:47:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function googleApiRequestAllItems(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
propertyName: string,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-08-17 08:50:24 -07:00
|
|
|
endpoint: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
uri: string | null = null,
|
|
|
|
): Promise<any> {
|
2020-11-18 22:47:26 -08:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
query.pageSize = 100;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, query, uri);
|
2022-12-02 12:54:28 -08:00
|
|
|
query.pageToken = responseData.nextPageToken;
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, responseData[propertyName] as IDataObject[]);
|
2022-12-02 12:54:28 -08:00
|
|
|
} while (responseData.nextPageToken !== undefined && responseData.nextPageToken !== '');
|
2020-11-18 22:47:26 -08:00
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const isValidDate = (str: string) =>
|
|
|
|
moment(str, ['YYYY-MM-DD HH:mm:ss Z', moment.ISO_8601], true).isValid();
|
2020-11-18 22:47:26 -08:00
|
|
|
|
|
|
|
// Both functions below were taken from Stack Overflow jsonToDocument was fixed as it was unable to handle null values correctly
|
|
|
|
// https://stackoverflow.com/questions/62246410/how-to-convert-a-firestore-document-to-plain-json-and-vice-versa
|
|
|
|
// Great thanks to https://stackoverflow.com/users/3915246/mahindar
|
|
|
|
export function jsonToDocument(value: string | number | IDataObject | IDataObject[]): IDataObject {
|
|
|
|
if (value === 'true' || value === 'false' || typeof value === 'boolean') {
|
2022-08-17 08:50:24 -07:00
|
|
|
return { booleanValue: value };
|
2020-11-18 22:47:26 -08:00
|
|
|
} else if (value === null) {
|
2022-08-17 08:50:24 -07:00
|
|
|
return { nullValue: null };
|
2023-09-12 02:03:33 -07:00
|
|
|
} else if (value !== '' && !isNaN(value as number)) {
|
2020-11-18 22:47:26 -08:00
|
|
|
if (value.toString().indexOf('.') !== -1) {
|
2022-08-17 08:50:24 -07:00
|
|
|
return { doubleValue: value };
|
2020-11-18 22:47:26 -08:00
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
return { integerValue: value };
|
2020-11-18 22:47:26 -08:00
|
|
|
}
|
2021-05-12 12:21:56 -07:00
|
|
|
} else if (isValidDate(value as string)) {
|
2020-11-18 22:47:26 -08:00
|
|
|
const date = new Date(Date.parse(value as string));
|
2022-08-17 08:50:24 -07:00
|
|
|
return { timestampValue: date.toISOString() };
|
2020-11-18 22:47:26 -08:00
|
|
|
} else if (typeof value === 'string') {
|
2022-08-17 08:50:24 -07:00
|
|
|
return { stringValue: value };
|
2020-11-18 22:47:26 -08:00
|
|
|
} else if (value && value.constructor === Array) {
|
2022-08-17 08:50:24 -07:00
|
|
|
return { arrayValue: { values: value.map((v) => jsonToDocument(v)) } };
|
2020-11-18 22:47:26 -08:00
|
|
|
} else if (typeof value === 'object') {
|
|
|
|
const obj = {};
|
|
|
|
for (const o of Object.keys(value)) {
|
|
|
|
//@ts-ignore
|
2023-02-27 19:39:43 -08:00
|
|
|
obj[o] = jsonToDocument(value[o] as IDataObject);
|
2020-11-18 22:47:26 -08:00
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
return { mapValue: { fields: obj } };
|
2020-11-18 22:47:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function documentToJson(fields: IDataObject): IDataObject {
|
2021-05-12 12:21:56 -07:00
|
|
|
if (fields === undefined) return {};
|
2020-11-18 22:47:26 -08:00
|
|
|
const result = {};
|
|
|
|
for (const f of Object.keys(fields)) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const key = f,
|
|
|
|
value = fields[f],
|
|
|
|
isDocumentType = [
|
|
|
|
'stringValue',
|
|
|
|
'booleanValue',
|
|
|
|
'doubleValue',
|
|
|
|
'integerValue',
|
|
|
|
'timestampValue',
|
|
|
|
'mapValue',
|
|
|
|
'arrayValue',
|
|
|
|
'nullValue',
|
|
|
|
'geoPointValue',
|
|
|
|
].find((t) => t === key);
|
2020-11-18 22:47:26 -08:00
|
|
|
if (isDocumentType) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const item = [
|
|
|
|
'stringValue',
|
|
|
|
'booleanValue',
|
|
|
|
'doubleValue',
|
|
|
|
'integerValue',
|
|
|
|
'timestampValue',
|
|
|
|
'nullValue',
|
|
|
|
'geoPointValue',
|
|
|
|
].find((t) => t === key);
|
2020-11-18 22:47:26 -08:00
|
|
|
if (item) {
|
|
|
|
return value as IDataObject;
|
|
|
|
} else if ('mapValue' === key) {
|
|
|
|
//@ts-ignore
|
2023-02-27 19:39:43 -08:00
|
|
|
return documentToJson((value!.fields as IDataObject) || {});
|
2020-11-18 22:47:26 -08:00
|
|
|
} else if ('arrayValue' === key) {
|
|
|
|
// @ts-ignore
|
|
|
|
const list = value.values as IDataObject[];
|
|
|
|
// @ts-ignore
|
2022-08-17 08:50:24 -07:00
|
|
|
return !!list ? list.map((l) => documentToJson(l)) : [];
|
2020-11-18 22:47:26 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// @ts-ignore
|
|
|
|
result[key] = documentToJson(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2023-01-13 09:11:56 -08:00
|
|
|
|
|
|
|
export function fullDocumentToJson(data: IDataObject): IDataObject {
|
|
|
|
if (data === undefined) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
_name: data.name,
|
|
|
|
_id: data.id,
|
|
|
|
_createTime: data.createTime,
|
|
|
|
_updateTime: data.updateTime,
|
|
|
|
...documentToJson(data.fields as IDataObject),
|
|
|
|
};
|
|
|
|
}
|