import { IsInt, IsObject, IsOptional, IsString } from "class-validator"; import { JwtCreator } from '../../jwtcreator'; import { Runner } from '../entities/Runner'; import { ResponseObjectType } from '../enums/ResponseObjectType'; import { IResponse } from './IResponse'; import { ResponseParticipant } from './ResponseParticipant'; import { ResponseRunnerGroup } from './ResponseRunnerGroup'; /** * Defines the runner response. */ export class ResponseRunner extends ResponseParticipant implements IResponse { /** * The responseType. * This contains the type of class/entity this response contains. */ responseType: ResponseObjectType = ResponseObjectType.RUNNER; /** * The runner's currently ran distance in meters. */ @IsInt() distance: number; /** * The runner's current donation amount based on distance. * Only available for queries for single runners. */ @IsInt() donationAmount: number; /** * The runner's group. */ @IsObject() group: ResponseRunnerGroup; /** * A selfservice link for our new runner. */ @IsOptional() @IsString() selfserviceLink: string; /** * Creates a ResponseRunner object from a runner. * @param runner The user the response shall be build for. */ public constructor(runner: Runner, generateSelfServiceLink: boolean = false) { super(runner); if (!runner.scans) { this.distance = 0 } else { this.distance = runner.validScans.reduce((sum, current) => sum + current.distance, 0); } if (runner.group) { this.group = runner.group.toResponse(); } if (runner.distanceDonations) { this.donationAmount = runner.distanceDonations.reduce((sum, current) => sum + (current.amountPerDistance * runner.distance / 1000), 0); } if (generateSelfServiceLink) { const token = JwtCreator.createSelfService(runner); this.selfserviceLink = `${process.env.SELFSERVICE_URL}/profile/${token}`; } } }