Skip to content

Commit

Permalink
feat(users): Users can see and edit other users source and target lan…
Browse files Browse the repository at this point in the history
…guages
  • Loading branch information
kwiky committed Jan 5, 2024
1 parent 9436223 commit 0fd1a08
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
12 changes: 10 additions & 2 deletions src/projects/dto/update-role.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
public sourceLanguagesIds: string;
public targetLanguagesIds: string;
}
11 changes: 9 additions & 2 deletions src/projects/model/project-user.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Role from "../../roles/role.enum";
import Language from "../../languages/language.entity";

/**
* Represents an user within a project.
Expand All @@ -11,20 +12,26 @@ export default class ProjectUser {
role: Role;
pending: boolean;
invitationId: number | null;
sourceLanguages: Language[];
targetLanguages: Language[];

constructor(
userId: string,
username: string | null,
email: string,
role: Role,
pending: boolean,
invitationId: number | null
invitationId: number | null,
sourceLanguages: Language[] = [],
targetLanguages: Language[] = [],
) {
this.userId = userId;
this.username = username;
this.email = email;
this.role = role;
this.pending = pending;
this.invitationId = invitationId;
this.sourceLanguages = sourceLanguages;
this.targetLanguages = targetLanguages;
}
}
}
22 changes: 19 additions & 3 deletions src/projects/projects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,14 +362,20 @@ 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,
relation.user.username,
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) : []
)
});

Expand All @@ -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);
Expand Down Expand Up @@ -429,7 +437,13 @@ export default class ProjectsService {
}
}

const languages = await this.languagesRepository.findBy({
projectId: projectId
});

changingRelation.role = <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})
Expand All @@ -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) : []
);
}

Expand Down
7 changes: 4 additions & 3 deletions tests/projects/projects.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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", () => {
Expand Down Expand Up @@ -208,4 +209,4 @@ describe("ProjectsController", function () {
});
});
});
});
});

0 comments on commit 0fd1a08

Please sign in to comment.