Skip to content

Commit

Permalink
FIX - clean code and fix some condition
Browse files Browse the repository at this point in the history
  • Loading branch information
juliecoust committed Jul 31, 2024
1 parent 5a36373 commit 16e3b60
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/domain/repositories/instrument_model-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export class InstrumentModelRepositoryImpl implements InstrumentModelRepository
this.instrument_modelDataSource = instrument_modelDataSource
}

async getOneInstrumentModel(instrument_model_id: InstrumentModelRequestModel): Promise<InstrumentModelResponseModel | null> {
const result = await this.instrument_modelDataSource.getOne(instrument_model_id);
async getOneInstrumentModel(instrument_model_request: InstrumentModelRequestModel): Promise<InstrumentModelResponseModel | null> {
const result = await this.instrument_modelDataSource.getOne(instrument_model_request);
return result;
}

Expand Down
23 changes: 15 additions & 8 deletions src/domain/repositories/privilege-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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<boolean> {
Expand All @@ -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

}
Expand Down Expand Up @@ -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];
}

Expand Down
6 changes: 2 additions & 4 deletions src/domain/repositories/user-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,11 @@ export class UserRepositoryImpl implements UserRepository {
return publicUser
}

async isDeleted(user: UserResponseModel | null): Promise<boolean> {
if (!user) return false;
async isDeleted(user: UserResponseModel): Promise<boolean> {
return user.deleted ? true : false
}

async isValidated(user: UserResponseModel | null): Promise<boolean> {
if (!user) return false;
async isValidated(user: UserResponseModel): Promise<boolean> {
return user.valid_email ? true : false
}

Expand Down
5 changes: 1 addition & 4 deletions src/domain/use-cases/auth/reset-password-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
8 changes: 1 addition & 7 deletions src/domain/use-cases/auth/reset-password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

}
}
13 changes: 8 additions & 5 deletions src/domain/use-cases/project/create-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 2 additions & 2 deletions src/domain/use-cases/project/search-projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class SearchProject implements SearchProjectsUseCase {
page: 1
});

if (!instrumentModels) {
if (instrumentModels.total === 0) {
throw new Error("Instrument model not found");
}

Expand All @@ -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 });
Expand Down

0 comments on commit 16e3b60

Please sign in to comment.