added cargobike equipment resolvers

pull/11/head
leonnicolas 4 years ago
parent 1830107f2b
commit 35144d6e73
No known key found for this signature in database
GPG Key ID: 088D0743E2B65C07

@ -128,6 +128,7 @@ export class CargoBikeAPI extends DataSource {
.getOne(); .getOne();
} }
// think this can go
async findEquipmentJoinBikeById (id: number) { async findEquipmentJoinBikeById (id: number) {
return await this.connection.getRepository(Equipment) return await this.connection.getRepository(Equipment)
.createQueryBuilder('equipment') .createQueryBuilder('equipment')
@ -136,6 +137,14 @@ export class CargoBikeAPI extends DataSource {
.getOne(); .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 }) { async createEquipment ({ equipment }: { equipment: any }) {
const inserts = await this.connection.getRepository(Equipment) const inserts = await this.connection.getRepository(Equipment)
.createQueryBuilder('equipment') .createQueryBuilder('equipment')
@ -150,29 +159,34 @@ export class CargoBikeAPI extends DataSource {
.relation(Equipment, 'cargoBike') .relation(Equipment, 'cargoBike')
.of(equipment.id) .of(equipment.id)
.set(equipment.cargoBikeId); .set(equipment.cargoBikeId);
return this.findEquipmentJoinBikeById(inserts.identifiers[0].id); // return this.findEquipmentJoinBikeById(inserts.identifiers[0].id);
} }
return this.findEquipmentById(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 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 * Will return updated Equipment joined with CargoBike only if cargoBike is was set in param0
* @param param0 struct with equipment properites * @param param0 struct with equipment properites
*/ */
async updateEquipment ({ equipment }: { equipment: any }) { async updateEquipment ({ equipment }: { equipment: any }) {
console.log(equipment);
const cargoBikeId = equipment.cargoBikeId; const cargoBikeId = equipment.cargoBikeId;
delete equipment.cargoBikeId; delete equipment.cargoBikeId;
console.log(equipment); await this.connection.getRepository(Equipment)
const inserts = await this.connection.getRepository(Equipment)
.createQueryBuilder('equipment') .createQueryBuilder('equipment')
.update() .update()
.set({ ...equipment }) .set({ ...equipment })
.where('id = :id', { id: equipment.id }) .where('id = :id', { id: equipment.id })
.returning('*') .returning('*')
.execute(); .execute();
console.log(inserts.raw[0]);
if (cargoBikeId || cargoBikeId === null) { if (cargoBikeId || cargoBikeId === null) {
await this.connection.getRepository(Equipment) await this.connection.getRepository(Equipment)
.createQueryBuilder() .createQueryBuilder()

@ -34,7 +34,6 @@ export class LendingStationAPI extends DataSource {
* @param param0 new lendingStation * @param param0 new lendingStation
*/ */
async createLendingStation ({ lendingStation }:{ lendingStation: any }) { async createLendingStation ({ lendingStation }:{ lendingStation: any }) {
console.log(lendingStation);
const inserts = await this.connection.manager const inserts = await this.connection.manager
.createQueryBuilder() .createQueryBuilder()
.insert() .insert()

@ -1,5 +1,5 @@
import { DataSource } from 'apollo-datasource'; import { DataSource } from 'apollo-datasource';
import { GraphQLBoolean, GraphQLError } from 'graphql'; import { GraphQLError } from 'graphql';
import { Connection, getConnection } from 'typeorm'; import { Connection, getConnection } from 'typeorm';
import { CargoBike } from '../../model/CargoBike'; import { CargoBike } from '../../model/CargoBike';
import { ContactInformation } from '../../model/ContactInformation'; import { ContactInformation } from '../../model/ContactInformation';
@ -38,6 +38,16 @@ export class ParticipantAPI extends DataSource {
.andWhere('engagement."participantId" = participant.id') .andWhere('engagement."participantId" = participant.id')
.getOne()).participant; .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) { async engagementByParticipantId (id: number) {
return await this.connection.getRepository(Engagement) return await this.connection.getRepository(Engagement)
.createQueryBuilder('engagement') .createQueryBuilder('engagement')
@ -46,6 +56,20 @@ export class ParticipantAPI extends DataSource {
.getOne(); .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) { async engagementById (id: number) {
return await this.connection.getRepository(Engagement) return await this.connection.getRepository(Engagement)
.createQueryBuilder('engagement') .createQueryBuilder('engagement')
@ -108,7 +132,7 @@ export class ParticipantAPI extends DataSource {
.relation(Participant, 'contactInformation') .relation(Participant, 'contactInformation')
.of(inserts.identifiers[0].id) .of(inserts.identifiers[0].id)
.set(participant.contactInformationId); .set(participant.contactInformationId);
return this.getParticipantById((await inserts).identifiers[0].id); return this.getParticipantById(inserts.identifiers[0].id);
} }
async createContactInformation (contactInformation: any) { async createContactInformation (contactInformation: any) {
@ -152,7 +176,6 @@ export class ParticipantAPI extends DataSource {
.relation(Engagement, 'participant') .relation(Engagement, 'participant')
.of(inserts.identifiers[0].id) .of(inserts.identifiers[0].id)
.set(engagement.participantId); .set(engagement.participantId);
console.log(inserts);
return this.engagementById(inserts.identifiers[0].id); return this.engagementById(inserts.identifiers[0].id);
} }
} }

@ -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: { Mutation: {
createCargoBike: (_: any, { cargoBike }: { cargoBike: any }, { dataSources, req }:{dataSources: any, req: any }) => { createCargoBike: (_: any, { cargoBike }: { cargoBike: any }, { dataSources, req }:{dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.WriteBike)) { if (req.permissions.includes(Permission.WriteBike)) {

@ -29,10 +29,9 @@ type CargoBike {
""" """
dimensionsAndLoad: DimensionsAndLoad! dimensionsAndLoad: DimensionsAndLoad!
bikeEvents: [BikeEvent] bikeEvents: [BikeEvent]
equipment: [Equipment] equipment(offset: Int!, limit: Int!): [Equipment]
"Refers to equipment that is not unique. See kommentierte info tabelle -> Fragen -> Frage 2" "Refers to equipment that is not unique. See kommentierte info tabelle -> Fragen -> Frage 2"
miscellaneousEquipment: [String] miscellaneousEquipment: [String]
chainSwaps: [ChainSwap]
"Sticker State" "Sticker State"
stickerBikeNameState: StickerBikeNameState stickerBikeNameState: StickerBikeNameState
note: String note: String
@ -41,6 +40,7 @@ type CargoBike {
insuranceData: InsuranceData! insuranceData: InsuranceData!
lendingStation: LendingStation lendingStation: LendingStation
taxes: Taxes taxes: Taxes
engagement(offset: Int!, limit: Int!): [Engagement]
"null if not locked by other user" "null if not locked by other user"
lockedBy: ID lockedBy: ID
lockedUntil: Date lockedUntil: Date

Loading…
Cancel
Save