resolver cargoBike.timeFramesimplemented

pull/14/head
leonnicolas 4 years ago
parent 71e737b1c6
commit e1e2302a1a
No known key found for this signature in database
GPG Key ID: 088D0743E2B65C07

@ -1,4 +1,5 @@
import { DataSource } from 'apollo-datasource';
import { ApolloError } from 'apollo-server';
import { GraphQLError } from 'graphql';
import { Connection, EntityManager, getConnection } from 'typeorm';
import { CargoBike } from '../../model/CargoBike';
@ -59,6 +60,14 @@ export class LendingStationAPI extends DataSource {
.getMany() || [];
}
async timeFramesByCargoBikeId (id: number) {
return await this.connection.getRepository(CargoBike)
.createQueryBuilder('cargobike')
.relation(CargoBike, 'timeFrames')
.of(id)
.loadMany();
}
async timeFramesByLendingStationId (id: number) {
return await this.connection.getRepository(TimeFrame)
.createQueryBuilder('timeFrame')
@ -134,6 +143,7 @@ export class LendingStationAPI extends DataSource {
}
async createTimeFrame (timeFrame: any) {
// TODO check overlapping time frames
let inserts: any;
try {
await this.connection.transaction(async (entityManager: EntityManager) => {
@ -159,7 +169,8 @@ export class LendingStationAPI extends DataSource {
.set(timeFrame.lendingStationId);
});
} catch (e) {
return new GraphQLError('Transaction could not be completed');
// TODO: throw diffrent error when date input is wrong
return new ApolloError('Transaction could not be completed');
}
inserts.generatedMaps[0].id = inserts.identifiers[0].id;
return inserts.generatedMaps[0];

@ -127,7 +127,7 @@ export class CargoBike extends Bike implements Lockable {
@OneToMany(type => TimeFrame, loanPeriod => loanPeriod.cargoBike, {
nullable: true
})
loanPeriods: 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, {

@ -24,6 +24,6 @@ export class TimeFrame {
})
note: string;
@ManyToOne(type => CargoBike, cargoBike => cargoBike.loanPeriods)
@ManyToOne(type => CargoBike, cargoBike => cargoBike.timeFrames)
cargoBike: CargoBike;
}

@ -57,6 +57,9 @@ export default {
},
lockedBy (): any {
return null;
},
timeFrames (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) {
return dataSources.lendingStationAPI.timeFramesByCargoBikeId(parent.id, req, dataSources);
}
},
Equipment: {

@ -44,6 +44,7 @@ type CargoBike {
lendingStation: LendingStation
taxes: Taxes
engagement(offset: Int!, limit: Int!): [Engagement]
timeFrames: [TimeFrame]
isLocked: Boolean!
"null if not locked by other user"
lockedBy: ID

Loading…
Cancel
Save