diff --git a/src/datasources/db/utils.ts b/src/datasources/db/utils.ts index 272844c..fb9c815 100644 --- a/src/datasources/db/utils.ts +++ b/src/datasources/db/utils.ts @@ -56,7 +56,9 @@ export class LockUtils { .createQueryBuilder(alias) .select() .where(alias + '.id = :id', { id: id }) - .getOne(); + .getOne().catch(() => { + throw new UserInputError('ID not found'); + }); } static async lockEntity (connection: Connection, target: ObjectType, alias: string, id: number, userId: number): Promise { @@ -87,7 +89,7 @@ export class LockUtils { return await this.findById(connection, target, alias, id); } - static async unlockEntity (connection: Connection, target: ObjectType, alias: string, id: number, userId: number): Promise { + static async unlockEntity_old (connection: Connection, target: ObjectType, alias: string, id: number, userId: number): Promise { const lock = await connection.getRepository(target) .createQueryBuilder(alias) .select([ @@ -121,6 +123,20 @@ export class LockUtils { } } + static async unlockEntity (connection: Connection, target: ObjectType, alias: string, id: number, userId: number): Promise { + await connection.getRepository(target) + .createQueryBuilder(alias) + .update() + .set({ + lockedUntil: null, + lockedBy: null + }) + .where('id = :id', { id: id }) + .andWhere('lockedBy = :uid OR lockedUntil < CURRENT_TIMESTAMP', { uid: userId }) + .execute(); + return await this.findById(connection, target, alias, id); + } + /** * Returns true if Entity is locked by another user. * @param connection diff --git a/src/schema/type-defs.ts b/src/schema/type-defs.ts index c03ebfa..99027da 100644 --- a/src/schema/type-defs.ts +++ b/src/schema/type-defs.ts @@ -955,7 +955,7 @@ type Mutation { "lock cargoBike returns bike if bike is not locked and locks bike or Error if bike cannot be locked" lockCargoBike(id: ID!): CargoBike! "unlock cargoBike, returns true if Bike does not exist" - unlockCargoBike(id: ID!): Boolean! + unlockCargoBike(id: ID!): CargoBike! "updates cargoBike of given ID with supplied fields and returns updated cargoBike" updateCargoBike(cargoBike: CargoBikeUpdateInput!): CargoBike! "true on success" @@ -968,13 +968,13 @@ type Mutation { "lock equipment returns true if bike is not locked or if it doesnt exist" lockEquipment(id: ID!): Equipment! "unlock Equipment, returns true if Bike does not exist" - unlockEquipment(id: ID!): Boolean! + unlockEquipment(id: ID!): Equipment! "update Equipment, returns updated equipment. CargoBike will be null, if cargoBikeId is not set. Pass null for cargoBikeIs to delete the relation" updateEquipment(equipment: EquipmentUpdateInput!): Equipment! deleteEquipment(id: ID!): Boolean! createEquipmentType(equipmentType: EquipmentTypeCreateInput!): EquipmentType! lockEquipmentType(id: ID!): EquipmentType! - unlockEquipmentType(id: ID!): Boolean! + unlockEquipmentType(id: ID!): EquipmentType! updateEquipmentType(equipmentType: EquipmentTypeUpdateInput!): EquipmentType! deleteEquipmentType(id: ID!): Boolean! """ @@ -982,14 +982,14 @@ type Mutation { creates new lendingStation and returns lendingStation with new ID """ createLendingStation(lendingStation: LendingStationCreateInput): LendingStation! - lockLendingStation(id: ID!): LendingStation - unlockLendingStation(id: ID!): Boolean! + lockLendingStation(id: ID!): LendingStation! + unlockLendingStation(id: ID!): LendingStation! "updates lendingStation of given ID with supplied fields and returns updated lendingStation" updateLendingStation(lendingStation: LendingStationUpdateInput!): LendingStation! deleteLendingStation(id: ID!): Boolean! createTimeFrame(timeFrame: TimeFrameCreateInput!): TimeFrame! lockTimeFrame(id: ID!): TimeFrame! - unlockTimeFrame(id: ID!): Boolean! + unlockTimeFrame(id: ID!): TimeFrame! updateTimeFrame(timeFrame: TimeFrameUpdateInput!): TimeFrame! deleteTimeFrame(id: ID!): Boolean! """ @@ -997,13 +997,13 @@ type Mutation { """ createBikeEventType(name: String!): BikeEventType! lockBikeEventType(id: ID!): BikeEventType! - unlockBikeEventType(id:ID!): Boolean! + unlockBikeEventType(id:ID!): BikeEventType! updateBikeEventType(bikeEventType: BikeEventTypeUpdateInput!): BikeEventType! deleteBikeEventType(id: ID!): Boolean! "creates new BikeEvent" createBikeEvent(bikeEvent: BikeEventCreateInput!): BikeEvent! - lockBikeEvent(id: ID!): BikeEvent - unlockBikeEvent(id: ID!): Boolean! + lockBikeEvent(id: ID!): BikeEvent! + unlockBikeEvent(id: ID!): BikeEvent! updateBikeEvent(bikeEvent: BikeEventUpdateInput!): BikeEvent deleteBikeEvent(id: ID!): Boolean! """