Skip to content

Commit

Permalink
feat(languages): Handle source and target languages
Browse files Browse the repository at this point in the history
  • Loading branch information
kwiky committed Jan 2, 2024
1 parent 604e553 commit 350ddbc
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 16 deletions.
12 changes: 10 additions & 2 deletions src/invitations/dto/create-invitation.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@ export default class CreateInvitationDto extends BaseDto {

projectId: Joi
.number()
.required()
.required(),

sourceLanguagesIds: Joi
.string(),

targetLanguagesIds: Joi
.string()
});

public projectId: number;
public email: string;
public role: string;
}
public sourceLanguagesIds: string;
public targetLanguagesIds: string;
}
10 changes: 9 additions & 1 deletion src/invitations/invitation.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,12 @@ export default class Invitation {
@CreateDateColumn()
@ApiProperty()
readonly createdAt: Date;
}

@Column({nullable: true})
@ApiProperty()
sourceLanguagesIds: string;

@Column({nullable: true})
@ApiProperty()
targetLanguagesIds: string;
}
18 changes: 13 additions & 5 deletions src/invitations/invitation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export default class InvitationService {
invitation.guest = guest;
invitation.owner = owner;
invitation.project = project;
invitation.sourceLanguagesIds = createInvitationDto.sourceLanguagesIds;
invitation.targetLanguagesIds = createInvitationDto.targetLanguagesIds;
invitation.role = <Role>createInvitationDto.role;
return await this.invitationRepository.save(invitation);
}
Expand All @@ -57,19 +59,25 @@ export default class InvitationService {
}

public async acceptInvitation(userId: string, invitationId: number): Promise<void> {
const invitation = await this.invitationRepository.findOneById(invitationId);
const invitation = await this.invitationRepository.findOneBy({id: invitationId});
if (!invitation) {
throw new NotFoundException();
}
if (userId !== invitation.guestId) {
throw new ForbiddenException(null, "Can't accept invitation for someone else");
}
await this.projectsService.createUserProjectRelation(userId, invitation.projectId, invitation.role);
await this.projectsService.createUserProjectRelation(
userId,
invitation.projectId,
invitation.role,
invitation.sourceLanguagesIds,
invitation.targetLanguagesIds
);
await this.invitationRepository.delete(invitationId);
}

public async declineInvitation(userId: string, invitationId: number): Promise<void> {
const invitation = await this.invitationRepository.findOneById(invitationId);
const invitation = await this.invitationRepository.findOneBy({id: invitationId});
if (!invitation) {
throw new NotFoundException();
}
Expand All @@ -80,7 +88,7 @@ export default class InvitationService {
}

public async deleteInvitation(userId: string, invitationId: number): Promise<void> {
const invitation = await this.invitationRepository.findOneById(invitationId);
const invitation = await this.invitationRepository.findOneBy({id: invitationId});
if (!invitation) {
throw new NotFoundException();
}
Expand All @@ -94,4 +102,4 @@ export default class InvitationService {
}
await this.invitationRepository.delete(invitationId);
}
}
}
12 changes: 11 additions & 1 deletion src/languages/language.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import {PostgresUniqueKeys} from "../data/database/postgres-unique-keys.enum";

export const ProjectLanguagesTableName: string = "project_languages";


export enum LanguageAccess {
all = "all",
source = "source",
target = "target"
}

@Entity(ProjectLanguagesTableName)
@Unique(PostgresUniqueKeys.LanguageInProject, ["name", "project"])
export default class Language {
Expand Down Expand Up @@ -32,4 +39,7 @@ export default class Language {
@UpdateDateColumn()
@ApiProperty()
readonly updatedAt: Date;
}

@ApiProperty()
public access: LanguageAccess = LanguageAccess.all;
}
31 changes: 26 additions & 5 deletions src/projects/projects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {In, Repository} from "typeorm";
import Group, {DefaultGroupName} from "../groups/group.entity";
import GroupService from "../groups/group.service";
import Invitation from "../invitations/invitation.entity";
import Language from "../languages/language.entity";
import Language, {LanguageAccess} from "../languages/language.entity";
import Role from "../roles/role.enum";
import TranslationService from "../translation/translation.service";
import TranslationKey from "../translation/translation_key.entity";
Expand Down Expand Up @@ -45,11 +45,13 @@ export default class ProjectsService {

private readonly logger = new Logger("ProjectsService");

public async createUserProjectRelation(userId: string, projectId: number, role: Role): Promise<any> {
public async createUserProjectRelation(userId: string, projectId: number, role: Role, sourceLanguagesIds: string = null, targetLanguagesIds: string = null): Promise<any> {
return this.usersProjectsRepository.save({
"projectId": projectId,
"userId": userId,
"role": role
"role": role,
"sourceLanguagesIds": sourceLanguagesIds,
"targetLanguagesIds": targetLanguagesIds,
});
}

Expand Down Expand Up @@ -243,18 +245,37 @@ export default class ProjectsService {

public async getAllLanguages(userId: string, projectId: number): Promise<Language[]> {
const project = await this.getProject(userId, projectId);
return await this.languagesRepository.find({
const languages = await this.languagesRepository.find({
where: {
project: {
id: project.id
}
}
});
const userProject = project.userProjects.find(relation => relation.userId === userId);
if (userProject.sourceLanguagesIds === null || userProject.targetLanguagesIds === null) {
return languages;
} else {
const sourceLanguagesIds = userProject.sourceLanguagesIds.split(",").map(id => parseInt(id));
const targetLanguagesIds = userProject.targetLanguagesIds.split(",").map(id => parseInt(id));
const languagesIds = sourceLanguagesIds.concat(targetLanguagesIds);
return languages
.filter(language => languagesIds.indexOf(language.id) >= 0)
.map(language => {
if (sourceLanguagesIds.indexOf(language.id) >= 0) {
language.access = LanguageAccess.source;
}
if (targetLanguagesIds.indexOf(language.id) >= 0) {
language.access = LanguageAccess.target;
}
return language
});
}
}

public async getLanguage(userId: string, projectId: number, languageId: number): Promise<Language> {
const project = await this.getProject(userId, projectId);
const language: Language = await this.languagesRepository.findOneById(languageId);
const language: Language = await this.languagesRepository.findOneBy({id: languageId});

if (!language || language.projectId != project.id) {
throw new NotFoundException();
Expand Down
10 changes: 9 additions & 1 deletion src/users-projects/user_project.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ export default class UserProject {
@Column()
@ApiProperty()
public role: Role;
}

@Column({nullable: true})
@ApiProperty()
sourceLanguagesIds: string;

@Column({nullable: true})
@ApiProperty()
targetLanguagesIds: string;
}
4 changes: 3 additions & 1 deletion tests/invitations/invitations.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ describe("Invitations", () => {
.send(new CreateInvitationDto({
email: "[email protected]",
projectId: populatedProjects[0].id,
role: Role.Manager
role: Role.Manager,
sourceLanguagesIds: "1",
targetLanguagesIds: "2"
}));
expect(invitationResp.status).toEqual(201);
});
Expand Down

0 comments on commit 350ddbc

Please sign in to comment.