diff --git a/src/datasources/db/cargobikeAPI.ts b/src/datasources/db/cargobikeAPI.ts index af7d5f8..c0d17d5 100644 --- a/src/datasources/db/cargobikeAPI.ts +++ b/src/datasources/db/cargobikeAPI.ts @@ -118,11 +118,6 @@ export class CargoBikeAPI extends DataSource { .values([cargoBike]) .returning('*') .execute(); - await entityManager.getRepository(CargoBike) - .createQueryBuilder('cargobike') - .relation(CargoBike, 'provider') - .of(inserts.identifiers[0].id) - .set(cargoBike?.providerId); cargoBike?.equipmentTypeIds && await entityManager.getRepository(CargoBike) .createQueryBuilder('cargobike') .relation(CargoBike, 'equipmentTypeIds') @@ -372,7 +367,7 @@ export class CargoBikeAPI extends DataSource { .getOne(); } - async eqiupmentTypes (offset: number, limit: number) { + async equipmentTypes (offset: number, limit: number) { return await this.connection.getRepository(EquipmentType) .createQueryBuilder('et') .select() diff --git a/src/datasources/db/lendingstationAPI.ts b/src/datasources/db/lendingstationAPI.ts index ffaff79..12c4f67 100644 --- a/src/datasources/db/lendingstationAPI.ts +++ b/src/datasources/db/lendingstationAPI.ts @@ -41,18 +41,22 @@ export class LendingStationAPI extends DataSource { * @param id of cargoBike */ async lendingStationByCargoBikeId (id: number) { - return (await this.connection.getRepository(TimeFrame) - .createQueryBuilder('timeframe') - .leftJoinAndSelect('timeframe.lendingStation', 'lendingStation') - .where('timeframe."cargoBikeId" = :id', { id: id }) - .andWhere('timeframe."dateRange" && daterange(CURRENT_DATE,CURRENT_DATE,\'[]\')') - .getOne())?.lendingStation; + return await this.connection.getRepository(TimeFrame) + .createQueryBuilder('tf') + .relation(TimeFrame, 'lendingStationId') + .of((await this.connection.getRepository(TimeFrame)// TODO maybe this can be done with a sub query + .createQueryBuilder('tf') + .select() + .where('"cargoBikeId" = :cid', { cid: id }) + .andWhere('"dateRange" && daterange(CURRENT_DATE,CURRENT_DATE,\'[]\')') + .getOne())?.id) + .loadOne(); } async lendingStationByTimeFrameId (id: number) { return await this.connection.getRepository(LendingStation) .createQueryBuilder('lendingStation') - .relation(TimeFrame, 'lendingStation') + .relation(TimeFrame, 'lendingStationId') .of(id) .loadOne(); } @@ -198,7 +202,7 @@ export class LendingStationAPI extends DataSource { .returning('*') .values([timeFrame]) .execute(); - await entityManager.getRepository(TimeFrame) + /* await entityManager.getRepository(TimeFrame) .createQueryBuilder() .relation(TimeFrame, 'cargoBike') .of(inserts.identifiers[0].id) @@ -208,6 +212,7 @@ export class LendingStationAPI extends DataSource { .relation(TimeFrame, 'lendingStation') .of(inserts.identifiers[0].id) .set(timeFrame.lendingStationId); + */ }); } catch (e) { if (e instanceof UserInputError) { diff --git a/src/datasources/db/participantAPI.ts b/src/datasources/db/participantAPI.ts index 5bf9a9e..d3d8a28 100644 --- a/src/datasources/db/participantAPI.ts +++ b/src/datasources/db/participantAPI.ts @@ -40,12 +40,16 @@ export class ParticipantAPI extends DataSource { } async participantByCargoBikeId (id:number) { - return await this.connection.getRepository(Participant) - .createQueryBuilder('participant') - .leftJoinAndSelect('participant.cargoBike', 'cargoBike') - .where('"cargoBike".id = :id', { id: id }) - .andWhere('"cargoBike"."participantId" = participant.id') - .getOne(); + return await this.connection.getRepository(Engagement) + .createQueryBuilder('e') + .relation(Engagement, 'participantId') + .of((await this.connection.getRepository(Engagement)// TODO do this with a sub query + .createQueryBuilder('e') + .select() + .where('"dateRange" && daterange(CURRENT_DATE,CURRENT_DATE,\'[]\')') + .andWhere('"cargoBikeId" = :id', { id: id }) + .getOne())?.id + ).loadOne(); } async engagementByParticipantId (id: number) { @@ -128,7 +132,7 @@ export class ParticipantAPI extends DataSource { } /** - * creates participant and creates realtion to given contactInformation + * creates participant and creates relation to given contactInformation * @param participant to be created */ async createParticipant (participant: any) { @@ -142,7 +146,7 @@ export class ParticipantAPI extends DataSource { .returning('*') .execute(); }); - return this.getParticipantById(inserts.identifiers[0].id); + return this.getParticipantById(inserts?.identifiers[0].id); } async createEngagement (engagement: any) { @@ -167,7 +171,7 @@ export class ParticipantAPI extends DataSource { .returning('*') .execute(); }); - return this.engagementById(inserts.identifiers[0].id); + return this.engagementById(inserts?.identifiers[0].id); } async createEngagementType (engagementType: any) { diff --git a/src/datasources/db/providerAPI.ts b/src/datasources/db/providerAPI.ts index 96b4101..21cea62 100644 --- a/src/datasources/db/providerAPI.ts +++ b/src/datasources/db/providerAPI.ts @@ -40,7 +40,7 @@ export class ProviderAPI extends DataSource { async providerByCargoBikeId (id: number) { return await this.connection.getRepository(CargoBike) .createQueryBuilder('cb') - .relation(CargoBike, 'provider') + .relation(CargoBike, 'providerId') .of(id) .loadOne(); } diff --git a/src/model/CargoBike.ts b/src/model/CargoBike.ts index 6445735..6543e45 100644 --- a/src/model/CargoBike.ts +++ b/src/model/CargoBike.ts @@ -163,7 +163,7 @@ export class CargoBike implements Lockable { // Equipment that is not unique and is supposed to be selected out of a list e.g. drop down @ManyToMany(type => EquipmentType, equipmentType => equipmentType.cargoBikeIds) @JoinTable() - equipmentTypeIds: number[]; + equipmentTypeIds: number[]; // Security information @Column(type => Security) @@ -181,10 +181,10 @@ export class CargoBike implements Lockable { }) note: string; - @ManyToOne(type => Provider, { + @ManyToOne(type => Provider, provider => provider.cargoBikeIds, { nullable: true }) - provider: Provider; + providerId: number; @OneToMany(type => BikeEvent, bikeEvent => bikeEvent.cargoBikeId, { nullable: true, @@ -196,7 +196,7 @@ export class CargoBike implements Lockable { @Column(type => InsuranceData) insuranceData: InsuranceData; - @OneToMany(type => TimeFrame, loanPeriod => loanPeriod.cargoBike, { + @OneToMany(type => TimeFrame, timeFrame => timeFrame.cargoBikeId, { nullable: true }) timeFrames: TimeFrame[]; diff --git a/src/model/Equipment.ts b/src/model/Equipment.ts index b5a0d57..a4d9b9b 100644 --- a/src/model/Equipment.ts +++ b/src/model/Equipment.ts @@ -3,20 +3,6 @@ import { CargoBike, Lockable } from './CargoBike'; @Entity() export class Equipment implements Lockable { - setValues ({ id, serialNo, title, description, cargoBike }: { - id: number, - serialNo: string, - title: string, - description: string, - cargoBike: CargoBike - }) { - this.id = id; - this.serialNo = serialNo; - this.title = title; - this.description = description; - this.cargoBike = cargoBike; - } - @PrimaryGeneratedColumn() id: number; diff --git a/src/model/LendingStation.ts b/src/model/LendingStation.ts index 9897da3..00a567e 100644 --- a/src/model/LendingStation.ts +++ b/src/model/LendingStation.ts @@ -56,7 +56,7 @@ export class LendingStation implements Lockable { @Column(type => LoanPeriod) loanPeriod: LoanPeriod; - @OneToMany(type => TimeFrame, timeFrame => timeFrame.lendingStation) + @OneToMany(type => TimeFrame, timeFrame => timeFrame.lendingStationId) timeFrames: TimeFrame[]; @ManyToOne(type => Organisation, organization => organization.lendingStations) diff --git a/src/model/Provider.ts b/src/model/Provider.ts index 3509143..529f8bd 100644 --- a/src/model/Provider.ts +++ b/src/model/Provider.ts @@ -55,8 +55,8 @@ export class Provider implements Lockable { }) organisationId: number; - @OneToMany(type => CargoBike, cargoBike => cargoBike.provider) - cargoBikes: CargoBike[]; + @OneToMany(type => CargoBike, cargoBike => cargoBike.providerId) + cargoBikeIds: number[]; @Column({ nullable: true, diff --git a/src/model/TimeFrame.ts b/src/model/TimeFrame.ts index cc45f9e..c02686d 100644 --- a/src/model/TimeFrame.ts +++ b/src/model/TimeFrame.ts @@ -1,4 +1,4 @@ -import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm'; +import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from 'typeorm'; import { LendingStation } from './LendingStation'; import { CargoBike, Lockable } from './CargoBike'; @@ -16,7 +16,10 @@ export class TimeFrame implements Lockable { dateRange: Date[]; @ManyToOne(type => LendingStation, lendingStation => lendingStation.timeFrames) - lendingStation: LendingStation; + @JoinColumn({ + name: 'lendingStationId' + }) + lendingStationId: number; @Column({ nullable: true @@ -24,7 +27,10 @@ export class TimeFrame implements Lockable { note: string; @ManyToOne(type => CargoBike, cargoBike => cargoBike.timeFrames) - cargoBike: CargoBike; + @JoinColumn({ + name: 'cargoBikeId' + }) + cargoBikeId: number; @Column({ nullable: true,