Skip to content

Commit

Permalink
Merge pull request #49 from Horizontal-org/1.1
Browse files Browse the repository at this point in the history
[WIP] 1.1
  • Loading branch information
juandans01 authored Feb 16, 2024
2 parents 2bbf4b7 + 484f9ef commit e8e69a8
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 11 deletions.
21 changes: 21 additions & 0 deletions src/migrations/1707919420747-alter_users_table_fk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {MigrationInterface, QueryRunner} from "typeorm";

export class alterUsersTableFk1707919420747 implements MigrationInterface {

public async up(queryRunner: QueryRunner): Promise<void> {
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<void> {
}

}
19 changes: 19 additions & 0 deletions src/migrations/1707923513216-alter_reports_table_fk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {MigrationInterface, QueryRunner} from "typeorm";

export class alterReportsTableFk1707923513216 implements MigrationInterface {

public async up(queryRunner: QueryRunner): Promise<void> {
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<void> {
}

}
20 changes: 18 additions & 2 deletions src/modules/project/services/get-by-id.project.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProjectEntity>,
@InjectRepository(UserEntity)
private userRepository: Repository<UserEntity>,
) {}

async execute(projectId: string): Promise<ProjectEntity> {
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
}
}
5 changes: 4 additions & 1 deletion src/modules/user/domain/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
PrimaryGeneratedColumn,
JoinColumn,
OneToOne,
BeforeRemove,
} from 'typeorm';

import { ReportEntity } from 'modules/report/domain/report.entity';
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/modules/user/services/batch-delete.user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export class BatchDeleteUsersService implements IBatchDeleteUsersService {
async execute(toDelete: Array<string>): Promise<boolean> {
await getConnection()
.createQueryBuilder()
.update(UserEntity)
.set({ deletedAt: new Date() })
.delete()
.from(UserEntity)
.where('id IN (:...toDelete)', { toDelete: toDelete }) //delete users by username
.execute();

Expand Down
5 changes: 2 additions & 3 deletions src/modules/user/services/delete-by-id.user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ export class DeleteByIdUserService implements IDeleteByIdUserService {
) {}

async execute(userId: string): Promise<boolean> {
const user = await this.userRepository.findOne(userId)
user.update({ deletedAt: new Date() });
const { affected } = await this.userRepository.delete({ id: userId });

return true;
return !!(affected);
}
}
2 changes: 1 addition & 1 deletion src/modules/user/services/find-by-id.user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class FindByidUserService implements IFindByIdUserService {
) {}

async execute(id: string): Promise<UserEntity> {
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;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/user/services/find-by-username.user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class FindByUsernameUserService implements IFindByUsernameUserService {
) {}

async execute(username: string): Promise<UserEntity> {
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()
Expand Down
1 change: 0 additions & 1 deletion src/modules/user/services/list.user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}%` });
Expand Down

0 comments on commit e8e69a8

Please sign in to comment.