Files
KintoneAppBuilder/plugin/kintone-addins/src/actions/end-of-month.ts
2024-09-13 08:58:24 +09:00

77 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { actionAddins } from ".";
import { IAction, IActionResult, IActionNode, IActionProperty, IField ,IContext, IVarName} from "../types/ActionTypes";
/**
* アクションの属性定義
*/
interface IEndOfMonthProps {
verNameGet:string;
verName:IVarName;
}
/**
* 月末算出アクション
*/
export class EndOfMonthAction implements IAction {
name: string;
actionProps: IActionProperty[];
props: IEndOfMonthProps;
constructor() {
this.name = "月末算出";
this.actionProps = [];
this.props = {
verNameGet:'',
verName:{name:''}
}
this.register();
}
/**
* アクションの実行を呼び出す
* @param actionNode
* @param event
* @returns
*/
async process(actionNode: IActionNode, event: any,context:IContext): Promise<IActionResult> {
let result = {
canNext: true,
result: false
};
try {
//属性設定を取得する
this.actionProps = actionNode.actionProps;
if (!('verName' in actionNode.ActionValue) && !('verNameGet' in actionNode.ActionValue) ) {
return result
}
this.props = actionNode.ActionValue as IEndOfMonthProps;
////////////////////////////////////////////////////////////////////////////////////////////////
//本番コード開始:
//取得変数の値を呼び出して代入する:
const getContextVarByPath = (obj: any, path: string) => {
return path.split(".").reduce((o, k) => (o || {})[k], obj);
};
let verNameGetValue = getContextVarByPath(context.variables,this.props.verNameGet);
////////////////////////////////////////////////////////////////////////////////////////////////
//取得変数の値Dateオブジェクトに変換
let dateObj = new Date(verNameGetValue);
// 月末を計算
let year = dateObj.getFullYear();
let month = dateObj.getMonth() + 1; //月は0から始まるため、1を足す
let lastDayOfMonth = new Date(year, month, 0); // 翌月の0日目は今月の月末
if(this.props.verName && this.props.verName.name!==''){
context.variables[this.props.verName.name]=lastDayOfMonth.toISOString();
}
////////////////////////////////////////////////////////////////////////////////////////////////
result = {
canNext:true,
result:true
}
return result;
}catch(error){
context.errors.handleError(error,actionNode);
result.canNext=false;
return result;
}
};
register(): void {
actionAddins[this.name] = this;
}
}
new EndOfMonthAction();