import { each } from "jquery"; import { actionAddins } from "."; import { IAction,IActionResult, IActionNode, IActionProperty, IField, IContext } from "../types/ActionTypes"; import { ConditionTree } from '../types/Conditions'; /** * アクションの属性定義 */ interface IInsertValueProps{ field:IField; condition:string; value:string; show:string; } /** * */ export class InsertValueAction implements IAction{ name: string; actionProps: IActionProperty[]; props:IInsertValueProps; constructor(){ this.name="値を挿入する";// DBに登録したアクション名 this.actionProps=[]; //プロパティ属性の初期化 this.props={ field:{code:''}, condition:'', value:'', show:'' } //アクションを登録する this.register(); } /** * 空白文字を空白文字が非対応のフィールドに挿入しようとしていないか、必須項目フィールドに挿入しようとしていないかチェックする * @param {string} inputValue - 挿入する値 * @return {boolean} -入力値が有効な日付形式の場合はtrueを返し、そうでない場合は例外を発生させる */ checkInputValueBlank(fieldType :string | undefined,inputValueArray :any,fieldCode :string,fieldRequired :boolean | undefined,event :any): boolean{ let blankCheck; //正規表現チェック for(let i =0;i{ //ユーザー名を格納する変数 let usersName = []; try{ //APIでユーザー情報を取得する const resp =await kintone.api(kintone.api.url('/v1/users', true), 'GET', {codes: inputValue.join(',')}) let usersInfo = resp.users; if (usersInfo.length !== inputValue.length) { throw new Error(); } //入力されたログイン名がユーザー情報に登録されている場合、そのユーザー名を取得する for (let indexUsersInfo in usersInfo) { for(let indexInputUser in inputValue){ if(usersInfo[indexUsersInfo].code === inputValue[indexInputUser]){ usersName.push(usersInfo[indexUsersInfo].name); } } } //ユーザー名が取得できた場合、ログイン名とユーザー名をフィールドにセットする if(usersName === undefined){ throw new Error(); } }catch{ return false; } return usersName; } /** * 入力値がフィールドタイプ(組織選択)で、組織情報に存在する値かチェックする関数 * @param {string} inputValue - 挿入する値 * @return {string | boolean} 入力値が登録されている組織情報から見つかった場合、trueを返し、見つからなかった場合、falseを返す */ async setInputOrganization(inputValue :string): Promise{ //組織名を格納する変数 let organizationName; const organizationsInfoColumnIndex=0; try{ //APIで組織情報を取得する const resp =await kintone.api(kintone.api.url('/v1/organizations.json', true), 'GET', {codes:[inputValue ]}) //入力された組織コードが組織情報に登録されている場合、その組織名を取得する if (resp.organizations[organizationsInfoColumnIndex].code === inputValue) { organizationName=resp.organizations[organizationsInfoColumnIndex].name; } //組織名が取得できた場合、組織コードと組織名をフィールドにセットする if(organizationName === undefined){ throw new Error(); } }catch{ return false; } return organizationName; } /** * 入力値がフィールドタイプ(グループ選択)で、グループ情報に存在する値かチェックする関数 * @param {string} inputValue - 挿入する値 * @return {string | boolean} 入力値が登録されているグループ情報から見つかった場合、trueを返し、見つからなかった場合、falseを返す */ async setInputGroup(inputValue :string): Promise{ //グループ名を格納する変数 let groupsName; const groupsInfoColumnIndex=0; try{ //APIでグループ情報を取得する const resp =await kintone.api(kintone.api.url('/v1/groups.json', true), 'GET', {codes:[inputValue ]}) //入力されたグループコードがグループ情報に登録されている場合、そのグループ名を取得する if (resp.groups[groupsInfoColumnIndex].code === inputValue) { groupsName=resp.groups[groupsInfoColumnIndex].name; } //グループ名が取得できた場合、グループコードとグループ名をフィールドにセットする if(groupsName === undefined){ throw new Error(); } }catch{ return false; } return groupsName; } /** * ユーザーオブジェクトを挿入する場合、挿入先フィールドによって、適切なオブジェクトの値を取得し、セットする * @param {string} inputValue -入力された値 * @param {string} objectValue -オブジェクト変数 * @param {string} fieldType -挿入先フィールドタイプ * @param {string} fieldCode -挿入先フィールドタイプ * @return {string} -挿入先フィールドによって、ログインユーザーオブジェクトの何の値を返すか、変わる */ setValueOfUserObject(inputValue :any,objectValue :any,fieldType : any,fieldCode : any): any{ //変数の値 let variableValue = []; //ユーザー選択フィールドに挿入時、ユーザーオブジェクトのcodeを代入する if(fieldType.includes('USER_SELECT')){ //変数の値取得 // variableValue = getContextVarByPath(context.variables,inputValue); if(!Array.isArray(objectValue)){ variableValue.push(objectValue.code); }else{ for(let i=0;i { let result={ canNext:true, result:false }; try{ //属性設定を取得する this.actionProps = actionNode.actionProps; if (!('field' in actionNode.ActionValue) && !('value' in actionNode.ActionValue)) { return result } const fieldColumnIndex=1; const valueColumnIndex=3; //プロパティで選択されたフィールド const field=this.actionProps[fieldColumnIndex].props.modelValue.type; //プロパティの挿入する値 const value=this.actionProps[valueColumnIndex].props.modelValue; //条件式の結果を取得 const conditionResult = this.getConditionResult(context); if(!conditionResult){ return result; } //プロパティの値を挿入するフィールドが未選択の場合、例外を発生させる if(field === null){ throw new Error("「値を挿入する」コンポーネントで、値を挿入するフィールドが指定されていなかったため、処理が中断されました。"); } //プロパティの値を挿入するフィールドが非対応フィールドの場合、例外を発生させる //添付ファイル・テーブル・カテゴリー・ステータス・作成者・更新者・作業者・リビジョン番号・レコード番号・レコードID・計算・作成日時・更新日時フィールドが選択されている場合、例外を発生させる if(field === "FILE" || field === "SUBTABLE" || field === "CATEGORY" || field === "STATUS" || field === "STATUS_ASSIGNEE" || field === "CREATOR" || field === "MODIFIER" || field === "__REVISION__" || field === "RECORD_NUMBER"|| field === "__ID__" || field ==="CALC" || field === "CREATED_TIME" || field === "UPDATED_TIME" ){ throw new Error("「値を挿入する」コンポーネントで、選択されたフィールドは、値を挿入するコンポーネントでは非対応のフィールドのため、処理を中断しました。"); } //プロパティの挿入する値が未入力の場合、例外を発生させる if(value === ""){ throw new Error("「値を挿入する」コンポーネントで、フィールドに挿入する値が指定されていなかったため、処理が中断されました。"); } //既定のプロパティのインターフェースへ変換する this.props = actionNode.ActionValue as IInsertValueProps; //プロパティの挿入する値を取得 let fieldValue = this.props.value; //フィールドの種類を取得 const fieldType = this.props.field.type; //フィールドが必須項目なのか取得 const fieldRequired=this.props.field.required; //挿入するフィールドのコードを取得 const fieldCode=this.props.field.code; //挿入する値の形式(手入力か変数)を取得 const insertValueType=this.props.show; //ラジオボタン・チェックボックス・複数選択・ドロップダウンの選択肢を取得 let fieldOptions =this.props.field.options; //変数の値を格納する let variableValue :any; //挿入する値を格納する let fieldValueArray = []; let objectValue :any; let notInputError; //変数の場合、値が取得できるかチェック if(insertValueType === "変数" && conditionResult){ //変数の値を呼び出して代入する const getContextVarByPath = (obj: any, path: string) => { return path.split(".").reduce((o, k) => (o || {})[k], obj); }; //オブジェクト変数で、値の指定がある場合は、変数名のみ取得 var variablesName = fieldValue.substr(0, fieldValue.indexOf('.')); if(variablesName){ //変数の値取得 objectValue = getContextVarByPath(context.variables,variablesName) }else{ //変数の値取得 objectValue = getContextVarByPath(context.variables,fieldValue) } //オブジェクト変数の場合、プロパティを取得し、何のオブジェクト変数か判断する if(objectValue !== null && typeof objectValue === 'object'){ let objectProperties=[] if(objectValue.length > 0){ objectProperties=Object.keys(objectValue[0]); }else{ objectProperties = Object.keys(objectValue); } //(ログインユーザー・値取得のコンポーネントからの)ユーザーオブジェクトを挿入時、挿入先フィールドによって、値を指定する if(objectProperties.includes('code') && objectProperties.includes('name')){ objectValue=this.setValueOfUserObject(fieldValue,objectValue,fieldType,fieldCode); //ユーザーオブジェクトから取得した値を、fieldValueArrayに代入 for (const value of objectValue) { fieldValueArray.push(value); } } }else{ //オブジェクト変数でない場合、変数をfieldValueArrayに代入 variableValue = context.variables[fieldValue]; fieldValueArray[0] = variableValue; } //変数がfieldValueArrayに代入できなかった場合は、エラーを出す if(objectValue === undefined && fieldValueArray === undefined){ throw new Error("「"+fieldCode+"」"+"フィールドに入れようとした変数は、無効な入力形式です。"); } //変数の値にエラー(空文字・空白文字の混入)がないことをチェックする notInputError=this.checkVariableValueBlank(fieldType,fieldValueArray,fieldCode,fieldRequired,event); //手入力の場合、プロパティ挿入する値をfieldArrayに代入 }else{ fieldValueArray.push(fieldValue); //入力エラー(空白文字の混入)がないことをチェック notInputError=this.checkInputValueBlank(fieldType,fieldValueArray,fieldCode,fieldRequired,event); } //形式変換、型変換した値を格納する変数 let correctFormattedValue = undefined; //形式変換、型変換した値を格納する配列 let correctValues :any[] = []; //条件式の結果がtrue、入力エラー(空白文字の混入)がない場合、挿入する値をフィールドタイプ別にチェックする if(conditionResult && notInputError){ //文字列型のフィールドに挿入しようとしている値が適切の場合、correctFormattedValueに代入する if(fieldType === "SINGLE_LINE_TEXT" || fieldType === "MULTI_LINE_TEXT" || fieldType === "RICH_TEXT" || fieldType === "LINK" ){ correctFormattedValue = fieldValueArray.join(','); //数値型のフィールドに挿入しようとしている値が適切の場合、数値型に型変換してcorrectFormattedValueに代入する }else if(fieldType === "NUMBER" ){ if(this.checkInputNumber(fieldValueArray[0],fieldCode,event)){//入力値チェック correctFormattedValue = Number(fieldValueArray[0]);//型変換 } //日付・日時型のフィールドに挿入しようとしている値が適切の場合、指定の日付・日時に形式変換してcorrectFormattedValueに代入する }else if(fieldType === "DATE" || fieldType === "DATETIME" ){ if(this.checkInputDate(fieldValueArray[0],fieldCode,event)){//入力値チェック let formattedDate = this.changeDateFormat(fieldValueArray[0],fieldType,fieldCode,event) if(formattedDate){ correctFormattedValue = formattedDate } } //時刻フィールドに挿入しようとしている値が適切の場合、correctFormattedValueに代入する }else if(fieldType === "TIME"){ if(this.checkInputTime(fieldValueArray[0],fieldCode,event)){//入力値チェック correctFormattedValue = fieldValueArray[0]; } //ラジオボタン・ドロップダウンのフィールドの選択肢と入力値が一致した場合、correctFormattedValueに代入する }else if(fieldType === "RADIO_BUTTON" || fieldType === "DROP_DOWN"){ if(this.checkInputOption(fieldValueArray[0],fieldOptions,fieldCode,event)){//入力値チェック correctFormattedValue = fieldValueArray[0]; } //チェックボックス・複数選択のフィールドの選択肢と入力値が一致した場合、correctValuesの配列に代入する }else if(fieldType === "CHECK_BOX" || fieldType === "MULTI_SELECT" ){ if(this.checkInputOption(fieldValueArray[0],fieldOptions,fieldCode,event)){//入力値チェック correctValues[0] = fieldValueArray[0]; } //ユーザー情報フィードに挿入しようとした値が適切な場合、correctValues(配列)に代入する }else if(fieldType === "USER_SELECT"){ //挿入する値がユーザー情報から見つかれば、ユーザー名を格納 let usersName=await this.setInputUser(fieldValueArray); //ユーザー名が格納できている場合、ログイン名とユーザー名をcorrectValues(配列)に代入する if(!usersName){ event.record[fieldCode]['error']="ユーザー選択に、挿入しようとしたユーザー情報は見つかりませんでした。「値を挿入する」コンポーネントの処理を中断しました。"; throw new Error("ユーザー選択に、挿入しようとしたユーザー情報は見つかりませんでした。「値を挿入する」コンポーネントの処理を中断しました。"); } for(let indexInputUser in fieldValueArray){ correctValues.push({ code: fieldValueArray[indexInputUser], name: usersName[indexInputUser] }); } //組織情報フィードに挿入しようとした値が適切な場合、correctFormattedValueに代入する }else if(fieldType === "ORGANIZATION_SELECT"){ //挿入する値が組織情報から見つかれば、組織名を格納 let organizations=await this.setInputOrganization(fieldValue); //組織名が格納できている場合、組織コードと組織名をcorrectFormattedValueに代入する if(!organizations){ event.record[fieldCode]['error']="組織選択フィールドに、挿入しようとした組織情報は見つかりませんでした。「値を挿入する」コンポーネントの処理を中断しました。"; throw new Error("組織選択フィールドに、挿入しようとした組織情報は見つかりませんでした。「値を挿入する」コンポーネントの処理を中断しました。"); }else{ correctFormattedValue=[{ code: fieldValue, name: organizations}]; } //グループ情報フィードに挿入しようとした値が適切な場合、correctFormattedValueに代入する }else if(fieldType === "GROUP_SELECT"){ //挿入する値がグループ情報から見つかれば、グループ名を格納 let groups=await this.setInputGroup(fieldValue); //グループ名が格納できている場合、グループコードとグループ名をcorrectFormattedValueに代入する if(!groups){ event.record[fieldCode]['error']="グループ選択フィールドに、挿入しようとしたグループ情報は見つかりませんでした。「値を挿入する」コンポーネントの処理を中断しました。"; throw new Error("グループ選択フィールドに、挿入しようとしたグループ情報は見つかりませんでした。「値を挿入する」コンポーネントの処理を中断しました。"); }else{ correctFormattedValue=[{ code: fieldValue, name: groups}]; } } } //保存成功イベントの場合、kintone async/await による非同期処理でフィールドに値を挿入する if(!event.type.includes('success')){ //条件式の結果がtrueかつ挿入する値が変換できた場合、フィールド(ラジオボタン・ドロップダウン・チェックボックス・複数選択・文字列一行・文字列複数行・リッチエディタ・数値・日付・日時・時刻)にセット if(conditionResult){ //条件式の結果がtureかつ、値を正しい形式に変換できた場合、フィールドに値をセットする if(correctFormattedValue !== null && correctFormattedValue !== undefined){ event.record[fieldCode].value = correctFormattedValue; //条件式の結果がtureかつ、値を正しい形式(配列)に変換できた場合、フィールドに値(配列)をセットする }else if(correctValues.length > 0 && correctValues !== null && correctValues !== undefined){ event.record[fieldCode].value = correctValues; } } }else{ //kintone async/await による非同期処理(保存成功時イベントREST API処理時) async function updateRecord(fieldCode:string,event:any,insertValue:any) { return new Promise((resolve, reject) => { var updatedRecord = { app: event.appId, id: event.recordId, record: {[fieldCode]:{"value":insertValue}} }; kintone.api(kintone.api.url('/k/v1/record', true), 'PUT', updatedRecord, (resp) => { resolve(resp); }, (error) => { reject(error); }); }); } //条件式の結果がtrueかつ挿入する値が変換できた場合、フィールド(ラジオボタン・ドロップダウン・チェックボックス・複数選択・文字列一行・文字列複数行・リッチエディタ・数値・日付・日時・時刻)にセット if(conditionResult && (correctFormattedValue || correctValues)){ //条件式の結果がtureかつ、値を正しい形式に変換できた場合、フィールドに値をセットする if(correctFormattedValue){ event.record[fieldCode].value = correctFormattedValue; //条件式の結果がtureかつ、値を正しい形式(配列)に変換できた場合、フィールドに値(配列)をセットする }else if(correctValues.length > 0){ event.record[fieldCode].value = correctValues; } } //kintone async/await による非同期処理(レコード更新) await updateRecord(fieldCode,event,correctFormattedValue); }; result= { canNext:true, result:true } return result; }catch(error:any){ event.record; event.error=error.message; console.error(error); result.canNext=true;//次のノードは処理を続ける return result; } } /** * * @param context 条件式を実行する * @returns */ getConditionResult(context:any):boolean{ //プロパティ`condition`から条件ツリーを取得する const tree =this.getCondition(this.props.condition); if(!tree){ //条件を設定されていません return true; } return tree.evaluate(tree.root,context); } /** * @param condition 条件式ツリーを取得する * @returns */ getCondition(condition:string):ConditionTree|null{ try{ const tree = new ConditionTree(); tree.fromJson(condition); if(tree.getConditions(tree.root).length>0){ return tree; }else{ return null; } }catch(error){ return null; } } register(): void { actionAddins[this.name]=this; } } new InsertValueAction();