All files / projects-dev/apps/mc-api/src/modules/encounters encounters.service.ts

0% Statements 0/101
0% Branches 0/1
0% Functions 0/1
0% Lines 0/101

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                                                                                                                                                                                                           
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Connection, getConnection, QueryRunner, Repository } from "typeorm";
// eslint-disable-next-line import/extensions
import { ApiResponse, ApiResponseStatus } from "@jga/apis";

import { Encounter } from "./encounter.entity";
import { AspectEnum, EncounterInterface } from "../../models";

@Injectable()
export class EncountersService {
    public constructor(
        @InjectRepository(Encounter)
        private readonly repository: Repository<Encounter>,
    ) {}

    public async getEncounters(sort: "ASC" | "DESC" = "ASC"): Promise<ApiResponse<string, Encounter[]>> {
        const options = {
            relations: ["scenario", "modularSets", "heroes"],
            order: { id: sort },
        };

        const encounters = await this.repository.find(options);

        const result = await Promise.all(
            encounters.map(async (encounter) => {
                // eslint-disable-next-line no-param-reassign
                encounter.heroes = await Promise.all(
                    encounter.heroes.map(async (hero) => {
                        const encounterHeroAspects = await this.getAspectEncounterByIdAndHeroId(encounter.id, hero.id);
                        // eslint-disable-next-line no-param-reassign
                        hero.aspects = encounterHeroAspects?.split(",") as AspectEnum[];

                        return hero;
                    }),
                );

                return encounter;
            }),
        );

        return {
            data: result,
            status: ApiResponseStatus.SUCCESS,
        };
    }

    public async createEncounter(encounter: Partial<EncounterInterface>): Promise<ApiResponse<string, Encounter>> {
        // eslint-disable-next-line no-param-reassign
        encounter.id = null;
        // eslint-disable-next-line no-param-reassign
        encounter.date = new Date();

        const result = await this.repository.save(encounter);

        // save aspect from encounter
        const connection: Connection = getConnection();
        const queryRunner: QueryRunner = connection.createQueryRunner();

        await queryRunner.connect();

        encounter.heroes.forEach(async (hero) => {
            await queryRunner.query(
                `
                    UPDATE encounters_heroes
                    SET aspects = ?
                    WHERE encounter = ?
                    AND hero = ?
                `,
                [hero.aspects?.join(","), result.id, hero.id],
            );
        });

        await queryRunner.release();

        return {
            data: result,
            status: ApiResponseStatus.SUCCESS,
        };
    }

    // eslint-disable-next-line class-methods-use-this
    protected async getAspectEncounterByIdAndHeroId(encounterId: number, heroId: number): Promise<string> {
        const connection: Connection = getConnection();
        const queryRunner: QueryRunner = connection.createQueryRunner();

        await queryRunner.connect();
        const result = await queryRunner.query(
            `
            SELECT aspects
            FROM encounters_heroes
            WHERE encounter = ?
            AND hero = ?
            `,
            [encounterId, heroId],
        );
        await queryRunner.release();

        return result[0].aspects;
    }
}