bikeevent, bikeeventype read and create

pull/14/head
leonnicolas 4 years ago
parent 77fd61194b
commit 81a7870cbb
No known key found for this signature in database
GPG Key ID: 088D0743E2B65C07

@ -9,6 +9,7 @@ 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'; import { EquipmentType } from '../../model/EquipmentType';
import { BikeEventType } from '../../model/BikeEventType';
/** /**
* extended datasource to feed resolvers with data about cargoBikes * extended datasource to feed resolvers with data about cargoBikes
@ -158,13 +159,62 @@ export class CargoBikeAPI extends DataSource {
} }
async createBikeEvent ({ bikeEvent }: { bikeEvent: any }) { async createBikeEvent ({ bikeEvent }: { bikeEvent: any }) {
const event = new BikeEvent(); return (await this.connection.getRepository(BikeEvent)
event.setValues(bikeEvent); .createQueryBuilder('be')
event.cargoBike = await this.findCargoBikeById(bikeEvent.cargoBikeId) as unknown as CargoBike; .insert()
if (event.cargoBike instanceof GraphQLError) { .values([bikeEvent])
return event.cargoBike; .returning('*')
.execute()).generatedMaps[0];
}
async cargoBikeByEventId (id: number) {
return await this.connection.getRepository(BikeEvent)
.createQueryBuilder('be')
.relation(BikeEvent, 'cargoBikeId')
.of(id)
.loadOne();
} }
return await this.connection.manager.save(event);
async bikeEventTypeByBikeEventId (id: number) {
return await this.connection.getRepository(BikeEvent)
.createQueryBuilder('be')
.relation(BikeEvent, 'bikeEventTypeId')
.of(id)
.loadOne();
}
async createBikeEventType (bikeEventType: any) {
return (await this.connection.getRepository(BikeEventType)
.createQueryBuilder('bet')
.insert()
.values([{ name: bikeEventType }])
.returning('*')
.execute())?.generatedMaps[0];
}
async bikeEventTypes (offset: number, limit: number) {
return await this.connection.getRepository(BikeEventType)
.createQueryBuilder('bet')
.select()
.skip(offset)
.take(limit)
.getMany();
}
async responsibleByBikeEventId (id: number) {
return await this.connection.getRepository(BikeEvent)
.createQueryBuilder('be')
.relation(BikeEvent, 'responsibleId')
.of(id)
.loadOne();
}
async relatedByBikeEventId (id: number) {
return await this.connection.getRepository(BikeEvent)
.createQueryBuilder('be')
.relation(BikeEvent, 'relatedId')
.of(id)
.loadOne();
} }
/** /**

@ -2,6 +2,7 @@ import { DataSource } from 'apollo-datasource';
import { Connection, EntityManager, getConnection } from 'typeorm'; import { Connection, EntityManager, getConnection } from 'typeorm';
import { Provider } from '../../model/Provider'; import { Provider } from '../../model/Provider';
import { Organisation } from '../../model/Organisation'; import { Organisation } from '../../model/Organisation';
import { UserInputError } from 'apollo-server';
export class ProviderAPI extends DataSource { export class ProviderAPI extends DataSource {
connection : Connection connection : Connection
@ -51,7 +52,16 @@ export class ProviderAPI extends DataSource {
.loadOne(); .loadOne();
} }
async privatePersonByProviderId (id: number) {
return await this.connection.getRepository(Provider)
.createQueryBuilder('p')
.relation(Provider, 'privatePersonId')
.of(id)
.loadOne();
}
async createProvider (provider: any) { async createProvider (provider: any) {
if (!provider.privatePersonId === !provider.organisationId) { return new UserInputError('Provider must have either privatePersonId or organisationId'); }
let inserts: any = null; let inserts: any = null;
await this.connection.transaction(async (entityManager: any) => { await this.connection.transaction(async (entityManager: any) => {
inserts = await entityManager.getRepository(Provider) inserts = await entityManager.getRepository(Provider)

@ -6,7 +6,8 @@ export enum Permission {
WritePerson = 'PERSON_WRITE', WritePerson = 'PERSON_WRITE',
ReadPerson = 'PERSON_READ', ReadPerson = 'PERSON_READ',
WriteProvider = 'PROVIDER_WRITE', WriteProvider = 'PROVIDER_WRITE',
WriteWorkshopType = 'WORKSHOP_TYPE_WRITE' WriteWorkshopType = 'WORKSHOP_TYPE_WRITE',
WriteEventType = 'BIKE_EVENT_TYPE_WRITE'
} }
// Permissions where the creation will be requested on startup // Permissions where the creation will be requested on startup
@ -38,5 +39,9 @@ export const requiredPermissions = [
{ {
name: Permission.WriteWorkshopType, name: Permission.WriteWorkshopType,
description: 'Allows to create and modify workshop types' description: 'Allows to create and modify workshop types'
},
{
name: Permission.WriteEventType,
description: 'Allows modification of bike event types'
} }
]; ];

@ -7,14 +7,6 @@ import { type } from 'os';
@Entity() @Entity()
export class BikeEvent { export class BikeEvent {
public setValues ({ id, remark, date, documents, cargoBike }: { id: number, remark: string, date: Date, documents: string[], cargoBike: CargoBike}): void {
this.id = id;
this.remark = remark;
this.date = date;
this.documents = documents;
this.cargoBike = cargoBike;
}
@PrimaryGeneratedColumn() @PrimaryGeneratedColumn()
id: number; id: number;
@ -38,22 +30,35 @@ export class BikeEvent {
date: Date; date: Date;
@ManyToOne(type => Participant) @ManyToOne(type => Participant)
responsible: Participant; @JoinColumn({
name: 'responsibleId'
})
responsibleId: number;
@ManyToOne(type => Participant) @ManyToOne(type => Participant)
related: Participant; @JoinColumn({
name: 'relatedId'
})
relatedId: number;
@Column('simple-array', { @Column('simple-array', {
nullable: true nullable: true
}) })
documents: string[]; documents: string[];
@ManyToOne(tpye => CargoBike, cargoBike => cargoBike.bikeEvents, { @ManyToOne(type => CargoBike, cargoBike => cargoBike.bikeEvents, {
nullable: false nullable: false
}) })
@JoinColumn({ name: 'cargoBikeId' }) @JoinColumn({
cargoBike: CargoBike; name: 'cargoBikeId'
})
cargoBikeId: number;
@ManyToOne(type => BikeEventType) @ManyToOne(type => BikeEventType, {
bikeEventType: BikeEventType; nullable: false
})
@JoinColumn({
name: 'bikeEventTypeId'
})
bikeEventTypeId: number;
} }

@ -6,7 +6,9 @@ export class BikeEventType implements Lockable {
@PrimaryGeneratedColumn() @PrimaryGeneratedColumn()
id: number; id: number;
@Column() @Column({
unique: true
})
name: string; name: string;
@Column({ @Column({

@ -186,7 +186,7 @@ export class CargoBike implements Lockable {
}) })
provider: Provider; provider: Provider;
@OneToMany(type => BikeEvent, bikeEvent => bikeEvent.cargoBike, { @OneToMany(type => BikeEvent, bikeEvent => bikeEvent.cargoBikeId, {
nullable: true, nullable: true,
cascade: true cascade: true
}) })

@ -41,8 +41,10 @@ export class Provider implements Lockable {
@OneToOne(type => ContactInformation, { @OneToOne(type => ContactInformation, {
nullable: true nullable: true
}) })
@JoinColumn() @JoinColumn({
contactInformationId: number; name: 'privatePersonId'
})
privatePersonId: number;
// is null when Provider is a private Person // is null when Provider is a private Person
@OneToOne(type => Organisation, organization => organization.providerId, { @OneToOne(type => Organisation, organization => organization.providerId, {

@ -37,6 +37,13 @@ export default {
} else { } else {
return new GraphQLError('Insufficiant Permissions'); return new GraphQLError('Insufficiant Permissions');
} }
},
bikeEventTypes: (_:any, { offset, limit }: { offset: number, limit: number }, { dataSources, req }: { dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.ReadBike)) {
return dataSources.cargoBikeAPI.bikeEventTypes(offset, limit);
} else {
return new GraphQLError('Insufficiant Permissions');
}
} }
}, },
CargoBike: { CargoBike: {
@ -71,29 +78,43 @@ export default {
return dataSources.cargoBikeAPI.cargoBikeByEquipmentId(parent.id); return dataSources.cargoBikeAPI.cargoBikeByEquipmentId(parent.id);
} }
}, },
BikeEvent: {
cargoBike (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) {
return dataSources.cargoBikeAPI.cargoBikeByEventId(parent.id);
},
bikeEventType (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) {
return dataSources.cargoBikeAPI.bikeEventTypeByBikeEventId(parent.id);
},
responsible (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) {
return dataSources.cargoBikeAPI.responsibleByBikeEventId(parent.id);
},
related (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) {
return dataSources.cargoBikeAPI.relatedByBikeEventId(parent.id);
}
},
Mutation: { Mutation: {
createCargoBike: (_: any, { cargoBike }: { cargoBike: any }, { 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.createCargoBike({ cargoBike }); return dataSources.cargoBikeAPI.createCargoBike({ cargoBike });
} else { } else {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }
}, },
lockCargoBikeById: (_: any, { id }: { id: number }, { dataSources, req }:{dataSources: any, req: any }) => { lockCargoBikeById: (_: any, { id }: { id: number }, { dataSources, req }: { dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.WriteBike)) { if (req.permissions.includes(Permission.WriteBike)) {
return dataSources.cargoBikeAPI.lockCargoBike(id, req, dataSources); return dataSources.cargoBikeAPI.lockCargoBike(id, req, dataSources);
} else { } else {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }
}, },
unlockCargoBikeById: (_: any, { id }: { id: number }, { dataSources, req }:{dataSources: any, req: any }) => { unlockCargoBikeById: (_: any, { id }: { id: number }, { dataSources, req }: { dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.WriteBike)) { if (req.permissions.includes(Permission.WriteBike)) {
return dataSources.cargoBikeAPI.unlockCargoBike(id, req, dataSources); return dataSources.cargoBikeAPI.unlockCargoBike(id, req, dataSources);
} else { } else {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }
}, },
updateCargoBike: (_: 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.updateCargoBike(cargoBike, req, dataSources); return dataSources.cargoBikeAPI.updateCargoBike(cargoBike, req, dataSources);
} else { } else {
@ -141,6 +162,13 @@ export default {
} else { } else {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }
},
createBikeEventType: (_: any, { name }: { name: any }, { dataSources, req }: { dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.WriteEventType)) {
return dataSources.cargoBikeAPI.createBikeEventType(name);
} else {
return new GraphQLError('Insufficient Permissions');
}
} }
} }
}; };

@ -28,6 +28,9 @@ export default {
}, },
organisation: (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) => { organisation: (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) => {
return dataSources.providerAPI.organisationByProviderId(parent.id); return dataSources.providerAPI.organisationByProviderId(parent.id);
},
privatePerson: (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) => {
return dataSources.providerAPI.privatePersonByProviderId(parent.id);
} }
}, },
Organisation: { Organisation: {

@ -412,41 +412,41 @@ input EquipmentTypeUpdateInput {
"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! bikeEventType: BikeEventType!
cargoBike: CargoBike! cargoBike: CargoBike!
responsible: Participant
related: Participant
date: Date! date: Date!
note: String description: String
""" """
Path to documents Path to documents
""" """
documents: [String]! documents: [String]!
remark: String
} }
input BikeEventCreateInput { input BikeEventCreateInput {
eventType: BikeEventType! bikeEventTypeId: ID!
"it is enough to pass the cargoBike id"
cargoBikeId: ID! cargoBikeId: ID!
responsibleId: ID
relatedId: ID
date: Date! date: Date!
note: String description: String
""" """
Path to documents Path to documents
""" """
documents: [String]! documents: [String]
remark: String
} }
"TODO: Some eventTypes are missing" type BikeEventType {
enum BikeEventType { id: ID!
""" name: String!
The enum EventType can also be represented as an enum in postgresQL. }
It is possible to add items to an enum in postgresQL without changing the source code.
However, it not possible to change the graphQL schema. input BikeEventTypeInput {
Concluding we should not use an enum here, if users want to add EventTypes to the enum. id: ID!
""" name: String
KAUF
INBETRIEBNAHME
AUSFALL
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."
@ -803,12 +803,16 @@ type Query {
timeframes(offset: Int!, limit: Int!): [TimeFrame]! timeframes(offset: Int!, limit: Int!): [TimeFrame]!
contactInformation(offset: Int!, limit: Int!): [ContactInformation]! contactInformation(offset: Int!, limit: Int!): [ContactInformation]!
persons(offset: Int!, limit: Int!): [Person] persons(offset: Int!, limit: Int!): [Person]
bikeEventTypes(offset: Int!, limit: Int!): [BikeEventType]
"returns BikeEvent with CargoBike" "returns BikeEvent with CargoBike"
bikeEventById(id:ID!): BikeEvent! bikeEventById(id:ID!): BikeEvent!
} }
type Mutation { type Mutation {
"creates new cargoBike and returns cargobike with new ID" """
CargoBikes
creates new cargoBike and returns cargobike with new ID
"""
createCargoBike(cargoBike: CargoBikeCreateInput!): CargoBike! createCargoBike(cargoBike: CargoBikeCreateInput!): CargoBike!
"lock cargoBike returns bike if bike is not locked and locks bike or Error if bike cannot be locked" "lock cargoBike returns bike if bike is not locked and locks bike or Error if bike cannot be locked"
lockCargoBikeById(id: ID!): CargoBike! lockCargoBikeById(id: ID!): CargoBike!
@ -816,7 +820,10 @@ type Mutation {
unlockCargoBikeById(id: ID!): Boolean! unlockCargoBikeById(id: ID!): Boolean!
"updates cargoBike of given ID with supplied fields and returns updated cargoBike" "updates cargoBike of given ID with supplied fields and returns updated cargoBike"
updateCargoBike(cargoBike: CargoBikeUpdateInput!): CargoBike! updateCargoBike(cargoBike: CargoBikeUpdateInput!): CargoBike!
"creates new peace of unique Equipment" """
EQUIPMENT
creates new peace of unique Equipment
"""
createEquipment(equipment: EquipmentCreateInput!): Equipment! createEquipment(equipment: EquipmentCreateInput!): Equipment!
"lock equipment returns true if bike is not locked or if it doesnt exist" "lock equipment returns true if bike is not locked or if it doesnt exist"
lockEquipmentById(id: ID!): Equipment! lockEquipmentById(id: ID!): Equipment!
@ -825,13 +832,20 @@ type Mutation {
"update Equipment, returns updated equipment. CargoBike will be null, if cargoBikeId is not set. Pass null for cargoBikeIs to delete the relation" "update Equipment, returns updated equipment. CargoBike will be null, if cargoBikeId is not set. Pass null for cargoBikeIs to delete the relation"
updateEquipment(equipment: EquipmentUpdateInput!): Equipment! updateEquipment(equipment: EquipmentUpdateInput!): Equipment!
createEquipmentType(equipmentType: EquipmentTypeCreateInput!): EquipmentType! createEquipmentType(equipmentType: EquipmentTypeCreateInput!): EquipmentType!
"creates new lendingStation and returns lendingStation with new ID" """
LENDINGSTATION
creates new lendingStation and returns lendingStation with new ID
"""
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!
createTimeFrame(timeFrame: TimeFrameCreateInput!): TimeFrame! createTimeFrame(timeFrame: TimeFrameCreateInput!): TimeFrame!
"""
BIKEEVENT
"""
createBikeEventType(name: String!): BikeEventType!
"creates new BikeEvent" "creates new BikeEvent"
createBikeEvent(bikeEvent: BikeEventCreateInput): BikeEvent! createBikeEvent(bikeEvent: BikeEventCreateInput!): BikeEvent!
"create participant" "create participant"
createParticipant(participant: ParticipantCreateInput!): Participant! createParticipant(participant: ParticipantCreateInput!): Participant!
createWorkshopType(workshopType: WorkshopTypeCreateInput!): WorkshopType! createWorkshopType(workshopType: WorkshopTypeCreateInput!): WorkshopType!

Loading…
Cancel
Save