resolvers: contactPerson for lendingStation

pull/14/head
leonnicolas 4 years ago
parent 847e78768f
commit 80438de4fc
No known key found for this signature in database
GPG Key ID: 088D0743E2B65C07

@ -163,6 +163,10 @@ export class CargoBikeAPI extends DataSource {
* @param param0 cargoBike to be updated * @param param0 cargoBike to be updated
*/ */
async updateCargoBike (cargoBike: any, req: any, dataSources: any) { async updateCargoBike (cargoBike: any, req: any, dataSources: any) {
// TODO let lock cargoBike can return error to save one sql query, this will be a complex sql query
if (!await this.checkId(CargoBike, 'cargobike', cargoBike.id)) {
return new GraphQLError('ID not found');
}
if (!await this.lockCargoBike(cargoBike.id, req, dataSources)) { if (!await this.lockCargoBike(cargoBike.id, req, dataSources)) {
return new GraphQLError('Bike locked by other user'); return new GraphQLError('Bike locked by other user');
} }
@ -244,6 +248,17 @@ export class CargoBikeAPI extends DataSource {
.getOne(); .getOne();
} }
async checkId (target: ObjectType<Lockable>, alias: string, id: number) {
const result = await this.connection.getRepository(target)
.createQueryBuilder(alias)
.select([
alias + '.id'
])
.where('id = :id', { id: id })
.getCount();
return result === 1;
}
// think this can go // think this can go
async findEquipmentJoinBikeById (id: number) { async findEquipmentJoinBikeById (id: number) {
return await this.connection.getRepository(Equipment) return await this.connection.getRepository(Equipment)
@ -289,7 +304,15 @@ export class CargoBikeAPI extends DataSource {
} }
async lockEquipment (id: number, req: any, dataSources: any) { async lockEquipment (id: number, req: any, dataSources: any) {
return this.lockEntity(Equipment, 'equipment', id, req, dataSources); if (await this.lockEntity(Equipment, 'equipment', id, req, dataSources)) {
return this.findEquipmentById(id);
} else {
return new GraphQLError('Equipment locked by other user');
}
}
async unlockEquipment (id: number, req: any, dataSources: any) {
return await this.unlockEntity(Equipment, 'equipment', id, req, dataSources);
} }
/** /**
@ -298,9 +321,15 @@ export class CargoBikeAPI extends DataSource {
* @param param0 struct with equipment properites * @param param0 struct with equipment properites
*/ */
async updateEquipment (equipment: any, req: any, dataSources: any) { async updateEquipment (equipment: any, req: any, dataSources: any) {
// TODO let lock cargoBike can return error to save one sql query, this will be a complex sql query
if (!await this.checkId(Equipment, 'alias', equipment.id)) {
return new GraphQLError('ID not found in DB');
}
if (!await this.lockEntity(Equipment, 'equipment', equipment.id, req, dataSources)) { if (!await this.lockEntity(Equipment, 'equipment', equipment.id, req, dataSources)) {
return new GraphQLError('Equipment locked by other user'); return new GraphQLError('Equipment locked by other user');
} }
const keepLock = equipment.keepLock;
delete equipment.keepLock;
const cargoBikeId = equipment.cargoBikeId; const cargoBikeId = equipment.cargoBikeId;
delete equipment.cargoBikeId; delete equipment.cargoBikeId;
await this.connection.getRepository(Equipment) await this.connection.getRepository(Equipment)
@ -316,7 +345,8 @@ export class CargoBikeAPI extends DataSource {
.relation(Equipment, 'cargoBike') .relation(Equipment, 'cargoBike')
.of(equipment.id) .of(equipment.id)
.set(cargoBikeId); .set(cargoBikeId);
return this.findEquipmentJoinBikeById(equipment.id); !keepLock && this.unlockCargoBike(equipment.id, req, dataSources);
return this.findEquipmentById(equipment.id);
} }
return this.findEquipmentById(equipment.id); return this.findEquipmentById(equipment.id);
} }

@ -1,7 +1,9 @@
import { DataSource } from 'apollo-datasource'; import { DataSource } from 'apollo-datasource';
import { GraphQLError } from 'graphql';
import { Connection, getConnection } from 'typeorm'; import { Connection, getConnection } from 'typeorm';
import { ContactInformation } from '../../model/ContactInformation'; import { ContactInformation } from '../../model/ContactInformation';
import { ContactPerson } from '../../model/ContactPerson'; import { ContactPerson } from '../../model/ContactPerson';
import { LendingStation } from '../../model/LendingStation';
export class ContactInformationAPI extends DataSource { export class ContactInformationAPI extends DataSource {
connection : Connection connection : Connection
@ -26,6 +28,15 @@ export class ContactInformationAPI extends DataSource {
.getCount(); .getCount();
} }
async contactInformation (offset: number, limit: number) {
return await this.connection.getRepository(ContactInformation)
.createQueryBuilder('contactinformation')
.select()
.offset(offset)
.limit(limit)
.getMany();
}
async contactInformationById (id: number) { async contactInformationById (id: number) {
return await this.connection.getRepository(ContactInformation) return await this.connection.getRepository(ContactInformation)
.createQueryBuilder('contactInformation') .createQueryBuilder('contactInformation')
@ -34,12 +45,12 @@ export class ContactInformationAPI extends DataSource {
.getOne(); .getOne();
} }
async contactPersonByLendingStationId (id: number) { async contactPersonsByLendingStationId (id: number) {
return await this.connection.getRepository(ContactPerson) return await this.connection
.createQueryBuilder('contactPerson') .createQueryBuilder()
.leftJoinAndSelect('contactPerson.lendingStation', 'lendingStation') .relation(LendingStation, 'contactPersons')
.where('"lendingStation".id = :id', { id: id }) .of(id)
.getMany().catch(() => { return []; }); .loadMany();
} }
async contactInformationByContactPersonId (id: number) { async contactInformationByContactPersonId (id: number) {
@ -47,22 +58,27 @@ export class ContactInformationAPI extends DataSource {
.createQueryBuilder('contactPerson') .createQueryBuilder('contactPerson')
.leftJoinAndSelect('contactPerson.contactInformation', 'contactInformation') .leftJoinAndSelect('contactPerson.contactInformation', 'contactInformation')
.where('"contactPerson".id = :id', { id: id }) .where('"contactPerson".id = :id', { id: id })
.getOne())?.contactInformation; .getOne())?.contactInformation || new GraphQLError('ContactPerson has no ContactInformtion');
} }
async createContactPerson (contactPerson: any) { async createContactPerson (contactPerson: any) {
if (await this.contactInformationById(contactPerson.contactInformationId)) { if (await this.contactInformationById(contactPerson.contactInformationId)) {
const inserts = await this.connection.getRepository(ContactPerson) let inserts: any;
.createQueryBuilder('contactPerson') try {
await this.connection.transaction(async entiyManager => {
inserts = await entiyManager.createQueryBuilder(ContactPerson, 'contactPerson')
.insert() .insert()
.values([contactPerson]) .values([contactPerson])
.returning('*') .returning('*')
.execute(); .execute();
await this.connection.getRepository(ContactPerson) await entiyManager.createQueryBuilder()
.createQueryBuilder('contactPerson')
.relation(ContactPerson, 'contactInformation') .relation(ContactPerson, 'contactInformation')
.of(inserts.identifiers[0].id) .of(inserts.identifiers[0].id)
.set(contactPerson.contactInformationId); .set(contactPerson.contactInformationId);
});
} catch (e: any) {
return new GraphQLError('Transaction could not be completed');
}
return this.contactPersonById(inserts.identifiers[0].id); return this.contactPersonById(inserts.identifiers[0].id);
} else { } else {
return null; return null;

@ -70,14 +70,24 @@ export class LendingStationAPI extends DataSource {
* creates new lendingStation and returns new lendingStation with its new id * creates new lendingStation and returns new lendingStation with its new id
* @param param0 new lendingStation * @param param0 new lendingStation
*/ */
async createLendingStation ({ lendingStation }:{ lendingStation: any }) { async createLendingStation (lendingStation: any) {
const inserts = await this.connection.manager let inserts: any;
.createQueryBuilder() try {
await this.connection.transaction(async entiyManager => {
inserts = await entiyManager.createQueryBuilder(LendingStation, 'lendingstation')
.insert() .insert()
.into(LendingStation)
.values([lendingStation]) .values([lendingStation])
.returning('*') .returning('*')
.execute(); .execute();
await entiyManager.getRepository(LendingStation)
.createQueryBuilder('lendingstation')
.relation(LendingStation, 'contactPersons')
.of(lendingStation.id)
.add(lendingStation?.contactPersonIds.map((e: any) => { return Number(e); }));
});
} catch (e :any) {
return new GraphQLError('Transaction could not be completed');
}
const newLendingStaion = inserts.generatedMaps[0]; const newLendingStaion = inserts.generatedMaps[0];
newLendingStaion.id = inserts.identifiers[0].id; newLendingStaion.id = inserts.identifiers[0].id;
return newLendingStaion; return newLendingStaion;

@ -1,17 +0,0 @@
/*
import {Sequelize} from 'sequelize-typescript';
export class DButils {
dbclient: any;
constructor(){
this.dbclient = new Sequelize({
database: 'apollo',
dialect: 'postgres',
username: 'postgres',
password: 'apollo',
storage: ':memory:',
models: [__dirname + '/models'], // or [Player, Team],
});
}
}
*/

@ -32,6 +32,7 @@ export enum StickerBikeNameState {
} }
export interface Lockable { export interface Lockable {
id: number,
lockedBy: number, lockedBy: number,
lockedUntil: Date lockedUntil: Date
} }

@ -14,7 +14,7 @@ export class ContactPerson {
@ManyToMany(type => LendingStation, lendingStation => lendingStation.contactPersons, { @ManyToMany(type => LendingStation, lendingStation => lendingStation.contactPersons, {
nullable: true nullable: true
}) })
lendingStation: LendingStation; lendingStations: LendingStation[];
@ManyToMany(type => Provider, provider => provider.contactPersons, { @ManyToMany(type => Provider, provider => provider.contactPersons, {
nullable: true nullable: true

@ -114,6 +114,13 @@ export default {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }
}, },
unlockEquipment: (_: any, { id }: { id: number }, { dataSources, req }: { dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.WriteBike)) {
return dataSources.cargoBikeAPI.unlockEquipment(id, req, dataSources);
} else {
return new GraphQLError('Insufficient Permissions');
}
},
updateEquipment: (_: any, { equipment }: { equipment: any }, { dataSources, req }: { dataSources: any, req: any }) => { updateEquipment: (_: any, { equipment }: { equipment: any }, { dataSources, req }: { dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.WriteBike)) { if (req.permissions.includes(Permission.WriteBike)) {
return dataSources.cargoBikeAPI.updateEquipment(equipment, req, dataSources); return dataSources.cargoBikeAPI.updateEquipment(equipment, req, dataSources);

@ -2,6 +2,15 @@ import { GraphQLError } from 'graphql';
import { Permission } from '../datasources/userserver/permission'; import { Permission } from '../datasources/userserver/permission';
export default { export default {
Query: {
contactInformation: (_: any, { offset, limit }: { offset: number, limit: number }, { dataSources, req }: { dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.WriteBike)) {
return dataSources.contactInformationAPI.contactInformation(offset, limit);
} else {
return new GraphQLError('Insufficient Permissions');
}
}
},
ContactPerson: { ContactPerson: {
contactInformation: (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) => { contactInformation: (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.ReadBike)) { if (req.permissions.includes(Permission.ReadBike)) {

@ -21,7 +21,7 @@ export default {
}, },
LendingStation: { LendingStation: {
contactPersons (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) { contactPersons (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) {
return dataSources.contactInformationAPI.contactPersonByLendingStationId(parent.id); return dataSources.contactInformationAPI.contactPersonsByLendingStationId(parent.id);
}, },
timeFrames (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) { timeFrames (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) {
return dataSources.lendingStationAPI.timeFramesByLendingStationId(parent.id); return dataSources.lendingStationAPI.timeFramesByLendingStationId(parent.id);
@ -36,7 +36,7 @@ export default {
Mutation: { Mutation: {
createLendingStation: (_: any, { lendingStation }:{ lendingStation: LendingStation }, { dataSources, req }:{dataSources: any, req: any }) => { createLendingStation: (_: any, { lendingStation }:{ lendingStation: LendingStation }, { dataSources, req }:{dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.WriteBike)) { if (req.permissions.includes(Permission.WriteBike)) {
return dataSources.lendingStationAPI.createLendingStation({ lendingStation }); return dataSources.lendingStationAPI.createLendingStation(lendingStation);
} else { } else {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }

@ -2,7 +2,10 @@ import { gql } from 'apollo-server';
export default gql` export default gql`
"timestamp object YYYY-MM-ddThh:mm:ss.sssZ"
scalar Date scalar Date
"only time hh-mm-ss"
scalar Time
"The CargoBike type is central to the graph. You could call it the root." "The CargoBike type is central to the graph. You could call it the root."
type CargoBike { type CargoBike {
@ -347,6 +350,8 @@ input EquipmentUpdateInput {
description: String description: String
investable: Boolean investable: Boolean
cargoBikeId: ID cargoBikeId: ID
"will keep Bike locked if set to true, default = false"
keepLock: Boolean
} }
"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."
@ -598,10 +603,10 @@ type LendingStation {
input LendingStationCreateInput { input LendingStationCreateInput {
name: String! name: String!
contactInformation: [ContactInformationCreateInput]! contactPersonIds: [ID]!
address: AddressCreateInput! address: AddressCreateInput!
loanPeriods: LoanPeriodsInput loanPeriods: LoanPeriodsInput
timeFrames: [TimeFrameCreateInput]! timeFrameIds: [ID]!
} }
input LendingStationUpdateInput { input LendingStationUpdateInput {
@ -705,7 +710,7 @@ type Query {
participants(offset: Int!, limit: Int!): [ Participant]! participants(offset: Int!, limit: Int!): [ Participant]!
lendingStationById(id:ID!): LendingStation lendingStationById(id:ID!): LendingStation
lendingStations(offset: Int!, limit: Int!): [LendingStation]! lendingStations(offset: Int!, limit: Int!): [LendingStation]!
contactInformation: [ContactInformation]! contactInformation(offset: Int!, limit: Int!): [ContactInformation]!
"returns BikeEvent with CargoBike" "returns BikeEvent with CargoBike"
bikeEventById(id:ID!): BikeEvent! bikeEventById(id:ID!): BikeEvent!
} }
@ -713,16 +718,18 @@ type Query {
type Mutation { type Mutation {
"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!
"lock cargoBike returns bike if bike is not locked or Error" "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!
"unlock cargoBike" "unlock cargoBike, returns true if Bike does not exist"
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" "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!): Boolean! lockEquipmentById(id: ID!): Equipment!
"unlock Equopment, returns true if Bike does not exist"
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!
"creates new lendingStation and returns lendingStation with new ID" "creates new lendingStation and returns lendingStation with new ID"

Loading…
Cancel
Save