diff --git a/src/datasources/db/cargobikeAPI.ts b/src/datasources/db/cargobikeAPI.ts index 13bb44c..6b3ed2d 100644 --- a/src/datasources/db/cargobikeAPI.ts +++ b/src/datasources/db/cargobikeAPI.ts @@ -128,6 +128,7 @@ export class CargoBikeAPI extends DataSource { .getOne(); } + // think this can go async findEquipmentJoinBikeById (id: number) { return await this.connection.getRepository(Equipment) .createQueryBuilder('equipment') @@ -136,6 +137,14 @@ export class CargoBikeAPI extends DataSource { .getOne(); } + async equipmentByCargoBikeId (offset: number, limit: number, id: number) { + return await this.connection.getRepository(Equipment) + .createQueryBuilder('equipment') + .select() + .where('equipment."cargoBikeId" = :id', { id: id }) + .getMany(); + } + async createEquipment ({ equipment }: { equipment: any }) { const inserts = await this.connection.getRepository(Equipment) .createQueryBuilder('equipment') @@ -150,29 +159,34 @@ export class CargoBikeAPI extends DataSource { .relation(Equipment, 'cargoBike') .of(equipment.id) .set(equipment.cargoBikeId); - return this.findEquipmentJoinBikeById(inserts.identifiers[0].id); + // return this.findEquipmentJoinBikeById(inserts.identifiers[0].id); } return this.findEquipmentById(inserts.identifiers[0].id); } + async cargoBikeByEquipmentId (id: number) { + return (await this.connection.getRepository(Equipment) + .createQueryBuilder('equipment') + .leftJoinAndSelect('equipment.cargoBike', 'cargoBike') + .where('equipment.id = :id', { id: id }) + .getOne())?.cargoBike; + } + /** * Will update Equipment, crashes when id not in db or cargoBikeId not db. * Will return updated Equipment joined with CargoBike only if cargoBike is was set in param0 * @param param0 struct with equipment properites */ async updateEquipment ({ equipment }: { equipment: any }) { - console.log(equipment); const cargoBikeId = equipment.cargoBikeId; delete equipment.cargoBikeId; - console.log(equipment); - const inserts = await this.connection.getRepository(Equipment) + await this.connection.getRepository(Equipment) .createQueryBuilder('equipment') .update() .set({ ...equipment }) .where('id = :id', { id: equipment.id }) .returning('*') .execute(); - console.log(inserts.raw[0]); if (cargoBikeId || cargoBikeId === null) { await this.connection.getRepository(Equipment) .createQueryBuilder() diff --git a/src/datasources/db/lendingstationAPI.ts b/src/datasources/db/lendingstationAPI.ts index 7658041..dc63848 100644 --- a/src/datasources/db/lendingstationAPI.ts +++ b/src/datasources/db/lendingstationAPI.ts @@ -34,7 +34,6 @@ export class LendingStationAPI extends DataSource { * @param param0 new lendingStation */ async createLendingStation ({ lendingStation }:{ lendingStation: any }) { - console.log(lendingStation); const inserts = await this.connection.manager .createQueryBuilder() .insert() diff --git a/src/datasources/db/participantAPI.ts b/src/datasources/db/participantAPI.ts index 9521a1a..5c4a320 100644 --- a/src/datasources/db/participantAPI.ts +++ b/src/datasources/db/participantAPI.ts @@ -1,5 +1,5 @@ import { DataSource } from 'apollo-datasource'; -import { GraphQLBoolean, GraphQLError } from 'graphql'; +import { GraphQLError } from 'graphql'; import { Connection, getConnection } from 'typeorm'; import { CargoBike } from '../../model/CargoBike'; import { ContactInformation } from '../../model/ContactInformation'; @@ -38,6 +38,16 @@ export class ParticipantAPI extends DataSource { .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') @@ -46,6 +56,20 @@ export class ParticipantAPI extends DataSource { .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') @@ -108,7 +132,7 @@ export class ParticipantAPI extends DataSource { .relation(Participant, 'contactInformation') .of(inserts.identifiers[0].id) .set(participant.contactInformationId); - return this.getParticipantById((await inserts).identifiers[0].id); + return this.getParticipantById(inserts.identifiers[0].id); } async createContactInformation (contactInformation: any) { @@ -152,7 +176,6 @@ export class ParticipantAPI extends DataSource { .relation(Engagement, 'participant') .of(inserts.identifiers[0].id) .set(engagement.participantId); - console.log(inserts); return this.engagementById(inserts.identifiers[0].id); } } diff --git a/src/resolvers/cargobikeResolver.ts b/src/resolvers/cargobikeResolver.ts index 6383be4..590753c 100644 --- a/src/resolvers/cargobikeResolver.ts +++ b/src/resolvers/cargobikeResolver.ts @@ -39,6 +39,22 @@ export default { } } }, + CargoBike: { + engagement (parent: any, { offset, limit }: { offset: number, limit: number }, { dataSources, req }: { dataSources: any, req: any }) { + return dataSources.participantAPI.engagementByCargoBikeId(offset, limit, parent.id); + }, + coordinator (parent: any, { offset, limit }: { offset: number, limit: number }, { dataSources, req }: { dataSources: any, req: any }) { + dataSources.participantAPI.participantByCargoBikeId(parent.id); + }, + equipment (parent: any, { offset, limit }: { offset: number, limit: number }, { dataSources, req }: { dataSources: any, req: any }) { + return dataSources.cargoBikeAPI.equipmentByCargoBikeId(offset, limit, parent.id); + } + }, + Equipment: { + cargoBike (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) { + return dataSources.cargoBikeAPI.cargoBikeByEquipmentId(parent.id); + } + }, Mutation: { createCargoBike: (_: any, { cargoBike }: { cargoBike: any }, { dataSources, req }:{dataSources: any, req: any }) => { if (req.permissions.includes(Permission.WriteBike)) { diff --git a/src/schema/type-defs.ts b/src/schema/type-defs.ts index a2374f0..fdd6a1d 100644 --- a/src/schema/type-defs.ts +++ b/src/schema/type-defs.ts @@ -29,10 +29,9 @@ type CargoBike { """ dimensionsAndLoad: DimensionsAndLoad! bikeEvents: [BikeEvent] - equipment: [Equipment] + equipment(offset: Int!, limit: Int!): [Equipment] "Refers to equipment that is not unique. See kommentierte info tabelle -> Fragen -> Frage 2" miscellaneousEquipment: [String] - chainSwaps: [ChainSwap] "Sticker State" stickerBikeNameState: StickerBikeNameState note: String @@ -41,6 +40,7 @@ type CargoBike { insuranceData: InsuranceData! lendingStation: LendingStation taxes: Taxes + engagement(offset: Int!, limit: Int!): [Engagement] "null if not locked by other user" lockedBy: ID lockedUntil: Date