mirror of
https://github.com/IT4Change/gradido.git
synced 2026-03-01 12:44:43 +00:00
correct implementation of command registering and creation
This commit is contained in:
parent
db2b35d148
commit
dd27cd5313
@ -2,13 +2,14 @@ import { Command } from './Command';
|
||||
import { BaseCommand } from './BaseCommand';
|
||||
import { getLogger } from 'log4js';
|
||||
import { LOG4JS_BASE_CATEGORY_NAME } from '../config/const';
|
||||
import { ICommandConstructor } from './CommandTypes';
|
||||
|
||||
const createLogger = (method: string) =>
|
||||
getLogger(`${LOG4JS_BASE_CATEGORY_NAME}.command.CommandFactory.${method}`)
|
||||
|
||||
export class CommandFactory {
|
||||
private static instance: CommandFactory;
|
||||
private commands: Map<string, new (params: any) => Command> = new Map();
|
||||
private commands: Map<string, ICommandConstructor> = new Map();
|
||||
|
||||
private constructor() {}
|
||||
|
||||
@ -19,10 +20,10 @@ export class CommandFactory {
|
||||
return CommandFactory.instance;
|
||||
}
|
||||
|
||||
registerCommand(name: string, commandClass: new (params: any) => Command): void {
|
||||
registerCommand<T>(name: string, commandClass: ICommandConstructor<T>): void {
|
||||
const methodLogger = createLogger(`registerCommand`)
|
||||
if (methodLogger.isDebugEnabled()) {
|
||||
methodLogger.debug(`registerCommand() name=${name}`)
|
||||
methodLogger.debug(`registerCommand() name=${name}, commandClass=${commandClass.name}`)
|
||||
}
|
||||
this.commands.set(name, commandClass);
|
||||
if (methodLogger.isDebugEnabled()) {
|
||||
@ -36,6 +37,9 @@ export class CommandFactory {
|
||||
methodLogger.debug(`createCommand() name=${name} params=${JSON.stringify(params)}`)
|
||||
}
|
||||
const CommandClass = this.commands.get(name);
|
||||
if (methodLogger.isDebugEnabled()) {
|
||||
methodLogger.debug(`createCommand() name=${name} commandClass=${CommandClass ? CommandClass.name : 'null'}`)
|
||||
}
|
||||
if (!CommandClass) {
|
||||
const errmsg = `Command ${name} not found`;
|
||||
methodLogger.error(errmsg);
|
||||
|
||||
5
core/src/command/CommandTypes.ts
Normal file
5
core/src/command/CommandTypes.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import { Command } from "./Command";
|
||||
|
||||
export interface ICommandConstructor<T = any> {
|
||||
new (params: any): Command<T>;
|
||||
}
|
||||
@ -7,19 +7,20 @@ import { getLogger } from 'log4js';
|
||||
const createLogger = (method: string) =>
|
||||
getLogger(`${LOG4JS_BASE_CATEGORY_NAME}.command.CommandExecutor.${method}`)
|
||||
|
||||
export interface SendEmailCommandParams {
|
||||
mailType: string;
|
||||
senderComUuid: string;
|
||||
senderGradidoId: string;
|
||||
receiverComUuid: string;
|
||||
receiverGradidoId: string;
|
||||
memo?: string;
|
||||
amount?: number;
|
||||
}
|
||||
export class SendEmailCommand extends BaseCommand<{ success: boolean }> {
|
||||
static readonly SEND_MAIL_COMMAND = 'SEND_MAIL_COMMAND';
|
||||
protected requiredFields: string[] = ['mailType', 'senderComUuid', 'senderGradidoId', 'receiverComUuid', 'receiverGradidoId'];
|
||||
|
||||
constructor(params: {
|
||||
mailType: string,
|
||||
senderComUuid: string,
|
||||
senderGradidoId: string,
|
||||
receiverComUuid: string,
|
||||
receiverGradidoId: string,
|
||||
memo?: string,
|
||||
amount?: number,
|
||||
}) {
|
||||
constructor(params: SendEmailCommandParams) {
|
||||
super(params);
|
||||
}
|
||||
|
||||
@ -28,6 +29,8 @@ export class SendEmailCommand extends BaseCommand<{ success: boolean }> {
|
||||
if (!baseValid) {
|
||||
return false;
|
||||
}
|
||||
// Additional validations
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user