resolvers for Equipmentypes

pull/14/head
leonnicolas 4 years ago
parent 037cd06651
commit 9ba635d943
No known key found for this signature in database
GPG Key ID: 088D0743E2B65C07

@ -1,5 +1,5 @@
import { DataSource } from 'apollo-datasource'; import { DataSource } from 'apollo-datasource';
import { getConnection, Connection, ObjectType } from 'typeorm'; import { getConnection, Connection, ObjectType, EntityManager } from 'typeorm';
import { CargoBike, Lockable } from '../../model/CargoBike'; import { CargoBike, Lockable } from '../../model/CargoBike';
import { GraphQLError } from 'graphql'; import { GraphQLError } from 'graphql';
import { BikeEvent } from '../../model/BikeEvent'; import { BikeEvent } from '../../model/BikeEvent';
@ -8,6 +8,7 @@ import { Engagement } from '../../model/Engagement';
import { Provider } from '../../model/Provider'; 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';
/** /**
* extended datasource to feed resolvers with data about cargoBikes * extended datasource to feed resolvers with data about cargoBikes
@ -104,24 +105,27 @@ export class CargoBikeAPI extends DataSource {
} }
const keepLock = cargoBike?.keepLock; const keepLock = cargoBike?.keepLock;
delete cargoBike.keepLock; delete cargoBike.keepLock;
const bike = await this.connection.manager.createQueryBuilder()
.select('cargoBike')
.from(CargoBike, 'cargoBike')
.where('cargoBike.id = :id', { id: cargoBike.id })
.getOne();
if (bike.id) {
delete cargoBike.lendingStationId; delete cargoBike.lendingStationId;
await this.connection.manager let equipmentTypeIds: any = null;
.createQueryBuilder() if (cargoBike.equipmentTypeIds) {
.update(CargoBike) equipmentTypeIds = cargoBike.equipmentTypeIds;
delete cargoBike.equipmentTypeIds;
}
await this.connection.transaction(async (entityManager: EntityManager) => {
await entityManager.getRepository(CargoBike)
.createQueryBuilder('cargobike')
.update()
.set({ ...cargoBike }) .set({ ...cargoBike })
.where('id = :id', { id: bike.id }) .where('id = :id', { id: cargoBike.id })
.execute(); .execute();
equipmentTypeIds && await entityManager.getRepository(CargoBike)
.createQueryBuilder('cargobike')
.relation(CargoBike, 'equipmentTypeIds')
.of(cargoBike.id)
.addAndRemove(equipmentTypeIds, await this.equipmentTypeByCargoBikeId(cargoBike.id)); // TODO remove all existing relations
});
!keepLock && await this.unlockCargoBike(cargoBike.id, req, dataSources); !keepLock && await this.unlockCargoBike(cargoBike.id, req, dataSources);
return await this.findCargoBikeById(bike.id); return await this.findCargoBikeById(cargoBike.id);
} else {
return new GraphQLError('ID not in database');
}
} }
/** /**
@ -144,8 +148,14 @@ export class CargoBikeAPI extends DataSource {
.relation(CargoBike, 'provider') .relation(CargoBike, 'provider')
.of(inserts.identifiers[0].id) .of(inserts.identifiers[0].id)
.set(cargoBike?.providerId); .set(cargoBike?.providerId);
cargoBike?.equipmentTypeIds && await entityManager.getRepository(CargoBike)
.createQueryBuilder('cargobike')
.relation(CargoBike, 'equipmentTypeIds')
.of(inserts.identifiers[0].id)
.add(cargoBike.equipmentTypeIds);
}); });
} catch (e: any) { } catch (e: any) {
console.log(e);
return new GraphQLError('Transaction could not be completed'); return new GraphQLError('Transaction could not be completed');
} }
const newbike = inserts.generatedMaps[0]; const newbike = inserts.generatedMaps[0];
@ -285,4 +295,31 @@ export class CargoBikeAPI extends DataSource {
.limit(limit) .limit(limit)
.getMany(); .getMany();
} }
async createEquipmentType (equipmentType: any) {
const inserts = await this.connection.getRepository(EquipmentType)
.createQueryBuilder('equipment')
.insert()
.values([equipmentType])
.returning('*')
.execute();
inserts.generatedMaps[0].id = inserts.identifiers[0].id;
return inserts.generatedMaps[0];
}
async equipmentTypeById (id: number) {
return await this.connection.getRepository(EquipmentType)
.createQueryBuilder('equipmentType')
.select()
.where('"equipmentType".id = :id', { id: id })
.getOne();
}
async equipmentTypeByCargoBikeId (id: number) {
return this.connection.getRepository(CargoBike)
.createQueryBuilder('cargobike')
.relation(CargoBike, 'equipmentTypeIds')
.of(id)
.loadMany();
}
} }

@ -2,6 +2,7 @@
export enum Permission { export enum Permission {
ReadBike = 'BIKE_READ', ReadBike = 'BIKE_READ',
WriteBike = 'BIKE_WRITE', WriteBike = 'BIKE_WRITE',
WriteEquipmentType = 'EQUIPMENT_TYPE_WRITE',
} }
// Permissions where the creation will be requested on startup // Permissions where the creation will be requested on startup
@ -13,5 +14,9 @@ export const requiredPermissions = [
{ {
name: Permission.WriteBike, name: Permission.WriteBike,
description: 'Allows the modification of bike information' description: 'Allows the modification of bike information'
},
{
name: Permission.WriteEquipmentType,
description: 'Allows the modification of EquipmentTypes'
} }
]; ];

@ -1,5 +1,5 @@
/* eslint no-unused-vars: "off" */ /* eslint no-unused-vars: "off" */
import { Entity, Column, PrimaryGeneratedColumn, OneToMany, ManyToOne, JoinColumn, ManyToMany } from 'typeorm'; import { Entity, Column, PrimaryGeneratedColumn, OneToMany, ManyToOne, JoinColumn, ManyToMany, JoinTable } from 'typeorm';
import { Provider } from './Provider'; import { Provider } from './Provider';
import { Participant } from './Participant'; import { Participant } from './Participant';
import { InsuranceData } from './InsuranceData'; import { InsuranceData } from './InsuranceData';
@ -161,8 +161,9 @@ export class CargoBike implements Lockable {
equipment: Equipment[]; equipment: Equipment[];
// Equipment that is not unique and is supposed to be selected out of a list e.g. drop down // Equipment that is not unique and is supposed to be selected out of a list e.g. drop down
@ManyToMany(type => EquipmentType, equipmentType => equipmentType.cargoBikes) @ManyToMany(type => EquipmentType, equipmentType => equipmentType.cargoBikeIds)
miscellaneousEquipment: EquipmentType[]; @JoinTable()
equipmentTypeIds: number[];
// Security information // Security information
@Column(type => Security) @Column(type => Security)
@ -185,11 +186,6 @@ export class CargoBike implements Lockable {
}) })
provider: Provider; provider: Provider;
@ManyToOne(type => Participant, participant => participant.cargoBikes, {
nullable: true
})
coordinator: Participant;
@OneToMany(type => BikeEvent, bikeEvent => bikeEvent.cargoBike, { @OneToMany(type => BikeEvent, bikeEvent => bikeEvent.cargoBike, {
nullable: true, nullable: true,
cascade: true cascade: true

@ -15,8 +15,8 @@ export class EquipmentType implements Lockable {
}) })
description: string; description: string;
@ManyToMany(type => CargoBike, cargoBike => cargoBike.miscellaneousEquipment) @ManyToMany(type => CargoBike, cargoBike => cargoBike.equipmentTypeIds)
cargoBikes: CargoBike[]; cargoBikeIds: number[];
@Column({ @Column({
nullable: true, nullable: true,

@ -41,10 +41,6 @@ export class Participant {
}) })
locationZIPs: string[]; locationZIPs: string[];
// this should go, we dont need it
@OneToMany(type => CargoBike, cargoBike => cargoBike.coordinator)
cargoBikes: CargoBike[];
@OneToMany(type => Engagement, engagement => engagement.participant) @OneToMany(type => Engagement, engagement => engagement.participant)
engagement: Engagement[]; engagement: Engagement[];

@ -60,7 +60,11 @@ export default {
}, },
timeFrames (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) { timeFrames (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) {
return dataSources.lendingStationAPI.timeFramesByCargoBikeId(parent.id, req, dataSources); return dataSources.lendingStationAPI.timeFramesByCargoBikeId(parent.id, req, dataSources);
},
equipmentType (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) {
return dataSources.cargoBikeAPI.equipmentTypeByCargoBikeId(parent.id, req, dataSources);
} }
}, },
Equipment: { Equipment: {
cargoBike (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) { cargoBike (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) {
@ -130,6 +134,13 @@ export default {
} else { } else {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }
},
createEquipmentType: (_: any, { equipmentType }: { equipmentType: any }, { dataSources, req }: { dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.WriteEquipmentType)) {
return dataSources.cargoBikeAPI.createEquipmentType(equipmentType);
} else {
return new GraphQLError('Insufficient Permissions');
}
} }
} }
}; };

@ -34,7 +34,7 @@ type CargoBike {
bikeEvents: [BikeEvent] bikeEvents: [BikeEvent]
equipment(offset: Int!, limit: Int!): [Equipment] equipment(offset: Int!, limit: Int!): [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"
miscellaneousEquipment: [String] equipmentType: [EquipmentType]
"Sticker State" "Sticker State"
stickerBikeNameState: StickerBikeNameState stickerBikeNameState: StickerBikeNameState
note: String note: String
@ -77,8 +77,7 @@ input CargoBikeCreateInput {
""" """
dimensionsAndLoad: DimensionsAndLoadCreateInput! dimensionsAndLoad: DimensionsAndLoadCreateInput!
"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"
miscellaneousEquipment: [String] equipmentTypeIds: [ID]
"Sticker State" "Sticker State"
stickerBikeNameState: StickerBikeNameState stickerBikeNameState: StickerBikeNameState
note: String note: String
@ -113,8 +112,11 @@ input CargoBikeUpdateInput {
Does not refer to an extra table in the database. Does not refer to an extra table in the database.
""" """
dimensionsAndLoad: DimensionsAndLoadUpdateInput dimensionsAndLoad: DimensionsAndLoadUpdateInput
"Refers to equipment that is not unique. See kommentierte info tabelle -> Fragen -> Frage 2" """
miscellaneousEquipment: [String] Refers to equipment that is not unique. See kommentierte info tabelle -> Fragen -> Frage 2
If set, ols realtions will be over written. Set [] to delete all
"""
equipmentTypeIds: [ID]
"Sticker State" "Sticker State"
stickerBikeNameState: StickerBikeNameState stickerBikeNameState: StickerBikeNameState
note: String note: String
@ -353,6 +355,23 @@ input EquipmentUpdateInput {
keepLock: Boolean keepLock: Boolean
} }
type EquipmentType {
id: ID!
name: String!
description: String!
}
input EquipmentTypeCreateInput {
name: String
description: String
}
input EquipmentTypeUpdateInput {
id: ID!
name: String
description: String
}
"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!
@ -746,6 +765,7 @@ type Mutation {
unlockEquipment(id: ID!): Boolean! unlockEquipment(id: ID!): Boolean!
"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!
"creates new lendingStation and returns lendingStation with new ID" "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"

Loading…
Cancel
Save