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

@ -192,7 +192,7 @@ export class CargoBike implements Lockable {
nullable: true, nullable: true,
eager: 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 // 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) @ManyToMany(type => EquipmentType, equipmentType => equipmentType.cargoBikeIds)

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

@ -216,7 +216,7 @@ export default {
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)) {
return dataSources.cargoBikeAPI.createCargoBike({ cargoBike }); return dataSources.cargoBikeAPI.createCargoBike(cargoBike);
} else { } else {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }

@ -97,7 +97,6 @@ export default gql`
numberOfChildren: Int! numberOfChildren: Int!
""" """
Safety is a custom type, that stores information about security features. Safety is a custom type, that stores information about security features.
TODO: Should this be called Security?
""" """
security: SecurityCreateInput! security: SecurityCreateInput!
""" """
@ -108,8 +107,16 @@ export default gql`
Does not refer to an extra table in the database. Does not refer to an extra table in the database.
""" """
dimensionsAndLoad: DimensionsAndLoadCreateInput! 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] equipmentTypeIds: [ID]
"""
Refers to unique equipment
When set to null or [], no relations will be added.
"""
equipmentIds: [ID]
"Sticker State" "Sticker State"
stickerBikeNameState: StickerBikeNameState stickerBikeNameState: StickerBikeNameState
note: String 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 { input CargoBikeUpdateInput {
id: ID! id: ID!
@ -133,7 +140,6 @@ export default gql`
numberOfChildren: Int numberOfChildren: Int
""" """
Safety is a custom type, that stores information about security features. Safety is a custom type, that stores information about security features.
TODO: Should this be called Security?
""" """
security: SecurityUpdateInput security: SecurityUpdateInput
""" """
@ -146,9 +152,18 @@ export default gql`
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
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] 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" "Sticker State"
stickerBikeNameState: StickerBikeNameState stickerBikeNameState: StickerBikeNameState
note: String note: String
@ -606,7 +621,7 @@ export default gql`
} }
input EquipmentTypeCreateInput { input EquipmentTypeCreateInput {
name: String name: String!
description: String description: String
} }

Loading…
Cancel
Save