From 0fd1a08174f360cd5d116d4c5d756e9cfcaad37d Mon Sep 17 00:00:00 2001 From: Steve Grosbois Date: Fri, 5 Jan 2024 16:02:14 +0100 Subject: [PATCH] feat(users): Users can see and edit other users source and target languages --- src/projects/dto/update-role.dto.ts | 12 ++++++++++-- src/projects/model/project-user.model.ts | 11 +++++++++-- src/projects/projects.service.ts | 22 +++++++++++++++++++--- tests/projects/projects.controller.spec.ts | 7 ++++--- 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/projects/dto/update-role.dto.ts b/src/projects/dto/update-role.dto.ts index d11fe61..06d6c5a 100644 --- a/src/projects/dto/update-role.dto.ts +++ b/src/projects/dto/update-role.dto.ts @@ -7,7 +7,15 @@ export default class UpdateRoleDto extends BaseDto { role: Joi .string() .valid(Role.Owner, Role.Manager, Role.Editor, Role.Translator, Role.Reviewer) - .required() + .required(), + + sourceLanguagesIds: Joi + .string(), + + targetLanguagesIds: Joi + .string() }); public role: string; -} \ No newline at end of file + public sourceLanguagesIds: string; + public targetLanguagesIds: string; +} diff --git a/src/projects/model/project-user.model.ts b/src/projects/model/project-user.model.ts index 7c3ac96..475c35d 100644 --- a/src/projects/model/project-user.model.ts +++ b/src/projects/model/project-user.model.ts @@ -1,4 +1,5 @@ import Role from "../../roles/role.enum"; +import Language from "../../languages/language.entity"; /** * Represents an user within a project. @@ -11,6 +12,8 @@ export default class ProjectUser { role: Role; pending: boolean; invitationId: number | null; + sourceLanguages: Language[]; + targetLanguages: Language[]; constructor( userId: string, @@ -18,7 +21,9 @@ export default class ProjectUser { email: string, role: Role, pending: boolean, - invitationId: number | null + invitationId: number | null, + sourceLanguages: Language[] = [], + targetLanguages: Language[] = [], ) { this.userId = userId; this.username = username; @@ -26,5 +31,7 @@ export default class ProjectUser { this.role = role; this.pending = pending; this.invitationId = invitationId; + this.sourceLanguages = sourceLanguages; + this.targetLanguages = targetLanguages; } -} \ No newline at end of file +} diff --git a/src/projects/projects.service.ts b/src/projects/projects.service.ts index 1929fc4..f164c8d 100644 --- a/src/projects/projects.service.ts +++ b/src/projects/projects.service.ts @@ -362,6 +362,10 @@ export default class ProjectsService { relations: ["guest"] }) || []; + const languages = await this.languagesRepository.findBy({ + projectId: project.id + }); + const usersFromRelations = relations.map(relation => { return new ProjectUser( relation.userId, @@ -369,7 +373,9 @@ export default class ProjectsService { relation.user.email, relation.role, false, - null + null, + relation.sourceLanguagesIds ? relation.sourceLanguagesIds.split(",").map(id => languages.find(language => language.id === parseInt(id))).filter(language => language !== undefined) : [], + relation.targetLanguagesIds ? relation.targetLanguagesIds.split(",").map(id => languages.find(language => language.id === parseInt(id))).filter(language => language !== undefined) : [] ) }); @@ -379,7 +385,9 @@ export default class ProjectsService { invitation.guest.email, invitation.role, true, - invitation.id + invitation.id, + invitation.sourceLanguagesIds ? invitation.sourceLanguagesIds.split(",").map(id => languages.find(language => language.id === parseInt(id))).filter(language => language !== undefined) : [], + invitation.targetLanguagesIds ? invitation.targetLanguagesIds.split(",").map(id => languages.find(language => language.id === parseInt(id))).filter(language => language !== undefined) : [] )); return usersFromRelations.concat(usersFromInvitations); @@ -429,7 +437,13 @@ export default class ProjectsService { } } + const languages = await this.languagesRepository.findBy({ + projectId: projectId + }); + changingRelation.role = updateRoleDto.role; + changingRelation.sourceLanguagesIds = updateRoleDto.sourceLanguagesIds; + changingRelation.targetLanguagesIds = updateRoleDto.targetLanguagesIds; const updatedRelation = await this.usersProjectsRepository.save(changingRelation); const updatedUser = await this.usersRepository.findOneBy({id: userIdToUpdate}) @@ -440,7 +454,9 @@ export default class ProjectsService { updatedUser.email, updatedRelation.role, false, - null + null, + updatedRelation.sourceLanguagesIds ? updatedRelation.sourceLanguagesIds.split(",").map(id => languages.find(language => language.id === parseInt(id))).filter(language => language !== undefined) : [], + updatedRelation.targetLanguagesIds ? updatedRelation.targetLanguagesIds.split(",").map(id => languages.find(language => language.id === parseInt(id))).filter(language => language !== undefined) : [] ); } diff --git a/tests/projects/projects.controller.spec.ts b/tests/projects/projects.controller.spec.ts index 8cbe0a0..9494ec6 100644 --- a/tests/projects/projects.controller.spec.ts +++ b/tests/projects/projects.controller.spec.ts @@ -8,7 +8,7 @@ import CreateProjectDto from "../../src/projects/dto/create-project.dto"; import ProjectsModule from "../../src/projects/projects.module"; import TestDatabaseModule from "../database/test-database.module"; import CreateLanguageDto from "../../src/projects/dto/create-language.dto"; -import Language from "../../src/languages/language.entity"; +import Language, {LanguageAccess} from "../../src/languages/language.entity"; import ProjectsService from "../../src/projects/projects.service"; describe("ProjectsController", function () { @@ -125,7 +125,8 @@ describe("ProjectsController", function () { projectId: mockedProject.id, project: mockedProject, createdAt: new Date(), - updatedAt: new Date() + updatedAt: new Date(), + access: LanguageAccess.all }; describe("Create", () => { @@ -208,4 +209,4 @@ describe("ProjectsController", function () { }); }); }); -}); \ No newline at end of file +});