Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/techs select redo #125

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ Another example [here](https://co-pilot.dev/changelog)
- Add e2e tests for forms controller ([#107](https://github.com/chingu-x/chingu-dashboard-be/pull/107))
- Add e2e tests for sprint controller ([#113](https://github.com/chingu-x/chingu-dashboard-be/pull/113))
- Add new endpoint to revoke refresh token ([#116](https://github.com/chingu-x/chingu-dashboard-be/pull/116))
- Add meetingId to sprints/teams endpoint (([#1169](https://github.com/chingu-x/chingu-dashboard-be/pull/119)))
- Add meetingId to sprints/teams endpoint (([#119](https://github.com/chingu-x/chingu-dashboard-be/pull/119)))
- - Add new endpoint to select tech stack items ([#125](https://github.com/chingu-x/chingu-dashboard-be/pull/125))

### Changed

Expand Down
2 changes: 2 additions & 0 deletions prisma/migrations/20240329024350_tech_select/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "TeamTechStackItem" ADD COLUMN "isSelected" BOOLEAN NOT NULL DEFAULT false;
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ model TeamTechStackItem {
categoryId Int?
voyageTeam VoyageTeam @relation(fields: [voyageTeamId], references: [id], onUpdate: Cascade, onDelete: Cascade)
voyageTeamId Int
isSelected Boolean @default(false)

createdAt DateTime @default(now()) @db.Timestamptz()
updatedAt DateTime @updatedAt
Expand Down
39 changes: 39 additions & 0 deletions src/techs/dto/update-tech-selections.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { ApiProperty } from "@nestjs/swagger";

export class TechSelectionDto {
@ApiProperty({
description: "tech id",
example: 1,
})
techId: number;

@ApiProperty({
description: "Selected flag",
example: true,
})
isSelected: boolean;
}

export class TechCategoryDto {
@ApiProperty({
description: "tech id",
example: 1,
})
categoryId: number;

@ApiProperty({
description: "Array of tech items to update 'isSelected'.",
type: TechSelectionDto,
isArray: true,
})
techs: TechSelectionDto[];
}

export class UpdateTechSelectionsDto {
@ApiProperty({
description: "Array of categories with tech selection values",
type: TechCategoryDto,
isArray: true,
})
categories: TechCategoryDto[];
}
43 changes: 43 additions & 0 deletions src/techs/techs.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Controller,
Get,
Post,
Patch,
Body,
Param,
Delete,
Expand All @@ -13,6 +14,7 @@ import {
import { TechsService } from "./techs.service";
import { ApiOperation, ApiParam, ApiResponse, ApiTags } from "@nestjs/swagger";
import { CreateTeamTechDto } from "./dto/create-tech.dto";
import { UpdateTechSelectionsDto } from "./dto/update-tech-selections.dto";
import { TeamTechResponse, TechItemResponse } from "./techs.response";
import {
BadRequestErrorResponse,
Expand Down Expand Up @@ -181,4 +183,45 @@ export class TechsController {
) {
return this.techsService.removeVote(req, teamId, teamTechId);
}

@ApiOperation({
summary:
"Updates arrays of tech stack items, grouped by categoryId, sets 'isSelected' values",
description:
"Maximum of 3 selections per category allowed. All tech items (isSelected === true/false) are required for updated categories. Login required.",
})
@ApiResponse({
status: HttpStatus.OK,
description: "Successfully updated selected tech stack items",
type: TechItemResponse,
})
@ApiResponse({
status: HttpStatus.BAD_REQUEST,
description: "Invalid TeamId or UserId",
type: BadRequestErrorResponse,
})
@ApiResponse({
status: HttpStatus.UNAUTHORIZED,
description: "Unauthorized",
type: UnauthorizedErrorResponse,
})
@ApiParam({
name: "teamId",
description: "voyage team Id",
type: "Integer",
required: true,
example: 2,
})
@Patch("/selections")
updateTechStackSelections(
@Request() req,
@Param("teamId", ParseIntPipe) teamId: number,
@Body(ValidationPipe) updateTechSelectionsDto: UpdateTechSelectionsDto,
) {
return this.techsService.updateTechStackSelections(
req,
teamId,
updateTechSelectionsDto,
);
}
}
66 changes: 58 additions & 8 deletions src/techs/techs.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,26 @@ import {
} from "@nestjs/common";
import { PrismaService } from "../prisma/prisma.service";
import { CreateTeamTechDto } from "./dto/create-tech.dto";
import { UpdateTechSelectionsDto } from "./dto/update-tech-selections.dto";

const MAX_SELECTION_COUNT = 3;

@Injectable()
export class TechsService {
constructor(private prisma: PrismaService) {}

validateTeamId = async (teamId: number) => {
const voyageTeam = await this.prisma.voyageTeam.findUnique({
where: {
id: teamId,
},
});

if (!voyageTeam) {
throw new NotFoundException(`Team (id: ${teamId}) doesn't exist.`);
}
};

findVoyageMemberId = async (
req,
teamId: number,
Expand All @@ -28,15 +43,8 @@ export class TechsService {
};

getAllTechItemsByTeamId = async (teamId: number) => {
const voyageTeam = await this.prisma.voyageTeam.findUnique({
where: {
id: teamId,
},
});
this.validateTeamId(teamId);

if (!voyageTeam) {
throw new NotFoundException(`Team (id: ${teamId}) doesn't exist.`);
}
return this.prisma.techStackCategory.findMany({
select: {
id: true,
Expand Down Expand Up @@ -71,6 +79,48 @@ export class TechsService {
});
};

async updateTechStackSelections(
req,
teamId: number,
updateTechSelectionsDto: UpdateTechSelectionsDto,
) {
const categories = updateTechSelectionsDto.categories;

//count selections in categories for exceeding MAX_SELECT_COUNT
categories.forEach((category) => {
const selectCount = category.techs.reduce(
(acc: number, tech) => acc + (tech.isSelected ? 1 : 0),
0,
);
if (selectCount > MAX_SELECTION_COUNT)
throw new BadRequestException(
`Only ${MAX_SELECTION_COUNT} selections allowed per category`,
);
});

const voyageMemberId = await this.findVoyageMemberId(req, teamId);
if (!voyageMemberId)
throw new BadRequestException("Invalid User or Team Id");

//extract techs to an array for .map
const techsArray: any[] = [];
categories.forEach((category) => {
category.techs.forEach((tech) => techsArray.push(tech));
});
return this.prisma.$transaction(
techsArray.map((tech) => {
return this.prisma.teamTechStackItem.update({
where: {
id: tech.techId,
},
data: {
isSelected: tech.isSelected,
},
});
}),
);
}

async addNewTeamTech(
req,
teamId: number,
Expand Down
Loading
Loading