use transaktions for some updates

pull/14/head
leonnicolas 4 years ago
parent 80438de4fc
commit e1a90670f6
No known key found for this signature in database
GPG Key ID: 088D0743E2B65C07

@ -5,6 +5,7 @@ import { GraphQLError } from 'graphql';
import { BikeEvent } from '../../model/BikeEvent'; import { BikeEvent } from '../../model/BikeEvent';
import { Equipment } from '../../model/Equipment'; import { Equipment } from '../../model/Equipment';
import { Engagement } from '../../model/Engagement'; import { Engagement } from '../../model/Engagement';
import { Provider } from '../../model/Provider';
/** /**
* extended datasource to feed resolvers with data about cargoBikes * extended datasource to feed resolvers with data about cargoBikes
@ -47,6 +48,14 @@ export class CargoBikeAPI extends DataSource {
.getOne())?.cargoBike; .getOne())?.cargoBike;
} }
async cargoBikesByProviderId (id: number) {
return await this.connection
.createQueryBuilder()
.relation(Provider, 'cargoBikes')
.of(id)
.loadMany();
}
async lockCargoBike (id: number, req: any, dataSources: any) { async lockCargoBike (id: number, req: any, dataSources: any) {
if (await this.lockEntity(CargoBike, 'cargobike', id, req, dataSources)) { if (await this.lockEntity(CargoBike, 'cargobike', id, req, dataSources)) {
return this.findCargoBikeById(id); return this.findCargoBikeById(id);
@ -206,13 +215,29 @@ export class CargoBikeAPI extends DataSource {
* @param param0 cargoBike to be created * @param param0 cargoBike to be created
*/ */
async createCargoBike ({ cargoBike }: { cargoBike: any }) { async createCargoBike ({ cargoBike }: { cargoBike: any }) {
const inserts = await this.connection.manager let inserts: any;
.createQueryBuilder() try {
await this.connection.transaction(async (entityManager:any) => {
inserts = await entityManager.getRepository(CargoBike)
.createQueryBuilder('cargobike')
.insert() .insert()
.into(CargoBike)
.values([cargoBike]) .values([cargoBike])
.returning('*') .returning('*')
.execute(); .execute();
await entityManager.getRepository(CargoBike)
.createQueryBuilder('cargobike')
.relation(CargoBike, 'lendingStation')
.of(inserts.identifiers[0].id)
.set(cargoBike?.lendingStationId);
await entityManager.getRepository(CargoBike)
.createQueryBuilder('cargobike')
.relation(CargoBike, 'provider')
.of(inserts.identifiers[0].id)
.set(cargoBike?.providerId);
});
} catch (e: any) {
return new GraphQLError('Transaction could not be completed');
}
const newbike = inserts.generatedMaps[0]; const newbike = inserts.generatedMaps[0];
newbike.id = inserts.identifiers[0].id; newbike.id = inserts.identifiers[0].id;
return newbike; return newbike;

@ -38,7 +38,7 @@ export class LendingStationAPI extends DataSource {
return await this.connection.getRepository(LendingStation) return await this.connection.getRepository(LendingStation)
.createQueryBuilder('lendingStation') .createQueryBuilder('lendingStation')
.leftJoinAndSelect('lendingStation.cargoBikes', 'cargoBikes') .leftJoinAndSelect('lendingStation.cargoBikes', 'cargoBikes')
.where('"cargoBikes"."lendingStationId" = :id', { id: id }) .where('"cargoBikes".id = :id', { id: id })
.getOne().catch(() => { return null; }); .getOne().catch(() => { return null; });
} }

@ -1,4 +1,5 @@
import { DataSource } from 'apollo-datasource'; import { DataSource } from 'apollo-datasource';
import { GraphQLError } from 'graphql';
import { Connection, getConnection } from 'typeorm'; import { Connection, getConnection } from 'typeorm';
import { Provider } from '../../model/Provider'; import { Provider } from '../../model/Provider';
@ -16,4 +17,43 @@ export class ProviderAPI extends DataSource {
.where('provider.id = :id', { id: id }) .where('provider.id = :id', { id: id })
.getOne().catch(() => { return null; }); .getOne().catch(() => { return null; });
} }
async provider (offset: number, limit: number) {
return await this.connection.getRepository(Provider)
.createQueryBuilder('provider')
.select()
.offset(offset)
.limit(limit)
.getMany();
}
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');
}
const ret = inserts.generatedMaps[0];
ret.id = inserts.identifiers[0].id;
return ret;
}
} }

@ -16,7 +16,7 @@ export class ContactPerson {
}) })
lendingStations: LendingStation[]; lendingStations: LendingStation[];
@ManyToMany(type => Provider, provider => provider.contactPersons, { @ManyToOne(type => Provider, provider => provider.contactPersons, {
nullable: true nullable: true
}) })
provider: Provider[]; provider: Provider[];

@ -1,6 +1,7 @@
/* eslint no-unused-vars: "off" */ /* eslint no-unused-vars: "off" */
import { PrimaryGeneratedColumn, Column, OneToMany, Entity, OneToOne, ChildEntity } from 'typeorm'; import { PrimaryGeneratedColumn, Column, OneToMany, Entity, OneToOne, ChildEntity } from 'typeorm';
import { CargoBike } from './CargoBike';
import { ContactInformation } from './ContactInformation'; import { ContactInformation } from './ContactInformation';
import { ContactPerson } from './ContactPerson'; import { ContactPerson } from './ContactPerson';
import { LendingStation } from './LendingStation'; import { LendingStation } from './LendingStation';
@ -37,6 +38,9 @@ export class Provider {
}) })
contactPersons: ContactPerson[]; contactPersons: ContactPerson[];
@OneToMany(type => CargoBike, cargoBike => cargoBike.provider)
cargoBikes: CargoBike[];
@Column() @Column()
isPrivatePerson: boolean; isPrivatePerson: boolean;

@ -3,7 +3,14 @@ import { Permission } from '../datasources/userserver/permission';
export default { export default {
Query: { Query: {
providerById: (_: any, { id }: { id: number, limit: number }, { dataSources, req }: { dataSources: any, req: any }) => { providers: (_: any, { offset, limit }: { offset: number, limit: number }, { dataSources, req }: { dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.ReadBike)) {
return dataSources.providerAPI.provider(offset, limit);
} else {
return new GraphQLError('Insufficient Permissions');
}
},
providerById: (_: any, { id }: { id: number }, { dataSources, req }: { dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.ReadBike)) { if (req.permissions.includes(Permission.ReadBike)) {
return dataSources.providerAPI.providerById(id); return dataSources.providerAPI.providerById(id);
} else { } else {
@ -11,6 +18,22 @@ export default {
} }
} }
}, },
Provider: {
cargoBikes: (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.ReadBike)) {
return dataSources.cargoBikeAPI.cargoBikesByProviderId(parent.id);
} else {
return new GraphQLError('Insufficient Permissions');
}
}
},
Mutation: { Mutation: {
createProvider: (_: any, { provider }: { provider: number }, { dataSources, req }: { dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.WriteBike)) {
return dataSources.providerAPI.createProvider(provider);
} else {
return new GraphQLError('Insufficient Permissions');
}
}
} }
}; };

@ -78,8 +78,9 @@ input CargoBikeCreateInput {
"Sticker State" "Sticker State"
stickerBikeNameState: StickerBikeNameState stickerBikeNameState: StickerBikeNameState
note: String note: String
provider: String providerId: ID
insuranceData: InsuranceDataCreateInput! insuranceData: InsuranceDataCreateInput!
lendingStationId: ID
taxes: TaxesCreateInput! taxes: TaxesCreateInput!
} }
@ -108,12 +109,12 @@ input CargoBikeUpdateInput {
dimensionsAndLoad: DimensionsAndLoadUpdateInput dimensionsAndLoad: DimensionsAndLoadUpdateInput
"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]
lendingStationId: ID
"Sticker State" "Sticker State"
stickerBikeNameState: StickerBikeNameState stickerBikeNameState: StickerBikeNameState
note: String note: String
provider: String provider: String
insuranceData: InsuranceDataUpdateInput insuranceData: InsuranceDataUpdateInput
lendingStationId: ID
taxes: TaxesUpdateInput taxes: TaxesUpdateInput
"will keep Bike locked if set to true, default = false" "will keep Bike locked if set to true, default = false"
keepLock: Boolean keepLock: Boolean
@ -504,19 +505,26 @@ enum StickerBikeNameState {
UNKNOWN UNKNOWN
} }
"(dt. Anbieter)" "(dt. Anbieter) bezieht sich auf die Benziehung einer Person oder Organisation zum Lastenrad"
type Provider { type Provider {
id: ID! id: ID!
name: String!
formularName: String formularName: String
contactPersons: [ContactInformation]!
providerContactPerson: [ContactInformation]
isPrivatePerson: Boolean! isPrivatePerson: Boolean!
organisation: Organisation organisation: Organisation
cargoBikes: [CargoBike]! cargoBikes: [CargoBike]!
} }
"(dt. Anbieter)"
input ProviderCreateInput {
formularName: String!
"i think it makes more sense to create Provider and then add new ContactPersons"
contactPersonIds: [ID]!
isPrivatePerson: Boolean!
organisationId: ID
cargoBikeIds: [ID]!
}
type ContactInformation { type ContactInformation {
id: ID! id: ID!
name: String! name: String!
@ -558,6 +566,7 @@ input ContactInformationUpdateInput {
note: String note: String
} }
"describes Relation of Contact to Provider"
type ContactPerson { type ContactPerson {
id: ID! id: ID!
intern: Boolean! intern: Boolean!
@ -703,7 +712,7 @@ type Query {
equipment(offset: Int!, limit: Int!): [Equipment]! equipment(offset: Int!, limit: Int!): [Equipment]!
"equipment by id, will return null if id not found" "equipment by id, will return null if id not found"
equipmentById(id: ID!): Equipment equipmentById(id: ID!): Equipment
providers: [Provider]! providers(offset: Int!, limit: Int!): [Provider]!
"particcipant by id" "particcipant by id"
participantById(id:ID!): Participant participantById(id:ID!): Participant
"p" "p"
@ -747,6 +756,8 @@ type Mutation {
"createContacPerson ,return null if contactInformationId does not exist" "createContacPerson ,return null if contactInformationId does not exist"
createContactPerson(contactPerson: ContactPersonCreateInput): ContactPerson createContactPerson(contactPerson: ContactPersonCreateInput): ContactPerson
updateContactPerson(contactPerson: ContactPersonUpdateInput): ContactPerson updateContactPerson(contactPerson: ContactPersonUpdateInput): ContactPerson
"create Provider, if cargoBikeIds or contactPersonIds are not valid, provider will still be created"
createProvider(provider: ProviderCreateInput!): Provider!
} }

Loading…
Cancel
Save