diff --git a/src/modules/icons/entities/group.entity.ts b/src/modules/icons/entities/group.entity.ts index 1208d0b..a62c4e4 100644 --- a/src/modules/icons/entities/group.entity.ts +++ b/src/modules/icons/entities/group.entity.ts @@ -1,4 +1,4 @@ -import { Entity, Index, Property, Collection, OneToMany } from '@mikro-orm/core'; +import { Entity, Index, Property, Collection, OneToMany, Cascade } from '@mikro-orm/core'; import { BaseEntity } from '../../../common/entities/base.entity'; import { Icon } from './icon.entity'; @@ -7,6 +7,9 @@ export class Group extends BaseEntity { @Property() name!: string; - @OneToMany(() => Icon, icon => icon.group) + @OneToMany(() => Icon, icon => icon.group,{ + cascade: [Cascade.ALL], + orphanRemoval: true, + }) icons = new Collection(this); } diff --git a/src/modules/icons/providers/group.service.ts b/src/modules/icons/providers/group.service.ts index 3e8afa6..545dfba 100644 --- a/src/modules/icons/providers/group.service.ts +++ b/src/modules/icons/providers/group.service.ts @@ -12,7 +12,7 @@ export class GroupService { constructor( private readonly groupRepository: GroupRepository, private readonly em: EntityManager, - ) {} + ) { } async create(dto: CreateGroupDto): Promise { const data: RequiredEntityData = { @@ -47,12 +47,11 @@ export class GroupService { } async remove(id: string): Promise { - const group = await this.groupRepository.findOne({ id }); + const group = await this.groupRepository.findOne({ id }, { populate: ['icons'] }); if (!group) { throw new NotFoundException(GroupMessage.NOT_FOUND); } - group.deletedAt = new Date(); - await this.em.persistAndFlush(group); + await this.em.removeAndFlush(group); } findAllPublic(): Promise { diff --git a/src/modules/icons/providers/icon.service.ts b/src/modules/icons/providers/icon.service.ts index dd61046..66ccfda 100644 --- a/src/modules/icons/providers/icon.service.ts +++ b/src/modules/icons/providers/icon.service.ts @@ -70,8 +70,7 @@ export class IconService { if (!icon) { throw new NotFoundException(IconMessage.NOT_FOUND); } - icon.deletedAt = new Date(); - await this.em.persistAndFlush(icon); + await this.em.removeAndFlush(icon); } }