lock for cargoBike

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

@ -49,31 +49,49 @@ export class CargoBikeAPI extends DataSource {
} }
async lockCargoBike (id: number, req: any, dataSources: any) { async lockCargoBike (id: number, req: any, dataSources: any) {
console.log("token:");
console.log(req.headers.authorization);
const token = req.headers.authorization?.replace('Bearer ', ''); const token = req.headers.authorization?.replace('Bearer ', '');
console.log(token); console.log(token);
console.log(await dataSources.userAPI.getUserId(token)); console.log(Date.now().toString());
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')
.select([ .select([
'cargoBike.lockedUntil', 'cargobike.lockedUntil',
'cargoBike.lockedBy' 'cargobike.lockedBy'
]) ])
.where('id = :id', { .where('id = :id', {
id: id id: id
}) })
.andWhere('cargobike.lockedUntil > CURRENT_TIMESTAMP')
.getOne(); .getOne();
//console.log(req); // eslint-disable-next-line eqeqeq
console.log(lock); if (!lock?.lockedUntil || lock?.lockedBy == userId) {
return false; // 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;
}
} }
/** /**
* Updates CargoBike and return updated cargoBike * Updates CargoBike and return updated cargoBike
* @param param0 cargoBike to be updated * @param param0 cargoBike to be updated
*/ */
async updateCargoBike ({ cargoBike }:{ cargoBike: any }) { async updateCargoBike (cargoBike: any, req: any, dataSources: any) {
if (!await this.lockCargoBike(cargoBike.id, req, dataSources)) {
return new GraphQLError('Bike locked by other user');
}
const bike = await this.connection.manager.createQueryBuilder() const bike = await this.connection.manager.createQueryBuilder()
.select('cargoBike') .select('cargoBike')
.from(CargoBike, 'cargoBike') .from(CargoBike, 'cargoBike')

@ -88,6 +88,9 @@ export class UserServerAPI extends DataSource {
* @param token * @param token
*/ */
async getUserId (token: String): Promise<number> { async getUserId (token: String): Promise<number> {
if (process.env.NODE_ENV === 'develop') {
return 0;
}
const response = await this.send<any>(new RPCMessage(Method.GetUserID, { token })); const response = await this.send<any>(new RPCMessage(Method.GetUserID, { token }));
return response.data; return response.data;
} }

@ -68,7 +68,6 @@ export default {
}, },
lockCargoBikeById: (_: any, { id }: { id: number }, { dataSources, req }:{dataSources: any, req: any }) => { lockCargoBikeById: (_: 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.lockCargoBike(id, req, dataSources); return dataSources.cargoBikeAPI.lockCargoBike(id, req, dataSources);
} else { } else {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
@ -76,7 +75,7 @@ export default {
}, },
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 }); return dataSources.cargoBikeAPI.updateCargoBike(cargoBike, req, dataSources);
} else { } else {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }

@ -710,7 +710,7 @@ type Query {
type Mutation { type Mutation {
"creates new cargoBike and returns cargobike with new ID" "creates new cargoBike and returns cargobike with new ID"
createCargoBike(cargoBike: CargoBikeCreateInput!): CargoBike! createCargoBike(cargoBike: CargoBikeCreateInput!): CargoBike!
"lock cargoBike - not implemented" "lock cargoBike returns true if bike is not locked or if it doesnt exist"
lockCargoBikeById(id: ID!): Boolean! lockCargoBikeById(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!

Loading…
Cancel
Save