|
|
@ -1,5 +1,5 @@
|
|
|
|
import { DataSource } from 'apollo-datasource';
|
|
|
|
import { DataSource } from 'apollo-datasource';
|
|
|
|
import { getConnection, Connection, ObjectType } from 'typeorm';
|
|
|
|
import { getConnection, Connection, ObjectType, EntityManager } from 'typeorm';
|
|
|
|
import { CargoBike, Lockable } from '../../model/CargoBike';
|
|
|
|
import { CargoBike, Lockable } from '../../model/CargoBike';
|
|
|
|
import { GraphQLError } from 'graphql';
|
|
|
|
import { GraphQLError } from 'graphql';
|
|
|
|
import { BikeEvent } from '../../model/BikeEvent';
|
|
|
|
import { BikeEvent } from '../../model/BikeEvent';
|
|
|
@ -8,6 +8,7 @@ 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 { LockUtils } from './utils';
|
|
|
|
|
|
|
|
import { EquipmentType } from '../../model/EquipmentType';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* extended datasource to feed resolvers with data about cargoBikes
|
|
|
|
* extended datasource to feed resolvers with data about cargoBikes
|
|
|
@ -104,24 +105,27 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const keepLock = cargoBike?.keepLock;
|
|
|
|
const keepLock = cargoBike?.keepLock;
|
|
|
|
delete cargoBike.keepLock;
|
|
|
|
delete cargoBike.keepLock;
|
|
|
|
const bike = await this.connection.manager.createQueryBuilder()
|
|
|
|
delete cargoBike.lendingStationId;
|
|
|
|
.select('cargoBike')
|
|
|
|
let equipmentTypeIds: any = null;
|
|
|
|
.from(CargoBike, 'cargoBike')
|
|
|
|
if (cargoBike.equipmentTypeIds) {
|
|
|
|
.where('cargoBike.id = :id', { id: cargoBike.id })
|
|
|
|
equipmentTypeIds = cargoBike.equipmentTypeIds;
|
|
|
|
.getOne();
|
|
|
|
delete cargoBike.equipmentTypeIds;
|
|
|
|
if (bike.id) {
|
|
|
|
}
|
|
|
|
delete cargoBike.lendingStationId;
|
|
|
|
await this.connection.transaction(async (entityManager: EntityManager) => {
|
|
|
|
await this.connection.manager
|
|
|
|
await entityManager.getRepository(CargoBike)
|
|
|
|
.createQueryBuilder()
|
|
|
|
.createQueryBuilder('cargobike')
|
|
|
|
.update(CargoBike)
|
|
|
|
.update()
|
|
|
|
.set({ ...cargoBike })
|
|
|
|
.set({ ...cargoBike })
|
|
|
|
.where('id = :id', { id: bike.id })
|
|
|
|
.where('id = :id', { id: cargoBike.id })
|
|
|
|
.execute();
|
|
|
|
.execute();
|
|
|
|
!keepLock && await this.unlockCargoBike(cargoBike.id, req, dataSources);
|
|
|
|
equipmentTypeIds && await entityManager.getRepository(CargoBike)
|
|
|
|
return await this.findCargoBikeById(bike.id);
|
|
|
|
.createQueryBuilder('cargobike')
|
|
|
|
} else {
|
|
|
|
.relation(CargoBike, 'equipmentTypeIds')
|
|
|
|
return new GraphQLError('ID not in database');
|
|
|
|
.of(cargoBike.id)
|
|
|
|
}
|
|
|
|
.addAndRemove(equipmentTypeIds, await this.equipmentTypeByCargoBikeId(cargoBike.id)); // TODO remove all existing relations
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
!keepLock && await this.unlockCargoBike(cargoBike.id, req, dataSources);
|
|
|
|
|
|
|
|
return await this.findCargoBikeById(cargoBike.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -144,8 +148,14 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
.relation(CargoBike, 'provider')
|
|
|
|
.relation(CargoBike, 'provider')
|
|
|
|
.of(inserts.identifiers[0].id)
|
|
|
|
.of(inserts.identifiers[0].id)
|
|
|
|
.set(cargoBike?.providerId);
|
|
|
|
.set(cargoBike?.providerId);
|
|
|
|
|
|
|
|
cargoBike?.equipmentTypeIds && await entityManager.getRepository(CargoBike)
|
|
|
|
|
|
|
|
.createQueryBuilder('cargobike')
|
|
|
|
|
|
|
|
.relation(CargoBike, 'equipmentTypeIds')
|
|
|
|
|
|
|
|
.of(inserts.identifiers[0].id)
|
|
|
|
|
|
|
|
.add(cargoBike.equipmentTypeIds);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch (e: any) {
|
|
|
|
} catch (e: any) {
|
|
|
|
|
|
|
|
console.log(e);
|
|
|
|
return new GraphQLError('Transaction could not be completed');
|
|
|
|
return new GraphQLError('Transaction could not be completed');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const newbike = inserts.generatedMaps[0];
|
|
|
|
const newbike = inserts.generatedMaps[0];
|
|
|
@ -285,4 +295,31 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
.limit(limit)
|
|
|
|
.limit(limit)
|
|
|
|
.getMany();
|
|
|
|
.getMany();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async createEquipmentType (equipmentType: any) {
|
|
|
|
|
|
|
|
const inserts = await this.connection.getRepository(EquipmentType)
|
|
|
|
|
|
|
|
.createQueryBuilder('equipment')
|
|
|
|
|
|
|
|
.insert()
|
|
|
|
|
|
|
|
.values([equipmentType])
|
|
|
|
|
|
|
|
.returning('*')
|
|
|
|
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
inserts.generatedMaps[0].id = inserts.identifiers[0].id;
|
|
|
|
|
|
|
|
return inserts.generatedMaps[0];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async equipmentTypeById (id: number) {
|
|
|
|
|
|
|
|
return await this.connection.getRepository(EquipmentType)
|
|
|
|
|
|
|
|
.createQueryBuilder('equipmentType')
|
|
|
|
|
|
|
|
.select()
|
|
|
|
|
|
|
|
.where('"equipmentType".id = :id', { id: id })
|
|
|
|
|
|
|
|
.getOne();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async equipmentTypeByCargoBikeId (id: number) {
|
|
|
|
|
|
|
|
return this.connection.getRepository(CargoBike)
|
|
|
|
|
|
|
|
.createQueryBuilder('cargobike')
|
|
|
|
|
|
|
|
.relation(CargoBike, 'equipmentTypeIds')
|
|
|
|
|
|
|
|
.of(id)
|
|
|
|
|
|
|
|
.loadMany();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|