commit
fcb983be58
@ -0,0 +1,181 @@
|
||||
import { DataSource } from 'apollo-datasource';
|
||||
import { GraphQLError } from 'graphql';
|
||||
import { Connection, getConnection } from 'typeorm';
|
||||
import { CargoBike } from '../../model/CargoBike';
|
||||
import { ContactInformation } from '../../model/ContactInformation';
|
||||
import { Engagement } from '../../model/Engagement';
|
||||
import { Participant } from '../../model/Participant';
|
||||
|
||||
export class ParticipantAPI extends DataSource {
|
||||
connection : Connection
|
||||
constructor () {
|
||||
super();
|
||||
this.connection = getConnection();
|
||||
}
|
||||
|
||||
async getParticipantById (id: number) {
|
||||
return await this.connection.getRepository(Participant)
|
||||
.createQueryBuilder('participant')
|
||||
.select()
|
||||
.where('participant.id = :id', { id: id })
|
||||
.getOne();
|
||||
}
|
||||
|
||||
async getParticipants (offset: number, limit: number) {
|
||||
return await this.connection.getRepository(Participant)
|
||||
.createQueryBuilder('participant')
|
||||
.select()
|
||||
.offset(offset)
|
||||
.limit(limit)
|
||||
.getMany();
|
||||
}
|
||||
|
||||
async participantByEngagementId (id: number) {
|
||||
return (await this.connection.getRepository(Engagement)
|
||||
.createQueryBuilder('engagement')
|
||||
.leftJoinAndSelect('engagement.participant', 'participant')
|
||||
.where('engagement.id = :id', { id: id })
|
||||
.andWhere('engagement."participantId" = participant.id')
|
||||
.getOne()).participant;
|
||||
}
|
||||
|
||||
async participantByCargoBikeId (id:number) {
|
||||
return await this.connection.getRepository(Participant)
|
||||
.createQueryBuilder('participant')
|
||||
.leftJoinAndSelect('participant.cargoBike', 'cargoBike')
|
||||
.where('"cargoBike".id = :id', { id: id })
|
||||
.andWhere('"cargoBike"."participantId" = participant.id')
|
||||
.getOne();
|
||||
}
|
||||
|
||||
async engagementByParticipantId (id: number) {
|
||||
return await this.connection.getRepository(Engagement)
|
||||
.createQueryBuilder('engagement')
|
||||
.select()
|
||||
.where('engagement."participantId" = :id', { id: id })
|
||||
.getOne();
|
||||
}
|
||||
|
||||
async engagementByCargoBikeId (offset: number, limit: number, id: number) {
|
||||
return await this.connection.getRepository(Engagement)
|
||||
.createQueryBuilder('engagement')
|
||||
.select()
|
||||
.where('engagement."cargoBikeId" = :id', {
|
||||
id: id
|
||||
})
|
||||
.offset(offset)
|
||||
.limit(limit)
|
||||
.orderBy('engagement.from', 'DESC')
|
||||
.addOrderBy('engagement.to', 'DESC', 'NULLS FIRST')
|
||||
.getMany();
|
||||
}
|
||||
|
||||
async engagementById (id: number) {
|
||||
return await this.connection.getRepository(Engagement)
|
||||
.createQueryBuilder('engagement')
|
||||
.select()
|
||||
.where('engagement.id = :id', { id: id })
|
||||
.getOne();
|
||||
}
|
||||
|
||||
async contactInformationById (id: number) {
|
||||
return await this.connection.getRepository(ContactInformation)
|
||||
.createQueryBuilder('contactInformation')
|
||||
.select()
|
||||
.where('contactInformation.id = :id', { id: id })
|
||||
.getOne();
|
||||
}
|
||||
|
||||
async contactInformationByParticipantId (id: number) {
|
||||
const ret = (await this.connection.getRepository(Participant)
|
||||
.createQueryBuilder('participant')
|
||||
.leftJoinAndSelect('participant.contactInformation', 'contactInformation')
|
||||
.where('participant."contactInformationId" = "contactInformation".id')
|
||||
.andWhere('participant.id = :id', { id: id })
|
||||
.printSql()
|
||||
.getOne());
|
||||
return (ret) ? ret.contactInformation : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* creates participant and creates realtion to given contactInformation
|
||||
* @param participant to be created
|
||||
*/
|
||||
async createParticipant (participant: any) {
|
||||
let count = this.connection.getRepository(ContactInformation)
|
||||
.createQueryBuilder('contactInformation')
|
||||
.select()
|
||||
.where('contactInformation.id = :id', { id: participant.contactInformationId })
|
||||
.getCount();
|
||||
if ((await count) !== 1) {
|
||||
return new GraphQLError('contactInformationId not found.');
|
||||
}
|
||||
count = this.connection.getRepository(Participant)
|
||||
.createQueryBuilder('participant')
|
||||
.select()
|
||||
.where('participant."contactInformationId" = :id', {
|
||||
id: participant.contactInformationId
|
||||
})
|
||||
.getCount();
|
||||
if ((await count) !== 0) {
|
||||
return new GraphQLError('contactInformationId already used by other participant.');
|
||||
}
|
||||
const inserts = await this.connection.getRepository(Participant)
|
||||
.createQueryBuilder('participant')
|
||||
.insert()
|
||||
.into(Participant)
|
||||
.values([participant])
|
||||
.returning('*')
|
||||
.execute();
|
||||
await this.connection.getRepository(Participant)
|
||||
.createQueryBuilder('participant')
|
||||
.relation(Participant, 'contactInformation')
|
||||
.of(inserts.identifiers[0].id)
|
||||
.set(participant.contactInformationId);
|
||||
return this.getParticipantById(inserts.identifiers[0].id);
|
||||
}
|
||||
|
||||
async createContactInformation (contactInformation: any) {
|
||||
const inserts = await this.connection.getRepository(ContactInformation)
|
||||
.createQueryBuilder('contactInformation')
|
||||
.insert()
|
||||
.into(ContactInformation)
|
||||
.values([contactInformation])
|
||||
.returning('*')
|
||||
.execute();
|
||||
return this.contactInformationById(inserts.identifiers[0].id);
|
||||
}
|
||||
|
||||
async createEngagement (engagement: any) {
|
||||
const countB = this.connection.getRepository(CargoBike)
|
||||
.createQueryBuilder('cargoBike')
|
||||
.select()
|
||||
.where('cargoBike.id = :id', { id: engagement.cargoBikeId })
|
||||
.getCount();
|
||||
const countP = this.connection.getRepository(Participant)
|
||||
.createQueryBuilder('participant')
|
||||
.select()
|
||||
.where('participant.id = :id', { id: engagement.participantId })
|
||||
.getCount();
|
||||
if ((await countB) !== 1) { return new GraphQLError('BikeId not found'); }
|
||||
if ((await countP) !== 1) { return new GraphQLError('ParticipantId not found'); }
|
||||
// TODO check whether someone is already engaged with the bike
|
||||
const inserts = await this.connection.getRepository(Engagement)
|
||||
.createQueryBuilder('engagement')
|
||||
.insert()
|
||||
.values([engagement])
|
||||
.returning('*')
|
||||
.execute();
|
||||
this.connection.getRepository(Engagement)
|
||||
.createQueryBuilder('engagement')
|
||||
.relation(Engagement, 'cargoBike')
|
||||
.of(inserts.identifiers[0].id)
|
||||
.set(engagement.cargoBikeId);
|
||||
this.connection.getRepository(Engagement)
|
||||
.createQueryBuilder('engagement')
|
||||
.relation(Engagement, 'participant')
|
||||
.of(inserts.identifiers[0].id)
|
||||
.set(engagement.participantId);
|
||||
return this.engagementById(inserts.identifiers[0].id);
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
import { Entity, Column, PrimaryGeneratedColumn, ManyToOne } from 'typeorm';
|
||||
import { CargoBike } from './CargoBike';
|
||||
|
||||
@Entity()
|
||||
export class ChainSwap {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
mechanic: string;
|
||||
|
||||
@Column({
|
||||
type: 'date'
|
||||
})
|
||||
time: Date;
|
||||
|
||||
@Column()
|
||||
kexNoOldAXAChain: string;
|
||||
|
||||
@ManyToOne(type => CargoBike, cargoBike => cargoBike.chainSwaps)
|
||||
cargoBike: CargoBike;
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
import { Column, Entity, ManyToMany, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
|
||||
import { ContactInformation } from './ContactInformation';
|
||||
import { LendingStation } from './LendingStation';
|
||||
import { Provider } from './Provider';
|
||||
|
||||
@Entity()
|
||||
export class ContactPerson {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@ManyToOne(type => ContactInformation)
|
||||
contactInformation: ContactInformation;
|
||||
|
||||
@ManyToMany(type => LendingStation, lendingStation => lendingStation.contactPersons, {
|
||||
nullable: true
|
||||
})
|
||||
lendingStation: LendingStation;
|
||||
|
||||
@ManyToMany(type => Provider, provider => provider.contactPersons, {
|
||||
nullable: true
|
||||
})
|
||||
provider: Provider[];
|
||||
|
||||
@Column({
|
||||
type: 'boolean'
|
||||
})
|
||||
intern: boolean;
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
import { GraphQLError } from 'graphql';
|
||||
import { Permission } from '../datasources/userserver/permission';
|
||||
|
||||
export default {
|
||||
Query: {
|
||||
participantById: (_: any, { id }: { id: any }, { dataSources, req }: { dataSources: any, req: any }) => {
|
||||
if (req.permissions.includes(Permission.ReadBike)) {
|
||||
return dataSources.participantAPI.getParticipantById(id);
|
||||
} else {
|
||||
return new GraphQLError('Insufficient Permissions');
|
||||
}
|
||||
},
|
||||
participants: (_: any, { offset, limit }: { offset: number, limit: number }, { dataSources, req }: { dataSources: any, req: any }) => {
|
||||
if (req.permissions.includes(Permission.ReadBike)) {
|
||||
return dataSources.participantAPI.getParticipants(offset, limit);
|
||||
} else {
|
||||
return new GraphQLError('Insufficient Permissions');
|
||||
}
|
||||
}
|
||||
},
|
||||
Participant: {
|
||||
engagement (parent: any, _: any, { dataSources, req }: { dataSources: any, req: any }) {
|
||||
return dataSources.participantAPI.engagementByParticipantId(parent.id);
|
||||
},
|
||||
contactInformation (parent: any, _: any, { dataSources, req }: { dataSources: any, req: any }) {
|
||||
return (dataSources.participantAPI.contactInformationByParticipantId(parent.id));
|
||||
}
|
||||
},
|
||||
Engagement: {
|
||||
cargoBike (parent: any, _: any, { dataSources, req }: { dataSources: any, req: any }) {
|
||||
return dataSources.cargoBikeAPI.findCargoBikeByEngagementId(parent.id);
|
||||
},
|
||||
participant (parent: any, _: any, { dataSources, req }: { dataSources: any, req: any }) {
|
||||
return dataSources.participantAPI.participantByEngagementId(parent.id);
|
||||
}
|
||||
},
|
||||
Mutation: {
|
||||
createParticipant: (_: any, { participant }: { participant: any }, { dataSources, req }: { dataSources: any, req: any }) => {
|
||||
if (req.permissions.includes(Permission.WriteBike)) {
|
||||
return dataSources.participantAPI.createParticipant(participant);
|
||||
} else {
|
||||
return new GraphQLError('Insufficient Permissions');
|
||||
}
|
||||
},
|
||||
createContactInformation: (_: any, { contactInformation }: { contactInformation: any }, { dataSources, req }: { dataSources: any, req: any }) => {
|
||||
if (req.permissions.includes(Permission.WriteBike)) {
|
||||
return dataSources.participantAPI.createContactInformation(contactInformation);
|
||||
} else {
|
||||
return new GraphQLError('Insufficient Permissions');
|
||||
}
|
||||
},
|
||||
createEngagement: (_: any, { engagement }: { engagement: any }, { dataSources, req }: { dataSources: any, req: any }) => {
|
||||
if (req.permissions.includes(Permission.WriteBike)) {
|
||||
return dataSources.participantAPI.createEngagement(engagement);
|
||||
} else {
|
||||
return new GraphQLError('Insufficient Permissions');
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue