diff --git a/src/project-teams/dto/set-project-interest.dto.ts b/src/project-teams/dto/set-project-interest.dto.ts index 52cf98d..86d2979 100644 --- a/src/project-teams/dto/set-project-interest.dto.ts +++ b/src/project-teams/dto/set-project-interest.dto.ts @@ -1,15 +1,17 @@ -import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'; +import { ApiPropertyOptional } from '@nestjs/swagger'; import { IsOptional, IsString, MaxLength, MinLength } from 'class-validator'; export class SetProjectInterestDto { - @ApiProperty({ + @ApiPropertyOptional({ example: 'Front-end', - description: 'Cargo da ideia em que a pessoa quer atuar', + description: + 'Cargo da ideia em que a pessoa quer atuar. Sem cargo, vale como interesse geral no projeto.', }) + @IsOptional() @IsString() @MinLength(2) @MaxLength(40) - role: string; + role?: string; @ApiPropertyOptional({ example: 'Tenho 2 anos de React e tempo nos fins de semana.', diff --git a/src/project-teams/infrastructure/persistence/relational/repositories/project-idea.repository.ts b/src/project-teams/infrastructure/persistence/relational/repositories/project-idea.repository.ts index d627180..51b6834 100644 --- a/src/project-teams/infrastructure/persistence/relational/repositories/project-idea.repository.ts +++ b/src/project-teams/infrastructure/persistence/relational/repositories/project-idea.repository.ts @@ -57,12 +57,27 @@ export class ProjectIdeaRelationalRepository implements ProjectIdeaRepository { }); const teamByIdea = new Map(teams.map((t) => [t.ideaId, t.id])); - return entities.map((entity) => - ProjectMapper.ideaToDomain(entity, { + const authorIds = [...new Set(entities.map((entity) => entity.authorId))]; + const profiles = await this.interestRepository.manager + .getRepository(GamificationProfileEntity) + .find({ + where: { userId: In(authorIds) }, + select: { userId: true, username: true }, + }); + const usernameByUser = new Map( + profiles.map((profile) => [profile.userId, profile.username]), + ); + + return entities.map((entity) => { + const idea = ProjectMapper.ideaToDomain(entity, { interestCount: countByIdea.get(entity.id) ?? 0, teamId: teamByIdea.get(entity.id) ?? null, - }), - ); + }); + if (idea.author) { + idea.author.username = usernameByUser.get(entity.authorId) ?? null; + } + return idea; + }); } private async findList( diff --git a/src/project-teams/project-ideas.service.ts b/src/project-teams/project-ideas.service.ts index 0afdf94..3eb9342 100644 --- a/src/project-teams/project-ideas.service.ts +++ b/src/project-teams/project-ideas.service.ts @@ -29,6 +29,9 @@ const PUBLIC_STATUSES = [ ProjectIdeaStatus.TEAM_FORMED, ]; +/** Interesse geral no projeto, sem escolher um cargo. */ +const DEFAULT_INTEREST_ROLE = 'Interessado'; + @Injectable() export class ProjectIdeasService { constructor( @@ -218,21 +221,24 @@ export class ProjectIdeasService { dto: SetProjectInterestDto, ) { const idea = await this.findPublicDetail(id); - const roles = await this.ensureRoles(idea); - if (!roles.some((item) => item.name === dto.role)) { - throw new UnprocessableEntityException({ - status: HttpStatus.UNPROCESSABLE_ENTITY, - errors: { role: 'Este cargo não existe neste projeto.' }, - }); + const role = dto.role?.trim() || DEFAULT_INTEREST_ROLE; + if (dto.role) { + const roles = await this.ensureRoles(idea); + if (!roles.some((item) => item.name === role)) { + throw new UnprocessableEntityException({ + status: HttpStatus.UNPROCESSABLE_ENTITY, + errors: { role: 'Este cargo não existe neste projeto.' }, + }); + } } const existing = ( await this.ideaRepository.findUserInterests(id, userId) - ).find((item) => item.role === dto.role); + ).find((item) => item.role === role); const interest = await this.ideaRepository.upsertInterest( id, userId, - dto.role, + role, dto.message?.trim() || null, ); @@ -241,8 +247,8 @@ export class ProjectIdeasService { .create({ userId: idea.authorId, type: NotificationType.PROJECT_IDEA_INTEREST, - title: `Nova participação em: ${idea.title}`, - body: 'Alguém entrou em um cargo do seu projeto. Veja o time na página dele.', + title: `Novo interessado em: ${idea.title}`, + body: 'Alguém quer participar do seu projeto. Veja quem tem interesse na página dele.', relatedId: idea.id, link: `/projetos/${idea.id}`, payload: { ideaId: idea.id, userId },