Implemented the basics for the runner selfservice registration endpoint
ref #112
This commit is contained in:
parent
10af1ba341
commit
5288c701c1
@ -1,23 +1,28 @@
|
||||
import * as jwt from "jsonwebtoken";
|
||||
import { Get, JsonController, OnUndefined, Param } from 'routing-controllers';
|
||||
import { Body, Get, JsonController, OnUndefined, Param, Post } from 'routing-controllers';
|
||||
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
||||
import { getConnectionManager, Repository } from 'typeorm';
|
||||
import { config } from '../config';
|
||||
import { InvalidCredentialsError } from '../errors/AuthError';
|
||||
import { RunnerNotFoundError } from '../errors/RunnerErrors';
|
||||
import { RunnerGroupNotFoundError } from '../errors/RunnerGroupErrors';
|
||||
import { CreateSelfServiceRunner } from '../models/actions/create/CreateSelfServiceRunner';
|
||||
import { Runner } from '../models/entities/Runner';
|
||||
import { RunnerGroup } from '../models/entities/RunnerGroup';
|
||||
import { ResponseSelfServiceRunner } from '../models/responses/ResponseSelfServiceRunner';
|
||||
|
||||
|
||||
@JsonController('/runners')
|
||||
export class RunnerSelfServiceController {
|
||||
private runnerRepository: Repository<Runner>;
|
||||
private groupRepository: Repository<RunnerGroup>;
|
||||
|
||||
/**
|
||||
* Gets the repository of this controller's model/entity.
|
||||
*/
|
||||
constructor() {
|
||||
this.runnerRepository = getConnectionManager().get().getRepository(Runner);
|
||||
this.groupRepository = getConnectionManager().get().getRepository(RunnerGroup);
|
||||
}
|
||||
|
||||
@Get('/me/:jwt')
|
||||
@ -29,6 +34,19 @@ export class RunnerSelfServiceController {
|
||||
return (new ResponseSelfServiceRunner(await this.getRunner(token)));
|
||||
}
|
||||
|
||||
@Post('/register/:token')
|
||||
@ResponseSchema(ResponseSelfServiceRunner)
|
||||
@ResponseSchema(RunnerGroupNotFoundError, { statusCode: 404 })
|
||||
@OpenAPI({ description: 'TODO:' })
|
||||
async registerOrganisationRunner(@Param('token') token: number, @Body({ validate: true }) createRunner: CreateSelfServiceRunner) {
|
||||
const org = await this.getOrgansisation(token);
|
||||
|
||||
let runner = await createRunner.toEntity(org);
|
||||
runner = await this.runnerRepository.save(runner);
|
||||
|
||||
return new ResponseSelfServiceRunner(await this.runnerRepository.findOne(runner, { relations: ['scans', 'group', 'scans.track', 'cards', 'distanceDonations', 'distanceDonations.donor', 'distanceDonations.runner', 'distanceDonations.runner.scans', 'distanceDonations.runner.scans.track'] }));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get's a runner by a provided jwt token.
|
||||
* @param token The runner jwt provided by the runner to identitfy themselves.
|
||||
@ -46,4 +64,8 @@ export class RunnerSelfServiceController {
|
||||
return runner;
|
||||
}
|
||||
|
||||
private async getOrgansisation(token: number): Promise<RunnerGroup> {
|
||||
//TODO: Implement the real token checker
|
||||
return await this.groupRepository.findOne({ id: token });
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user