All files / events events.service.ts

75.31% Statements 119/158
70.58% Branches 12/17
80% Functions 8/10
75.31% Lines 119/158

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 1591x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 9x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 3x 1x 1x 3x 1x 1x                                           1x 1x                                     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 1x 2x 2x 1x 1x 2x 2x 1x  
import { Injectable } from "@nestjs/common";
import { CreateEventDto } from "./dto/create-event.dto";
import { UpdateEventDto } from "./dto/update-event.dto";
import { InjectRepository } from "@nestjs/typeorm";
import { Event } from "./entities/event.entity";
import { Repository } from "typeorm";
import { ScoreService } from "src/score/score.service";
 
type EventError = 
    | "EVENT DOESNT EXIST";
 
export class EventException extends Error {
    name: EventError;
 
    constructor(name: EventError) {
        super();
        this.name = name;
    }
}
 
@Injectable()
export class EventsService {
    constructor(
        @InjectRepository(Event)
        private eventRepository: Repository<Event>,
        private scoreService: ScoreService,
    ) { }
 
    async create(createEventDto: CreateEventDto, username: string): Promise<Event> {
        const { id } = await this.eventRepository.save({
            user: { username },
            ...createEventDto,
        });
 
        return this.findOne(id, username);
    }
 
    async findAll(username: string) {
        const events = await this.eventRepository.find({
            relations: {
                user: {
                    badges: true,
                    currentBadge: true,
                    expertises: true,
                },
            }
        });
 
        return Promise.all(
            events.map(async event => ({
                ...event,
                score: await this.scoreService.getPostScore(event.id),
                userScore: await this.scoreService.getUserScoreOnPost(event.id, username) ?? 0,
            }))
        );
    }
 
    async findOne(id: number, username: string) {
        const event = await this.eventRepository.findOne({
            where: {
                id
            },
            relations: {
                user: {
                    badges: true,
                    currentBadge: true,
                    expertises: true,
                },
            },
        });
 
        if (event) {
            return {
                ...event,
                score: await this.scoreService.getPostScore(id),
                userScore: await this.scoreService.getUserScoreOnPost(id, username) ?? 0,
            };
        } else {
            throw new EventException("EVENT DOESNT EXIST");
        }
    }
 
    async getRecents(username: string) {
        const events = await this.eventRepository.find({
            relations: {
                user: {
                    badges: true,
                    currentBadge: true,
                    expertises: true,
                },
            },
            order: {
                postDate: "DESC"
            }
        });

        return Promise.all(
            events.map(async event => ({
                ...event,
                score: await this.scoreService.getPostScore(event.id),
                userScore: await this.scoreService.getUserScoreOnPost(event.id, username) ?? 0
            }))
        );
    }
 
    async getForYou(username: string) {
        const events = await this.eventRepository.find({
            relations: {
                user: {
                    badges: true,
                    currentBadge: true,
                    expertises: true,
                },
            }
        });

        return (await Promise.all(
            events.map(async event => ({
                ...event,
                score: await this.scoreService.getPostScore(event.id),
                userScore: await this.scoreService.getUserScoreOnPost(event.id, username) ?? 0
            }))
        )).sort((a, b) => b.score - a.score);
    }
 
    async find(usernameTarget: string, usernameViewer: string) {
        const events = await this.eventRepository.find({
            where: {
                user: {
                    username: usernameTarget,
                },
            },
            relations: {
                user: {
                    badges: true,
                    currentBadge: true,
                    expertises: true,
                },
            },
        });
 
        return Promise.all(
            events.map(async event => ({
                ...event,
                score: await this.scoreService.getPostScore(event.id),
                userScore: await this.scoreService.getUserScoreOnPost(event.id, usernameViewer)  ?? 0,
            }))
        );
    }
 
    async update(id: number, username: string, updateEventDto: UpdateEventDto) {
        return (await this.eventRepository.update({ id, user: { username } }, updateEventDto))!.affected! > 0;
    }
 
    async remove(id: number, username: string) {
        return (await this.eventRepository.delete({ id, user: { username } })).affected! > 0;
    }
}