From e92d379a530d740615a1a2a210871c47e5c99294 Mon Sep 17 00:00:00 2001 From: leonnicolas Date: Sun, 27 Sep 2020 02:44:45 +0200 Subject: [PATCH] src/model/*: applied changes from flotte --- src/datasources/db/contactinformationAPI.ts | 16 +++---- src/datasources/db/utils.ts | 26 +++++++++++- src/index.ts | 12 +++++- src/model/BikeEvent.ts | 46 ++++++++------------- src/model/BikeEventType.ts | 22 ++++++++++ src/model/CargoBike.ts | 11 +++-- src/model/ContactInformation.ts | 42 +++++++++++-------- src/model/ContactPerson.ts | 28 ------------- src/model/Engagement.ts | 7 ++-- src/model/EngagementType.ts | 32 ++++++++++++++ src/model/EquipmentType.ts | 31 ++++++++++++++ src/model/LendingStation.ts | 12 +++--- src/model/Participant.ts | 27 +++++------- src/model/Person.ts | 28 +++++++++++++ src/model/Provider.ts | 46 ++++++++++++++------- src/model/Workshop.ts | 31 ++++++++++++-- src/model/WorkshopType.ts | 26 ++++++++++++ 17 files changed, 308 insertions(+), 135 deletions(-) create mode 100644 src/model/BikeEventType.ts delete mode 100644 src/model/ContactPerson.ts create mode 100644 src/model/EngagementType.ts create mode 100644 src/model/EquipmentType.ts create mode 100644 src/model/Person.ts create mode 100644 src/model/WorkshopType.ts diff --git a/src/datasources/db/contactinformationAPI.ts b/src/datasources/db/contactinformationAPI.ts index 2745e72..0541e4b 100644 --- a/src/datasources/db/contactinformationAPI.ts +++ b/src/datasources/db/contactinformationAPI.ts @@ -1,8 +1,6 @@ import { DataSource } from 'apollo-datasource'; -import { GraphQLError } from 'graphql'; import { Connection, getConnection } from 'typeorm'; import { ContactInformation } from '../../model/ContactInformation'; -import { ContactPerson } from '../../model/ContactPerson'; import { LendingStation } from '../../model/LendingStation'; export class ContactInformationAPI extends DataSource { @@ -13,11 +11,6 @@ export class ContactInformationAPI extends DataSource { } async contactPersonById (id: number) { - return await this.connection.getRepository(ContactPerson) - .createQueryBuilder('contactPerson') - .select() - .where('"contactPerson".id = :id', { id: id }) - .getOne(); } async numContactInformationById (id: number) { @@ -45,6 +38,7 @@ export class ContactInformationAPI extends DataSource { .getOne(); } + // TODO change to contactinformation async contactPersonsByLendingStationId (id: number) { return await this.connection .createQueryBuilder() @@ -54,14 +48,16 @@ export class ContactInformationAPI extends DataSource { } async contactInformationByContactPersonId (id: number) { - return (await this.connection.getRepository(ContactPerson) + /* return (await this.connection.getRepository(ContactPerson) .createQueryBuilder('contactPerson') .leftJoinAndSelect('contactPerson.contactInformation', 'contactInformation') .where('"contactPerson".id = :id', { id: id }) .getOne())?.contactInformation || new GraphQLError('ContactPerson has no ContactInformtion'); + */ } async createContactPerson (contactPerson: any) { + /* if (await this.contactInformationById(contactPerson.contactInformationId)) { let inserts: any; try { @@ -83,9 +79,11 @@ export class ContactInformationAPI extends DataSource { } else { return null; } + */ } async updateContactPerson (contactPerson: any) { + /* if (await this.contactPersonById(contactPerson.id)) { const contactInformationId = contactPerson.contactInformationId; delete contactPerson.contactInformationId; @@ -120,5 +118,7 @@ export class ContactInformationAPI extends DataSource { // updated bike not found return null; } + + */ } } diff --git a/src/datasources/db/utils.ts b/src/datasources/db/utils.ts index 688f8b1..b409165 100644 --- a/src/datasources/db/utils.ts +++ b/src/datasources/db/utils.ts @@ -7,7 +7,13 @@ export class LockUtils { } /** - * locks any entity that implements Lockable + * Locks any Entity, that + * @param connection + * @param target + * @param alias + * @param id + * @param req + * @param dataSources */ static async lockEntity (connection: Connection, target: ObjectType, alias: string, id: number, req: any, dataSources: any) { const token = this.getToken(req); @@ -42,6 +48,15 @@ export class LockUtils { } } + /** + * Unlocks any entity that implements Lockable. + * @param connection + * @param target + * @param alias + * @param id + * @param req + * @param dataSources + */ static async unlockEntity (connection: Connection, target: ObjectType, alias: string, id: number, req: any, dataSources: any) { const token = this.getToken(req); const userId = await dataSources.userAPI.getUserId(token); @@ -78,6 +93,15 @@ export class LockUtils { } } + /** + * Returns true if Entity is locked by another user. + * @param connection + * @param target + * @param alias + * @param id + * @param req + * @param dataSources + */ static async isLocked (connection: Connection, target: ObjectType, alias: string, id: number, req: any, dataSources: any) { const token = this.getToken(req); const userId = await dataSources.userAPI.getUserId(token); diff --git a/src/index.ts b/src/index.ts index d191039..b2084b6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,11 +23,15 @@ import { LendingStationAPI } from './datasources/db/lendingstationAPI'; import lendingstationResolvers from './resolvers/lendingstationResolvers'; import { ParticipantAPI } from './datasources/db/participantAPI'; import participantResolvers from './resolvers/participantResolvers'; -import { ContactPerson } from './model/ContactPerson'; import { ContactInformationAPI } from './datasources/db/contactinformationAPI'; import providerResolvers from './resolvers/providerResolvers'; import { ProviderAPI } from './datasources/db/providerAPI'; import contactinformationResolvers from './resolvers/contactinformationResolvers'; +import { Person } from './model/Person'; +import { WorkshopType } from './model/WorkshopType'; +import { EngagementType } from './model/EngagementType'; +import { EquipmentType } from './model/EquipmentType'; +import { BikeEventType } from './model/BikeEventType'; require('dotenv').config(); @@ -64,17 +68,21 @@ createConnection({ entities: [ CargoBike, BikeEvent, + BikeEventType, BikeModel, ContactInformation, Equipment, + EquipmentType, LendingStation, TimeFrame, Organisation, Participant, Provider, Engagement, + EngagementType, Workshop, - ContactPerson + Person, + WorkshopType ], synchronize: true, logging: false diff --git a/src/model/BikeEvent.ts b/src/model/BikeEvent.ts index 481e17d..091b66a 100644 --- a/src/model/BikeEvent.ts +++ b/src/model/BikeEvent.ts @@ -1,54 +1,47 @@ /* eslint no-unused-vars: "off" */ import { Entity, Column, PrimaryGeneratedColumn, ManyToOne, JoinColumn, TreeLevelColumn } from 'typeorm'; import { CargoBike } from './CargoBike'; - -export enum BikeEventType { - KAUF = 'KAUF', - INBETRIEBNAHME = 'INBETRIEBNAHME', - AUSFALL = 'AUSFALL', - WARTUNG = 'WARTUNG', - KETTENWECHSEL = 'KETTENWECHSEL', - ANDERE = 'ANDERE' -} +import { BikeEventType } from './BikeEventType'; +import { Participant } from './Participant'; +import { type } from 'os'; @Entity() export class BikeEvent { - public setValues ({ id, remark, date, documents, cargoBike, eventType }: { id: number, remark: string, date: Date, documents: string[], cargoBike: CargoBike, eventType: BikeEventType}): void { + 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; - this.eventType = eventType; } @PrimaryGeneratedColumn() id: number; @Column({ - nullable: true + type: 'text', + nullable: false, + default: '' }) - name: string; + description: string; @Column({ - nullable: true + nullable: false, + default: '' }) remark: string; @Column({ - type: 'date' + type: 'date', + default: () => 'CURRENT_DATE' }) date: Date; - @Column({ - nullable: true - }) - mechanic: string; + @ManyToOne(type => Participant) + responsible: Participant; - @Column({ - nullable: true - }) - kexNoOldAXAChain: string; + @ManyToOne(type => Participant) + related: Participant; @Column('simple-array', { nullable: true @@ -61,9 +54,6 @@ export class BikeEvent { @JoinColumn({ name: 'cargoBikeId' }) cargoBike: CargoBike; - @Column({ - type: 'enum', - enum: BikeEventType - }) - eventType: BikeEventType + @ManyToOne(type => BikeEventType) + bikeEventType: BikeEventType; } diff --git a/src/model/BikeEventType.ts b/src/model/BikeEventType.ts new file mode 100644 index 0000000..ada338a --- /dev/null +++ b/src/model/BikeEventType.ts @@ -0,0 +1,22 @@ +import { Lockable } from './CargoBike'; +import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; + +@Entity() +export class BikeEventType implements Lockable { + @PrimaryGeneratedColumn() + id: number; + + @Column() + name: string; + + @Column({ + nullable: true, + type: 'timestamp' + }) + lockedUntil: Date; + + @Column({ + nullable: true + }) + lockedBy: number; +} diff --git a/src/model/CargoBike.ts b/src/model/CargoBike.ts index fed636c..35ab3f3 100644 --- a/src/model/CargoBike.ts +++ b/src/model/CargoBike.ts @@ -1,5 +1,5 @@ /* eslint no-unused-vars: "off" */ -import { Entity, Column, PrimaryGeneratedColumn, OneToMany, ManyToOne, JoinColumn } from 'typeorm'; +import { Entity, Column, PrimaryGeneratedColumn, OneToMany, ManyToOne, JoinColumn, ManyToMany } from 'typeorm'; import { Bike } from './BikeFeatures'; import { Provider } from './Provider'; import { Participant } from './Participant'; @@ -9,6 +9,7 @@ import { Taxes } from './Taxes'; import { Equipment } from './Equipment'; import { Engagement } from './Engagement'; import { BikeEvent } from './BikeEvent'; +import { EquipmentType } from './EquipmentType'; export enum Group { KL = 'KL', @@ -81,11 +82,9 @@ export class CargoBike extends Bike implements Lockable { }) equipment: Equipment[]; - @Column({ - type: 'simple-array', - nullable: true - }) - miscellaneousEquipment: string[]; + // 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) + miscellaneousEquipment: EquipmentType[]; // Security information @Column(type => Security) diff --git a/src/model/ContactInformation.ts b/src/model/ContactInformation.ts index 3595bdb..b87495e 100644 --- a/src/model/ContactInformation.ts +++ b/src/model/ContactInformation.ts @@ -1,54 +1,60 @@ -import { PrimaryGeneratedColumn, Column, Entity } from 'typeorm'; +import { PrimaryGeneratedColumn, Column, Entity, ManyToOne, OneToOne } from 'typeorm'; +import { Lockable } from './CargoBike'; +import { Person } from './Person'; +import { Address } from './Provider'; +import { Participant } from './Participant'; @Entity() -export class ContactInformation { +export class ContactInformation implements Lockable { @PrimaryGeneratedColumn() id: number; - @Column() - name: string; + @ManyToOne(type => Person, person => person.contactInformation) + person: Person; - @Column() - firstName: string; - - @Column({ - type: 'date', + @OneToOne(type => Participant, participant => participant.contactInformation, { nullable: true }) - retiredAt: Date; + participant: Participant; + + @Column(type => { + return Address; + }) + address: Address; @Column({ nullable: true }) - phoneExtern: string; + phone: string; @Column({ nullable: true }) - phone2Extern: string; + phone2: string; @Column({ nullable: true }) - phoneIntern: string; + email: string; @Column({ nullable: true }) - phone2Intern: string; + email2: string; @Column({ nullable: true }) - emailExtern: string; + note: string; @Column({ - nullable: true + nullable: true, + type: 'timestamp' }) - emailIntern: string; + lockedUntil: Date; @Column({ nullable: true }) - note: string; + lockedBy: number; } diff --git a/src/model/ContactPerson.ts b/src/model/ContactPerson.ts deleted file mode 100644 index 34476b0..0000000 --- a/src/model/ContactPerson.ts +++ /dev/null @@ -1,28 +0,0 @@ -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 - }) - lendingStations: LendingStation[]; - - @ManyToOne(type => Provider, provider => provider.contactPersons, { - nullable: true - }) - provider: Provider[]; - - @Column({ - type: 'boolean' - }) - intern: boolean; -} diff --git a/src/model/Engagement.ts b/src/model/Engagement.ts index 86d3f0a..1711bf1 100644 --- a/src/model/Engagement.ts +++ b/src/model/Engagement.ts @@ -1,6 +1,7 @@ import { Entity, PrimaryGeneratedColumn, ManyToOne, Column, JoinColumn } from 'typeorm'; import { Participant } from './Participant'; import { CargoBike } from './CargoBike'; +import { EngagementType } from './EngagementType'; @Entity() export class Engagement { @@ -17,6 +18,9 @@ export class Engagement { @ManyToOne(type => CargoBike, cargoBike => cargoBike.engagement) cargoBike: CargoBike; + @ManyToOne(type => EngagementType, engagementType => engagementType.engagementIds) + engagementTypeId: number; + @Column({ type: 'date' }) @@ -31,9 +35,6 @@ export class Engagement { @Column() roleCoordinator: boolean; - @Column() - roleEmployeeADFC: boolean; - @Column() roleMentor: boolean; diff --git a/src/model/EngagementType.ts b/src/model/EngagementType.ts new file mode 100644 index 0000000..9745258 --- /dev/null +++ b/src/model/EngagementType.ts @@ -0,0 +1,32 @@ +import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from 'typeorm'; +import { Lockable } from './CargoBike'; +import { Engagement } from './Engagement'; + +@Entity() +export class EngagementType implements Lockable { + @PrimaryGeneratedColumn() + id: number; + + @Column() + name: string; + + @Column({ + type: 'text', + nullable: true + }) + description: string; + + @OneToMany(type => Engagement, engagement => engagement.engagementTypeId) + engagementIds: number[]; + + @Column({ + nullable: true, + type: 'timestamp' + }) + lockedUntil: Date; + + @Column({ + nullable: true + }) + lockedBy: number; +} diff --git a/src/model/EquipmentType.ts b/src/model/EquipmentType.ts new file mode 100644 index 0000000..2ce82cc --- /dev/null +++ b/src/model/EquipmentType.ts @@ -0,0 +1,31 @@ +import { Column, Entity, ManyToMany, OneToMany, PrimaryGeneratedColumn } from 'typeorm'; +import { CargoBike, Lockable } from './CargoBike'; + +@Entity() +export class EquipmentType implements Lockable { + @PrimaryGeneratedColumn() + id: number; + + @Column() + name: string; + + @Column({ + type: 'text', + default: '' + }) + description: string; + + @ManyToMany(type => CargoBike, cargoBike => cargoBike.miscellaneousEquipment) + cargoBikes: CargoBike[]; + + @Column({ + nullable: true, + type: 'timestamp' + }) + lockedUntil: Date; + + @Column({ + nullable: true + }) + lockedBy: number; +} diff --git a/src/model/LendingStation.ts b/src/model/LendingStation.ts index 068e7ee..9c84b92 100644 --- a/src/model/LendingStation.ts +++ b/src/model/LendingStation.ts @@ -1,8 +1,8 @@ -import { Entity, PrimaryGeneratedColumn, Column, ManyToMany, JoinTable, OneToMany, ManyToOne } from 'typeorm'; +import { Entity, PrimaryGeneratedColumn, Column, OneToMany, ManyToOne } from 'typeorm'; import { TimeFrame } from './TimeFrame'; import { Organisation } from './Organisation'; import { Address } from './Provider'; -import { ContactPerson } from './ContactPerson'; +import { ContactInformation } from './ContactInformation'; @Entity() export class LendingStation { @@ -12,9 +12,11 @@ export class LendingStation { @Column() name: string; - @ManyToMany(type => ContactPerson) - @JoinTable() - contactPersons: ContactPerson[]; + @ManyToOne(type => ContactInformation) + contactInformationIntern: ContactInformation; + + @ManyToOne(type => ContactInformation) + contactInformationExtern: ContactInformation; @Column(type => Address) address: Address; diff --git a/src/model/Participant.ts b/src/model/Participant.ts index 9da9c57..3fbb94a 100644 --- a/src/model/Participant.ts +++ b/src/model/Participant.ts @@ -20,7 +20,7 @@ export class Participant { }) end: Date; - @OneToOne(type => ContactInformation, { + @OneToOne(type => ContactInformation, contactInformation => contactInformation.participant, { nullable: true }) @JoinColumn() @@ -36,14 +36,12 @@ export class Participant { }) usernameSlack: string; - @Column() - memberADFC: boolean; - @Column({ type: 'simple-array' }) locationZIPs: string[]; + // this should go, we dont need it @OneToMany(type => CargoBike, cargoBike => cargoBike.coordinator) cargoBikes: CargoBike[]; @@ -55,23 +53,18 @@ export class Participant { }) workshops: Workshop[]; - @Column() - memberCoreTeam: boolean; - @Column({ - type: 'date', - nullable: true + nullable: false, + default: false }) - workshopMentor: Date; + memberCoreTeam: boolean; @Column({ - type: 'date', - nullable: true + nullable: false, + default: false }) - workshopAmbulance: Date; + employeeADFC: boolean; - @Column({ - nullable: true - }) - reserve: string; + @Column() + memberADFC: boolean; } diff --git a/src/model/Person.ts b/src/model/Person.ts new file mode 100644 index 0000000..1540206 --- /dev/null +++ b/src/model/Person.ts @@ -0,0 +1,28 @@ +import { Lockable } from './CargoBike'; +import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from 'typeorm'; +import { ContactInformation } from './ContactInformation'; + +@Entity() +export class Person implements Lockable { + @PrimaryGeneratedColumn() + id: number; + + @Column() + firstName: string; + + @Column() + name: string; + + @OneToMany(type => ContactInformation, contactInformation => contactInformation.person) + contactInformation: ContactInformation; + + @Column({ + nullable: true + }) + lockedUntil: Date; + + @Column({ + nullable: true + }) + lockedBy: number; +} diff --git a/src/model/Provider.ts b/src/model/Provider.ts index 56e6b31..992e41b 100644 --- a/src/model/Provider.ts +++ b/src/model/Provider.ts @@ -1,30 +1,34 @@ /* eslint no-unused-vars: "off" */ -import { PrimaryGeneratedColumn, Column, OneToMany, Entity, OneToOne, ChildEntity } from 'typeorm'; -import { CargoBike } from './CargoBike'; +import { PrimaryGeneratedColumn, Column, OneToMany, Entity, OneToOne, ChildEntity, ManyToOne, JoinColumn } from 'typeorm'; +import { CargoBike, Lockable } from './CargoBike'; import { ContactInformation } from './ContactInformation'; -import { ContactPerson } from './ContactPerson'; -import { LendingStation } from './LendingStation'; import { Organisation } from './Organisation'; export class Address { - @Column() + @Column({ + default: '' + }) street: string; - @Column() + @Column({ + default: '' + }) number: string; - @Column() + @Column({ + default: '' + }) zip: string; @Column({ - nullable: true + default: '' }) city: string; } @Entity() -export class Provider { +export class Provider implements Lockable { @PrimaryGeneratedColumn() id: number; @@ -33,19 +37,31 @@ export class Provider { }) formName: String; - @OneToMany(type => ContactPerson, contactPerson => contactPerson.provider, { + // is null when Provider is an organisation + @OneToOne(type => ContactInformation, { nullable: true }) - contactPersons: ContactPerson[]; + @JoinColumn() + contactInformationId: number; + + // is null when Provider is a private Person + @OneToOne(type => Organisation, organization => organization.provider, { + nullable: true + }) + @JoinColumn() + organization: Organisation; @OneToMany(type => CargoBike, cargoBike => cargoBike.provider) cargoBikes: CargoBike[]; - @Column() - isPrivatePerson: boolean; + @Column({ + nullable: true, + type: 'timestamp' + }) + lockedUntil: Date; - @OneToOne(type => Organisation, organization => organization.provider, { + @Column({ nullable: true }) - organization: Organisation; + lockedBy: number; } diff --git a/src/model/Workshop.ts b/src/model/Workshop.ts index 5c7d81e..c5b0acc 100644 --- a/src/model/Workshop.ts +++ b/src/model/Workshop.ts @@ -1,13 +1,15 @@ -import { Entity, PrimaryGeneratedColumn, Column, ManyToMany } from 'typeorm'; +import { Entity, PrimaryGeneratedColumn, Column, ManyToMany, ManyToOne } from 'typeorm'; import { Participant } from './Participant'; +import { WorkshopType } from './WorkshopType'; +import { Lockable } from './CargoBike'; @Entity() -export class Workshop { +export class Workshop implements Lockable { @PrimaryGeneratedColumn() id: number; - @Column() - type: string; + @ManyToOne(type => WorkshopType, workshopType => workshopType.workshopIds) + workshopTypeId: number; @Column() title: string; @@ -24,4 +26,25 @@ export class Workshop { nullable: true }) participants: Participant[]; + + @ManyToOne(type => Participant, { + nullable: false + }) + trainer1Id: number; + + @ManyToOne(type => Participant, { + nullable: true + }) + trainer2: Participant; + + @Column({ + nullable: true, + type: 'timestamp' + }) + lockedUntil: Date; + + @Column({ + nullable: true + }) + lockedBy: number; } diff --git a/src/model/WorkshopType.ts b/src/model/WorkshopType.ts new file mode 100644 index 0000000..667952a --- /dev/null +++ b/src/model/WorkshopType.ts @@ -0,0 +1,26 @@ +import { Lockable } from './CargoBike'; +import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from 'typeorm'; +import { Workshop } from './Workshop'; + +@Entity() +export class WorkshopType implements Lockable { + @PrimaryGeneratedColumn() + id: number; + + @Column() + name: string; + + @OneToMany(type => Workshop, workshop => workshop.workshopTypeId) + workshopIds: number[]; + + @Column({ + nullable: true, + type: 'timestamp' + }) + lockedUntil: Date; + + @Column({ + nullable: true + }) + lockedBy: number; +}