自動検索プラグインを追加
This commit is contained in:
145
plugin/kintone-addins/src/actions/auto-lookup.ts
Normal file
145
plugin/kintone-addins/src/actions/auto-lookup.ts
Normal file
@@ -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<IActionResult> {
|
||||||
|
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<any>;
|
||||||
|
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();
|
||||||
@@ -18,6 +18,7 @@ import '../actions/value-getter';
|
|||||||
import '../actions/string-join';
|
import '../actions/string-join';
|
||||||
import '../actions/validation-fullwidth';
|
import '../actions/validation-fullwidth';
|
||||||
import '../actions/validation-halfwidth';
|
import '../actions/validation-halfwidth';
|
||||||
|
import '../actions/auto-lookup';
|
||||||
|
|
||||||
import { ActionFlow,IActionFlow, IActionResult,IContext } from "./ActionTypes";
|
import { ActionFlow,IActionFlow, IActionResult,IContext } from "./ActionTypes";
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user