diff --git a/src/domain/repositories/instrument_model-repository.ts b/src/domain/repositories/instrument_model-repository.ts index 993d72f..008bd2e 100644 --- a/src/domain/repositories/instrument_model-repository.ts +++ b/src/domain/repositories/instrument_model-repository.ts @@ -15,8 +15,8 @@ export class InstrumentModelRepositoryImpl implements InstrumentModelRepository this.instrument_modelDataSource = instrument_modelDataSource } - async getOneInstrumentModel(instrument_model_id: InstrumentModelRequestModel): Promise { - const result = await this.instrument_modelDataSource.getOne(instrument_model_id); + async getOneInstrumentModel(instrument_model_request: InstrumentModelRequestModel): Promise { + const result = await this.instrument_modelDataSource.getOne(instrument_model_request); return result; } diff --git a/src/domain/repositories/privilege-repository.ts b/src/domain/repositories/privilege-repository.ts index 071f34c..2f02ae1 100644 --- a/src/domain/repositories/privilege-repository.ts +++ b/src/domain/repositories/privilege-repository.ts @@ -16,8 +16,12 @@ export class PrivilegeRepositoryImpl implements PrivilegeRepository { const privileges = await this.publicToPrivate(publicPrivilege) let result = 0; for (const privilege of privileges) { - await this.privilegeDataSource.create(privilege) - result += 1 + try { + await this.privilegeDataSource.create(privilege) + result += 1 + } catch (error) { + console.error(error) + } } return result; } @@ -102,6 +106,7 @@ export class PrivilegeRepositoryImpl implements PrivilegeRepository { }]; const privileges = await this.getPrivilegesByFilter(filter) return privileges.items.map(privilege => privilege.project_id); + // [ ...new Set(privileges.items.map(privilege => privilege.project_id);)] } // Get all projects where a list of user ids have at least one privilege @@ -112,7 +117,7 @@ export class PrivilegeRepositoryImpl implements PrivilegeRepository { value: user_ids }]; const privileges = await this.getPrivilegesByFilter(filter) - return privileges.items.map(privilege => privilege.project_id); + return [...new Set(privileges.items.map(privilege => privilege.project_id))] } // Get all projects where a list of user ids are contact @@ -128,7 +133,7 @@ export class PrivilegeRepositoryImpl implements PrivilegeRepository { value: true }]; const privileges = await this.getPrivilegesByFilter(filter) - return privileges.items.map(privilege => privilege.project_id); + return [...new Set(privileges.items.map(privilege => privilege.project_id))] } // Get all projects where a list of user ids are managers @@ -144,7 +149,7 @@ export class PrivilegeRepositoryImpl implements PrivilegeRepository { value: "manager" }]; const privileges = await this.getPrivilegesByFilter(filter) - return privileges.items.map(privilege => privilege.project_id); + return [...new Set(privileges.items.map(privilege => privilege.project_id))] } // Get all projects where a list of user ids are members @@ -160,7 +165,7 @@ export class PrivilegeRepositoryImpl implements PrivilegeRepository { value: "member" }]; const privileges = await this.getPrivilegesByFilter(filter) - return privileges.items.map(privilege => privilege.project_id); + return [...new Set(privileges.items.map(privilege => privilege.project_id))] } async isGranted(privilege: PrivilegeRequestModel): Promise { @@ -181,7 +186,7 @@ export class PrivilegeRepositoryImpl implements PrivilegeRepository { } ], sort_by: [{ sort_by: "privilege_creation_date", order_by: "desc" }], - limit: 1000, + limit: 1, page: 1 } @@ -234,7 +239,9 @@ export class PrivilegeRepositoryImpl implements PrivilegeRepository { } const privileges = await this.privilegeDataSource.getAll(prepare_options) - + if (privileges.items.length === 0) { + throw new Error("No contact found for this project") + } return privileges.items[0]; } diff --git a/src/domain/repositories/user-repository.ts b/src/domain/repositories/user-repository.ts index dc50167..2ce1cef 100644 --- a/src/domain/repositories/user-repository.ts +++ b/src/domain/repositories/user-repository.ts @@ -247,13 +247,11 @@ export class UserRepositoryImpl implements UserRepository { return publicUser } - async isDeleted(user: UserResponseModel | null): Promise { - if (!user) return false; + async isDeleted(user: UserResponseModel): Promise { return user.deleted ? true : false } - async isValidated(user: UserResponseModel | null): Promise { - if (!user) return false; + async isValidated(user: UserResponseModel): Promise { return user.valid_email ? true : false } diff --git a/src/domain/use-cases/auth/reset-password-request.ts b/src/domain/use-cases/auth/reset-password-request.ts index 5a310bf..70b70cb 100644 --- a/src/domain/use-cases/auth/reset-password-request.ts +++ b/src/domain/use-cases/auth/reset-password-request.ts @@ -21,12 +21,9 @@ export class ResetPasswordRequest implements ResetPasswordRequestUseCase { const preexistant_user = await this.userRepository.getUser(user) if (!preexistant_user) throw new Error("User does not exist"); - // User should not be deleted + // User should not be deleted nor unvalidated await this.userRepository.ensureUserCanBeUsed(preexistant_user.user_id); - // Is the user validated ? - if (!preexistant_user.valid_email) throw new Error("User email is not validated"); - // Generate a reset password token and add it to the user const updateCount = await this.userRepository.setResetPasswordCode({ user_id: preexistant_user.user_id }) if (updateCount === 0) throw new Error("Cannot set password reset code"); diff --git a/src/domain/use-cases/auth/reset-password.ts b/src/domain/use-cases/auth/reset-password.ts index 9683f3a..18e9a35 100644 --- a/src/domain/use-cases/auth/reset-password.ts +++ b/src/domain/use-cases/auth/reset-password.ts @@ -28,17 +28,11 @@ export class ResetPassword implements ResetPasswordUseCase { reset_password_code: decoded_token.reset_password_code } ) - // If the user does not exist or the reset_password_code is not valid + // If the user reset_password_code is not valid if (!preexistant_user) throw new Error("User does not exist or reset_password_code is not valid"); - // is the user validated ? - if (!preexistant_user.valid_email) throw new Error("User email is not validated"); - // Change the password nb_of_updated_user = await this.userRepository.changePassword({ ...preexistant_user, ...credentials }) if (nb_of_updated_user == 0) throw new Error("Cannot change password"); - // TODO clean - //if (updatedUser.reset_password_code !== null) throw new Error("User reset password code not reset"); - } } \ No newline at end of file diff --git a/src/domain/use-cases/project/create-project.ts b/src/domain/use-cases/project/create-project.ts index dae8941..040e137 100644 --- a/src/domain/use-cases/project/create-project.ts +++ b/src/domain/use-cases/project/create-project.ts @@ -90,13 +90,16 @@ export class CreateProject implements CreateProjectUseCase { // Create and retrieve privileges for the project private async createAndRetrievePrivileges(projectId: number, publicProject: PublicProjectRequestCreationModel) { // Create the privileges for the project - const createdPrivilegesIds: number = await this.privilegeRepository.createPrivileges({ project_id: projectId, members: publicProject.members, managers: publicProject.managers, contact: publicProject.contact }) - if (!createdPrivilegesIds) { throw new Error("Cannot create privileges for project"); } - + const number_of_created_privileges: number = await this.privilegeRepository.createPrivileges({ project_id: projectId, members: publicProject.members, managers: publicProject.managers, contact: publicProject.contact }) + // Ensure privileges were created correctly + if (number_of_created_privileges !== publicProject.members.length + publicProject.managers.length) { + throw new Error("Privileges partially created, please check members, managers and contact"); + } // Retrieve the newly created privileges const privileges = await this.privilegeRepository.getPublicPrivileges({ project_id: projectId }); - if (!privileges) { throw new Error("Cannot find created privileges"); } - + if (privileges.members.length + privileges.managers.length !== publicProject.members.length + publicProject.managers.length) { + throw new Error("Cant find created privileges, please check members, managers and contact"); + } return privileges; } } diff --git a/src/domain/use-cases/project/search-projects.ts b/src/domain/use-cases/project/search-projects.ts index 648fd4d..91ecc6b 100644 --- a/src/domain/use-cases/project/search-projects.ts +++ b/src/domain/use-cases/project/search-projects.ts @@ -79,7 +79,7 @@ export class SearchProject implements SearchProjectsUseCase { page: 1 }); - if (!instrumentModels) { + if (instrumentModels.total === 0) { throw new Error("Instrument model not found"); } @@ -105,7 +105,7 @@ export class SearchProject implements SearchProjectsUseCase { options.filter = options.filter.filter(f => f.field !== "for_managing"); // If filter is for managing = true, get all projects where current_user have at least one privilege - if (managingFilter.value === "true") { + if (managingFilter.value === "true" || managingFilter.value === true) { const projectIds = await this.privilegeRepository.getProjectsByUser({ user_id: current_user.user_id }); // Add the new filter to the list of filters options.filter.push({ field: "project_id", operator: "IN", value: projectIds });