diff --git a/src/migrations/1707919420747-alter_users_table_fk.ts b/src/migrations/1707919420747-alter_users_table_fk.ts new file mode 100644 index 0000000..485859d --- /dev/null +++ b/src/migrations/1707919420747-alter_users_table_fk.ts @@ -0,0 +1,21 @@ +import {MigrationInterface, QueryRunner} from "typeorm"; + +export class alterUsersTableFk1707919420747 implements MigrationInterface { + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + "ALTER TABLE projects_users DROP FOREIGN KEY FK_274bd757ae91379bf033a2daccd;", + + ) + + await queryRunner.query( + "ALTER TABLE projects_users ADD FOREIGN KEY (user_id) REFERENCES user_entity(id) ON DELETE CASCADE;" + ) + + } + + + public async down(queryRunner: QueryRunner): Promise { + } + +} diff --git a/src/migrations/1707923513216-alter_reports_table_fk.ts b/src/migrations/1707923513216-alter_reports_table_fk.ts new file mode 100644 index 0000000..6dbc078 --- /dev/null +++ b/src/migrations/1707923513216-alter_reports_table_fk.ts @@ -0,0 +1,19 @@ +import {MigrationInterface, QueryRunner} from "typeorm"; + +export class alterReportsTableFk1707923513216 implements MigrationInterface { + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + "ALTER TABLE report_entity DROP FOREIGN KEY FK_8a0601e87d9a8c43442f906eca3;", + + ) + + await queryRunner.query( + "ALTER TABLE report_entity ADD FOREIGN KEY (authorId) REFERENCES user_entity(id) ON DELETE SET NULL;" + ) + } + + public async down(queryRunner: QueryRunner): Promise { + } + +} diff --git a/src/modules/project/services/get-by-id.project.service.ts b/src/modules/project/services/get-by-id.project.service.ts index e9ee290..9a93e83 100644 --- a/src/modules/project/services/get-by-id.project.service.ts +++ b/src/modules/project/services/get-by-id.project.service.ts @@ -4,19 +4,35 @@ import { InjectRepository } from '@nestjs/typeorm'; import { ProjectEntity } from '../domain'; import { IGetByIdProjectService } from '../interfaces/service/get-by-id.project.service.interface'; - +import { RolesUser, UserEntity } from 'modules/user/domain'; +import { clone } from 'lodash' @Injectable() export class GetByIdProjectService implements IGetByIdProjectService { constructor( @InjectRepository(ProjectEntity) private projectRepository: Repository, + @InjectRepository(UserEntity) + private userRepository: Repository, ) {} async execute(projectId: string): Promise { - return this.projectRepository.findOne(projectId, { relations: [ + let project = await this.projectRepository.findOne(projectId, { relations: [ 'users', 'resources', 'resources.projects' ]}); + + const adminUsers = await this.userRepository.find({ role: RolesUser.ADMIN }) + + let newUsers = clone(project.users) + adminUsers.forEach((u) => { + const hasUser = !!(project.users.find(pu => pu.id == u.id)) + if (!hasUser) { + newUsers.push(u) + } + }) + + project.users = newUsers + return project } } diff --git a/src/modules/user/domain/user.entity.ts b/src/modules/user/domain/user.entity.ts index 1ce64cd..a5a9785 100644 --- a/src/modules/user/domain/user.entity.ts +++ b/src/modules/user/domain/user.entity.ts @@ -8,6 +8,7 @@ import { PrimaryGeneratedColumn, JoinColumn, OneToOne, + BeforeRemove, } from 'typeorm'; import { ReportEntity } from 'modules/report/domain/report.entity'; @@ -73,7 +74,9 @@ export class UserEntity { @JoinColumn({ name: "user_id" }) recovery_keys: RecoveryKeyEntity[]; - @ManyToMany(() => ProjectEntity, project => project.users) + @ManyToMany(() => ProjectEntity, project => project.users, { + onDelete: 'CASCADE' + }) projects: ProjectEntity[]; @BeforeInsert() diff --git a/src/modules/user/services/batch-delete.user.service.ts b/src/modules/user/services/batch-delete.user.service.ts index 15b28dd..b3fdc9c 100644 --- a/src/modules/user/services/batch-delete.user.service.ts +++ b/src/modules/user/services/batch-delete.user.service.ts @@ -14,8 +14,8 @@ export class BatchDeleteUsersService implements IBatchDeleteUsersService { async execute(toDelete: Array): Promise { await getConnection() .createQueryBuilder() - .update(UserEntity) - .set({ deletedAt: new Date() }) + .delete() + .from(UserEntity) .where('id IN (:...toDelete)', { toDelete: toDelete }) //delete users by username .execute(); diff --git a/src/modules/user/services/delete-by-id.user.service.ts b/src/modules/user/services/delete-by-id.user.service.ts index 4451c7a..8f8980f 100644 --- a/src/modules/user/services/delete-by-id.user.service.ts +++ b/src/modules/user/services/delete-by-id.user.service.ts @@ -12,9 +12,8 @@ export class DeleteByIdUserService implements IDeleteByIdUserService { ) {} async execute(userId: string): Promise { - const user = await this.userRepository.findOne(userId) - user.update({ deletedAt: new Date() }); + const { affected } = await this.userRepository.delete({ id: userId }); - return true; + return !!(affected); } } diff --git a/src/modules/user/services/find-by-id.user.service.ts b/src/modules/user/services/find-by-id.user.service.ts index c66e15b..cdd3c30 100644 --- a/src/modules/user/services/find-by-id.user.service.ts +++ b/src/modules/user/services/find-by-id.user.service.ts @@ -12,7 +12,7 @@ export class FindByidUserService implements IFindByIdUserService { ) {} async execute(id: string): Promise { - const user = await this.userRepository.findOne({ where: { id: id, deletedAt: null } }); + const user = await this.userRepository.findOne({ where: { id: id } }); if (!user) throw new NotFoundUserException(); return user; diff --git a/src/modules/user/services/find-by-username.user.service.ts b/src/modules/user/services/find-by-username.user.service.ts index aa520ff..b02aeee 100644 --- a/src/modules/user/services/find-by-username.user.service.ts +++ b/src/modules/user/services/find-by-username.user.service.ts @@ -15,7 +15,7 @@ export class FindByUsernameUserService implements IFindByUsernameUserService { ) {} async execute(username: string): Promise { - const user = await this.userRepository.findOne({ where: { username: username, deletedAt: null } }); + const user = await this.userRepository.findOne({ where: { username: username } }); const ability = this.abilityFactory.createForUser(user) if (ability.cannot(Actions.Read, user)) throw new UnauthorizedException() diff --git a/src/modules/user/services/list.user.service.ts b/src/modules/user/services/list.user.service.ts index 1ca3aca..bcf6eea 100644 --- a/src/modules/user/services/list.user.service.ts +++ b/src/modules/user/services/list.user.service.ts @@ -25,7 +25,6 @@ export class ListUserService implements IListUserService { .createQueryBuilder('user') .skip(skip) .take(take) - .where({ deletedAt: null }) if (search && search.length > 0) { query.andWhere('user.username like :search', { search: `%${search}%` });