fix(backend): Insert weggewerkt

In de plaats optionele check aan DwengoEntityRepository.save(...) toegevoegd die eist dat de entity nog niet bestaat.
This commit is contained in:
Gerald Schmittinger 2025-03-30 14:19:57 +02:00
parent 148de14b78
commit e8add72de4
5 changed files with 1281 additions and 515 deletions

View file

@ -1,8 +1,12 @@
import { EntityRepository, FilterQuery } from '@mikro-orm/core';
import {EntityRepository, FilterQuery} from '@mikro-orm/core';
import {EntityAlreadyExistsException} from "../exceptions/entity-already-exists-exception";
export abstract class DwengoEntityRepository<T extends object> extends EntityRepository<T> {
public async save(entity: T) {
await this.getEntityManager().insert(entity);
public async save(entity: T, options?: {preventOverwrite?: Boolean}): Promise<void> {
if (options?.preventOverwrite && await this.findOne(entity)) {
throw new EntityAlreadyExistsException(`A ${this.getEntityName()} with this identifier already exists.`);
}
await this.getEntityManager().persistAndFlush(entity);
}
public async deleteWhere(query: FilterQuery<T>) {
const toDelete = await this.findOne(query);