30 lines
906 B
TypeScript
30 lines
906 B
TypeScript
import { IDeckRepository } from '../../../Domain/IRepository/IDeckRepository';
|
|
import { logAuth, logError } from '../../Services/Logger';
|
|
import { DeleteDeckCommand } from './DeleteDeckCommand';
|
|
|
|
export class DeleteDeckCommandHandler {
|
|
constructor(private readonly deckRepo: IDeckRepository) {}
|
|
|
|
async execute(cmd: DeleteDeckCommand): Promise<boolean> {
|
|
|
|
//get decks userid
|
|
const deck = await this.deckRepo.findById(cmd.id);
|
|
if (!deck) {
|
|
logError(`Deck not found with ID: ${cmd.id}`);
|
|
throw new Error('Deck not found');
|
|
}
|
|
|
|
if(cmd.authLevel !==1 && deck.userid !== cmd.userid) {
|
|
logAuth(`Unauthorized access attempt to deck with ID: ${cmd.id}, UserID: ${cmd.userid}`);
|
|
throw new Error('Unauthorized');
|
|
}
|
|
|
|
if (cmd.soft) {
|
|
await this.deckRepo.softDelete(cmd.id);
|
|
} else {
|
|
await this.deckRepo.delete(cmd.id);
|
|
}
|
|
return true;
|
|
}
|
|
}
|