271 lines
6.6 KiB
TypeScript
271 lines
6.6 KiB
TypeScript
import {
|
|
IAction,
|
|
IActionResult,
|
|
IActionNode,
|
|
IActionProperty,
|
|
IContext,
|
|
} from "../types/ActionTypes";
|
|
import { actionAddins } from ".";
|
|
import { KintoneRestAPIClient } from "@kintone/rest-api-client";
|
|
|
|
interface Props {
|
|
displayName: string;
|
|
sources: Sources;
|
|
dataMapping: DataMapping;
|
|
}
|
|
|
|
interface DataMapping {
|
|
data: Mapping[];
|
|
createWithNull: boolean;
|
|
}
|
|
|
|
interface Mapping {
|
|
id: string;
|
|
from: From;
|
|
to: To;
|
|
isKey: boolean;
|
|
}
|
|
|
|
interface To {
|
|
app: App;
|
|
fields: {
|
|
name: string;
|
|
type: string;
|
|
code: string;
|
|
label: string;
|
|
noLabel: boolean;
|
|
required: boolean;
|
|
minLength: string;
|
|
maxLength: string;
|
|
expression: string;
|
|
hideExpression: boolean;
|
|
unique: boolean;
|
|
defaultValue: string;
|
|
}[];
|
|
isDialogVisible: boolean;
|
|
}
|
|
|
|
interface From {
|
|
sharedText: string;
|
|
_t: string;
|
|
id: string;
|
|
objectType: string;
|
|
name: {
|
|
name: string;
|
|
};
|
|
actionName: string;
|
|
displayName: string;
|
|
}
|
|
|
|
interface Sources {
|
|
app: App;
|
|
}
|
|
|
|
interface App {
|
|
id: string;
|
|
name: string;
|
|
description: string;
|
|
createdate: string;
|
|
}
|
|
|
|
export class DataMappingAction implements IAction {
|
|
name: string;
|
|
actionProps: IActionProperty[];
|
|
dataMappingProps: Props;
|
|
constructor() {
|
|
this.name = "データマッピング";
|
|
this.actionProps = [];
|
|
this.dataMappingProps = {} as Props;
|
|
this.register();
|
|
}
|
|
|
|
async process(
|
|
prop: IActionNode,
|
|
event: any,
|
|
context: IContext
|
|
): Promise<IActionResult> {
|
|
this.actionProps = prop.actionProps;
|
|
this.dataMappingProps = prop.ActionValue as Props;
|
|
console.log(prop.ActionValue);
|
|
let result = {
|
|
canNext: true,
|
|
result: "",
|
|
} as IActionResult;
|
|
try {
|
|
// createWithNull が有効な場合は、4 番目のパラメーターを true にして doUpdate 関数ブランチを実行します。
|
|
if (this.dataMappingProps.dataMapping.createWithNull === true) {
|
|
await doUpdate(
|
|
this.dataMappingProps.dataMapping.data,
|
|
this.dataMappingProps.sources.app.id,
|
|
context,
|
|
true // キーがない場合、またはキーでターゲットが見つからない場合に、マッピング条件によって新しいレコードを作成するかどうかを決定するために使用されます。
|
|
);
|
|
} else if (
|
|
// キーがないと更新対象を取得できないため、この時点でのみ更新が行われます。 doUpdate 関数の 4 番目のパラメーターは false です。
|
|
this.dataMappingProps.dataMapping.data
|
|
.map((m) => m.isKey)
|
|
.find((isKey) => isKey === true)
|
|
) {
|
|
await doUpdate(
|
|
this.dataMappingProps.dataMapping.data,
|
|
this.dataMappingProps.sources.app.id,
|
|
context,
|
|
false
|
|
);
|
|
} else {
|
|
await doCreate(
|
|
this.dataMappingProps.dataMapping.data,
|
|
this.dataMappingProps.sources.app.id,
|
|
context
|
|
);
|
|
}
|
|
} catch (error) {
|
|
console.error("DataMappingAction error", error);
|
|
result.canNext = false;
|
|
}
|
|
console.log("dataMappingProps", this.dataMappingProps);
|
|
|
|
return result;
|
|
}
|
|
|
|
register(): void {
|
|
actionAddins[this.name] = this;
|
|
}
|
|
}
|
|
|
|
new DataMappingAction();
|
|
|
|
const getContextVarByPath = (obj: any, path: string) => {
|
|
return path.split(".").reduce((o, k) => (o || {})[k], obj);
|
|
};
|
|
|
|
interface UpdateRecord {
|
|
id: string;
|
|
record: {
|
|
[key: string]: {
|
|
value: any;
|
|
};
|
|
};
|
|
}
|
|
|
|
const client = new KintoneRestAPIClient();
|
|
|
|
const doUpdate = async (
|
|
mappingData: Mapping[],
|
|
appId: string,
|
|
context: any,
|
|
needCreate: boolean
|
|
) => {
|
|
const targetField = await findUpdateField(mappingData, appId, context);
|
|
console.log(targetField);
|
|
if (targetField.records.length === 0 && needCreate) {
|
|
await doCreate(mappingData, appId, context);
|
|
} else {
|
|
// マッピングデータを単純なオブジェクトに処理し、ソース値が変数の場合は変数を置き換えます。
|
|
const mappingRules = mappingData
|
|
.filter((m) => Object.keys(m.from).length > 0)
|
|
.map((m) => {
|
|
if (m.from.objectType === "variable") {
|
|
return {
|
|
value: getContextVarByPath(context.variables, m.from.name.name),
|
|
code: m.to.fields[0].code,
|
|
};
|
|
} else {
|
|
return {
|
|
value: m.from.sharedText,
|
|
code: m.to.fields[0].code,
|
|
};
|
|
}
|
|
});
|
|
|
|
const updateRecords: UpdateRecord[] = targetField.records.map(
|
|
(targetRecord) => {
|
|
const updateRecord: UpdateRecord["record"] = {};
|
|
|
|
// マッピング内のルールにヒットしたフィールドのみが更新されます。
|
|
for (const mapping of mappingRules) {
|
|
if (targetRecord[mapping.code]) {
|
|
updateRecord[mapping.code] = {
|
|
value: mapping.value,
|
|
};
|
|
}
|
|
}
|
|
|
|
return {
|
|
id: targetRecord.$id.value as string,
|
|
record: updateRecord,
|
|
};
|
|
}
|
|
);
|
|
|
|
console.log(updateRecords);
|
|
|
|
await client.record.updateRecords({
|
|
app: appId,
|
|
records: updateRecords,
|
|
});
|
|
}
|
|
};
|
|
|
|
const findUpdateField = async (
|
|
mappingData: Mapping[],
|
|
appId: string,
|
|
context: any
|
|
) => {
|
|
const queryStr = mappingData
|
|
.filter((m) => m.to.app && m.to.fields && m.to.fields.length > 0 && m.isKey)
|
|
.map((m) => {
|
|
if (m.from.objectType === "variable") {
|
|
return `${m.to.fields[0].code} = "${getContextVarByPath(
|
|
context.variables,
|
|
m.from.name.name
|
|
)}"`;
|
|
} else {
|
|
return `${m.to.fields[0].code}=${m.from.sharedText}`;
|
|
}
|
|
})
|
|
.join("&");
|
|
// 検索条件が空の場合は全レコードを返すため、検索対象が見つからない場合は検索は行われません。
|
|
if (queryStr.length === 0) {
|
|
return {
|
|
records: [],
|
|
};
|
|
} else {
|
|
return await client.record.getRecords({
|
|
app: appId,
|
|
// query: undefined
|
|
query: queryStr,
|
|
});
|
|
}
|
|
};
|
|
|
|
const doCreate = async (
|
|
mappingData: Mapping[],
|
|
appId: string,
|
|
context: any
|
|
) => {
|
|
const record = mappingData
|
|
.filter(
|
|
(item) =>
|
|
item.from.objectType === "variable" &&
|
|
item.from.name.name &&
|
|
item.to.app &&
|
|
item.to.fields &&
|
|
item.to.fields.length > 0
|
|
)
|
|
.reduce((accumulator, item) => {
|
|
return {
|
|
...accumulator,
|
|
[item.to.fields[0].code]: {
|
|
value: getContextVarByPath(context.variables, item.from.name.name),
|
|
},
|
|
};
|
|
}, {});
|
|
if (record && Object.keys(record).length > 0) {
|
|
await kintone.api(kintone.api.url("/k/v1/record.json", true), "POST", {
|
|
app: appId,
|
|
record: record,
|
|
});
|
|
}
|
|
};
|