Engagement, EngagementType: read and create

pull/14/head
leonnicolas 4 years ago
parent 7b48573eb3
commit 45a7fa6ec7
No known key found for this signature in database
GPG Key ID: 088D0743E2B65C07

@ -43,12 +43,11 @@ export class CargoBikeAPI extends DataSource {
} }
async findCargoBikeByEngagementId (id: number) { async findCargoBikeByEngagementId (id: number) {
return (await this.connection.getRepository(Engagement) return await this.connection.getRepository(Engagement)
.createQueryBuilder('engagement') .createQueryBuilder('engagement')
.leftJoinAndSelect('engagement.cargoBike', 'cargoBike') .relation(Engagement, 'cargoBikeId')
.where('engagement."cargoBikeId" = "cargoBike".id') .of(id)
.andWhere('engagement.id = :id', { id: id }) .loadOne();
.getOne())?.cargoBike;
} }
async cargoBikesByProviderId (id: number) { async cargoBikesByProviderId (id: number) {

@ -1,11 +1,11 @@
import { DataSource } from 'apollo-datasource'; import { DataSource } from 'apollo-datasource';
import { GraphQLError } from 'graphql';
import { Connection, EntityManager, getConnection } from 'typeorm'; import { Connection, EntityManager, getConnection } from 'typeorm';
import { CargoBike } from '../../model/CargoBike';
import { ContactInformation } from '../../model/ContactInformation'; import { ContactInformation } from '../../model/ContactInformation';
import { Engagement } from '../../model/Engagement'; import { Engagement } from '../../model/Engagement';
import { Participant } from '../../model/Participant'; import { Participant } from '../../model/Participant';
import { EngagementType } from '../../model/EngagementType'; import { EngagementType } from '../../model/EngagementType';
import { genDateRange } from './utils';
import { UserInputError } from 'apollo-server';
export class ParticipantAPI extends DataSource { export class ParticipantAPI extends DataSource {
connection : Connection connection : Connection
@ -32,12 +32,11 @@ export class ParticipantAPI extends DataSource {
} }
async participantByEngagementId (id: number) { async participantByEngagementId (id: number) {
return (await this.connection.getRepository(Engagement) return await this.connection.getRepository(Engagement)
.createQueryBuilder('engagement') .createQueryBuilder('engagement')
.leftJoinAndSelect('engagement.participant', 'participant') .relation(Engagement, 'participantId')
.where('engagement.id = :id', { id: id }) .of(id)
.andWhere('engagement."participantId" = participant.id') .loadOne();
.getOne()).participant;
} }
async participantByCargoBikeId (id:number) { async participantByCargoBikeId (id:number) {
@ -54,7 +53,7 @@ export class ParticipantAPI extends DataSource {
.createQueryBuilder('engagement') .createQueryBuilder('engagement')
.select() .select()
.where('engagement."participantId" = :id', { id: id }) .where('engagement."participantId" = :id', { id: id })
.getOne(); .getMany();
} }
async engagementByCargoBikeId (offset: number, limit: number, id: number) { async engagementByCargoBikeId (offset: number, limit: number, id: number) {
@ -64,10 +63,9 @@ export class ParticipantAPI extends DataSource {
.where('engagement."cargoBikeId" = :id', { .where('engagement."cargoBikeId" = :id', {
id: id id: id
}) })
.offset(offset) .skip(offset)
.limit(limit) .take(limit)
.orderBy('engagement.from', 'DESC') .orderBy('engagement."dateRange"', 'DESC')
.addOrderBy('engagement.to', 'DESC', 'NULLS FIRST')
.getMany(); .getMany();
} }
@ -82,7 +80,9 @@ export class ParticipantAPI extends DataSource {
async engagementTypeByEngagementId (id: number) { async engagementTypeByEngagementId (id: number) {
return await this.connection.getRepository(Engagement) return await this.connection.getRepository(Engagement)
.createQueryBuilder('engagement') .createQueryBuilder('engagement')
.relation(Engagement, 'engageMent'); .relation(Engagement, 'engagementTypeId')
.of(id)
.loadOne();
} }
async contactInformationById (id: number) { async contactInformationById (id: number) {
@ -106,24 +106,6 @@ export class ParticipantAPI extends DataSource {
* @param participant to be created * @param participant to be created
*/ */
async createParticipant (participant: any) { 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.');
} */
let inserts: any; let inserts: any;
await this.connection.transaction(async (entityManager: EntityManager) => { await this.connection.transaction(async (entityManager: EntityManager) => {
inserts = await entityManager.getRepository(Participant) inserts = await entityManager.getRepository(Participant)
@ -133,59 +115,32 @@ export class ParticipantAPI extends DataSource {
.values([participant]) .values([participant])
.returning('*') .returning('*')
.execute(); .execute();
/* await entityManager.getRepository(Participant)
.createQueryBuilder('participant')
.relation(Participant, 'contactInformation')
.of(inserts.identifiers[0].id)
.set(participant.contactInformationId);
*/
}); });
/* 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); return this.getParticipantById(inserts.identifiers[0].id);
} }
async createEngagement (engagement: any) { async createEngagement (engagement: any) {
const countB = this.connection.getRepository(CargoBike) let inserts: any;
.createQueryBuilder('cargoBike') genDateRange(engagement);
.select() await this.connection.transaction(async (entityManager: EntityManager) => {
.where('cargoBike.id = :id', { id: engagement.cargoBikeId }) // check for overlapping engagements
.getCount(); const overlapping = await entityManager.getRepository(Engagement)
const countP = this.connection.getRepository(Participant) .createQueryBuilder('e')
.createQueryBuilder('participant')
.select() .select()
.where('participant.id = :id', { id: engagement.participantId }) .where('e."cargoBikeId" = :id', { id: engagement.cargoBikeId })
.getCount(); .andWhere('e."dateRange" && :dr', { dr: engagement.dateRange })
if ((await countB) !== 1) { return new GraphQLError('BikeId not found'); } .andWhere('e."engagementTypeId" = :etId', { etId: engagement.engagementTypeId })
if ((await countP) !== 1) { return new GraphQLError('ParticipantId not found'); } .getMany();
// TODO check whether someone is already engaged with the bike if (overlapping.length > 0) {
const inserts = await this.connection.getRepository(Engagement) throw new UserInputError('Engagements with ids: ' + overlapping.map((e) => { return e.id + ', '; }) + 'are overlapping');
}
inserts = await entityManager.getRepository(Engagement)
.createQueryBuilder('engagement') .createQueryBuilder('engagement')
.insert() .insert()
.values([engagement]) .values([engagement])
.returning('*') .returning('*')
.execute(); .execute();
await this.connection.getRepository(Engagement) });
.createQueryBuilder('engagement')
.relation(Engagement, 'cargoBike')
.of(inserts.identifiers[0].id)
.set(engagement.cargoBikeId);
await this.connection.getRepository(Engagement)
.createQueryBuilder('engagement')
.relation(Engagement, 'participant')
.of(inserts.identifiers[0].id)
.set(engagement.participantId);
return this.engagementById(inserts.identifiers[0].id); return this.engagementById(inserts.identifiers[0].id);
} }

@ -1,6 +1,19 @@
import { Connection, ObjectType } from 'typeorm'; import { Connection, ObjectType } from 'typeorm';
import { CargoBike, Lockable } from '../../model/CargoBike'; import { CargoBike, Lockable } from '../../model/CargoBike';
export function genDateRange (struct: any) {
if (struct.to === undefined) {
struct.to = '';
}
if (struct.to === undefined) {
struct.to = '';
}
struct.dateRange = '[' + struct.from + ',' + struct.to + ')';
if (struct.from === undefined) {
delete struct.dateRange;
}
}
export class LockUtils { export class LockUtils {
static getToken (req: any) : string { static getToken (req: any) : string {
return req.headers.authorization?.replace('Bearer ', ''); return req.headers.authorization?.replace('Bearer ', '');

@ -201,7 +201,7 @@ export class CargoBike implements Lockable {
}) })
timeFrames: TimeFrame[]; timeFrames: TimeFrame[];
@OneToMany(type => Engagement, engagement => engagement.cargoBike) @OneToMany(type => Engagement, engagement => engagement.cargoBikeId)
engagement: Engagement[]; engagement: Engagement[];
@Column(type => Taxes) @Column(type => Taxes)

@ -1,4 +1,4 @@
import { Entity, PrimaryGeneratedColumn, ManyToOne, Column, JoinColumn } from 'typeorm'; import { Entity, PrimaryGeneratedColumn, ManyToOne, Column, JoinColumn, JoinTable } from 'typeorm';
import { Participant } from './Participant'; import { Participant } from './Participant';
import { CargoBike } from './CargoBike'; import { CargoBike } from './CargoBike';
import { EngagementType } from './EngagementType'; import { EngagementType } from './EngagementType';
@ -9,33 +9,57 @@ export class Engagement {
id: number; id: number;
@ManyToOne(type => Participant, participant => participant.engagement, { @ManyToOne(type => Participant, participant => participant.engagement, {
nullable: false
}) })
@JoinColumn({ @JoinColumn({
name: 'participantId' name: 'participantId'
}) })
participant: Participant; participantId: number;
@ManyToOne(type => CargoBike, cargoBike => cargoBike.engagement) @ManyToOne(type => CargoBike, cargoBike => cargoBike.engagement, {
cargoBike: CargoBike; nullable: false
})
@JoinColumn({
name: 'cargoBikeId'
})
cargoBikeId: number;
@ManyToOne(type => EngagementType, engagementType => engagementType.engagementIds) @ManyToOne(type => EngagementType, engagementType => engagementType.engagementIds, {
nullable: false
})
@JoinColumn({
name: 'engagementTypeId'
})
engagementTypeId: number; engagementTypeId: number;
// I have to find out how typorm will map the datetange data type. // I have to find out how typorm will map the datetange data type.
@Column({ @Column({
type: 'daterange' type: 'daterange',
default: () => 'daterange(CURRENT_DATE,\'infinity\',\'[)\')'
}) })
dateRange: Date[]; dateRange: Date[];
@Column() @Column({
type: 'boolean',
default: false
})
roleCoordinator: boolean; roleCoordinator: boolean;
@Column() @Column({
type: 'boolean',
default: false
})
roleMentor: boolean; roleMentor: boolean;
@Column() @Column({
type: 'boolean',
default: false
})
roleAmbulance: boolean; roleAmbulance: boolean;
@Column() @Column({
type: 'boolean',
default: false
})
roleBringer: boolean; roleBringer: boolean;
} }

@ -1,6 +1,5 @@
import { Entity, PrimaryGeneratedColumn, Column, OneToOne, JoinColumn, OneToMany, ManyToMany } from 'typeorm'; import { Entity, PrimaryGeneratedColumn, Column, OneToOne, JoinColumn, OneToMany, ManyToMany } from 'typeorm';
import { ContactInformation } from './ContactInformation'; import { ContactInformation } from './ContactInformation';
import { CargoBike } from './CargoBike';
import { Engagement } from './Engagement'; import { Engagement } from './Engagement';
import { Workshop } from './Workshop'; import { Workshop } from './Workshop';
@ -44,7 +43,7 @@ export class Participant {
}) })
locationZIPs: string[]; locationZIPs: string[];
@OneToMany(type => Engagement, engagement => engagement.participant) @OneToMany(type => Engagement, engagement => engagement.participantId)
engagement: Engagement[]; engagement: Engagement[];
@ManyToMany(type => Workshop, workshop => workshop.participants, { @ManyToMany(type => Workshop, workshop => workshop.participants, {

@ -37,13 +37,12 @@ export default {
engagementType (parent: any, _: any, { dataSources, req }: { dataSources: any; req: any }): Promise<EngagementType> { engagementType (parent: any, _: any, { dataSources, req }: { dataSources: any; req: any }): Promise<EngagementType> {
return dataSources.participantAPI.engagementTypeByEngagementId(parent.id); return dataSources.participantAPI.engagementTypeByEngagementId(parent.id);
}, },
from (parent: any) { from (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) {
// TODO return (parent.dateRange as string).split(',')[0].replace('[', '');
return parent.dateRange;
}, },
to (parent: any) { to (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) {
// TODO const str = (parent.dateRange as string).split(',')[1].replace(')', '');
return parent.dateRange; return (str.length > 0) ? str : null;
} }
}, },
Mutation: { Mutation: {

@ -258,8 +258,8 @@ type Engagement {
engagementType: EngagementType! engagementType: EngagementType!
from: Date! from: Date!
to: Date to: Date
participant: Participant participant: Participant!
cargoBike: CargoBike cargoBike: CargoBike!
roleCoordinator: Boolean! roleCoordinator: Boolean!
roleEmployeeADFC: Boolean! roleEmployeeADFC: Boolean!
""" """
@ -278,14 +278,19 @@ input EngagementCreateInput {
to: Date to: Date
participantId: ID! participantId: ID!
cargoBikeId: ID! cargoBikeId: ID!
roleCoordinator: Boolean! "default: false"
roleEmployeeADFC: Boolean! roleCoordinator: Boolean
"default: false"
roleEmployeeADFC: Boolean
""" """
Wahr, wenn die Person Pate ist. Wahr, wenn die Person Pate ist.
default: false
""" """
roleMentor: Boolean! roleMentor: Boolean
roleAmbulance: Boolean! "default: false"
roleBringer: Boolean! roleAmbulance: Boolean
"default: false"
roleBringer: Boolean
} }
type Taxes { type Taxes {

Loading…
Cancel
Save