Merge pull request #9 from flotte-goes-smart/dev

added some resolvers and paging on resolver cargobikes
pull/14/head
leonnicolas 4 years ago committed by GitHub
commit dcd96f00b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,6 +2,7 @@ import { DataSource } from 'apollo-datasource';
import { getConnection, Connection } from 'typeorm'; import { getConnection, Connection } from 'typeorm';
import { CargoBike } from '../../model/CargoBike'; import { CargoBike } from '../../model/CargoBike';
import { GraphQLError } from 'graphql'; import { GraphQLError } from 'graphql';
import { BikeEvent } from '../../model/BikeEvent';
/** /**
* extended datasource to feed resolvers with data about cargoBikes * extended datasource to feed resolvers with data about cargoBikes
*/ */
@ -12,10 +13,13 @@ export class CargoBikeAPI extends DataSource {
this.connection = getConnection(); this.connection = getConnection();
} }
async getCargoBikes () { async getCargoBikes (offset: number, limit: number) {
return await this.connection.createQueryBuilder() return await this.connection.createQueryBuilder()
.select('cargoBikes') .select('cargoBikes')
.from(CargoBike, 'cargoBikes') .from(CargoBike, 'cargoBikes')
.orderBy('name', 'ASC')
.offset(offset)
.limit(limit)
.getMany(); .getMany();
} }
@ -32,34 +36,18 @@ export class CargoBikeAPI extends DataSource {
.getOne() || new GraphQLError('ID not found'); .getOne() || new GraphQLError('ID not found');
} }
async updateBike ({ id, name }:{id:any, name: string }) {
const bike = new CargoBike();
bike.id = id;
bike.description = 'text';
bike.name = name;
await this.connection.manager.save(bike);
return {
success: true,
message: 'bla',
cargoBike: {
id,
name
}
};
}
/** /**
* Updates CargoBike and return updated cargoBike * Updates CargoBike and return updated cargoBike
* @param param0 cargoBike to be updated * @param param0 cargoBike to be updated
*/ */
async updateCargoBike ({ cargoBike }:{ cargoBike: any }) { async updateCargoBike ({ cargoBike }:{ cargoBike: CargoBike }) {
const bike = await this.connection.manager.createQueryBuilder() const bike = await this.connection.manager.createQueryBuilder()
.select('cargoBike') .select('cargoBike')
.from(CargoBike, 'cargoBike') .from(CargoBike, 'cargoBike')
.where('cargoBike.id = :id', { id: cargoBike.id }) .where('cargoBike.id = :id', { id: cargoBike.id })
.getOne(); .getOne();
if (bike) { if (bike) {
await this.connection await this.connection.manager
.createQueryBuilder() .createQueryBuilder()
.update(CargoBike) .update(CargoBike)
.set({ ...cargoBike }) .set({ ...cargoBike })
@ -88,4 +76,26 @@ export class CargoBikeAPI extends DataSource {
newbike.id = inserts.identifiers[0].id; newbike.id = inserts.identifiers[0].id;
return newbike; return newbike;
} }
async createBikeEvent ({ bikeEvent }: { bikeEvent: any }) {
const event = new BikeEvent();
event.setValues(bikeEvent);
event.cargoBike = await this.findCargoBikeById({ id: bikeEvent.cargoBikeId }) as unknown as CargoBike;
if (event.cargoBike instanceof GraphQLError) {
return event.cargoBike;
}
return await this.connection.manager.save(event);
}
/**
* return bikeEvent including CargoBike
* @param id of event
*/
async findBikeEventById (id: number) {
return await this.connection.getRepository(BikeEvent)
.createQueryBuilder('bikeEvent')
.leftJoinAndSelect('bikeEvent.cargoBike', 'cargoBike')
.where('bikeEvent.id = :id', { id: id })
.getOne();
}
} }

@ -1,15 +1,26 @@
/* eslint no-unused-vars: "off" */ /* eslint no-unused-vars: "off" */
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; import { Entity, Column, PrimaryGeneratedColumn, ManyToOne, JoinColumn } from 'typeorm';
import { CargoBike } from './CargoBike';
export enum BikeEventType { export enum BikeEventType {
KAUF = 'Kauf', KAUF = 'KAUF',
INBETRIEBNAHME = 'Inbetriebnahme', INBETRIEBNAHME = 'INBETRIEBNAHME',
AUSFALL = 'Ausfall', AUSFALL = 'AUSFALL',
WARTUNG = 'Wartung' WARTUNG = 'WARTUNG',
ANDERE = 'ANDERE'
} }
@Entity() @Entity()
export class BikeEvent { export class BikeEvent {
public setValues ({ id, note, date, documents, cargoBike, eventType }: { id: number, note: string, date: Date, documents: string[], cargoBike: CargoBike, eventType: BikeEventType}): void {
this.id = id;
this.note = note;
this.date = date;
this.documents = documents;
this.cargoBike = cargoBike;
this.eventType = eventType;
}
@PrimaryGeneratedColumn() @PrimaryGeneratedColumn()
id: number; id: number;
@ -28,6 +39,12 @@ export class BikeEvent {
}) })
documents: string[]; documents: string[];
@ManyToOne(tpye => CargoBike, cargoBike => cargoBike.bikeEvents, {
nullable: false
})
@JoinColumn({ name: 'cargoBikeId' })
cargoBike: CargoBike;
@Column({ @Column({
type: 'enum', type: 'enum',
enum: BikeEventType enum: BikeEventType

@ -1,5 +1,5 @@
/* eslint no-unused-vars: "off" */ /* eslint no-unused-vars: "off" */
import { Entity, Column, PrimaryGeneratedColumn, OneToMany, ManyToOne, OneToOne } from 'typeorm'; import { Entity, Column, PrimaryGeneratedColumn, OneToMany, ManyToOne, JoinColumn } from 'typeorm';
import { Bike } from './BikeFeatures'; import { Bike } from './BikeFeatures';
import { ChainSwap } from './ChainSwap'; import { ChainSwap } from './ChainSwap';
import { Provider } from './Provider'; import { Provider } from './Provider';
@ -10,6 +10,7 @@ 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';
import { BikeEvent } from './BikeEvent';
export enum Group { export enum Group {
KL = 'KL', KL = 'KL',
@ -112,6 +113,13 @@ export class CargoBike extends Bike {
}) })
coordinator: Participant; coordinator: Participant;
@OneToMany(type => BikeEvent, bikeEvent => bikeEvent.cargoBike, {
nullable: true,
cascade: true
})
@JoinColumn()
bikeEvents: BikeEvent[];
@Column(type => InsuranceData) @Column(type => InsuranceData)
insuranceData: InsuranceData; insuranceData: InsuranceData;

@ -9,6 +9,14 @@ export class Equipment {
@Column() @Column()
serialNo: string; serialNo: string;
@Column()
title: string;
@Column({
nullable: true
})
description: string;
@OneToMany(type => CargoBike, cargoBike => cargoBike.equipment, { @OneToMany(type => CargoBike, cargoBike => cargoBike.equipment, {
nullable: true nullable: true
}) })

@ -15,6 +15,7 @@ export class Organization {
}) })
provider: Provider; provider: Provider;
// Court where association was registerd
@Column({ @Column({
nullable: true nullable: true
}) })

@ -2,8 +2,8 @@
import { Column } from 'typeorm'; import { Column } from 'typeorm';
export enum OrganizationArea { export enum OrganizationArea {
IB, IB = 'IB',
ZB ZB = 'ZB'
} }
export class Taxes { export class Taxes {
@Column() @Column()

@ -10,32 +10,39 @@ export default {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }
}, },
cargoBikes: (_: any, __: any, { dataSources, req }: { dataSources: any, req: any }) => { cargoBikes: (_: any, { offset, limit }: { offset: number, limit: number }, { dataSources, req }: { dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.ReadBike)) { if (req.permissions.includes(Permission.ReadBike)) {
return dataSources.cargoBikeAPI.getCargoBikes(); return dataSources.cargoBikeAPI.getCargoBikes(offset, limit);
} else {
return new GraphQLError('Insufficiant Permissions');
}
},
bikeEventById: (_:any, { id }: { id: number }, { dataSources, req }: { dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.ReadBike)) {
return dataSources.cargoBikeAPI.findBikeEventById(id);
} else { } else {
return new GraphQLError('Insufficiant Permissions'); return new GraphQLError('Insufficiant Permissions');
} }
} }
}, },
Mutation: { Mutation: {
addBike: (_: any, { id, name }:{id: any, name:string}, { dataSources, req }:{dataSources: any, req: any }) => { createCargoBike: (_: any, { cargoBike }: { cargoBike: any }, { dataSources, req }:{dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.WriteBike)) { if (req.permissions.includes(Permission.WriteBike)) {
return dataSources.cargoBikeAPI.updateBike({ id, name }); return dataSources.cargoBikeAPI.createCargoBike({ cargoBike });
} else { } else {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }
}, },
createCargoBike: (_: any, { cargoBike }: { cargoBike: any }, { dataSources, req }:{dataSources: any, req: any }) => { updateCargoBike: (_: any, { cargoBike }: { cargoBike: any }, { dataSources, req }:{dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.WriteBike)) { if (req.permissions.includes(Permission.WriteBike)) {
return dataSources.cargoBikeAPI.createCargoBike({ cargoBike }); return dataSources.cargoBikeAPI.updateCargoBike({ cargoBike });
} else { } else {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }
}, },
updateCargoBike: (_: any, { cargoBike }: { cargoBike: any }, { dataSources, req }:{dataSources: any, req: any }) => { createBikeEvent: (_: any, { bikeEvent }: { bikeEvent: any }, { dataSources, req }: { dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.WriteBike)) { if (req.permissions.includes(Permission.WriteBike)) {
return dataSources.cargoBikeAPI.updateCargoBike({ cargoBike }); return dataSources.cargoBikeAPI.createBikeEvent({ bikeEvent });
} else { } else {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }

@ -28,7 +28,7 @@ type CargoBike {
Does not refere to an extra table in the database. Does not refere to an extra table in the database.
""" """
dimensionsAndLoad: DimensionsAndLoad! dimensionsAndLoad: DimensionsAndLoad!
events: [BikeEvent] bikeEvents: [BikeEvent]
equipment: [Equipment] equipment: [Equipment]
"Refers to equipment that is not unique. See kommentierte info tabelle -> Fragen -> Frage 2" "Refers to equipment that is not unique. See kommentierte info tabelle -> Fragen -> Frage 2"
otherEquipment: [String] otherEquipment: [String]
@ -76,11 +76,11 @@ input CargoBikeCreateInput {
note: String note: String
provider: String provider: String
insuranceData: InsuranceDataCreateInput! insuranceData: InsuranceDataCreateInput!
taxes: TaxesCreateInput taxes: TaxesCreateInput!
} }
input CargoBikeUpdateInput { input CargoBikeUpdateInput {
id: ID! id: Int!
"see column A in info tabelle" "see column A in info tabelle"
group: Group group: Group
name: String name: String
@ -272,13 +272,26 @@ type Equipment {
"An Event is a point in time, when the state of the bike somehow changed." "An Event is a point in time, when the state of the bike somehow changed."
type BikeEvent { type BikeEvent {
id: ID! id: ID!
eventType: BikeEventType eventType: BikeEventType!
cargoBike: CargoBike!
date: Date!
note: String
"""
Path to documents
"""
documents: [String]!
}
input BikeEventCreateInput {
eventType: BikeEventType!
"it is enough to pass the cargoBike id"
cargoBikeId: ID!
date: Date! date: Date!
note: String note: String
""" """
Path to documents Path to documents
""" """
documents: [String] documents: [String]!
} }
"TODO: Some eventTypes are missing (und auf deutsch)" "TODO: Some eventTypes are missing (und auf deutsch)"
@ -293,6 +306,7 @@ enum BikeEventType {
INBETRIEBNAHME INBETRIEBNAHME
AUSFALL AUSFALL
WARTUNG WARTUNG
ANDERE
} }
"How are the dimensions and how much weight can handle a bike. This data is merged in the CargoBike table and the BikeModel table." "How are the dimensions and how much weight can handle a bike. This data is merged in the CargoBike table and the BikeModel table."
@ -574,8 +588,8 @@ input AddressUpdateInput {
type Query { type Query {
cargoBikeById(id:ID!): CargoBike cargoBikeById(id:ID!): CargoBike
"returns all cargoBikes" "returns cargoBikes ordered by name ascending"
cargoBikes: [CargoBike]! cargoBikes(offset: Int!, limit: Int!): [CargoBike]!
"not important, you can just use providerById {cargoBikes}" "not important, you can just use providerById {cargoBikes}"
cargoBikesByProvider(providerId:ID!): [CargoBike]! cargoBikesByProvider(providerId:ID!): [CargoBike]!
providerById(id:ID!): Provider providerById(id:ID!): Provider
@ -585,11 +599,11 @@ type Query {
lendingStationById(id:ID!): LendingStation lendingStationById(id:ID!): LendingStation
lendingStations: [LendingStation]! lendingStations: [LendingStation]!
contactInformation: [ContactInformation]! contactInformation: [ContactInformation]!
"returns BikeEvent with CargoBike"
bikeEventById(id:ID!): BikeEvent!
} }
type Mutation { type Mutation {
"for testing"
addBike(id: ID!, name: String): CargoBike!
"creates new cargoBike and returns cargobike with new ID" "creates new cargoBike and returns cargobike with new ID"
createCargoBike(cargoBike: CargoBikeCreateInput!): CargoBike! createCargoBike(cargoBike: CargoBikeCreateInput!): CargoBike!
"updates cargoBike of given ID with supplied fields and returns updated cargoBike" "updates cargoBike of given ID with supplied fields and returns updated cargoBike"
@ -598,6 +612,8 @@ type Mutation {
createLendingStation(lendingStation: LendingStationCreateInput): LendingStation! createLendingStation(lendingStation: LendingStationCreateInput): LendingStation!
"updates lendingStation of given ID with supplied fields and returns updated lendingStation" "updates lendingStation of given ID with supplied fields and returns updated lendingStation"
updateLendingStation(lendingstation: LendingStationUpdateInput!): LendingStation! updateLendingStation(lendingstation: LendingStationUpdateInput!): LendingStation!
"creates new BikeEvent"
createBikeEvent(bikeEvent: BikeEventCreateInput): BikeEvent!
} }
`; `;

Loading…
Cancel
Save