From 5ca16d7b45e7403937cd9453966877d2d1735b1f Mon Sep 17 00:00:00 2001 From: Mouriya Date: Mon, 1 Jul 2024 16:20:58 +0900 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8B=95=E6=A4=9C=E7=B4=A2=E3=83=97?= =?UTF-8?q?=E3=83=A9=E3=82=B0=E3=82=A4=E3=83=B3=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kintone-addins/src/actions/auto-lookup.ts | 145 ++++++++++++++++++ .../src/types/action-process.ts | 1 + 2 files changed, 146 insertions(+) create mode 100644 plugin/kintone-addins/src/actions/auto-lookup.ts diff --git a/plugin/kintone-addins/src/actions/auto-lookup.ts b/plugin/kintone-addins/src/actions/auto-lookup.ts new file mode 100644 index 0000000..52b3ba4 --- /dev/null +++ b/plugin/kintone-addins/src/actions/auto-lookup.ts @@ -0,0 +1,145 @@ +import { + IAction, + IActionResult, + IActionNode, + IActionProperty, + IContext, +} from "../types/ActionTypes"; +import { actionAddins } from "."; +import { before } from "node:test"; + +interface Props { + displayName: string; + lookupField: LookupField; + condition: Condition; +} + +interface Condition { + queryString: string; + index: number; + type: string; + children: Child[]; + parent: null; + logicalOperator: string; +} + +interface Child { + index: number; + type: string; + parent: string; + object: any; + operator: string; + value: string; +} + +interface LookupField { + app: App; + fields: Field[]; +} + +interface Field { + name: string; + type: string; + code: string; + label: string; + noLabel: boolean; + required: boolean; + lookup: Lookup; +} + +interface Lookup { + relatedApp: RelatedApp; + relatedKeyField: string; + fieldMappings: FieldMapping[]; + lookupPickerFields: any[]; + filterCond: string; + sort: string; +} + +interface FieldMapping { + field: string; + relatedField: string; +} + +interface RelatedApp { + app: string; + code: string; +} + +interface App { + id: string; + name: string; + description: string; + createdate: string; +} + +export class AutoLookUpAction implements IAction { + name: string; + actionProps: IActionProperty[]; + props: Props; + constructor() { + this.name = "ルックアップ更新"; + this.actionProps = []; + this.props = {} as Props; + this.register(); + } + + async process( + prop: IActionNode, + event: any, + context: IContext + ): Promise { + this.actionProps = prop.actionProps; + this.props = { + ...prop.ActionValue, + condition: JSON.parse((prop.ActionValue as any).condition), + } as Props; + console.log(context); + + let result = { + canNext: true, + result: "", + } as IActionResult; + try { + const lookUpField = this.props.lookupField.fields.filter( + (f) => f.lookup + )[0]; + if (!lookUpField) { + throw new Error( + `can't find look up field, selected field is ${this.props.lookupField.fields}` + ); + } + + const key = context.record[lookUpField.lookup.relatedKeyField].value; + + const updateRecords = ( + await kintone.api(kintone.api.url("/k/v1/records", true), "GET", { + app: this.props.lookupField.app.id, + query: `${lookUpField.code} = ${key}`, + }) + ).records as Array; + console.log("updateRecords", updateRecords); + if (updateRecords && updateRecords.length > 0) { + await kintone.api(kintone.api.url("/k/v1/records.json", true), "PUT", { + app: this.props.lookupField.app.id, + records: updateRecords.map((r) => ({ + id: Number(r["$id"].value), + record: { [lookUpField.code]: { value: key } }, + })), + }); + } + } catch (error) { + console.error("AutoLookUpAction error", error); + result.canNext = false; + } + console.log("autoLookupProps", this.props); + + return result; + } + + register(): void { + actionAddins[this.name] = this; + } +} + +new AutoLookUpAction(); \ No newline at end of file diff --git a/plugin/kintone-addins/src/types/action-process.ts b/plugin/kintone-addins/src/types/action-process.ts index 1ec55f8..0dc58d1 100644 --- a/plugin/kintone-addins/src/types/action-process.ts +++ b/plugin/kintone-addins/src/types/action-process.ts @@ -18,6 +18,7 @@ import '../actions/value-getter'; import '../actions/string-join'; import '../actions/validation-fullwidth'; import '../actions/validation-halfwidth'; +import '../actions/auto-lookup'; import { ActionFlow,IActionFlow, IActionResult,IContext } from "./ActionTypes";