Merge pull request #22 from fLotte-meets-HWR-DB/dev

Dev
pull/26/head
leonnicolas 4 years ago committed by GitHub
commit 11568af457
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -26,7 +26,7 @@ import { Equipment } from '../../model/Equipment';
import { Engagement } from '../../model/Engagement';
import { Provider } from '../../model/Provider';
import { TimeFrame } from '../../model/TimeFrame';
import { ActionLogger, DBUtils, LockUtils } from './utils';
import { ActionLogger, DBUtils, genBoxDimensions, LockUtils } from './utils';
import { EquipmentType } from '../../model/EquipmentType';
import { BikeEventType } from '../../model/BikeEventType';
import { UserInputError } from 'apollo-server-express';
@ -98,11 +98,13 @@ export class CargoBikeAPI extends DataSource {
async updateCargoBike (cargoBike: any, userId:number) {
const keepLock = cargoBike?.keepLock;
delete cargoBike.keepLock;
delete cargoBike.lendingStationId;
const equipmentTypeIds = cargoBike?.equipmentTypeIds;
delete cargoBike?.equipmentTypeIds;
const equipmentIds = cargoBike?.equipmentIds;
delete cargoBike?.equipmentIds;
// generate ranges for box dimensions
genBoxDimensions(cargoBike);
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');
@ -150,6 +152,7 @@ export class CargoBikeAPI extends DataSource {
*/
async createCargoBike (cargoBike: any) {
let inserts: any = {};
genBoxDimensions(cargoBike);
await this.connection.transaction(async (entityManager:any) => {
inserts = await entityManager.getRepository(CargoBike)
.createQueryBuilder('cb')

@ -19,7 +19,6 @@ This file is part of fLotte-API-Server.
import { DataSource } from 'apollo-datasource';
import { UserInputError } from 'apollo-server-express';
import { GraphQLError } from 'graphql';
import { Connection, EntityManager, getConnection } from 'typeorm';
import { CargoBike } from '../../model/CargoBike';
import { LendingStation } from '../../model/LendingStation';

@ -35,6 +35,33 @@ export function genDateRange (struct: any) {
delete struct.to;
}
/**
* This function prepares the cargoBike struct, to be used in an update or create.
* It creates the numrange attributes than can be understood by postgres.
* @param from
* @param to
*/
function genNumRange (from: number, to: number) {
if (from === null || from === undefined) {
from = to;
} else if (to === null || to === undefined) {
to = from;
}
return from ? '[' + from + ',' + to + ']' : null;
}
export function genBoxDimensions (cargoBike: any) {
cargoBike.dimensionsAndLoad.boxLengthRange = genNumRange(cargoBike.dimensionsAndLoad.minBoxLength, cargoBike.dimensionsAndLoad.maxBoxLength);
cargoBike.dimensionsAndLoad.boxWidthRange = genNumRange(cargoBike.dimensionsAndLoad.minBoxWidth, cargoBike.dimensionsAndLoad.maxBoxWidth);
cargoBike.dimensionsAndLoad.boxHeightRange = genNumRange(cargoBike.dimensionsAndLoad.minBoxHeight, cargoBike.dimensionsAndLoad.maxBoxHeight);
// delete this so update cargo bike works
delete cargoBike.dimensionsAndLoad.minBoxLength;
delete cargoBike.dimensionsAndLoad.maxBoxLength;
delete cargoBike.dimensionsAndLoad.minBoxWidth;
delete cargoBike.dimensionsAndLoad.maxBoxWidth;
delete cargoBike.dimensionsAndLoad.minBoxHeight;
delete cargoBike.dimensionsAndLoad.maxBoxHeight;
}
/**
* Can be used in resolvers to specify, if entry is locked by other user.
* Returns true if locked by other user.

@ -113,43 +113,50 @@ export class DimensionsAndLoad {
lockable:boolean;
@Column({
type: 'decimal'
type: 'numrange',
nullable: true
})
boxLength: number;
boxLengthRange: string;
@Column({
type: 'decimal'
type: 'numrange',
nullable: true
})
boxWidth: number;
boxWidthRange: string;
@Column({
type: 'decimal'
type: 'numrange',
nullable: true
})
boxHeight: number;
boxHeightRange: string;
@Column({
type: 'decimal'
type: 'decimal',
nullable: true
})
maxWeightBox: number;
maxWeightBox: string;
@Column({
type: 'decimal'
type: 'decimal',
nullable: true
})
maxWeightLuggageRack: number;
@Column({
type: 'decimal'
type: 'decimal',
nullable: true
})
maxWeightTotal: number;
@Column({
type: 'decimal'
type: 'decimal',
nullable: true
})
bikeLength: number;
@Column({
nullable: true,
type: 'decimal'
type: 'numrange'
})
bikeWidth: number;

@ -137,8 +137,6 @@ export default {
return new GraphQLError('Insufficient Permissions');
}
},
isLockedByMe: (parent: any, __: any, { req }: { req: any }) => isLockedByMe(parent, { req }),
isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req }),
timeFrames (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) {
if (req.permissions.includes(Permission.ReadTimeFrame)) {
return dataSources.lendingStationAPI.timeFramesByCargoBikeId(parent.id);
@ -159,8 +157,50 @@ export default {
} else {
return new GraphQLError('Insufficient Permissions');
}
},
isLockedByMe: (parent: any, __: any, { req }: { req: any }) => isLockedByMe(parent, { req }),
isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req })
},
DimensionsAndLoad: {
minBoxLength: (parent: any) => {
if (!parent.boxLengthRange || parent.boxLengthRange === 'empty') {
return null;
}
return parent.boxLengthRange ? (parent.boxLengthRange as string).split(',')[0].replace('[', '') : null;
},
maxBoxLength: (parent: any) => {
if (!parent.boxLengthRange || parent.boxLengthRange === 'empty') {
return null;
}
const str = (parent.boxLengthRange as string).split(',')[1].replace(']', '');
return (str.length > 0) ? str : null;
},
minBoxWidth: (parent: any) => {
if (!parent.boxWidthRange || parent.boxWidthRange === 'empty') {
return null;
}
return parent.boxWidthRange ? (parent.boxWidthRange as string).split(',')[0].replace('[', '') : null;
},
maxBoxWidth: (parent: any) => {
if (!parent.boxWidthRange || parent.boxWidthRange === 'empty') {
return null;
}
const str = (parent.boxWidthRange as string).split(',')[1].replace(']', '');
return (str.length > 0) ? str : null;
},
minBoxHeight: (parent: any) => {
if (!parent.boxHeightRange || parent.boxHeightRange === 'empty') {
return null;
}
return parent.boxHeightRange ? (parent.boxHeightRange as string).split(',')[0].replace('[', '') : null;
},
maxBoxHeight: (parent: any) => {
if (!parent.boxHeightRange || parent.boxHeightRange === 'empty') {
return null;
}
const str = (parent.boxHeightRange as string).split(',')[1].replace(']', '');
return (str.length > 0) ? str : null;
}
},
Equipment: {
cargoBike (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) {

@ -115,6 +115,7 @@ export default gql`
"""
Refers to unique equipment
When set to null or [], no relations will be added.
When specified id is in a relation with another bike, this relation will be deleted.
"""
equipmentIds: [ID]
"Sticker State"
@ -162,6 +163,7 @@ export default gql`
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.
When specified id is in a relation with another bike, this relation will be deleted.
"""
equipmentIds: [ID]
"Sticker State"
@ -257,13 +259,16 @@ export default gql`
hasCoverBox: Boolean!
"cover box can be locked"
lockable: Boolean!
boxLength: Float!
boxWidth: Float!
boxHeight: Float!
maxWeightBox: Float!
maxWeightLuggageRack: Float!
maxWeightTotal: Float!
bikeLength: Float!
minBoxLength: Float
maxBoxLength: Float
minBoxWidth: Float
maxBoxWidth: Float
minBoxHeight: Float
maxBoxHeight: Float
maxWeightBox: Float
maxWeightLuggageRack: Float
maxWeightTotal: Float
bikeLength: Float
bikeWidth: Float
bikeHeight: Float
bikeWeight: Float
@ -272,13 +277,16 @@ export default gql`
input DimensionsAndLoadCreateInput {
hasCoverBox: Boolean!
lockable: Boolean!
boxLength: Float!
boxWidth: Float!
boxHeight: Float!
maxWeightBox: Float!
maxWeightLuggageRack: Float!
maxWeightTotal: Float!
bikeLength: Float!
minBoxLength: Float
maxBoxLength: Float
minBoxWidth: Float
maxBoxWidth: Float
minBoxHeight: Float
maxBoxHeight: Float
maxWeightBox: Float
maxWeightLuggageRack: Float
maxWeightTotal: Float
bikeLength: Float
bikeWidth: Float
bikeHeight: Float
bikeWeight: Float
@ -287,9 +295,12 @@ export default gql`
input DimensionsAndLoadUpdateInput {
hasCoverBox: Boolean
lockable: Boolean
boxLength: Float
boxWidth: Float
boxHeight: Float
minBoxLength: Float
maxBoxLength: Float
minBoxWidth: Float
maxBoxWidth: Float
minBoxHeight: Float
maxBoxHeight: Float
maxWeightBox: Float
maxWeightLuggageRack: Float
maxWeightTotal: Float

Loading…
Cancel
Save