|
|
@ -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')
|
|
|
|