lock and unlock for cargoBike

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

@ -51,8 +51,12 @@ export class CargoBikeAPI extends DataSource {
return this.lockEntity(CargoBike, 'cargobike', id, req, dataSources); return this.lockEntity(CargoBike, 'cargobike', id, req, dataSources);
} }
async unlockCargoBike (id: number, req: any, dataSources: any) {
return this.unlockEntity(CargoBike, 'cargobike', id, req, dataSources);
}
/** /**
* lock any entity that implemts Lockable * locks any entity that implemts Lockable
*/ */
async lockEntity (target: ObjectType<Lockable>, alias: string, id: number, req: any, dataSources: any) { async lockEntity (target: ObjectType<Lockable>, alias: string, id: number, req: any, dataSources: any) {
const token = req.headers.authorization?.replace('Bearer ', ''); const token = req.headers.authorization?.replace('Bearer ', '');
@ -87,6 +91,42 @@ export class CargoBikeAPI extends DataSource {
} }
} }
async unlockEntity (target: ObjectType<Lockable>, alias: string, 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(target)
.createQueryBuilder(alias)
.select([
alias + '.lockedUntil',
alias + '.lockedBy'
])
.where('id = :id', {
id: id
})
.andWhere(alias + '.lockedUntil > CURRENT_TIMESTAMP')
.getOne();
if (!lock?.lockedUntil) {
// no lock
return true;
// eslint-disable-next-line eqeqeq
} else if (lock?.lockedBy == userId) {
// user can unlock
await this.connection.getRepository(target)
.createQueryBuilder(alias)
.update()
.set({
lockedUntil: null,
lockedBy: null
})
.where('id = :id', { id: id })
.execute();
return true;
} else {
// enity is locked by other user
return false;
}
}
/** /**
* Updates CargoBike and return updated cargoBike * Updates CargoBike and return updated cargoBike
* @param param0 cargoBike to be updated * @param param0 cargoBike to be updated
@ -215,7 +255,7 @@ export class CargoBikeAPI extends DataSource {
} }
async lockEquipment (id: number, req: any, dataSources: any) { async lockEquipment (id: number, req: any, dataSources: any) {
return this.lockEntity(Equipment, 'equipment', id, req, dataSources);
} }
/** /**
@ -223,7 +263,10 @@ export class CargoBikeAPI extends DataSource {
* Will return updated Equipment joined with CargoBike only if cargoBike is was set in param0 * Will return updated Equipment joined with CargoBike only if cargoBike is was set in param0
* @param param0 struct with equipment properites * @param param0 struct with equipment properites
*/ */
async updateEquipment ({ equipment }: { equipment: any }) { async updateEquipment (equipment: any, req: any, dataSources: any) {
if (!await this.lockEntity(Equipment, 'equipment', equipment.id, req, dataSources)) {
return new GraphQLError('Equipment locked by other user');
}
const cargoBikeId = equipment.cargoBikeId; const cargoBikeId = equipment.cargoBikeId;
delete equipment.cargoBikeId; delete equipment.cargoBikeId;
await this.connection.getRepository(Equipment) await this.connection.getRepository(Equipment)

@ -1,8 +1,8 @@
import { Entity, PrimaryGeneratedColumn, Column, JoinColumn, ManyToOne } from 'typeorm'; import { Entity, PrimaryGeneratedColumn, Column, JoinColumn, ManyToOne } from 'typeorm';
import { CargoBike } from './CargoBike'; import { CargoBike, Lockable } from './CargoBike';
@Entity() @Entity()
export class Equipment { export class Equipment implements Lockable {
setValues ({ id, serialNo, title, description, cargoBike }: { setValues ({ id, serialNo, title, description, cargoBike }: {
id: number, id: number,
serialNo: string, serialNo: string,
@ -39,5 +39,16 @@ export class Equipment {
}) })
cargoBike: CargoBike; cargoBike: CargoBike;
@Column({
type: 'timestamp',
nullable: true
})
lockedUntil: Date;
@Column({
nullable: true
})
lockedBy: number;
cargoBikeId: number; cargoBikeId: number;
} }

@ -73,6 +73,13 @@ export default {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }
}, },
unlockCargoBikeById: (_: any, { id }: { id: number }, { dataSources, req }:{dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.WriteBike)) {
return dataSources.cargoBikeAPI.unlockCargoBike(id, req, dataSources);
} else {
return new GraphQLError('Insufficient Permissions');
}
},
updateCargoBike: (_: any, { cargoBike }: { cargoBike: any }, { dataSources, req }:{dataSources: any, req: any }) => { updateCargoBike: (_: any, { cargoBike }: { cargoBike: any }, { dataSources, req }:{dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.WriteBike)) { if (req.permissions.includes(Permission.WriteBike)) {
return dataSources.cargoBikeAPI.updateCargoBike(cargoBike, req, dataSources); return dataSources.cargoBikeAPI.updateCargoBike(cargoBike, req, dataSources);
@ -94,16 +101,16 @@ export default {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }
}, },
lockEquipmentById: (_: any, { equipment }: { equipment: any }, { dataSources, req }: { dataSources: any, req: any }) => { lockEquipmentById: (_: any, { id }: { id: number }, { dataSources, req }: { dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.WriteBike)) { if (req.permissions.includes(Permission.WriteBike)) {
return dataSources.cargoBikeAPI.lockEquipment(equipment.id, req, dataSources); return dataSources.cargoBikeAPI.lockEquipment(id, req, dataSources);
} else { } else {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }
}, },
updateEquipment: (_: any, { equipment }: { equipment: any }, { dataSources, req }: { dataSources: any, req: any }) => { updateEquipment: (_: any, { equipment }: { equipment: any }, { dataSources, req }: { dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.WriteBike)) { if (req.permissions.includes(Permission.WriteBike)) {
return dataSources.cargoBikeAPI.updateEquipment({ equipment }); return dataSources.cargoBikeAPI.updateEquipment(equipment, req, dataSources);
} else { } else {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }

@ -712,6 +712,8 @@ type Mutation {
createCargoBike(cargoBike: CargoBikeCreateInput!): CargoBike! createCargoBike(cargoBike: CargoBikeCreateInput!): CargoBike!
"lock cargoBike returns true if bike is not locked or if it doesnt exist" "lock cargoBike returns true if bike is not locked or if it doesnt exist"
lockCargoBikeById(id: ID!): Boolean! lockCargoBikeById(id: ID!): Boolean!
"unlock cargoBike"
unlockCargoBikeById(id: ID!): Boolean!
"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!
"creates new peace of unique Equipment" "creates new peace of unique Equipment"

Loading…
Cancel
Save