cleanup code

This commit is contained in:
clauspeterhuebner 2026-02-19 00:18:07 +01:00
parent 9b5e64d455
commit e67be63941
5 changed files with 35 additions and 58 deletions

View File

@ -12,7 +12,7 @@ export abstract class BaseCommand<T = any> implements Command<T> {
// this.validateRequiredFields();
}
abstract execute(): Promise<T>;
abstract execute(): Promise<Record<string, unknown> | boolean | null | Error>;
private validateRequiredFields(): void {
const methodLogger = createLogger(`validateRequiredFields`)

View File

@ -1,4 +1,4 @@
export interface Command<T = any> {
execute(): Promise<T>;
execute(): Promise<Record<string, unknown> | boolean | null | Error>;
validate?(): boolean;
}

View File

@ -2,7 +2,6 @@
import { CommandJwtPayloadType } from 'shared';
import { interpretEncryptedTransferArgs } from '../graphql/logic/interpretEncryptedTransferArgs';
import { EncryptedTransferArgs } from '../graphql/model/EncryptedTransferArgs';
import { BaseCommand } from './BaseCommand';
import { Command } from './Command';
import { getLogger } from 'log4js';
import { LOG4JS_BASE_CATEGORY_NAME } from '../config/const';
@ -24,20 +23,9 @@ export class CommandExecutor {
}
methodLogger.debug(`executeCommand() executing command=${command.constructor.name}`)
const result = await command.execute();
// "accepted":["stage5@gradido.net"],"rejected":[],"ehlo":["PIPELINING","SIZE 25600000","ETRN","AUTH DIGEST-MD5 CRAM-MD5 PLAIN LOGIN","ENHANCEDSTATUSCODES","8BITMIME","DSN","CHUNKING"],"envelopeTime":25,"messageTime":146,"messageSize":37478,"response":"250 2.0.0 Ok: queued as 14B46100B7F","envelope":{"from":"stage5@gradido.net","to":["stage5@gradido.net"]}
const resultMsg = this.isEmailResult(result) ? {
accepted: result.accepted,
messageSize: result.messageSize,
response: result.response,
envelope: result.envelope,
} : {
accepted: [],
messageSize: 0,
response: JSON.stringify(result),
envelope: null
};
methodLogger.debug(`executeCommand() executed result=${JSON.stringify(resultMsg)}`)
return { success: true, data: JSON.stringify(resultMsg) };
const resultMsg = this.getEmailResult(result);
methodLogger.debug(`executeCommand() executed email-result=${resultMsg}`)
return { success: true, data: resultMsg };
} catch (error) {
methodLogger.error(`executeCommand() error=${error}`)
return {
@ -84,18 +72,35 @@ export class CommandExecutor {
}
}
private isEmailResult(result: any): result is {
accepted: string[];
messageSize: number;
response: string;
envelope: any;
} {
return result &&
typeof result === 'object' &&
Array.isArray(result.accepted) &&
typeof result.messageSize === 'number' &&
typeof result.response === 'string' &&
typeof result.envelope === 'object';
private getEmailResult(result: Record<string, unknown> | boolean | null | Error): string {
const methodLogger = createLogger(`getEmailResult`)
if (methodLogger.isDebugEnabled()) {
methodLogger.debug(`getEmailResult() result=${JSON.stringify(result)}`)
}
let emailResult: string;
if(result === null) {
emailResult = `getEmailResult() result is null`
}
else if(typeof result === 'boolean') {
emailResult = `getEmailResult() result is ${result}`
}
else if(result instanceof Error) {
emailResult = `getEmailResult() error-message is ${result.message}`
}
else if(typeof result === 'object') {
// "accepted":["stage5@gradido.net"],"rejected":[],"ehlo":["PIPELINING","SIZE 25600000","ETRN","AUTH DIGEST-MD5 CRAM-MD5 PLAIN LOGIN","ENHANCEDSTATUSCODES","8BITMIME","DSN","CHUNKING"],"envelopeTime":25,"messageTime":146,"messageSize":37478,"response":"250 2.0.0 Ok: queued as 14B46100B7F","envelope":{"from":"stage5@gradido.net","to":["stage5@gradido.net"]}
const accepted = (result as Record<string, unknown>).accepted;
const messageSize = (result as Record<string, unknown>).messageSize;
const response = (result as Record<string, unknown>).response;
const envelope = (result as Record<string, unknown>).envelope;
emailResult = `getEmailResult() accepted=${accepted}, messageSize=${messageSize}, response=${response}, envelope=${envelope}`
}
else {
emailResult = `getEmailResult() result is unknown type`
}
return emailResult;
}
}

View File

@ -1,28 +0,0 @@
// core/src/command/commands/ExampleCommand.ts
/*
import { BaseCommand } from '../BaseCommand';
import { CommandRegistry } from '../CommandRegistry';
export interface ExampleCommandParams {
someData: string;
}
export class ExampleCommand extends BaseCommand<{ processed: boolean }> {
constructor(params: ExampleCommandParams) {
super('EXAMPLE_COMMAND', params);
}
validate(): boolean {
return !!this.params.someData;
}
async execute(): Promise<{ processed: boolean }> {
// Command implementation here
console.log('Executing ExampleCommand with data:', this.params.someData);
return { processed: true };
}
}
// Register the command
CommandRegistry.registerCommand('EXAMPLE_COMMAND', ExampleCommand);
*/

View File

@ -57,7 +57,7 @@ export class SendEmailCommand extends BaseCommand<Record<string, unknown> | bool
}
methodLogger.debug(`find recipient user: ${this.sendEmailCommandParams.receiverComUuid} ${this.sendEmailCommandParams.receiverGradidoId}`)
const recipientUser = await findUserByUuids(this.sendEmailCommandParams.receiverGradidoId, this.sendEmailCommandParams.receiverComUuid);
const recipientUser = await findUserByUuids(this.sendEmailCommandParams.receiverComUuid, this.sendEmailCommandParams.receiverGradidoId);
methodLogger.debug(`recipientUser=${JSON.stringify(recipientUser)}`)
if (!recipientUser) {
const errmsg = `Recipient user not found: ${this.sendEmailCommandParams.receiverComUuid} ${this.sendEmailCommandParams.receiverGradidoId}`;