mirror of
https://github.com/IT4Change/gradido.git
synced 2026-03-01 12:44:43 +00:00
change validation of mandatory Command-parameters
This commit is contained in:
parent
e9e8fbac98
commit
5d69d3720c
@ -1,10 +1,14 @@
|
||||
import { Command } from './Command';
|
||||
|
||||
export abstract class BaseCommand<T = any> implements Command<T> {
|
||||
protected constructor(protected readonly params: any = {}) {}
|
||||
protected abstract requiredFields: string[];
|
||||
|
||||
protected constructor(protected readonly params: any = {}) {
|
||||
this.validateRequiredFields();
|
||||
}
|
||||
|
||||
abstract execute(): Promise<T>;
|
||||
|
||||
/*
|
||||
validate(): boolean {
|
||||
return true; // Default implementation, override in subclasses
|
||||
}
|
||||
@ -12,4 +16,23 @@ export abstract class BaseCommand<T = any> implements Command<T> {
|
||||
protected validateParams(requiredParams: string[]): boolean {
|
||||
return requiredParams.every(param => this.params[param] !== undefined);
|
||||
}
|
||||
*/
|
||||
private validateRequiredFields(): void {
|
||||
const missingFields = this.requiredFields.filter(field =>
|
||||
this.params[field] === undefined || this.params[field] === null || this.params[field] === ''
|
||||
);
|
||||
|
||||
if (missingFields.length > 0) {
|
||||
throw new Error(`Missing required fields for ${this.constructor.name}: ${missingFields.join(', ')}`);
|
||||
}
|
||||
}
|
||||
|
||||
validate(): boolean {
|
||||
return this.requiredFields.every(field =>
|
||||
this.params[field] !== undefined &&
|
||||
this.params[field] !== null &&
|
||||
this.params[field] !== ''
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -15,9 +15,12 @@ const createLogger = (method: string) =>
|
||||
export class CommandExecutor {
|
||||
async executeCommand<T>(command: Command<T>): Promise<CommandResult> {
|
||||
const methodLogger = createLogger(`executeCommand`)
|
||||
methodLogger.debug(`executeCommand() command=${command.constructor.name}`)
|
||||
try {
|
||||
if (command.validate && !command.validate()) {
|
||||
return { success: false, error: 'Command validation failed' };
|
||||
const errmsg = `Command validation failed for command=${command.constructor.name}`
|
||||
methodLogger.error(errmsg)
|
||||
return { success: false, error: errmsg };
|
||||
}
|
||||
methodLogger.debug(`executeCommand() executing command=${command.constructor.name}`)
|
||||
const result = await command.execute();
|
||||
|
||||
@ -9,6 +9,7 @@ const createLogger = (method: string) =>
|
||||
|
||||
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,
|
||||
@ -23,7 +24,11 @@ export class SendEmailCommand extends BaseCommand<{ success: boolean }> {
|
||||
}
|
||||
|
||||
validate(): boolean {
|
||||
return this.validateParams(['mailType', 'senderComUuid', 'senderGradidoId', 'receiverComUuid', 'receiverGradidoId']);
|
||||
const baseValid = super.validate();
|
||||
if (!baseValid) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
async execute(): Promise<{ success: boolean }> {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user