データ追加&更新処理アクション修正完了
This commit is contained in:
@@ -8,7 +8,7 @@ import {
|
||||
import { actionAddins } from ".";
|
||||
import { KintoneRestAPIClient } from "@kintone/rest-api-client";
|
||||
import { Lookup } from "@kintone/rest-api-client/lib/src/KintoneFields/types/property";
|
||||
|
||||
import { FieldForm, FieldType } from "../types/FieldLayout";
|
||||
interface Props {
|
||||
displayName: string;
|
||||
sources: Sources;
|
||||
@@ -27,37 +27,30 @@ interface Mapping {
|
||||
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;
|
||||
}[];
|
||||
fields:FieldForm[];
|
||||
isDialogVisible: boolean;
|
||||
}
|
||||
|
||||
|
||||
interface From {
|
||||
sharedText: string;
|
||||
_t: string;
|
||||
id: string;
|
||||
objectType: string;
|
||||
name: {
|
||||
name: string;
|
||||
};
|
||||
actionName: string;
|
||||
displayName: string;
|
||||
objectType: 'variable'|'field'|'text';
|
||||
}
|
||||
|
||||
interface IVar extends From{
|
||||
name:{
|
||||
name:string;
|
||||
}
|
||||
}
|
||||
|
||||
interface IFromField extends From,FieldForm{
|
||||
|
||||
}
|
||||
interface Sources {
|
||||
app: App;
|
||||
}
|
||||
@@ -98,7 +91,7 @@ export class DataUpdateAction implements IAction {
|
||||
);
|
||||
|
||||
// createWithNull が有効な場合は、4 番目のパラメーターを true にして doUpdate 関数ブランチを実行します。
|
||||
if (this.dataMappingProps.dataMapping.createWithNull === true) {
|
||||
if (this.dataMappingProps.dataMapping.createWithNull) {
|
||||
await doUpdate(
|
||||
this.dataMappingProps.dataMapping.data,
|
||||
this.dataMappingProps.sources.app.id,
|
||||
@@ -158,10 +151,23 @@ interface UpdateRecord {
|
||||
|
||||
const client = new KintoneRestAPIClient();
|
||||
|
||||
const getFromValue=(item:Mapping,context:IContext)=>{
|
||||
if (item.from.objectType === "variable") {
|
||||
const rfrom =item.from as IVar;
|
||||
return getContextVarByPath(context.variables,rfrom.name.name);
|
||||
}else if(item.from.objectType === "field"){
|
||||
const field = item.from as IFromField;
|
||||
return context.record[field.code].value;
|
||||
}
|
||||
else {
|
||||
return item.from.sharedText;
|
||||
}
|
||||
}
|
||||
|
||||
const doUpdate = async (
|
||||
mappingData: Mapping[],
|
||||
appId: string,
|
||||
context: any,
|
||||
context: IContext,
|
||||
needCreate: boolean,
|
||||
lookupFixedFieldCodes: string[]
|
||||
) => {
|
||||
@@ -179,11 +185,19 @@ const doUpdate = async (
|
||||
)
|
||||
.map((m) => {
|
||||
if (m.from.objectType === "variable") {
|
||||
const rfrom =m.from as IVar;
|
||||
return {
|
||||
value: getContextVarByPath(context.variables, m.from.name.name),
|
||||
value: getContextVarByPath(context.variables,rfrom.name.name),
|
||||
code: m.to.fields[0].code,
|
||||
};
|
||||
} else {
|
||||
}else if(m.from.objectType === "field"){
|
||||
const field = m.from as IFromField;
|
||||
return {
|
||||
value: context.record[field.code].value,
|
||||
code: m.to.fields[0].code,
|
||||
}
|
||||
}
|
||||
else {
|
||||
return {
|
||||
value: m.from.sharedText,
|
||||
code: m.to.fields[0].code,
|
||||
@@ -211,6 +225,7 @@ const doUpdate = async (
|
||||
}
|
||||
);
|
||||
|
||||
console.log(updateRecords);
|
||||
await client.record.updateRecords({
|
||||
app: appId,
|
||||
records: updateRecords,
|
||||
@@ -218,21 +233,34 @@ const doUpdate = async (
|
||||
}
|
||||
};
|
||||
|
||||
const makeQuery=(field:FieldForm,key:any)=>{
|
||||
if(field.type===FieldType.NUMBER || field.type===FieldType.RECORD_NUMBER){
|
||||
return `${field.code} = ${Number(key)}`
|
||||
}
|
||||
if(typeof key==='string'){
|
||||
return `${field.code} = "${key}"`
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const findUpdateField = async (
|
||||
mappingData: Mapping[],
|
||||
appId: string,
|
||||
context: any
|
||||
context: IContext
|
||||
) => {
|
||||
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}`;
|
||||
const vfrom = m.from as IVar;
|
||||
return makeQuery(m.to.fields[0],getContextVarByPath(context.variables , vfrom.name.name));
|
||||
}
|
||||
else if(m.from.objectType === "field"){
|
||||
const field = m.from as IFromField;
|
||||
return makeQuery(m.to.fields[0],context.record[field.code].value);
|
||||
}
|
||||
else{
|
||||
return makeQuery(m.to.fields[0],m.from.sharedText);
|
||||
}
|
||||
})
|
||||
.join("&");
|
||||
@@ -253,38 +281,51 @@ const findUpdateField = async (
|
||||
const doCreate = async (
|
||||
mappingData: Mapping[],
|
||||
appId: string,
|
||||
context: any,
|
||||
context: IContext,
|
||||
lookupFixedFieldCodes: string[]
|
||||
) => {
|
||||
const filterHandler = (item:Mapping)=>{
|
||||
if(!item.to.fields || item.to.fields.length===0){
|
||||
return false;
|
||||
}
|
||||
if(item.from.objectType === "variable" && (item.from as IVar).name.name ){
|
||||
return true;
|
||||
}
|
||||
if(item.from.objectType === "field" && (item.from as IFromField).code){
|
||||
return true;
|
||||
}
|
||||
if(item.from.objectType === "text" && item.from.sharedText!==null){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
const record = mappingData
|
||||
.filter(
|
||||
(item) =>
|
||||
item.from.objectType === "variable" &&
|
||||
item.from.name.name &&
|
||||
item.to.app &&
|
||||
item.to.fields &&
|
||||
item.to.fields.length > 0
|
||||
)
|
||||
.filter(filterHandler)
|
||||
.filter((item) => !lookupFixedFieldCodes.includes(item.to.fields[0].code))
|
||||
.reduce((accumulator, item) => {
|
||||
return {
|
||||
...accumulator,
|
||||
[item.to.fields[0].code]: {
|
||||
value: getContextVarByPath(context.variables, item.from.name.name),
|
||||
value: getFromValue(item,context),
|
||||
},
|
||||
};
|
||||
}, {});
|
||||
if (record && Object.keys(record).length > 0) {
|
||||
await kintone.api(kintone.api.url("/k/v1/record.json", true), "POST", {
|
||||
app: appId,
|
||||
record: record,
|
||||
console.log(record);
|
||||
await client.record.addRecord({
|
||||
app:appId,
|
||||
record:record
|
||||
});
|
||||
// await kintone.api(kintone.api.url("/k/v1/record.json", true), "POST", {
|
||||
// app: appId,
|
||||
// record: record,
|
||||
// });
|
||||
}
|
||||
};
|
||||
|
||||
const getLookupFixedFieldCodes = async (appId: string) => {
|
||||
return await client.app
|
||||
.getFormFields({ app: appId, lang: "ja" })
|
||||
.getFormFields({ app: appId })
|
||||
.then((resp) =>
|
||||
Object.values(resp.properties)
|
||||
.filter((f) => (f as Lookup).lookup !== undefined)
|
||||
|
||||
Reference in New Issue
Block a user