fix learn endpoint

This commit is contained in:
2023-07-21 16:44:18 +02:00
parent 5fdd8b5bb1
commit a943838622
8 changed files with 72 additions and 9 deletions

View File

@@ -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<GetAllCardsInDe
private readonly cardsRepository: CardsRepository,
private readonly decksRepository: DecksRepository
) {}
private transformGrade(cards: PaginatedCardsWithGrade): PaginatedCards {
return {
...cards,
items: cards.items.map(card => {
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<PaginatedCards> {
const deck = await this.decksRepository.findDeckById(command.deckId)
@@ -29,7 +48,12 @@ export class GetAllCardsInDeckHandler implements ICommandHandler<GetAllCardsInDe
if (deck.userId !== command.userId && deck.isPrivate) {
throw new ForbiddenException(`You can't get a private deck that you don't own`)
}
const cards = await this.cardsRepository.findCardsByDeckId(
command.deckId,
command.userId,
command.params
)
return await this.cardsRepository.findCardsByDeckId(command.deckId, command.params)
return this.transformGrade(cards)
}
}