2023-06-12 08:42:59 +00:00
|
|
|
import { isEmpty, isNumber, merge } from "lodash";
|
|
|
|
|
import { BaseQueryGenerator } from "WidgetQueryGenerators/BaseQueryGenerator";
|
|
|
|
|
import { QUERY_TYPE } from "WidgetQueryGenerators/types";
|
|
|
|
|
import type {
|
|
|
|
|
WidgetQueryGenerationConfig,
|
|
|
|
|
WidgetQueryGenerationFormConfig,
|
|
|
|
|
GSheetsFormData,
|
|
|
|
|
ActionConfigurationGSheets,
|
|
|
|
|
} from "WidgetQueryGenerators/types";
|
2023-06-29 13:53:25 +00:00
|
|
|
import { removeSpecialChars } from "utils/helpers";
|
2023-07-20 06:22:20 +00:00
|
|
|
import { DatasourceConnectionMode } from "entities/Datasource";
|
2023-06-12 08:42:59 +00:00
|
|
|
|
|
|
|
|
enum COMMAND_TYPES {
|
|
|
|
|
"FIND" = "FETCH_MANY",
|
|
|
|
|
"INSERT" = "INSERT_ONE",
|
|
|
|
|
"UPDATE" = "UPDATE_ONE",
|
|
|
|
|
"COUNT" = "FETCH_MANY",
|
|
|
|
|
}
|
|
|
|
|
const COMMON_INITIAL_VALUE_KEYS = [
|
|
|
|
|
"smartSubstitution",
|
|
|
|
|
"entityType",
|
|
|
|
|
"queryFormat",
|
|
|
|
|
];
|
|
|
|
|
const SELECT_INITAL_VALUE_KEYS = [
|
|
|
|
|
"range",
|
|
|
|
|
"where",
|
|
|
|
|
"pagination",
|
|
|
|
|
"tableHeaderIndex",
|
|
|
|
|
"projection",
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
export default abstract class GSheets extends BaseQueryGenerator {
|
|
|
|
|
private static buildBasicConfig(
|
|
|
|
|
command: COMMAND_TYPES,
|
|
|
|
|
tableName: string,
|
|
|
|
|
sheetName?: string,
|
|
|
|
|
tableHeaderIndex?: number,
|
|
|
|
|
) {
|
|
|
|
|
return {
|
|
|
|
|
command: { data: command },
|
|
|
|
|
sheetUrl: { data: tableName },
|
|
|
|
|
sheetName: { data: sheetName },
|
|
|
|
|
tableHeaderIndex: {
|
|
|
|
|
data: isNumber(tableHeaderIndex)
|
|
|
|
|
? tableHeaderIndex.toString()
|
|
|
|
|
: undefined,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
private static buildFind(
|
|
|
|
|
widgetConfig: WidgetQueryGenerationConfig,
|
|
|
|
|
formConfig: WidgetQueryGenerationFormConfig,
|
|
|
|
|
) {
|
|
|
|
|
const { select } = widgetConfig;
|
|
|
|
|
|
2023-06-29 13:53:25 +00:00
|
|
|
if (select && formConfig.sheetName) {
|
2023-06-12 08:42:59 +00:00
|
|
|
return {
|
|
|
|
|
type: QUERY_TYPE.SELECT,
|
2023-06-29 13:53:25 +00:00
|
|
|
name: `Find_${removeSpecialChars(formConfig.sheetName)}`,
|
2023-06-12 08:42:59 +00:00
|
|
|
formData: {
|
|
|
|
|
where: {
|
|
|
|
|
data: {
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
condition: "CONTAINS",
|
|
|
|
|
key: `{{${select["where"]} ? "${formConfig.searchableColumn}" : ""}}`,
|
|
|
|
|
value: `{{${select["where"]}}}`,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
sortBy: {
|
|
|
|
|
data: [
|
|
|
|
|
{
|
|
|
|
|
column: `{{${select["orderBy"]}}}`,
|
|
|
|
|
order: select["sortOrder"],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
pagination: {
|
|
|
|
|
data: {
|
|
|
|
|
limit: `{{${select["limit"]}}}`,
|
|
|
|
|
offset: `{{${select["offset"]}}}`,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
...this.buildBasicConfig(
|
|
|
|
|
COMMAND_TYPES.FIND,
|
|
|
|
|
formConfig.tableName,
|
|
|
|
|
formConfig.sheetName,
|
|
|
|
|
formConfig.tableHeaderIndex,
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
dynamicBindingPathList: [
|
|
|
|
|
{
|
|
|
|
|
key: "formData.where.data",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
key: "formData.sortBy.data",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
key: "formData.pagination.data",
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static buildTotalRecord(
|
|
|
|
|
widgetConfig: WidgetQueryGenerationConfig,
|
|
|
|
|
formConfig: WidgetQueryGenerationFormConfig,
|
|
|
|
|
) {
|
|
|
|
|
const { select } = widgetConfig;
|
|
|
|
|
|
2023-06-29 13:53:25 +00:00
|
|
|
if (select && formConfig.sheetName) {
|
2023-06-12 08:42:59 +00:00
|
|
|
return {
|
|
|
|
|
type: QUERY_TYPE.TOTAL_RECORD,
|
2023-06-29 13:53:25 +00:00
|
|
|
name: `Total_record_${removeSpecialChars(formConfig.sheetName)}`,
|
2023-06-12 08:42:59 +00:00
|
|
|
formData: {
|
|
|
|
|
where: {
|
|
|
|
|
data: {
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
condition: "CONTAINS",
|
|
|
|
|
key: `{{${select["where"]} ? "${formConfig.searchableColumn}" : ""}}`,
|
|
|
|
|
value: `{{${select["where"]}}}`,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
...this.buildBasicConfig(
|
|
|
|
|
COMMAND_TYPES.COUNT,
|
|
|
|
|
formConfig.tableName,
|
|
|
|
|
formConfig.sheetName,
|
|
|
|
|
formConfig.tableHeaderIndex,
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
dynamicBindingPathList: [
|
|
|
|
|
{
|
|
|
|
|
key: "formData.where.data",
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static buildUpdate(
|
|
|
|
|
widgetConfig: WidgetQueryGenerationConfig,
|
|
|
|
|
formConfig: WidgetQueryGenerationFormConfig,
|
|
|
|
|
): Record<string, object | string> | undefined {
|
|
|
|
|
const { update } = widgetConfig;
|
|
|
|
|
|
2023-06-29 13:53:25 +00:00
|
|
|
if (update && formConfig.sheetName) {
|
2023-06-12 08:42:59 +00:00
|
|
|
return {
|
|
|
|
|
type: QUERY_TYPE.UPDATE,
|
2023-06-29 13:53:25 +00:00
|
|
|
name: `Update_${removeSpecialChars(formConfig.sheetName)}`,
|
2023-06-12 08:42:59 +00:00
|
|
|
formData: {
|
|
|
|
|
rowObjects: {
|
|
|
|
|
data: `{{${update.value}}}`,
|
|
|
|
|
},
|
|
|
|
|
...this.buildBasicConfig(
|
|
|
|
|
COMMAND_TYPES.UPDATE,
|
|
|
|
|
formConfig.tableName,
|
|
|
|
|
formConfig.sheetName,
|
|
|
|
|
formConfig.tableHeaderIndex,
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
dynamicBindingPathList: [
|
|
|
|
|
{
|
|
|
|
|
key: "formData.rowObjects.data",
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static buildInsert(
|
|
|
|
|
widgetConfig: WidgetQueryGenerationConfig,
|
|
|
|
|
formConfig: WidgetQueryGenerationFormConfig,
|
|
|
|
|
) {
|
|
|
|
|
const { create } = widgetConfig;
|
|
|
|
|
|
2023-06-29 13:53:25 +00:00
|
|
|
if (create && formConfig.sheetName) {
|
2023-06-12 08:42:59 +00:00
|
|
|
return {
|
|
|
|
|
type: QUERY_TYPE.CREATE,
|
2023-06-29 13:53:25 +00:00
|
|
|
name: `Insert_${removeSpecialChars(formConfig.sheetName)}`,
|
2023-06-12 08:42:59 +00:00
|
|
|
formData: {
|
|
|
|
|
rowObjects: {
|
|
|
|
|
data: `{{${create.value}}}`,
|
|
|
|
|
},
|
|
|
|
|
...this.buildBasicConfig(
|
|
|
|
|
COMMAND_TYPES.INSERT,
|
|
|
|
|
formConfig.tableName,
|
|
|
|
|
formConfig.sheetName,
|
|
|
|
|
formConfig.tableHeaderIndex,
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
dynamicBindingPathList: [
|
|
|
|
|
{
|
|
|
|
|
key: "formData.rowObjects.data",
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static createPayload(
|
|
|
|
|
initialValues: GSheetsFormData,
|
|
|
|
|
commandKey: string,
|
|
|
|
|
builtValues: Record<string, object | string> | undefined,
|
|
|
|
|
) {
|
|
|
|
|
if (!builtValues || isEmpty(builtValues)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!initialValues || isEmpty(initialValues)) {
|
|
|
|
|
return builtValues;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const allowedInitialValueKeys = [
|
|
|
|
|
...COMMON_INITIAL_VALUE_KEYS,
|
|
|
|
|
...(commandKey === "find" ? SELECT_INITAL_VALUE_KEYS : []),
|
|
|
|
|
];
|
|
|
|
|
const scrubedOutInitialValues = allowedInitialValueKeys
|
|
|
|
|
.filter((key) => initialValues[key as keyof GSheetsFormData])
|
|
|
|
|
.reduce((acc, key) => {
|
|
|
|
|
acc[key] = initialValues[key as keyof GSheetsFormData];
|
|
|
|
|
return acc;
|
|
|
|
|
}, {} as Record<string, object>);
|
|
|
|
|
|
|
|
|
|
const { formData, ...rest } = builtValues;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
payload: {
|
|
|
|
|
formData: merge({}, scrubedOutInitialValues, formData),
|
|
|
|
|
},
|
|
|
|
|
...rest,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static build(
|
|
|
|
|
widgetConfig: WidgetQueryGenerationConfig,
|
|
|
|
|
formConfig: WidgetQueryGenerationFormConfig,
|
|
|
|
|
pluginInitalValues: { actionConfiguration: ActionConfigurationGSheets },
|
|
|
|
|
) {
|
|
|
|
|
const configs = [];
|
|
|
|
|
|
|
|
|
|
const initialValues = pluginInitalValues?.actionConfiguration?.formData;
|
|
|
|
|
|
|
|
|
|
if (widgetConfig.select) {
|
|
|
|
|
configs.push(
|
|
|
|
|
this.createPayload(
|
|
|
|
|
initialValues,
|
|
|
|
|
"find",
|
|
|
|
|
this.buildFind(widgetConfig, formConfig),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-07-20 06:22:20 +00:00
|
|
|
if (
|
|
|
|
|
widgetConfig.update &&
|
|
|
|
|
formConfig.connectionMode === DatasourceConnectionMode.READ_WRITE
|
|
|
|
|
) {
|
2023-06-12 08:42:59 +00:00
|
|
|
configs.push(
|
|
|
|
|
this.createPayload(
|
|
|
|
|
initialValues,
|
|
|
|
|
"updateMany",
|
|
|
|
|
this.buildUpdate(widgetConfig, formConfig),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-07-20 06:22:20 +00:00
|
|
|
if (
|
|
|
|
|
widgetConfig.create &&
|
|
|
|
|
formConfig.connectionMode === DatasourceConnectionMode.READ_WRITE
|
|
|
|
|
) {
|
2023-06-12 08:42:59 +00:00
|
|
|
configs.push(
|
|
|
|
|
this.createPayload(
|
|
|
|
|
initialValues,
|
|
|
|
|
"insert",
|
|
|
|
|
this.buildInsert(widgetConfig, formConfig),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (widgetConfig.totalRecord) {
|
|
|
|
|
configs.push(
|
|
|
|
|
this.createPayload(
|
|
|
|
|
initialValues,
|
|
|
|
|
"count",
|
|
|
|
|
this.buildTotalRecord(widgetConfig, formConfig),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return configs.filter((val) => !!val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getTotalRecordExpression(binding: string) {
|
|
|
|
|
return `${binding}.length`;
|
|
|
|
|
}
|
|
|
|
|
}
|