removed cargoBike lendingStation relation and use timeFrame instead

pull/14/head
leonnicolas 4 years ago
parent 71024bc886
commit 614f9a652c
No known key found for this signature in database
GPG Key ID: 088D0743E2B65C07

@ -181,7 +181,7 @@ export class CargoBikeAPI extends DataSource {
* @param param0 cargoBike to be updated * @param param0 cargoBike to be updated
*/ */
async updateCargoBike (cargoBike: any, req: any, dataSources: any) { async updateCargoBike (cargoBike: any, req: any, dataSources: any) {
// TODO let lock cargoBike can return error to save one sql query, this will be a complex sql query // 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)) { if (!await this.checkId(CargoBike, 'cargobike', cargoBike.id)) {
return new GraphQLError('ID not found'); return new GraphQLError('ID not found');
} }
@ -196,7 +196,6 @@ export class CargoBikeAPI extends DataSource {
.where('cargoBike.id = :id', { id: cargoBike.id }) .where('cargoBike.id = :id', { id: cargoBike.id })
.getOne(); .getOne();
if (bike.id) { if (bike.id) {
const lendingStationId = cargoBike.lendingStationId;
delete cargoBike.lendingStationId; delete cargoBike.lendingStationId;
await this.connection.manager await this.connection.manager
.createQueryBuilder() .createQueryBuilder()
@ -204,14 +203,7 @@ export class CargoBikeAPI extends DataSource {
.set({ ...cargoBike }) .set({ ...cargoBike })
.where('id = :id', { id: bike.id }) .where('id = :id', { id: bike.id })
.execute(); .execute();
if (lendingStationId || lendingStationId === null) { !keepLock && await this.unlockCargoBike(cargoBike.id, req, dataSources);
await this.connection.getRepository(CargoBike)
.createQueryBuilder()
.relation(CargoBike, 'lendingStation')
.of(cargoBike.id)
.set(lendingStationId);
}
!keepLock && this.unlockCargoBike(cargoBike.id, req, dataSources);
return await this.findCargoBikeById(bike.id); return await this.findCargoBikeById(bike.id);
} else { } else {
return new GraphQLError('ID not in database'); return new GraphQLError('ID not in database');
@ -233,11 +225,6 @@ export class CargoBikeAPI extends DataSource {
.values([cargoBike]) .values([cargoBike])
.returning('*') .returning('*')
.execute(); .execute();
await entityManager.getRepository(CargoBike)
.createQueryBuilder('cargobike')
.relation(CargoBike, 'lendingStation')
.of(inserts.identifiers[0].id)
.set(cargoBike?.lendingStationId);
await entityManager.getRepository(CargoBike) await entityManager.getRepository(CargoBike)
.createQueryBuilder('cargobike') .createQueryBuilder('cargobike')
.relation(CargoBike, 'provider') .relation(CargoBike, 'provider')
@ -293,15 +280,6 @@ export class CargoBikeAPI extends DataSource {
return result === 1; return result === 1;
} }
// think this can go
async findEquipmentJoinBikeById (id: number) {
return await this.connection.getRepository(Equipment)
.createQueryBuilder('equipment')
.leftJoinAndSelect('equipment.cargoBike', 'cargoBike')
.where('equipment.id = :id', { id: id })
.getOne();
}
async equipmentByCargoBikeId (offset: number, limit: number, id: number) { async equipmentByCargoBikeId (offset: number, limit: number, id: number) {
return await this.connection.getRepository(Equipment) return await this.connection.getRepository(Equipment)
.createQueryBuilder('equipment') .createQueryBuilder('equipment')
@ -324,7 +302,6 @@ export class CargoBikeAPI extends DataSource {
.relation(Equipment, 'cargoBike') .relation(Equipment, 'cargoBike')
.of(equipment.id) .of(equipment.id)
.set(equipment.cargoBikeId); .set(equipment.cargoBikeId);
// return this.findEquipmentJoinBikeById(inserts.identifiers[0].id);
} }
return this.findEquipmentById(inserts.identifiers[0].id); return this.findEquipmentById(inserts.identifiers[0].id);
} }

@ -1,7 +1,7 @@
import { DataSource } from 'apollo-datasource'; import { DataSource } from 'apollo-datasource';
import { ApolloError, UserInputError } from 'apollo-server'; import { ApolloError, UserInputError } from 'apollo-server';
import { GraphQLError } from 'graphql'; import { GraphQLError } from 'graphql';
import {Connection, EntityManager, getConnection, QueryFailedError} from 'typeorm'; import { Connection, EntityManager, getConnection, QueryFailedError } from 'typeorm';
import { CargoBike } from '../../model/CargoBike'; import { CargoBike } from '../../model/CargoBike';
import { LendingStation } from '../../model/LendingStation'; import { LendingStation } from '../../model/LendingStation';
import { TimeFrame } from '../../model/TimeFrame'; import { TimeFrame } from '../../model/TimeFrame';
@ -35,17 +35,22 @@ export class LendingStationAPI extends DataSource {
.getMany() || new GraphQLError('Internal Server Error: could not query data from table lendingStation'); .getMany() || new GraphQLError('Internal Server Error: could not query data from table lendingStation');
} }
/**
* Finds LendingStation of a cargoBike. It will check timeFrames that overlap with today and return these records
* @param id of cargoBike
*/
async lendingStationByCargoBikeId (id: number) { async lendingStationByCargoBikeId (id: number) {
return await this.connection.getRepository(LendingStation) return (await this.connection.getRepository(TimeFrame)
.createQueryBuilder('lendingStation') .createQueryBuilder('timeframe')
.leftJoinAndSelect('lendingStation.cargoBikes', 'cargoBikes') .leftJoinAndSelect('timeframe.lendingStation', 'lendingStation')
.where('"cargoBikes".id = :id', { id: id }) .where('timeframe."cargoBikeId" = :id', { id: id })
.getOne().catch(() => { return null; }); .andWhere('timeframe."dateRange" && daterange(CURRENT_DATE,CURRENT_DATE,\'[]\')')
.getOne())?.lendingStation;
} }
async lendingStationByTimeFrameId (id: number) { async lendingStationByTimeFrameId (id: number) {
await this.connection.getRepository(TimeFrame) return await this.connection.getRepository(LendingStation)
.createQueryBuilder('timeframe') .createQueryBuilder('lendingStation')
.relation(TimeFrame, 'lendingStation') .relation(TimeFrame, 'lendingStation')
.of(id) .of(id)
.loadOne(); .loadOne();
@ -76,20 +81,32 @@ export class LendingStationAPI extends DataSource {
.getMany().catch(() => { return []; }); .getMany().catch(() => { return []; });
} }
/**
* Counts all timeframes with one lendingStation that overlap with today's date
* @param id of lendingStation
*/
async numCargoBikesByLendingStationId (id: number) { async numCargoBikesByLendingStationId (id: number) {
return await this.connection.getRepository(CargoBike) return await this.connection.getRepository(TimeFrame)
.createQueryBuilder('cargoBike') .createQueryBuilder('timeframe')
.select() .select()
.where('"cargoBike"."lendingStationId" = :id', { id: id }) .where('"timeframe"."lendingStationId" = :id', { id: id })
.andWhere('"timeframe"."dateRange" && daterange(CURRENT_DATE,CURRENT_DATE,\'[]\')')
.getCount(); .getCount();
} }
/**
* Finds cargoBikes that are currently at the lendingStation specified by id.
* It checks all timeFrames for that bike and checks whether today is within its timeRange.
* @param id of lendingStation
*/
async cargoBikesByLendingStationId (id: number) { async cargoBikesByLendingStationId (id: number) {
return await this.connection.getRepository(CargoBike) return await this.connection.getRepository(CargoBike)
.createQueryBuilder('cargoBike') .createQueryBuilder('cargoBike') // .addFrom(TimeFrame, 'timeframe')
.select() .leftJoinAndSelect('cargoBike.timeFrames', 'timeframes')
.where('"cargoBike"."lendingStationId" = :id', { id: id }) .where('timeframes."lendingStationId" = :id', { id: id })
.getMany().catch(() => { return []; }); .andWhere('timeframes."dateRange" && daterange(CURRENT_DATE,CURRENT_DATE,\'[]\')')
.printSql()
.getMany(); // .catch(() => { return []; });
} }
/** /**

@ -5,7 +5,6 @@ import { Provider } from './Provider';
import { Participant } from './Participant'; import { Participant } from './Participant';
import { InsuranceData } from './InsuranceData'; import { InsuranceData } from './InsuranceData';
import { TimeFrame } from './TimeFrame'; import { TimeFrame } from './TimeFrame';
import { LendingStation } from './LendingStation';
import { Taxes } from './Taxes'; import { Taxes } from './Taxes';
import { Equipment } from './Equipment'; import { Equipment } from './Equipment';
import { Engagement } from './Engagement'; import { Engagement } from './Engagement';
@ -129,13 +128,6 @@ export class CargoBike extends Bike implements Lockable {
}) })
timeFrames: TimeFrame[]; timeFrames: TimeFrame[];
// This relation is a little redundant because one could also check all LoanPeriods for current station
@ManyToOne(type => LendingStation, lendingStation => lendingStation.cargoBikes, {
nullable: true,
eager: true
})
lendingStation: LendingStation;
@OneToMany(type => Engagement, engagement => engagement.cargoBike) @OneToMany(type => Engagement, engagement => engagement.cargoBike)
engagement: Engagement[]; engagement: Engagement[];

@ -1,6 +1,5 @@
import { Entity, PrimaryGeneratedColumn, Column, ManyToMany, JoinTable, OneToMany, ManyToOne } from 'typeorm'; import { Entity, PrimaryGeneratedColumn, Column, ManyToMany, JoinTable, OneToMany, ManyToOne } from 'typeorm';
import { TimeFrame } from './TimeFrame'; import { TimeFrame } from './TimeFrame';
import { CargoBike } from './CargoBike';
import { Organisation } from './Organisation'; import { Organisation } from './Organisation';
import { Address } from './Provider'; import { Address } from './Provider';
import { ContactPerson } from './ContactPerson'; import { ContactPerson } from './ContactPerson';
@ -41,11 +40,6 @@ export class LendingStation {
@OneToMany(type => TimeFrame, loanPeriod => loanPeriod.lendingStation) @OneToMany(type => TimeFrame, loanPeriod => loanPeriod.lendingStation)
loanPeriods: TimeFrame[]; loanPeriods: TimeFrame[];
@OneToMany(type => CargoBike, cargoBike => cargoBike.lendingStation, {
eager: false
})
cargoBikes: CargoBike[];
@ManyToOne(type => Organisation, organization => organization.lendingStations) @ManyToOne(type => Organisation, organization => organization.lendingStations)
organization: Organisation; organization: Organisation;
} }

@ -51,6 +51,9 @@ type CargoBike {
lockedUntil: Date lockedUntil: Date
} }
"""
if you want to add bike to a lending station, create a new timeFrame with to: Date = null
"""
input CargoBikeCreateInput { input CargoBikeCreateInput {
"see column A in info tabelle" "see column A in info tabelle"
group: Group! group: Group!
@ -81,10 +84,12 @@ input CargoBikeCreateInput {
note: String note: String
providerId: ID providerId: ID
insuranceData: InsuranceDataCreateInput! insuranceData: InsuranceDataCreateInput!
lendingStationId: ID
taxes: TaxesCreateInput! taxes: TaxesCreateInput!
} }
"""
if you want to add bike to a lending station, create a new timeFrame with to: Date = null
"""
input CargoBikeUpdateInput { input CargoBikeUpdateInput {
id: ID! id: ID!
"see column A in info tabelle" "see column A in info tabelle"
@ -115,7 +120,6 @@ input CargoBikeUpdateInput {
note: String note: String
provider: String provider: String
insuranceData: InsuranceDataUpdateInput insuranceData: InsuranceDataUpdateInput
lendingStationId: ID
taxes: TaxesUpdateInput taxes: TaxesUpdateInput
"will keep Bike locked if set to true, default = false" "will keep Bike locked if set to true, default = false"
keepLock: Boolean keepLock: Boolean
@ -604,22 +608,25 @@ type LendingStation {
numCargoBikes: Int! numCargoBikes: Int!
} }
"""
If you want to create LendingStation with cargoBikes, use createTimeFrame and set to: Date = null
"""
input LendingStationCreateInput { input LendingStationCreateInput {
name: String! name: String!
contactPersonIds: [ID]! contactPersonIds: [ID]!
address: AddressCreateInput! address: AddressCreateInput!
loanPeriods: LoanPeriodsInput loanPeriods: LoanPeriodsInput
timeFrameIds: [ID]!
} }
"""
If you want to create LendingStation with cargoBikes, use createTimeFrame and set to: Date = null
"""
input LendingStationUpdateInput { input LendingStationUpdateInput {
id: ID! id: ID!
name: String name: String
contactInformation: [ContactInformationUpdateInput] contactInformation: [ContactInformationUpdateInput]
address: AddressUpdateInput address: AddressUpdateInput
loanPeriods: LoanPeriodsInput loanPeriods: LoanPeriodsInput
timeFrames: [TimeFrameUpdateInput]
} }
""" """

Loading…
Cancel
Save