isLocked in cargoBike

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

@ -127,6 +127,33 @@ export class CargoBikeAPI extends DataSource {
} }
} }
async isLocked (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(CargoBike)
.createQueryBuilder('cargobike')
.select([
'cargobike' + '.lockedUntil',
'cargobike' + '.lockedBy'
])
.where('id = :id', {
id: id
})
.andWhere('cargobike' + '.lockedUntil > CURRENT_TIMESTAMP')
.getOne();
if (!lock?.lockedUntil) {
// no lock
return false;
// eslint-disable-next-line eqeqeq
} else if (lock?.lockedBy == userId) {
// user has locked
return false;
} else {
// enity is locked by other user
return true;
}
}
/** /**
* Updates CargoBike and return updated cargoBike * Updates CargoBike and return updated cargoBike
* @param param0 cargoBike to be updated * @param param0 cargoBike to be updated

@ -49,6 +49,4 @@ export class Equipment implements Lockable {
nullable: true nullable: true
}) })
lockedBy: number; lockedBy: number;
cargoBikeId: number;
} }

@ -51,6 +51,9 @@ export default {
}, },
lendingStation (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) { lendingStation (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) {
return dataSources.lendingStationAPI.lendingStationByCargoBikeId(parent.id); return dataSources.lendingStationAPI.lendingStationByCargoBikeId(parent.id);
},
isLocked (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) {
return dataSources.cargoBikeAPI.isLocked(parent.id, req, dataSources);
} }
}, },
Equipment: { Equipment: {

@ -41,6 +41,7 @@ type CargoBike {
lendingStation: LendingStation lendingStation: LendingStation
taxes: Taxes taxes: Taxes
engagement(offset: Int!, limit: Int!): [Engagement] engagement(offset: Int!, limit: Int!): [Engagement]
isLocked: Boolean!
"null if not locked by other user" "null if not locked by other user"
lockedBy: ID lockedBy: ID
lockedUntil: Date lockedUntil: Date

Loading…
Cancel
Save