diff --git a/src/datasources/db/cargobikeAPI.ts b/src/datasources/db/cargobikeAPI.ts index 54c3fd0..4508d40 100644 --- a/src/datasources/db/cargobikeAPI.ts +++ b/src/datasources/db/cargobikeAPI.ts @@ -1,6 +1,6 @@ import { DataSource } from 'apollo-datasource'; import { getConnection, Connection, ObjectType } from 'typeorm'; -import { CargoBike } from '../../model/CargoBike'; +import { CargoBike, Lockable } from '../../model/CargoBike'; import { GraphQLError } from 'graphql'; import { BikeEvent } from '../../model/BikeEvent'; import { Equipment } from '../../model/Equipment'; @@ -80,7 +80,7 @@ export class CargoBikeAPI extends DataSource { } } - async lockEntity (target: ObjectType, alias: string, id: number, req: any, dataSources: any) { + async lockEntity (target: ObjectType, 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) @@ -97,8 +97,8 @@ export class CargoBikeAPI extends DataSource { // eslint-disable-next-line eqeqeq if (!lock?.lockedUntil || lock?.lockedBy == userId) { // no lock -> set lock - await this.connection.getRepository(CargoBike) - .createQueryBuilder('cargoBike') + await this.connection.getRepository(target) + .createQueryBuilder(alias) .update() .set({ lockedUntil: () => 'CURRENT_TIMESTAMP + INTERVAL \'10 MINUTE\'', diff --git a/src/model/CargoBike.ts b/src/model/CargoBike.ts index 1dca1bd..2e12dcb 100644 --- a/src/model/CargoBike.ts +++ b/src/model/CargoBike.ts @@ -31,6 +31,11 @@ export enum StickerBikeNameState { UNKNOWN } +export interface Lockable { + lockedBy: number, + lockedUntil: Date +} + export class Security { @Column() frameNumber: string; @@ -57,7 +62,7 @@ export class Security { } @Entity() -export class CargoBike extends Bike { +export class CargoBike extends Bike implements Lockable { @PrimaryGeneratedColumn() id: number;