From cbec1866c4cdf0a1271ac5c3ff8d99960866f089 Mon Sep 17 00:00:00 2001 From: leonnicolas Date: Wed, 23 Sep 2020 18:50:57 +0200 Subject: [PATCH] isLocked in cargoBike --- src/datasources/db/cargobikeAPI.ts | 27 +++++++++++++++++++++++++++ src/model/Equipment.ts | 2 -- src/resolvers/cargobikeResolver.ts | 3 +++ src/schema/type-defs.ts | 1 + 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/datasources/db/cargobikeAPI.ts b/src/datasources/db/cargobikeAPI.ts index 45400fa..39c7645 100644 --- a/src/datasources/db/cargobikeAPI.ts +++ b/src/datasources/db/cargobikeAPI.ts @@ -127,6 +127,33 @@ export class CargoBikeAPI extends DataSource { } } + async isLocked (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(CargoBike) + .createQueryBuilder('cargobike') + .select([ + 'cargobike' + '.lockedUntil', + 'cargobike' + '.lockedBy' + ]) + .where('id = :id', { + id: id + }) + .andWhere('cargobike' + '.lockedUntil > CURRENT_TIMESTAMP') + .getOne(); + if (!lock?.lockedUntil) { + // no lock + return false; + // eslint-disable-next-line eqeqeq + } else if (lock?.lockedBy == userId) { + // user has locked + return false; + } else { + // enity is locked by other user + return true; + } + } + /** * Updates CargoBike and return updated cargoBike * @param param0 cargoBike to be updated diff --git a/src/model/Equipment.ts b/src/model/Equipment.ts index fe675dc..b5a0d57 100644 --- a/src/model/Equipment.ts +++ b/src/model/Equipment.ts @@ -49,6 +49,4 @@ export class Equipment implements Lockable { nullable: true }) lockedBy: number; - - cargoBikeId: number; } diff --git a/src/resolvers/cargobikeResolver.ts b/src/resolvers/cargobikeResolver.ts index 731567f..6c01556 100644 --- a/src/resolvers/cargobikeResolver.ts +++ b/src/resolvers/cargobikeResolver.ts @@ -51,6 +51,9 @@ export default { }, lendingStation (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) { return dataSources.lendingStationAPI.lendingStationByCargoBikeId(parent.id); + }, + isLocked (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) { + return dataSources.cargoBikeAPI.isLocked(parent.id, req, dataSources); } }, Equipment: { diff --git a/src/schema/type-defs.ts b/src/schema/type-defs.ts index 62f3a47..28a9e02 100644 --- a/src/schema/type-defs.ts +++ b/src/schema/type-defs.ts @@ -41,6 +41,7 @@ type CargoBike { lendingStation: LendingStation taxes: Taxes engagement(offset: Int!, limit: Int!): [Engagement] + isLocked: Boolean! "null if not locked by other user" lockedBy: ID lockedUntil: Date