|
|
@ -1,6 +1,5 @@
|
|
|
|
import { Connection, EntityManager, ObjectType } from 'typeorm';
|
|
|
|
import { Connection, EntityManager, ObjectType } from 'typeorm';
|
|
|
|
import { Lockable } from '../../model/CargoBike';
|
|
|
|
import { Lockable } from '../../model/CargoBike';
|
|
|
|
import { GraphQLError } from 'graphql';
|
|
|
|
|
|
|
|
import { ActionLog, Actions } from '../../model/ActionLog';
|
|
|
|
import { ActionLog, Actions } from '../../model/ActionLog';
|
|
|
|
import { UserInputError } from 'apollo-server-express';
|
|
|
|
import { UserInputError } from 'apollo-server-express';
|
|
|
|
|
|
|
|
|
|
|
@ -24,11 +23,11 @@ export function genDateRange (struct: any) {
|
|
|
|
* @param dataSources
|
|
|
|
* @param dataSources
|
|
|
|
* @param req user request
|
|
|
|
* @param req user request
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
export function isLocked (parent: any, { dataSources, req }: { dataSources: any; req: any }) {
|
|
|
|
export function isLocked (parent: any, { req }: { req: any }) {
|
|
|
|
return req.userId !== parent.lockedBy && new Date() <= new Date(parent.lockedUntil);
|
|
|
|
return req.userId !== parent.lockedBy && new Date() <= new Date(parent.lockedUntil);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function isLockedByMe (parent: any, { dataSources, req }: { dataSources: any; req: any }) {
|
|
|
|
export function isLockedByMe (parent: any, { req }: { req: any }) {
|
|
|
|
return req.userId === parent.lockedBy && new Date() <= new Date(parent.lockedUntil);
|
|
|
|
return req.userId === parent.lockedBy && new Date() <= new Date(parent.lockedUntil);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -47,10 +46,6 @@ export async function deleteEntity (connection: Connection, target: ObjectType<L
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export class LockUtils {
|
|
|
|
export class LockUtils {
|
|
|
|
static getToken (req: any) : string {
|
|
|
|
|
|
|
|
return req.headers.authorization?.replace('Bearer ', '');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static async findById (connection: Connection, target: ObjectType<Lockable>, alias: string, id: number): Promise<Lockable> {
|
|
|
|
static async findById (connection: Connection, target: ObjectType<Lockable>, alias: string, id: number): Promise<Lockable> {
|
|
|
|
return await connection.getRepository(target)
|
|
|
|
return await connection.getRepository(target)
|
|
|
|
.createQueryBuilder(alias)
|
|
|
|
.createQueryBuilder(alias)
|
|
|
@ -89,40 +84,6 @@ export class LockUtils {
|
|
|
|
return await this.findById(connection, target, alias, id);
|
|
|
|
return await this.findById(connection, target, alias, id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static async unlockEntity_old (connection: Connection, target: ObjectType<Lockable>, alias: string, id: number, userId: number): Promise<boolean> {
|
|
|
|
|
|
|
|
const lock = await 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 connection.getRepository(target)
|
|
|
|
|
|
|
|
.createQueryBuilder(alias)
|
|
|
|
|
|
|
|
.update()
|
|
|
|
|
|
|
|
.set({
|
|
|
|
|
|
|
|
lockedUntil: null,
|
|
|
|
|
|
|
|
lockedBy: null
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.where('id = :id', { id: id })
|
|
|
|
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// entity is locked by other user
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static async unlockEntity (connection: Connection, target: ObjectType<Lockable>, alias: string, id: number, userId: number): Promise<Lockable> {
|
|
|
|
static async unlockEntity (connection: Connection, target: ObjectType<Lockable>, alias: string, id: number, userId: number): Promise<Lockable> {
|
|
|
|
await connection.getRepository(target)
|
|
|
|
await connection.getRepository(target)
|
|
|
|
.createQueryBuilder(alias)
|
|
|
|
.createQueryBuilder(alias)
|
|
|
@ -173,7 +134,8 @@ export class ActionLogger {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const ret :string[] = [];
|
|
|
|
const ret :string[] = [];
|
|
|
|
Object.keys(updates).forEach(value => {
|
|
|
|
Object.keys(updates).forEach(value => {
|
|
|
|
if (typeof updates[value] === 'object' && !Array.isArray(updates[value]) && updates[value]) {
|
|
|
|
// sometimes updates[value] is an array, e.g. timePeriods that are saved as a simple array in postgres
|
|
|
|
|
|
|
|
if (updates[value] && typeof updates[value] === 'object' && !Array.isArray(updates[value])) {
|
|
|
|
Object.keys(updates[value]).forEach(subValue => {
|
|
|
|
Object.keys(updates[value]).forEach(subValue => {
|
|
|
|
ret.push(alias + '."' + value + subValue[0].toUpperCase() + subValue.substr(1).toLowerCase() + '"');
|
|
|
|
ret.push(alias + '."' + value + subValue[0].toUpperCase() + subValue.substr(1).toLowerCase() + '"');
|
|
|
|
});
|
|
|
|
});
|
|
|
|