src/*: update equipment/type with cargoBike update

Now it is possible to to update equipment and equipment type via the
mutations carogBikeUpdate and cargoBikeCreate.
pull/21/head
leonnicolas 4 years ago
parent c193a63785
commit 9d1ba30e46
No known key found for this signature in database
GPG Key ID: 088D0743E2B65C07

@ -99,11 +99,10 @@ export class CargoBikeAPI extends DataSource {
const keepLock = cargoBike?.keepLock;
delete cargoBike.keepLock;
delete cargoBike.lendingStationId;
let equipmentTypeIds: any = null;
if (cargoBike.equipmentTypeIds) {
equipmentTypeIds = cargoBike.equipmentTypeIds;
delete cargoBike.equipmentTypeIds;
}
const equipmentTypeIds = cargoBike?.equipmentTypeIds;
delete cargoBike?.equipmentTypeIds;
const equipmentIds = cargoBike?.equipmentIds;
delete cargoBike?.equipmentIds;
await this.connection.transaction(async (entityManager: EntityManager) => {
if (await LockUtils.isLocked(entityManager, CargoBike, 'cb', cargoBike.id, userId)) {
throw new GraphQLError('CargoBike locked by other user');
@ -119,7 +118,12 @@ export class CargoBikeAPI extends DataSource {
.createQueryBuilder('cb')
.relation(CargoBike, 'equipmentTypeIds')
.of(cargoBike.id)
.addAndRemove(equipmentTypeIds, await this.equipmentTypeByCargoBikeId(cargoBike.id)); // TODO remove all existing relations
.addAndRemove(equipmentTypeIds, await this.equipmentTypeByCargoBikeId(cargoBike.id));
equipmentIds && await entityManager.getRepository(CargoBike)
.createQueryBuilder('cb')
.relation(CargoBike, 'equipmentIds')
.of(cargoBike.id)
.addAndRemove(equipmentIds, await this.equipmentByCargoBikeId(cargoBike.id));
});
!keepLock && await LockUtils.unlockEntity(this.connection, CargoBike, 'cb', cargoBike.id, userId);
return await this.findCargoBikeById(cargoBike.id);
@ -144,7 +148,7 @@ export class CargoBikeAPI extends DataSource {
* created CargoBike and returns created bike with new ID
* @param param0 cargoBike to be created
*/
async createCargoBike ({ cargoBike }: { cargoBike: any }) {
async createCargoBike (cargoBike: any) {
let inserts: any = {};
await this.connection.transaction(async (entityManager:any) => {
inserts = await entityManager.getRepository(CargoBike)
@ -158,6 +162,11 @@ export class CargoBikeAPI extends DataSource {
.relation(CargoBike, 'equipmentTypeIds')
.of(inserts.identifiers[0].id)
.add(cargoBike.equipmentTypeIds);
cargoBike?.equipmentIds && await entityManager.getRepository(CargoBike)
.createQueryBuilder('cb')
.relation(CargoBike, 'equipmentIds')
.of(inserts.identifiers[0].id)
.add(cargoBike.equipmentIds);
});
inserts.generatedMaps[0].id = inserts?.identifiers[0].id;
return inserts?.generatedMaps[0];
@ -411,14 +420,8 @@ export class CargoBikeAPI extends DataSource {
return await DBUtils.deleteEntity(this.connection, Equipment, 'e', id, userId);
}
async getEquipment (offset: number, limit: number) {
return await this.connection.getRepository(Equipment)
.createQueryBuilder('equipment')
.leftJoinAndSelect('equipment.cargoBike', 'cargoBike')
.orderBy('title', 'ASC')
.offset(offset)
.limit(limit)
.getMany();
async getEquipment (offset?: number, limit?: number) {
return await DBUtils.getAllEntity(this.connection, Equipment, 'e', offset, limit);
}
async createEquipmentType (equipmentType: any) {
@ -473,13 +476,8 @@ export class CargoBikeAPI extends DataSource {
.getOne();
}
async equipmentTypes (offset: number, limit: number) {
return await this.connection.getRepository(EquipmentType)
.createQueryBuilder('et')
.select()
.skip(offset)
.take(limit)
.getMany();
async equipmentTypes (offset?: number, limit?: number) {
return await DBUtils.getAllEntity(this.connection, EquipmentType, 'et', offset, limit);
}
async equipmentTypeByCargoBikeId (id: number) {

@ -192,7 +192,7 @@ export class CargoBike implements Lockable {
nullable: true,
eager: true
})
equipment: Equipment[];
equipmentIds: number[];
// Equipment that is not unique and is supposed to be selected out of a list e.g. drop down
@ManyToMany(type => EquipmentType, equipmentType => equipmentType.cargoBikeIds)

@ -26,7 +26,9 @@ export class Equipment implements Lockable {
@PrimaryGeneratedColumn()
id: number;
@Column()
@Column({
unique: true
})
serialNo: string;
@Column()
@ -37,7 +39,7 @@ export class Equipment implements Lockable {
})
description: string;
@ManyToOne(type => CargoBike, cargoBike => cargoBike.equipment, {
@ManyToOne(type => CargoBike, cargoBike => cargoBike.equipmentIds, {
nullable: true
})
@JoinColumn({

@ -216,7 +216,7 @@ export default {
Mutation: {
createCargoBike: (_: any, { cargoBike }: { cargoBike: any }, { dataSources, req }: { dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.WriteBike)) {
return dataSources.cargoBikeAPI.createCargoBike({ cargoBike });
return dataSources.cargoBikeAPI.createCargoBike(cargoBike);
} else {
return new GraphQLError('Insufficient Permissions');
}

@ -97,7 +97,6 @@ export default gql`
numberOfChildren: Int!
"""
Safety is a custom type, that stores information about security features.
TODO: Should this be called Security?
"""
security: SecurityCreateInput!
"""
@ -108,8 +107,16 @@ export default gql`
Does not refer to an extra table in the database.
"""
dimensionsAndLoad: DimensionsAndLoadCreateInput!
"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
When set to null or [], no relations will be added.
"""
equipmentTypeIds: [ID]
"""
Refers to unique equipment
When set to null or [], no relations will be added.
"""
equipmentIds: [ID]
"Sticker State"
stickerBikeNameState: StickerBikeNameState
note: String
@ -119,7 +126,7 @@ export default gql`
}
"""
if you want to add bike to a lending station, create a new timeFrame with to: Date = null
If you want to add bike to a lending station, create a new timeFrame with to: Date = null
"""
input CargoBikeUpdateInput {
id: ID!
@ -133,7 +140,6 @@ export default gql`
numberOfChildren: Int
"""
Safety is a custom type, that stores information about security features.
TODO: Should this be called Security?
"""
security: SecurityUpdateInput
"""
@ -146,9 +152,18 @@ export default gql`
dimensionsAndLoad: DimensionsAndLoadUpdateInput
"""
Refers to equipment that is not unique. See kommentierte info tabelle -> Fragen -> Frage 2
If set, ols relations will be over written. Set [] to delete all
When set to null, field will be ignored.
When set to [], all relations will be deleted.
Else all realtions will be deleted and the specified relations will be added.
"""
equipmentTypeIds: [ID]
"""
Refers to unique equipment
When set to null, field will be ignored.
When set to [], all relations will be deleted.
Else all realtions will be deleted and the specified relations will be added.
"""
equipmentIds: [ID]
"Sticker State"
stickerBikeNameState: StickerBikeNameState
note: String
@ -606,7 +621,7 @@ export default gql`
}
input EquipmentTypeCreateInput {
name: String
name: String!
description: String
}

Loading…
Cancel
Save