From a67c0ff6595c28963605c51b9af50abdf81efa28 Mon Sep 17 00:00:00 2001 From: leonnicolas Date: Sun, 27 Sep 2020 17:36:17 +0200 Subject: [PATCH] datasource/*: code a little clean up --- src/datasources/db/cargobikeAPI.ts | 41 +++++++++---------- src/datasources/db/contactinformationAPI.ts | 1 - src/datasources/db/lendingstationAPI.ts | 6 +-- src/datasources/db/providerAPI.ts | 42 +++++++++----------- src/resolvers/contactinformationResolvers.ts | 1 - 5 files changed, 39 insertions(+), 52 deletions(-) diff --git a/src/datasources/db/cargobikeAPI.ts b/src/datasources/db/cargobikeAPI.ts index e4ff927..c26136c 100644 --- a/src/datasources/db/cargobikeAPI.ts +++ b/src/datasources/db/cargobikeAPI.ts @@ -134,29 +134,24 @@ export class CargoBikeAPI extends DataSource { */ async createCargoBike ({ cargoBike }: { cargoBike: any }) { let inserts: any; - try { - await this.connection.transaction(async (entityManager:any) => { - inserts = await entityManager.getRepository(CargoBike) - .createQueryBuilder('cargobike') - .insert() - .values([cargoBike]) - .returning('*') - .execute(); - await entityManager.getRepository(CargoBike) - .createQueryBuilder('cargobike') - .relation(CargoBike, 'provider') - .of(inserts.identifiers[0].id) - .set(cargoBike?.providerId); - cargoBike?.equipmentTypeIds && await entityManager.getRepository(CargoBike) - .createQueryBuilder('cargobike') - .relation(CargoBike, 'equipmentTypeIds') - .of(inserts.identifiers[0].id) - .add(cargoBike.equipmentTypeIds); - }); - } catch (e: any) { - console.log(e); - return new GraphQLError('Transaction could not be completed'); - } + await this.connection.transaction(async (entityManager:any) => { + inserts = await entityManager.getRepository(CargoBike) + .createQueryBuilder('cargobike') + .insert() + .values([cargoBike]) + .returning('*') + .execute(); + await entityManager.getRepository(CargoBike) + .createQueryBuilder('cargobike') + .relation(CargoBike, 'provider') + .of(inserts.identifiers[0].id) + .set(cargoBike?.providerId); + cargoBike?.equipmentTypeIds && await entityManager.getRepository(CargoBike) + .createQueryBuilder('cargobike') + .relation(CargoBike, 'equipmentTypeIds') + .of(inserts.identifiers[0].id) + .add(cargoBike.equipmentTypeIds); + }); const newbike = inserts.generatedMaps[0]; newbike.id = inserts.identifiers[0].id; return newbike; diff --git a/src/datasources/db/contactinformationAPI.ts b/src/datasources/db/contactinformationAPI.ts index 6713209..fd7f3f2 100644 --- a/src/datasources/db/contactinformationAPI.ts +++ b/src/datasources/db/contactinformationAPI.ts @@ -105,7 +105,6 @@ export class ContactInformationAPI extends DataSource { .select() .where('ci."personId" = :id', { id: id }) .getMany(); - console.log(res); return res; } diff --git a/src/datasources/db/lendingstationAPI.ts b/src/datasources/db/lendingstationAPI.ts index bafc0d0..09f1108 100644 --- a/src/datasources/db/lendingstationAPI.ts +++ b/src/datasources/db/lendingstationAPI.ts @@ -176,7 +176,6 @@ export class LendingStationAPI extends DataSource { .where('timeframe."cargoBikeId" = :id', { id: timeFrame.cargoBikeId }) .andWhere('timeframe."dateRange" && :tr', { tr: timeFrame.dateRange }) .getMany(); - console.log(overlapping); if (overlapping.length !== 0) { throw new UserInputError('TimeFrames with ids: ' + overlapping.map((e) => { return e.id + ', '; }) + 'are overlapping'); } @@ -198,7 +197,6 @@ export class LendingStationAPI extends DataSource { .set(timeFrame.lendingStationId); }); } catch (e) { - console.log(e); if (e instanceof UserInputError) { return e; } else if (e instanceof QueryFailedError) { @@ -210,7 +208,7 @@ export class LendingStationAPI extends DataSource { return inserts.generatedMaps[0]; } - /* async updateTimeFrame (timeFrame: any) { + /* async updateTimeFrame (timeFrame: any) { try { await this.connection.transaction(async (entityManager: EntityManager) => { if (timeFrame.to === undefined) { @@ -260,5 +258,5 @@ export class LendingStationAPI extends DataSource { } inserts.generatedMaps[0].id = inserts.identifiers[0].id; return inserts.generatedMaps[0]; - }*/ + } */ } diff --git a/src/datasources/db/providerAPI.ts b/src/datasources/db/providerAPI.ts index e273f05..0e59179 100644 --- a/src/datasources/db/providerAPI.ts +++ b/src/datasources/db/providerAPI.ts @@ -29,29 +29,25 @@ export class ProviderAPI extends DataSource { async createProvider (provider: any) { let inserts: any; - try { - await this.connection.transaction(async (entityManager: any) => { - inserts = await entityManager.getRepository(Provider) - .createQueryBuilder('provider') - .insert() - .values([provider]) - .returning('*') - .execute(); - await entityManager.getRepository(Provider) - .createQueryBuilder('provider') - .relation(Provider, 'cargoBikes') - .of(inserts.identifiers[0].id) - .add(provider.cargoBikeIds); - await entityManager.getRepository(Provider) - .createQueryBuilder('provider') - .relation(Provider, 'contactPersons') - .of(inserts.identifiers[0].id) - .add(provider.contactPersonIds); - }); - } catch (e: any) { - console.log(e); - return new GraphQLError('Transaction could not be completed'); - } + await this.connection.transaction(async (entityManager: any) => { + inserts = await entityManager.getRepository(Provider) + .createQueryBuilder('provider') + .insert() + .values([provider]) + .returning('*') + .execute(); + await entityManager.getRepository(Provider) + .createQueryBuilder('provider') + .relation(Provider, 'cargoBikes') + .of(inserts.identifiers[0].id) + .add(provider.cargoBikeIds); + await entityManager.getRepository(Provider) + .createQueryBuilder('provider') + .relation(Provider, 'contactPersons') + .of(inserts.identifiers[0].id) + .add(provider.contactPersonIds); + }); + const ret = inserts.generatedMaps[0]; ret.id = inserts.identifiers[0].id; return ret; diff --git a/src/resolvers/contactinformationResolvers.ts b/src/resolvers/contactinformationResolvers.ts index 7fa0b79..bfdbcfe 100644 --- a/src/resolvers/contactinformationResolvers.ts +++ b/src/resolvers/contactinformationResolvers.ts @@ -40,7 +40,6 @@ export default { ContactInformation: { person: (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) => { if (req.permissions.includes(Permission.ReadPerson)) { - console.log(parent); return dataSources.contactInformationAPI.personByContactInformationId(parent.id); } else { return new GraphQLError('Insufficient Permissions');