|
|
@ -20,7 +20,6 @@ This file is part of fLotte-API-Server.
|
|
|
|
import { DataSource } from 'apollo-datasource';
|
|
|
|
import { DataSource } from 'apollo-datasource';
|
|
|
|
import { Connection, EntityManager, getConnection } from 'typeorm';
|
|
|
|
import { Connection, EntityManager, getConnection } from 'typeorm';
|
|
|
|
import { CargoBike } from '../../model/CargoBike';
|
|
|
|
import { CargoBike } from '../../model/CargoBike';
|
|
|
|
import { GraphQLError } from 'graphql';
|
|
|
|
|
|
|
|
import { BikeEvent } from '../../model/BikeEvent';
|
|
|
|
import { BikeEvent } from '../../model/BikeEvent';
|
|
|
|
import { Equipment } from '../../model/Equipment';
|
|
|
|
import { Equipment } from '../../model/Equipment';
|
|
|
|
import { Engagement } from '../../model/Engagement';
|
|
|
|
import { Engagement } from '../../model/Engagement';
|
|
|
@ -29,8 +28,9 @@ import { TimeFrame } from '../../model/TimeFrame';
|
|
|
|
import { ActionLogger, DBUtils, genBoxDimensions, LockUtils } from './utils';
|
|
|
|
import { ActionLogger, DBUtils, genBoxDimensions, LockUtils } from './utils';
|
|
|
|
import { EquipmentType } from '../../model/EquipmentType';
|
|
|
|
import { EquipmentType } from '../../model/EquipmentType';
|
|
|
|
import { BikeEventType } from '../../model/BikeEventType';
|
|
|
|
import { BikeEventType } from '../../model/BikeEventType';
|
|
|
|
import { UserInputError } from 'apollo-server-express';
|
|
|
|
|
|
|
|
import { Actions } from '../../model/ActionLog';
|
|
|
|
import { Actions } from '../../model/ActionLog';
|
|
|
|
|
|
|
|
import { ResourceLockedError } from '../../errors/ResourceLockedError';
|
|
|
|
|
|
|
|
import { NotFoundError } from '../../errors/NotFoundError';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* extended datasource to feed resolvers with data about cargoBikes
|
|
|
|
* extended datasource to feed resolvers with data about cargoBikes
|
|
|
@ -83,10 +83,16 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async lockCargoBike (id: number, userId: number) {
|
|
|
|
async lockCargoBike (id: number, userId: number) {
|
|
|
|
|
|
|
|
if (!await this.connection.getRepository(CargoBike).findOne(id)) {
|
|
|
|
|
|
|
|
throw new NotFoundError('CargoBike', 'id', id);
|
|
|
|
|
|
|
|
}
|
|
|
|
return await LockUtils.lockEntity(this.connection, CargoBike, 'cb', id, userId);
|
|
|
|
return await LockUtils.lockEntity(this.connection, CargoBike, 'cb', id, userId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async unlockCargoBike (id: number, userId: number) {
|
|
|
|
async unlockCargoBike (id: number, userId: number) {
|
|
|
|
|
|
|
|
if (!await this.connection.getRepository(CargoBike).findOne(id)) {
|
|
|
|
|
|
|
|
throw new NotFoundError('CargoBike', 'id', id);
|
|
|
|
|
|
|
|
}
|
|
|
|
return await LockUtils.unlockEntity(this.connection, CargoBike, 'cb', id, userId);
|
|
|
|
return await LockUtils.unlockEntity(this.connection, CargoBike, 'cb', id, userId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -107,7 +113,7 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
|
|
|
|
|
|
|
|
await this.connection.transaction(async (entityManager: EntityManager) => {
|
|
|
|
await this.connection.transaction(async (entityManager: EntityManager) => {
|
|
|
|
if (await LockUtils.isLocked(entityManager, CargoBike, 'cb', cargoBike.id, userId)) {
|
|
|
|
if (await LockUtils.isLocked(entityManager, CargoBike, 'cb', cargoBike.id, userId)) {
|
|
|
|
throw new GraphQLError('CargoBike locked by other user');
|
|
|
|
throw new ResourceLockedError('CargoBike');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await ActionLogger.log(entityManager, CargoBike, 'cb', cargoBike, userId);
|
|
|
|
await ActionLogger.log(entityManager, CargoBike, 'cb', cargoBike, userId);
|
|
|
|
await entityManager.getRepository(CargoBike)
|
|
|
|
await entityManager.getRepository(CargoBike)
|
|
|
@ -116,11 +122,13 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
.set({ ...cargoBike })
|
|
|
|
.set({ ...cargoBike })
|
|
|
|
.where('id = :id', { id: cargoBike.id })
|
|
|
|
.where('id = :id', { id: cargoBike.id })
|
|
|
|
.execute();
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
|
|
|
|
equipmentTypeIds && await entityManager.getRepository(CargoBike)
|
|
|
|
equipmentTypeIds && await entityManager.getRepository(CargoBike)
|
|
|
|
.createQueryBuilder('cb')
|
|
|
|
.createQueryBuilder('cb')
|
|
|
|
.relation(CargoBike, 'equipmentTypeIds')
|
|
|
|
.relation(CargoBike, 'equipmentTypeIds')
|
|
|
|
.of(cargoBike.id)
|
|
|
|
.of(cargoBike.id)
|
|
|
|
.addAndRemove(equipmentTypeIds, await this.equipmentTypeByCargoBikeId(cargoBike.id));
|
|
|
|
.addAndRemove(equipmentTypeIds, await this.equipmentTypeByCargoBikeId(cargoBike.id));
|
|
|
|
|
|
|
|
|
|
|
|
equipmentIds && await entityManager.getRepository(CargoBike)
|
|
|
|
equipmentIds && await entityManager.getRepository(CargoBike)
|
|
|
|
.createQueryBuilder('cb')
|
|
|
|
.createQueryBuilder('cb')
|
|
|
|
.relation(CargoBike, 'equipmentIds')
|
|
|
|
.relation(CargoBike, 'equipmentIds')
|
|
|
@ -134,7 +142,7 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
async deleteCargoBike (id: number, userId: number) {
|
|
|
|
async deleteCargoBike (id: number, userId: number) {
|
|
|
|
return await this.connection.transaction(async (entityManager: EntityManager) => {
|
|
|
|
return await this.connection.transaction(async (entityManager: EntityManager) => {
|
|
|
|
if (await LockUtils.isLocked(entityManager, CargoBike, 'cb', id, userId)) {
|
|
|
|
if (await LockUtils.isLocked(entityManager, CargoBike, 'cb', id, userId)) {
|
|
|
|
throw new UserInputError('Attempting to soft delete locked resource');
|
|
|
|
throw new ResourceLockedError('CargoBike', 'Attempting to soft delete locked resource');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await ActionLogger.log(entityManager, CargoBike, 'bg', { id: id }, userId, Actions.SOFT_DELETE);
|
|
|
|
await ActionLogger.log(entityManager, CargoBike, 'bg', { id: id }, userId, Actions.SOFT_DELETE);
|
|
|
|
return await entityManager.getRepository(CargoBike)
|
|
|
|
return await entityManager.getRepository(CargoBike)
|
|
|
@ -189,7 +197,7 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
delete bikeEvent.keepLock;
|
|
|
|
delete bikeEvent.keepLock;
|
|
|
|
await this.connection.transaction(async (entityManager: EntityManager) => {
|
|
|
|
await this.connection.transaction(async (entityManager: EntityManager) => {
|
|
|
|
if (await LockUtils.isLocked(entityManager, BikeEvent, 'be', bikeEvent.id, userId)) {
|
|
|
|
if (await LockUtils.isLocked(entityManager, BikeEvent, 'be', bikeEvent.id, userId)) {
|
|
|
|
throw new GraphQLError('BikeEvent locked by other user');
|
|
|
|
throw new ResourceLockedError('BikeEvents');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await ActionLogger.log(entityManager, BikeEvent, 'be', bikeEvent, userId);
|
|
|
|
await ActionLogger.log(entityManager, BikeEvent, 'be', bikeEvent, userId);
|
|
|
|
await entityManager.getRepository(BikeEvent)
|
|
|
|
await entityManager.getRepository(BikeEvent)
|
|
|
@ -267,7 +275,7 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
delete bikeEventType.keepLock;
|
|
|
|
delete bikeEventType.keepLock;
|
|
|
|
await this.connection.transaction(async (entityManager: EntityManager) => {
|
|
|
|
await this.connection.transaction(async (entityManager: EntityManager) => {
|
|
|
|
if (await LockUtils.isLocked(entityManager, BikeEventType, 'bet', bikeEventType.id, userId)) {
|
|
|
|
if (await LockUtils.isLocked(entityManager, BikeEventType, 'bet', bikeEventType.id, userId)) {
|
|
|
|
throw new GraphQLError('BikeEventType locked by other user');
|
|
|
|
throw new ResourceLockedError('BikeEventType');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await ActionLogger.log(entityManager, BikeEventType, 'bet', bikeEventType, userId);
|
|
|
|
await ActionLogger.log(entityManager, BikeEventType, 'bet', bikeEventType, userId);
|
|
|
|
await entityManager.getRepository(BikeEventType)
|
|
|
|
await entityManager.getRepository(BikeEventType)
|
|
|
@ -405,7 +413,7 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
delete equipment.keepLock;
|
|
|
|
delete equipment.keepLock;
|
|
|
|
await this.connection.transaction(async (entityManager: EntityManager) => {
|
|
|
|
await this.connection.transaction(async (entityManager: EntityManager) => {
|
|
|
|
if (await LockUtils.isLocked(entityManager, Equipment, 'equipment', equipment.id, userId)) {
|
|
|
|
if (await LockUtils.isLocked(entityManager, Equipment, 'equipment', equipment.id, userId)) {
|
|
|
|
return new GraphQLError('Equipment is locked by other user');
|
|
|
|
throw new ResourceLockedError('Equipment');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await ActionLogger.log(entityManager, Equipment, 'e', equipment, userId);
|
|
|
|
await ActionLogger.log(entityManager, Equipment, 'e', equipment, userId);
|
|
|
|
await entityManager.getRepository(Equipment)
|
|
|
|
await entityManager.getRepository(Equipment)
|
|
|
@ -453,7 +461,7 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
delete equipmentType.keepLock;
|
|
|
|
delete equipmentType.keepLock;
|
|
|
|
await this.connection.transaction(async (entityManager: EntityManager) => {
|
|
|
|
await this.connection.transaction(async (entityManager: EntityManager) => {
|
|
|
|
if (await LockUtils.isLocked(entityManager, EquipmentType, 'et', equipmentType.id, userId)) {
|
|
|
|
if (await LockUtils.isLocked(entityManager, EquipmentType, 'et', equipmentType.id, userId)) {
|
|
|
|
throw new GraphQLError('EquipmentType is locked by other user');
|
|
|
|
throw new ResourceLockedError('EquipmentType');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await ActionLogger.log(entityManager, EquipmentType, 'et', equipmentType, userId);
|
|
|
|
await ActionLogger.log(entityManager, EquipmentType, 'et', equipmentType, userId);
|
|
|
|
await entityManager.getRepository(EquipmentType)
|
|
|
|
await entityManager.getRepository(EquipmentType)
|
|
|
|