|
|
@ -1,5 +1,5 @@
|
|
|
|
import { DataSource } from 'apollo-datasource';
|
|
|
|
import { DataSource } from 'apollo-datasource';
|
|
|
|
import { getConnection, Connection } from 'typeorm';
|
|
|
|
import { getConnection, Connection, ObjectType } from 'typeorm';
|
|
|
|
import { CargoBike } from '../../model/CargoBike';
|
|
|
|
import { CargoBike } from '../../model/CargoBike';
|
|
|
|
import { GraphQLError } from 'graphql';
|
|
|
|
import { GraphQLError } from 'graphql';
|
|
|
|
import { BikeEvent } from '../../model/BikeEvent';
|
|
|
|
import { BikeEvent } from '../../model/BikeEvent';
|
|
|
@ -31,12 +31,11 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
* @param param0 id of bike
|
|
|
|
* @param param0 id of bike
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async findCargoBikeById (id: number) {
|
|
|
|
async findCargoBikeById (id: number) {
|
|
|
|
return (await this.connection.manager.getRepository(CargoBike).findByIds([id], { relations: ['lendingStation'] }))[0];
|
|
|
|
return await this.connection.getRepository(CargoBike)
|
|
|
|
/* .createQueryBuilder()
|
|
|
|
.createQueryBuilder('cargobike')
|
|
|
|
.select('cargoBike')
|
|
|
|
.select()
|
|
|
|
.from(CargoBike, 'cargoBike')
|
|
|
|
.where('cargobike.id = :id', { id })
|
|
|
|
.where('cargoBike.id = :id', { id })
|
|
|
|
.getOne();
|
|
|
|
.getOne() || new GraphQLError('ID not found'); */
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async findCargoBikeByEngagementId (id: number) {
|
|
|
|
async findCargoBikeByEngagementId (id: number) {
|
|
|
@ -50,8 +49,6 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
|
|
|
|
|
|
|
|
async lockCargoBike (id: number, req: any, dataSources: any) {
|
|
|
|
async lockCargoBike (id: number, req: any, dataSources: any) {
|
|
|
|
const token = req.headers.authorization?.replace('Bearer ', '');
|
|
|
|
const token = req.headers.authorization?.replace('Bearer ', '');
|
|
|
|
console.log(token);
|
|
|
|
|
|
|
|
console.log(Date.now().toString());
|
|
|
|
|
|
|
|
const userId = await dataSources.userAPI.getUserId(token);
|
|
|
|
const userId = await dataSources.userAPI.getUserId(token);
|
|
|
|
const lock = await this.connection.getRepository(CargoBike)
|
|
|
|
const lock = await this.connection.getRepository(CargoBike)
|
|
|
|
.createQueryBuilder('cargobike')
|
|
|
|
.createQueryBuilder('cargobike')
|
|
|
@ -67,7 +64,39 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
// eslint-disable-next-line eqeqeq
|
|
|
|
// eslint-disable-next-line eqeqeq
|
|
|
|
if (!lock?.lockedUntil || lock?.lockedBy == userId) {
|
|
|
|
if (!lock?.lockedUntil || lock?.lockedBy == userId) {
|
|
|
|
// no lock -> set lock
|
|
|
|
// no lock -> set lock
|
|
|
|
console.log("no lock")
|
|
|
|
await this.connection.getRepository(CargoBike)
|
|
|
|
|
|
|
|
.createQueryBuilder('cargoBike')
|
|
|
|
|
|
|
|
.update()
|
|
|
|
|
|
|
|
.set({
|
|
|
|
|
|
|
|
lockedUntil: () => 'CURRENT_TIMESTAMP + INTERVAL \'10 MINUTE\'',
|
|
|
|
|
|
|
|
lockedBy: userId
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.where('id = :id', { id: id })
|
|
|
|
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// lock was set
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async lockEntity<MyEntity> (target: ObjectType<MyEntity>, alias: string, id: number, req: any, dataSources: any) {
|
|
|
|
|
|
|
|
const token = req.headers.authorization?.replace('Bearer ', '');
|
|
|
|
|
|
|
|
const userId = await dataSources.userAPI.getUserId(token);
|
|
|
|
|
|
|
|
const lock = await this.connection.getRepository(target)
|
|
|
|
|
|
|
|
.createQueryBuilder(alias)
|
|
|
|
|
|
|
|
.select([
|
|
|
|
|
|
|
|
alias + '.lockedUntil',
|
|
|
|
|
|
|
|
alias + '.lockedBy'
|
|
|
|
|
|
|
|
])
|
|
|
|
|
|
|
|
.where('id = :id', {
|
|
|
|
|
|
|
|
id: id
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.andWhere(alias + '.lockedUntil > CURRENT_TIMESTAMP')
|
|
|
|
|
|
|
|
.getOne();
|
|
|
|
|
|
|
|
// eslint-disable-next-line eqeqeq
|
|
|
|
|
|
|
|
if (!lock?.lockedUntil || lock?.lockedBy == userId) {
|
|
|
|
|
|
|
|
// no lock -> set lock
|
|
|
|
await this.connection.getRepository(CargoBike)
|
|
|
|
await this.connection.getRepository(CargoBike)
|
|
|
|
.createQueryBuilder('cargoBike')
|
|
|
|
.createQueryBuilder('cargoBike')
|
|
|
|
.update()
|
|
|
|
.update()
|
|
|
@ -211,6 +240,10 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
.getOne())?.cargoBike;
|
|
|
|
.getOne())?.cargoBike;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async lockEquipment (id: number, req: any, dataSources: any) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Will update Equipment, crashes when id not in db or cargoBikeId not db.
|
|
|
|
* Will update Equipment, crashes when id not in db or cargoBikeId not db.
|
|
|
|
* Will return updated Equipment joined with CargoBike only if cargoBike is was set in param0
|
|
|
|
* Will return updated Equipment joined with CargoBike only if cargoBike is was set in param0
|
|
|
|