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