From 17b5c05685781938d4bebaa6671cbc4a6c42524b Mon Sep 17 00:00:00 2001 From: Andres Date: Mon, 7 Aug 2023 16:00:31 +0200 Subject: [PATCH] fix grades --- prisma/schema.prisma | 3 +- src/modules/cards/entities/cards.entity.ts | 2 +- .../cards/infrastructure/cards.repository.ts | 1 - .../decks/infrastructure/grades.repository.ts | 6 ++-- .../get-all-cards-in-deck-use-case.ts | 28 +++++++++------ .../get-random-card-in-deck-use-case.ts | 35 ++++++++++--------- 6 files changed, 43 insertions(+), 32 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index da45953..de41725 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -126,7 +126,7 @@ model deck { model grade { id String @id @default(cuid()) deckId String - cardId String @unique + cardId String userId String grade Int shots Int @@ -136,6 +136,7 @@ model grade { card card @relation(fields: [cardId], references: [id], onDelete: Cascade) deck deck @relation(fields: [deckId], references: [id], onDelete: Cascade) + @@unique([cardId, userId]) @@index([userId]) @@index([deckId]) @@index([cardId]) diff --git a/src/modules/cards/entities/cards.entity.ts b/src/modules/cards/entities/cards.entity.ts index bcd6a72..41eed80 100644 --- a/src/modules/cards/entities/cards.entity.ts +++ b/src/modules/cards/entities/cards.entity.ts @@ -17,7 +17,7 @@ export class Card { } export class PaginatedCards { - items: Card[] + items: Omit[] pagination: Pagination } diff --git a/src/modules/cards/infrastructure/cards.repository.ts b/src/modules/cards/infrastructure/cards.repository.ts index 49a499a..73d6901 100644 --- a/src/modules/cards/infrastructure/cards.repository.ts +++ b/src/modules/cards/infrastructure/cards.repository.ts @@ -111,7 +111,6 @@ export class CardsRepository { grades: { where: { userId, - deckId, }, }, }, diff --git a/src/modules/decks/infrastructure/grades.repository.ts b/src/modules/decks/infrastructure/grades.repository.ts index 86eae5f..a5b575a 100644 --- a/src/modules/decks/infrastructure/grades.repository.ts +++ b/src/modules/decks/infrastructure/grades.repository.ts @@ -22,7 +22,10 @@ export class GradesRepository { try { return await this.prisma.grade.upsert({ where: { - cardId, + cardId_userId: { + cardId, + userId, + }, }, update: { grade, @@ -45,7 +48,6 @@ export class GradesRepository { card: { connect: { id: cardId, - shots: 1, }, }, deck: { 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 3e7b584..8720c7d 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,6 +1,6 @@ import { ForbiddenException, NotFoundException } from '@nestjs/common' import { CommandHandler, ICommandHandler } from '@nestjs/cqrs' -import { omit } from 'remeda' +import { pick } from 'remeda' import { GetAllCardsInDeckDto } from '../../cards/dto' import { PaginatedCards, PaginatedCardsWithGrade } from '../../cards/entities/cards.entity' @@ -24,19 +24,27 @@ 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( + items: cards.items.map(card => + pick( { ...card, - grade, + grade: card.grades[0]?.grade || 0, }, - ['grades'] + [ + 'id', + 'question', + 'answer', + 'deckId', + 'questionImg', + 'answerImg', + 'questionVideo', + 'answerVideo', + 'created', + 'updated', + 'shots', + ] ) - }), + ), } } diff --git a/src/modules/decks/use-cases/get-random-card-in-deck-use-case.ts b/src/modules/decks/use-cases/get-random-card-in-deck-use-case.ts index 29fd2a1..b20e4c3 100644 --- a/src/modules/decks/use-cases/get-random-card-in-deck-use-case.ts +++ b/src/modules/decks/use-cases/get-random-card-in-deck-use-case.ts @@ -3,7 +3,6 @@ import { CommandHandler, ICommandHandler } from '@nestjs/cqrs' import { Prisma } from '@prisma/client' import { pick } from 'remeda' -import { Card } from '../../cards/entities/cards.entity' import { CardsRepository } from '../../cards/infrastructure/cards.repository' import { DecksRepository } from '../infrastructure/decks.repository' @@ -24,7 +23,7 @@ export class GetRandomCardInDeckHandler implements ICommandHandler): Promise { + private async getSmartRandomCard(cards: Array): Promise { const selectionPool: Array = [] console.log(cards.length) @@ -49,7 +48,7 @@ export class GetRandomCardInDeckHandler implements ICommandHandler, previousCardId: string - ): Promise { + ): Promise { const randomCard = await this.getSmartRandomCard(cards) if (randomCard.id === previousCardId && cards.length !== 1) { @@ -67,25 +66,27 @@ export class GetRandomCardInDeckHandler implements ICommandHandler