src/datasources/db/utils: return entity on unlockEntity

pull/14/head
leonnicolas 4 years ago
parent 47d71ea827
commit eb0d17a2ad
No known key found for this signature in database
GPG Key ID: 088D0743E2B65C07

@ -56,7 +56,9 @@ export class LockUtils {
.createQueryBuilder(alias) .createQueryBuilder(alias)
.select() .select()
.where(alias + '.id = :id', { id: id }) .where(alias + '.id = :id', { id: id })
.getOne(); .getOne().catch(() => {
throw new UserInputError('ID not found');
});
} }
static async lockEntity (connection: Connection, target: ObjectType<Lockable>, alias: string, id: number, userId: number): Promise<Lockable> { static async lockEntity (connection: Connection, target: ObjectType<Lockable>, alias: string, id: number, userId: number): Promise<Lockable> {
@ -87,7 +89,7 @@ export class LockUtils {
return await this.findById(connection, target, alias, id); return await this.findById(connection, target, alias, id);
} }
static async unlockEntity (connection: Connection, target: ObjectType<Lockable>, alias: string, id: number, userId: number): Promise<boolean> { static async unlockEntity_old (connection: Connection, target: ObjectType<Lockable>, alias: string, id: number, userId: number): Promise<boolean> {
const lock = await connection.getRepository(target) const lock = await connection.getRepository(target)
.createQueryBuilder(alias) .createQueryBuilder(alias)
.select([ .select([
@ -121,6 +123,20 @@ export class LockUtils {
} }
} }
static async unlockEntity (connection: Connection, target: ObjectType<Lockable>, alias: string, id: number, userId: number): Promise<Lockable> {
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. * Returns true if Entity is locked by another user.
* @param connection * @param connection

@ -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" "lock cargoBike returns bike if bike is not locked and locks bike or Error if bike cannot be locked"
lockCargoBike(id: ID!): CargoBike! lockCargoBike(id: ID!): CargoBike!
"unlock cargoBike, returns true if Bike does not exist" "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" "updates cargoBike of given ID with supplied fields and returns updated cargoBike"
updateCargoBike(cargoBike: CargoBikeUpdateInput!): CargoBike! updateCargoBike(cargoBike: CargoBikeUpdateInput!): CargoBike!
"true on success" "true on success"
@ -968,13 +968,13 @@ type Mutation {
"lock equipment returns true if bike is not locked or if it doesnt exist" "lock equipment returns true if bike is not locked or if it doesnt exist"
lockEquipment(id: ID!): Equipment! lockEquipment(id: ID!): Equipment!
"unlock Equipment, returns true if Bike does not exist" "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" "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! updateEquipment(equipment: EquipmentUpdateInput!): Equipment!
deleteEquipment(id: ID!): Boolean! deleteEquipment(id: ID!): Boolean!
createEquipmentType(equipmentType: EquipmentTypeCreateInput!): EquipmentType! createEquipmentType(equipmentType: EquipmentTypeCreateInput!): EquipmentType!
lockEquipmentType(id: ID!): EquipmentType! lockEquipmentType(id: ID!): EquipmentType!
unlockEquipmentType(id: ID!): Boolean! unlockEquipmentType(id: ID!): EquipmentType!
updateEquipmentType(equipmentType: EquipmentTypeUpdateInput!): EquipmentType! updateEquipmentType(equipmentType: EquipmentTypeUpdateInput!): EquipmentType!
deleteEquipmentType(id: ID!): Boolean! deleteEquipmentType(id: ID!): Boolean!
""" """
@ -982,14 +982,14 @@ type Mutation {
creates new lendingStation and returns lendingStation with new ID creates new lendingStation and returns lendingStation with new ID
""" """
createLendingStation(lendingStation: LendingStationCreateInput): LendingStation! createLendingStation(lendingStation: LendingStationCreateInput): LendingStation!
lockLendingStation(id: ID!): LendingStation lockLendingStation(id: ID!): LendingStation!
unlockLendingStation(id: ID!): Boolean! unlockLendingStation(id: ID!): LendingStation!
"updates lendingStation of given ID with supplied fields and returns updated lendingStation" "updates lendingStation of given ID with supplied fields and returns updated lendingStation"
updateLendingStation(lendingStation: LendingStationUpdateInput!): LendingStation! updateLendingStation(lendingStation: LendingStationUpdateInput!): LendingStation!
deleteLendingStation(id: ID!): Boolean! deleteLendingStation(id: ID!): Boolean!
createTimeFrame(timeFrame: TimeFrameCreateInput!): TimeFrame! createTimeFrame(timeFrame: TimeFrameCreateInput!): TimeFrame!
lockTimeFrame(id: ID!): TimeFrame! lockTimeFrame(id: ID!): TimeFrame!
unlockTimeFrame(id: ID!): Boolean! unlockTimeFrame(id: ID!): TimeFrame!
updateTimeFrame(timeFrame: TimeFrameUpdateInput!): TimeFrame! updateTimeFrame(timeFrame: TimeFrameUpdateInput!): TimeFrame!
deleteTimeFrame(id: ID!): Boolean! deleteTimeFrame(id: ID!): Boolean!
""" """
@ -997,13 +997,13 @@ type Mutation {
""" """
createBikeEventType(name: String!): BikeEventType! createBikeEventType(name: String!): BikeEventType!
lockBikeEventType(id: ID!): BikeEventType! lockBikeEventType(id: ID!): BikeEventType!
unlockBikeEventType(id:ID!): Boolean! unlockBikeEventType(id:ID!): BikeEventType!
updateBikeEventType(bikeEventType: BikeEventTypeUpdateInput!): BikeEventType! updateBikeEventType(bikeEventType: BikeEventTypeUpdateInput!): BikeEventType!
deleteBikeEventType(id: ID!): Boolean! deleteBikeEventType(id: ID!): Boolean!
"creates new BikeEvent" "creates new BikeEvent"
createBikeEvent(bikeEvent: BikeEventCreateInput!): BikeEvent! createBikeEvent(bikeEvent: BikeEventCreateInput!): BikeEvent!
lockBikeEvent(id: ID!): BikeEvent lockBikeEvent(id: ID!): BikeEvent!
unlockBikeEvent(id: ID!): Boolean! unlockBikeEvent(id: ID!): BikeEvent!
updateBikeEvent(bikeEvent: BikeEventUpdateInput!): BikeEvent updateBikeEvent(bikeEvent: BikeEventUpdateInput!): BikeEvent
deleteBikeEvent(id: ID!): Boolean! deleteBikeEvent(id: ID!): Boolean!
""" """

Loading…
Cancel
Save