From a94383862257be85d3d980471d8ca8c6a88f130f Mon Sep 17 00:00:00 2001 From: Andres Date: Fri, 21 Jul 2023 16:44:18 +0200 Subject: [PATCH] fix learn endpoint --- prisma/schema.prisma | 4 +-- src/modules/auth/auth.controller.ts | 2 ++ src/modules/auth/dto/save-grade.dto.ts | 7 +++++ src/modules/cards/entities/cards.entity.ts | 10 +++++++ .../cards/infrastructure/cards.repository.ts | 15 ++++++++-- .../decks/infrastructure/grades.repository.ts | 2 -- .../get-all-cards-in-deck-use-case.ts | 28 +++++++++++++++++-- .../get-random-card-in-deck-use-case.ts | 13 ++++++++- 8 files changed, 72 insertions(+), 9 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index f8ce169..49b6162 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -125,9 +125,9 @@ model deck { model grade { id String @id @default(cuid()) - deckId String @unique + deckId String cardId String @unique - userId String @unique + userId String grade Int shots Int created DateTime @default(now()) diff --git a/src/modules/auth/auth.controller.ts b/src/modules/auth/auth.controller.ts index 5e09abb..ce2c34e 100644 --- a/src/modules/auth/auth.controller.ts +++ b/src/modules/auth/auth.controller.ts @@ -159,7 +159,9 @@ export class AuthController { @Res({ passthrough: true }) res: ExpressResponse ): Promise { if (!accessToken) throw new UnauthorizedException() + await this.commandBus.execute(new LogoutCommand(accessToken)) + res.clearCookie('accessToken', { httpOnly: true, sameSite: 'none', secure: true }) res.clearCookie('refreshToken', { diff --git a/src/modules/auth/dto/save-grade.dto.ts b/src/modules/auth/dto/save-grade.dto.ts index de1bf03..13c307b 100644 --- a/src/modules/auth/dto/save-grade.dto.ts +++ b/src/modules/auth/dto/save-grade.dto.ts @@ -1,4 +1,11 @@ +import { IsNumber, IsString, Max, Min } from 'class-validator' + export class SaveGradeDto { + @IsString() cardId: string + + @IsNumber() + @Max(5) + @Min(1) grade: number } diff --git a/src/modules/cards/entities/cards.entity.ts b/src/modules/cards/entities/cards.entity.ts index 012d5de..bcd6a72 100644 --- a/src/modules/cards/entities/cards.entity.ts +++ b/src/modules/cards/entities/cards.entity.ts @@ -9,6 +9,8 @@ export class Card { shots: number answerImg: string questionImg: string + questionVideo: string + answerVideo: string rating: number created: Date updated: Date @@ -18,3 +20,11 @@ export class PaginatedCards { items: Card[] pagination: Pagination } + +export class PaginatedCardsWithGrade { + pagination: Pagination + items: CardWithGrade[] +} +export class CardWithGrade extends Card { + grades?: Array<{ grade: number }> +} diff --git a/src/modules/cards/infrastructure/cards.repository.ts b/src/modules/cards/infrastructure/cards.repository.ts index f17b452..49a499a 100644 --- a/src/modules/cards/infrastructure/cards.repository.ts +++ b/src/modules/cards/infrastructure/cards.repository.ts @@ -4,7 +4,7 @@ import { createPrismaOrderBy } from '../../../infrastructure/common/helpers/get- import { Pagination } from '../../../infrastructure/common/pagination/pagination.service' import { PrismaService } from '../../../prisma.service' import { CreateCardDto, GetAllCardsInDeckDto, UpdateCardDto } from '../dto' -import { PaginatedCards } from '../entities/cards.entity' +import { PaginatedCardsWithGrade } from '../entities/cards.entity' @Injectable() export class CardsRepository { @@ -53,6 +53,7 @@ export class CardsRepository { async findCardsByDeckId( deckId: string, + userId: string, { answer = undefined, question = undefined, @@ -60,7 +61,7 @@ export class CardsRepository { itemsPerPage, orderBy, }: GetAllCardsInDeckDto - ): Promise { + ): Promise { try { const where = { decks: { @@ -78,6 +79,16 @@ export class CardsRepository { this.prisma.card.findMany({ orderBy: createPrismaOrderBy(orderBy) || { updated: 'desc' }, where, + include: { + grades: { + where: { + userId, + }, + select: { + grade: true, + }, + }, + }, skip: (currentPage - 1) * itemsPerPage, take: itemsPerPage, }), diff --git a/src/modules/decks/infrastructure/grades.repository.ts b/src/modules/decks/infrastructure/grades.repository.ts index e789731..55b9f71 100644 --- a/src/modules/decks/infrastructure/grades.repository.ts +++ b/src/modules/decks/infrastructure/grades.repository.ts @@ -22,9 +22,7 @@ export class GradesRepository { try { return await this.prisma.grade.upsert({ where: { - userId, cardId, - deckId, }, update: { grade, diff --git a/src/modules/decks/use-cases/get-all-cards-in-deck-use-case.ts b/src/modules/decks/use-cases/get-all-cards-in-deck-use-case.ts index 6e0fb34..3e7b584 100644 --- a/src/modules/decks/use-cases/get-all-cards-in-deck-use-case.ts +++ b/src/modules/decks/use-cases/get-all-cards-in-deck-use-case.ts @@ -1,8 +1,9 @@ import { ForbiddenException, NotFoundException } from '@nestjs/common' import { CommandHandler, ICommandHandler } from '@nestjs/cqrs' +import { omit } from 'remeda' import { GetAllCardsInDeckDto } from '../../cards/dto' -import { PaginatedCards } from '../../cards/entities/cards.entity' +import { PaginatedCards, PaginatedCardsWithGrade } from '../../cards/entities/cards.entity' import { CardsRepository } from '../../cards/infrastructure/cards.repository' import { DecksRepository } from '../infrastructure/decks.repository' @@ -20,6 +21,24 @@ export class GetAllCardsInDeckHandler implements ICommandHandler { + if (card.grades.length === 0) return omit({ ...card, grade: 0 }, ['grades']) + + const grade = card.grades.reduce((acc, grade) => acc + grade.grade, 0) / card.grades.length + + return omit( + { + ...card, + grade, + }, + ['grades'] + ) + }), + } + } async execute(command: GetAllCardsInDeckCommand): Promise { const deck = await this.decksRepository.findDeckById(command.deckId) @@ -29,7 +48,12 @@ export class GetAllCardsInDeckHandler implements ICommandHandler