|
|
@ -7,7 +7,7 @@ import { Equipment } from '../../model/Equipment';
|
|
|
|
import { Engagement } from '../../model/Engagement';
|
|
|
|
import { Engagement } from '../../model/Engagement';
|
|
|
|
import { Provider } from '../../model/Provider';
|
|
|
|
import { Provider } from '../../model/Provider';
|
|
|
|
import { TimeFrame } from '../../model/TimeFrame';
|
|
|
|
import { TimeFrame } from '../../model/TimeFrame';
|
|
|
|
import { LockUtils } from './utils';
|
|
|
|
import { ActionLogger, LockUtils } from './utils';
|
|
|
|
import { EquipmentType } from '../../model/EquipmentType';
|
|
|
|
import { EquipmentType } from '../../model/EquipmentType';
|
|
|
|
import { BikeEventType } from '../../model/BikeEventType';
|
|
|
|
import { BikeEventType } from '../../model/BikeEventType';
|
|
|
|
|
|
|
|
|
|
|
@ -54,7 +54,7 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
async cargoBikesByProviderId (id: number) {
|
|
|
|
async cargoBikesByProviderId (id: number) {
|
|
|
|
return await this.connection
|
|
|
|
return await this.connection
|
|
|
|
.createQueryBuilder()
|
|
|
|
.createQueryBuilder()
|
|
|
|
.relation(Provider, 'cargoBikes')
|
|
|
|
.relation(Provider, 'cargoBikeIds')
|
|
|
|
.of(id)
|
|
|
|
.of(id)
|
|
|
|
.loadMany();
|
|
|
|
.loadMany();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -62,7 +62,7 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
async cargoBikeByTimeFrameId (id: number) {
|
|
|
|
async cargoBikeByTimeFrameId (id: number) {
|
|
|
|
return await this.connection.getRepository(TimeFrame)
|
|
|
|
return await this.connection.getRepository(TimeFrame)
|
|
|
|
.createQueryBuilder('timeframe')
|
|
|
|
.createQueryBuilder('timeframe')
|
|
|
|
.relation(TimeFrame, 'cargoBike')
|
|
|
|
.relation(TimeFrame, 'cargoBikeId')
|
|
|
|
.of(id)
|
|
|
|
.of(id)
|
|
|
|
.loadOne();
|
|
|
|
.loadOne();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -72,13 +72,6 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
* @param param0 cargoBike to be updated
|
|
|
|
* @param param0 cargoBike to be updated
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async updateCargoBike (cargoBike: any, userId:number) {
|
|
|
|
async updateCargoBike (cargoBike: any, userId:number) {
|
|
|
|
// TODO lock cargoBike can return error to save one sql query, this will be a complex sql query
|
|
|
|
|
|
|
|
if (!await this.checkId(CargoBike, 'cargobike', cargoBike.id)) {
|
|
|
|
|
|
|
|
return new GraphQLError('ID not found');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!await LockUtils.lockEntity(this.connection, CargoBike, 'cb', cargoBike.id, userId)) {
|
|
|
|
|
|
|
|
return new GraphQLError('Bike locked by other user');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const keepLock = cargoBike?.keepLock;
|
|
|
|
const keepLock = cargoBike?.keepLock;
|
|
|
|
delete cargoBike.keepLock;
|
|
|
|
delete cargoBike.keepLock;
|
|
|
|
delete cargoBike.lendingStationId;
|
|
|
|
delete cargoBike.lendingStationId;
|
|
|
@ -88,6 +81,10 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
delete cargoBike.equipmentTypeIds;
|
|
|
|
delete cargoBike.equipmentTypeIds;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await this.connection.transaction(async (entityManager: EntityManager) => {
|
|
|
|
await this.connection.transaction(async (entityManager: EntityManager) => {
|
|
|
|
|
|
|
|
if (await LockUtils.isLocked(entityManager, CargoBike, 'cb', cargoBike.id, userId)) {
|
|
|
|
|
|
|
|
throw new GraphQLError('CargoBike locked by other user');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
await ActionLogger.log(entityManager, CargoBike, 'cb', cargoBike, userId);
|
|
|
|
await entityManager.getRepository(CargoBike)
|
|
|
|
await entityManager.getRepository(CargoBike)
|
|
|
|
.createQueryBuilder('cargobike')
|
|
|
|
.createQueryBuilder('cargobike')
|
|
|
|
.update()
|
|
|
|
.update()
|
|
|
@ -138,6 +135,25 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
.execute()).generatedMaps[0];
|
|
|
|
.execute()).generatedMaps[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async updateBikeEvent (bikeEvent: any, userId: number) {
|
|
|
|
|
|
|
|
const keepLock = bikeEvent.keepLock;
|
|
|
|
|
|
|
|
delete bikeEvent.keepLock;
|
|
|
|
|
|
|
|
await this.connection.transaction(async (entityManager: EntityManager) => {
|
|
|
|
|
|
|
|
if (await LockUtils.isLocked(entityManager, BikeEvent, 'be', bikeEvent.id, userId)) {
|
|
|
|
|
|
|
|
throw new GraphQLError('BikeEvent locked by other user');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
await ActionLogger.log(entityManager, BikeEvent, 'be', bikeEvent, userId);
|
|
|
|
|
|
|
|
await entityManager.getRepository(BikeEvent)
|
|
|
|
|
|
|
|
.createQueryBuilder('be')
|
|
|
|
|
|
|
|
.update()
|
|
|
|
|
|
|
|
.set({ ...bikeEvent })
|
|
|
|
|
|
|
|
.where('id = :id', { id: bikeEvent.id })
|
|
|
|
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
!keepLock && await LockUtils.unlockEntity(this.connection, BikeEvent, 'be', bikeEvent.id, userId);
|
|
|
|
|
|
|
|
return await this.bikeEventById(bikeEvent.id);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async cargoBikeByEventId (id: number) {
|
|
|
|
async cargoBikeByEventId (id: number) {
|
|
|
|
return await this.connection.getRepository(BikeEvent)
|
|
|
|
return await this.connection.getRepository(BikeEvent)
|
|
|
|
.createQueryBuilder('be')
|
|
|
|
.createQueryBuilder('be')
|
|
|
@ -173,6 +189,33 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
.execute())?.generatedMaps[0];
|
|
|
|
.execute())?.generatedMaps[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async lockBikeEventType (id: number, userId: number) {
|
|
|
|
|
|
|
|
return await LockUtils.lockEntity(this.connection, BikeEventType, 'bet', id, userId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async unlockBikeEventType (id: number, userId: number) {
|
|
|
|
|
|
|
|
return await LockUtils.unlockEntity(this.connection, BikeEventType, 'bet', id, userId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async updateBikeEventType (bikeEventType: any, userId: number) {
|
|
|
|
|
|
|
|
const keepLock = bikeEventType.keepLock;
|
|
|
|
|
|
|
|
delete bikeEventType.keepLock;
|
|
|
|
|
|
|
|
await this.connection.transaction(async (entityManager: EntityManager) => {
|
|
|
|
|
|
|
|
if (await LockUtils.isLocked(entityManager, BikeEventType, 'bet', bikeEventType.id, userId)) {
|
|
|
|
|
|
|
|
throw new GraphQLError('BikeEventType locked by other user');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
await ActionLogger.log(entityManager, BikeEventType, 'bet', bikeEventType, userId);
|
|
|
|
|
|
|
|
await entityManager.getRepository(BikeEventType)
|
|
|
|
|
|
|
|
.createQueryBuilder('bet')
|
|
|
|
|
|
|
|
.update()
|
|
|
|
|
|
|
|
.set({ ...bikeEventType })
|
|
|
|
|
|
|
|
.where('id = :id', { id: bikeEventType.id })
|
|
|
|
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
!keepLock && await LockUtils.unlockEntity(this.connection, BikeEventType, 'bet', bikeEventType.id, userId);
|
|
|
|
|
|
|
|
return await this.bikeEventTypeById(bikeEventType.id);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async bikeEventTypes (offset: number, limit: number) {
|
|
|
|
async bikeEventTypes (offset: number, limit: number) {
|
|
|
|
return await this.connection.getRepository(BikeEventType)
|
|
|
|
return await this.connection.getRepository(BikeEventType)
|
|
|
|
.createQueryBuilder('bet')
|
|
|
|
.createQueryBuilder('bet')
|
|
|
@ -191,7 +234,7 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
.getMany();
|
|
|
|
.getMany();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async findBikeEventTypeById (id: number) {
|
|
|
|
async bikeEventTypeById (id: number) {
|
|
|
|
return await this.connection.getRepository(BikeEventType)
|
|
|
|
return await this.connection.getRepository(BikeEventType)
|
|
|
|
.createQueryBuilder('bet')
|
|
|
|
.createQueryBuilder('bet')
|
|
|
|
.select()
|
|
|
|
.select()
|
|
|
@ -219,11 +262,11 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
* return bikeEvent including CargoBike
|
|
|
|
* return bikeEvent including CargoBike
|
|
|
|
* @param id of event
|
|
|
|
* @param id of event
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async findBikeEventById (id: number) {
|
|
|
|
async bikeEventById (id: number) {
|
|
|
|
return await this.connection.getRepository(BikeEvent)
|
|
|
|
return await this.connection.getRepository(BikeEvent)
|
|
|
|
.createQueryBuilder('bikeEvent')
|
|
|
|
.createQueryBuilder('bikeEvent')
|
|
|
|
.leftJoinAndSelect('bikeEvent.cargoBike', 'cargoBike')
|
|
|
|
.select()
|
|
|
|
.where('bikeEvent.id = :id', { id: id })
|
|
|
|
.where('id = :id', { id: id })
|
|
|
|
.getOne();
|
|
|
|
.getOne();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -276,22 +319,15 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
.values([equipment])
|
|
|
|
.values([equipment])
|
|
|
|
.returning('*')
|
|
|
|
.returning('*')
|
|
|
|
.execute();
|
|
|
|
.execute();
|
|
|
|
if (equipment.cargoBikeId) {
|
|
|
|
|
|
|
|
await this.connection
|
|
|
|
|
|
|
|
.createQueryBuilder()
|
|
|
|
|
|
|
|
.relation(Equipment, 'cargoBike')
|
|
|
|
|
|
|
|
.of(equipment.id)
|
|
|
|
|
|
|
|
.set(equipment.cargoBikeId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.equipmentById(inserts.identifiers[0].id);
|
|
|
|
return this.equipmentById(inserts.identifiers[0].id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async cargoBikeByEquipmentId (id: number) {
|
|
|
|
async cargoBikeByEquipmentId (id: number) {
|
|
|
|
return (await this.connection.getRepository(Equipment)
|
|
|
|
return await this.connection.getRepository(Equipment)
|
|
|
|
.createQueryBuilder('equipment')
|
|
|
|
.createQueryBuilder('equipment')
|
|
|
|
.leftJoinAndSelect('equipment.cargoBike', 'cargoBike')
|
|
|
|
.relation(Equipment, 'cargoBikeId')
|
|
|
|
.where('equipment.id = :id', { id: id })
|
|
|
|
.of(id)
|
|
|
|
.getOne())?.cargoBike;
|
|
|
|
.loadOne();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async lockEquipment (id: number, userId: number) {
|
|
|
|
async lockEquipment (id: number, userId: number) {
|
|
|
@ -308,33 +344,33 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
* @param param0 struct with equipment properites
|
|
|
|
* @param param0 struct with equipment properites
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async updateEquipment (equipment: any, userId: number) {
|
|
|
|
async updateEquipment (equipment: any, userId: number) {
|
|
|
|
// TODO let lock cargoBike can return error to save one sql query, this will be a complex sql query
|
|
|
|
|
|
|
|
if (!await this.checkId(Equipment, 'alias', equipment.id)) {
|
|
|
|
|
|
|
|
return new GraphQLError('ID not found in DB');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!await LockUtils.lockEntity(this.connection, Equipment, 'equipment', equipment.id, userId)) {
|
|
|
|
|
|
|
|
return new GraphQLError('Equipment locked by other user');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const keepLock = equipment.keepLock;
|
|
|
|
const keepLock = equipment.keepLock;
|
|
|
|
delete equipment.keepLock;
|
|
|
|
delete equipment.keepLock;
|
|
|
|
const cargoBikeId = equipment.cargoBikeId;
|
|
|
|
// const cargoBikeId = equipment.cargoBikeId;
|
|
|
|
delete equipment.cargoBikeId;
|
|
|
|
// delete equipment.cargoBikeId;
|
|
|
|
await this.connection.getRepository(Equipment)
|
|
|
|
await this.connection.transaction(async (entityManager: EntityManager) => {
|
|
|
|
|
|
|
|
if (await LockUtils.isLocked(entityManager, Equipment, 'equipment', equipment.id, userId)) {
|
|
|
|
|
|
|
|
return new GraphQLError('Equipment is locked by other user');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
await ActionLogger.log(entityManager, Equipment, 'e', equipment, userId);
|
|
|
|
|
|
|
|
await entityManager.getRepository(Equipment)
|
|
|
|
.createQueryBuilder('equipment')
|
|
|
|
.createQueryBuilder('equipment')
|
|
|
|
.update()
|
|
|
|
.update()
|
|
|
|
.set({ ...equipment })
|
|
|
|
.set({ ...equipment })
|
|
|
|
.where('id = :id', { id: equipment.id })
|
|
|
|
.where('id = :id', { id: equipment.id })
|
|
|
|
.returning('*')
|
|
|
|
|
|
|
|
.execute();
|
|
|
|
.execute();
|
|
|
|
if (cargoBikeId || cargoBikeId === null) {
|
|
|
|
/* if (cargoBikeId || cargoBikeId === null) {
|
|
|
|
await this.connection.getRepository(Equipment)
|
|
|
|
await this.connection.getRepository(Equipment)
|
|
|
|
.createQueryBuilder()
|
|
|
|
.createQueryBuilder()
|
|
|
|
.relation(Equipment, 'cargoBike')
|
|
|
|
.relation(Equipment, 'cargoBike')
|
|
|
|
.of(equipment.id)
|
|
|
|
.of(equipment.id)
|
|
|
|
.set(cargoBikeId);
|
|
|
|
.set(cargoBikeId);
|
|
|
|
!keepLock && LockUtils.unlockEntity(this.connection, Equipment, 'e', equipment.id, userId);
|
|
|
|
|
|
|
|
return this.equipmentById(equipment.id);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
!keepLock && await LockUtils.unlockEntity(this.connection, Equipment, 'e', equipment.id, userId);
|
|
|
|
return this.equipmentById(equipment.id);
|
|
|
|
return this.equipmentById(equipment.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -359,6 +395,32 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
return inserts.generatedMaps[0];
|
|
|
|
return inserts.generatedMaps[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async lockEquipmentType (id: number, userId : number) {
|
|
|
|
|
|
|
|
return await LockUtils.lockEntity(this.connection, EquipmentType, 'et', id, userId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async unlockEquipmentType (id: number, userId : number) {
|
|
|
|
|
|
|
|
return await LockUtils.unlockEntity(this.connection, EquipmentType, 'et', id, userId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async updateEquipmentType (equipmentType: any, userId: number) {
|
|
|
|
|
|
|
|
const keepLock = equipmentType.keepLock;
|
|
|
|
|
|
|
|
delete equipmentType.keepLock;
|
|
|
|
|
|
|
|
await this.connection.transaction(async (entityManager: EntityManager) => {
|
|
|
|
|
|
|
|
if (await LockUtils.isLocked(entityManager, EquipmentType, 'et', equipmentType.id, userId)) {
|
|
|
|
|
|
|
|
throw new GraphQLError('EquipmentType is locked by other user');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
await entityManager.getRepository(EquipmentType)
|
|
|
|
|
|
|
|
.createQueryBuilder('et')
|
|
|
|
|
|
|
|
.update()
|
|
|
|
|
|
|
|
.set({ ...equipmentType })
|
|
|
|
|
|
|
|
.where('id = :id', { id: equipmentType.id })
|
|
|
|
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
!keepLock && await this.unlockEquipmentType(equipmentType.id, userId);
|
|
|
|
|
|
|
|
return await this.equipmentTypeById(equipmentType.id);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async equipmentTypeById (id: number) {
|
|
|
|
async equipmentTypeById (id: number) {
|
|
|
|
return await this.connection.getRepository(EquipmentType)
|
|
|
|
return await this.connection.getRepository(EquipmentType)
|
|
|
|
.createQueryBuilder('equipmentType')
|
|
|
|
.createQueryBuilder('equipmentType')
|
|
|
|