lock for cargoBike

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

@ -1,5 +1,5 @@
import { DataSource } from 'apollo-datasource'; import { DataSource } from 'apollo-datasource';
import { getConnection, Connection } from 'typeorm'; import { getConnection, Connection, ObjectType } from 'typeorm';
import { CargoBike } from '../../model/CargoBike'; import { CargoBike } from '../../model/CargoBike';
import { GraphQLError } from 'graphql'; import { GraphQLError } from 'graphql';
import { BikeEvent } from '../../model/BikeEvent'; import { BikeEvent } from '../../model/BikeEvent';
@ -31,12 +31,11 @@ export class CargoBikeAPI extends DataSource {
* @param param0 id of bike * @param param0 id of bike
*/ */
async findCargoBikeById (id: number) { async findCargoBikeById (id: number) {
return (await this.connection.manager.getRepository(CargoBike).findByIds([id], { relations: ['lendingStation'] }))[0]; return await this.connection.getRepository(CargoBike)
/* .createQueryBuilder() .createQueryBuilder('cargobike')
.select('cargoBike') .select()
.from(CargoBike, 'cargoBike') .where('cargobike.id = :id', { id })
.where('cargoBike.id = :id', { id }) .getOne();
.getOne() || new GraphQLError('ID not found'); */
} }
async findCargoBikeByEngagementId (id: number) { async findCargoBikeByEngagementId (id: number) {
@ -50,8 +49,6 @@ export class CargoBikeAPI extends DataSource {
async lockCargoBike (id: number, req: any, dataSources: any) { async lockCargoBike (id: number, req: any, dataSources: any) {
const token = req.headers.authorization?.replace('Bearer ', ''); const token = req.headers.authorization?.replace('Bearer ', '');
console.log(token);
console.log(Date.now().toString());
const userId = await dataSources.userAPI.getUserId(token); const userId = await dataSources.userAPI.getUserId(token);
const lock = await this.connection.getRepository(CargoBike) const lock = await this.connection.getRepository(CargoBike)
.createQueryBuilder('cargobike') .createQueryBuilder('cargobike')
@ -67,7 +64,39 @@ export class CargoBikeAPI extends DataSource {
// eslint-disable-next-line eqeqeq // eslint-disable-next-line eqeqeq
if (!lock?.lockedUntil || lock?.lockedBy == userId) { if (!lock?.lockedUntil || lock?.lockedBy == userId) {
// no lock -> set lock // no lock -> set lock
console.log("no lock") await this.connection.getRepository(CargoBike)
.createQueryBuilder('cargoBike')
.update()
.set({
lockedUntil: () => 'CURRENT_TIMESTAMP + INTERVAL \'10 MINUTE\'',
lockedBy: userId
})
.where('id = :id', { id: id })
.execute();
return true;
} else {
// lock was set
return false;
}
}
async lockEntity<MyEntity> (target: ObjectType<MyEntity>, 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();
// eslint-disable-next-line eqeqeq
if (!lock?.lockedUntil || lock?.lockedBy == userId) {
// no lock -> set lock
await this.connection.getRepository(CargoBike) await this.connection.getRepository(CargoBike)
.createQueryBuilder('cargoBike') .createQueryBuilder('cargoBike')
.update() .update()
@ -211,6 +240,10 @@ export class CargoBikeAPI extends DataSource {
.getOne())?.cargoBike; .getOne())?.cargoBike;
} }
async lockEquipment (id: number, req: any, dataSources: any) {
}
/** /**
* Will update Equipment, crashes when id not in db or cargoBikeId not db. * Will update Equipment, crashes when id not in db or cargoBikeId not db.
* 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

@ -38,12 +38,12 @@ export class Security {
@Column({ @Column({
nullable: true nullable: true
}) })
keyNoFrameLock: string; keyNumberFrameLock: string;
@Column({ @Column({
nullable: true nullable: true
}) })
keyNoAXAChain: string; keyNumberAXAChain: string;
@Column({ @Column({
nullable: true nullable: true

@ -94,6 +94,13 @@ export default {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }
}, },
lockEquipmentById: (_: any, { equipment }: { equipment: any }, { dataSources, req }: { dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.WriteBike)) {
return dataSources.cargoBikeAPI.lockEquipment(equipment.id, req, dataSources);
} else {
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 });

@ -716,6 +716,8 @@ type Mutation {
updateCargoBike(cargoBike: CargoBikeUpdateInput!): CargoBike! updateCargoBike(cargoBike: CargoBikeUpdateInput!): CargoBike!
"creates new peace of unique Equipment" "creates new peace of unique Equipment"
createEquipment(equipment: EquipmentCreateInput!): Equipment! createEquipment(equipment: EquipmentCreateInput!): Equipment!
"lock equipment returns true if bike is not locked or if it doesnt exist"
lockEquipmentById(id: ID!): Boolean!
"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!
"creates new lendingStation and returns lendingStation with new ID" "creates new lendingStation and returns lendingStation with new ID"

Loading…
Cancel
Save