From 847e78768f5828af021e793dedf0f3d71f2c42a4 Mon Sep 17 00:00:00 2001 From: leonnicolas Date: Wed, 23 Sep 2020 21:58:54 +0200 Subject: [PATCH] lockBike returns Bike or error, update bike takes keepLock --- src/datasources/db/cargobikeAPI.ts | 9 ++++++++- src/resolvers/cargobikeResolver.ts | 3 +++ src/schema/type-defs.ts | 6 ++++-- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/datasources/db/cargobikeAPI.ts b/src/datasources/db/cargobikeAPI.ts index 39c7645..b800a28 100644 --- a/src/datasources/db/cargobikeAPI.ts +++ b/src/datasources/db/cargobikeAPI.ts @@ -48,7 +48,11 @@ export class CargoBikeAPI extends DataSource { } async lockCargoBike (id: number, req: any, dataSources: any) { - return this.lockEntity(CargoBike, 'cargobike', id, req, dataSources); + if (await this.lockEntity(CargoBike, 'cargobike', id, req, dataSources)) { + return this.findCargoBikeById(id); + } else { + return new GraphQLError('CargoBike is locked by other user'); + } } async unlockCargoBike (id: number, req: any, dataSources: any) { @@ -162,6 +166,8 @@ export class CargoBikeAPI extends DataSource { if (!await this.lockCargoBike(cargoBike.id, req, dataSources)) { return new GraphQLError('Bike locked by other user'); } + const keepLock = cargoBike?.keepLock; + delete cargoBike.keepLock; const bike = await this.connection.manager.createQueryBuilder() .select('cargoBike') .from(CargoBike, 'cargoBike') @@ -183,6 +189,7 @@ export class CargoBikeAPI extends DataSource { .of(cargoBike.id) .set(lendingStationId); } + !keepLock && this.unlockCargoBike(cargoBike.id, req, dataSources); return await this.findCargoBikeById(bike.id); } else { return new GraphQLError('ID not in database'); diff --git a/src/resolvers/cargobikeResolver.ts b/src/resolvers/cargobikeResolver.ts index 6c01556..00730d8 100644 --- a/src/resolvers/cargobikeResolver.ts +++ b/src/resolvers/cargobikeResolver.ts @@ -54,6 +54,9 @@ export default { }, isLocked (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) { return dataSources.cargoBikeAPI.isLocked(parent.id, req, dataSources); + }, + lockedBy (): any { + return null; } }, Equipment: { diff --git a/src/schema/type-defs.ts b/src/schema/type-defs.ts index 28a9e02..0afe3b3 100644 --- a/src/schema/type-defs.ts +++ b/src/schema/type-defs.ts @@ -112,6 +112,8 @@ input CargoBikeUpdateInput { provider: String insuranceData: InsuranceDataUpdateInput taxes: TaxesUpdateInput + "will keep Bike locked if set to true, default = false" + keepLock: Boolean } type InsuranceData { @@ -711,8 +713,8 @@ type Query { type Mutation { "creates new cargoBike and returns cargobike with new ID" createCargoBike(cargoBike: CargoBikeCreateInput!): CargoBike! - "lock cargoBike returns true if bike is not locked or if it doesnt exist" - lockCargoBikeById(id: ID!): Boolean! + "lock cargoBike returns bike if bike is not locked or Error" + lockCargoBikeById(id: ID!): CargoBike! "unlock cargoBike" unlockCargoBikeById(id: ID!): Boolean! "updates cargoBike of given ID with supplied fields and returns updated cargoBike"