src: use range for dimensionAndLoad

Changes in the schema:
	insead of _boxWidth_ etc. min and maxBoxWidth is used
In the postgres db, this is saved as numrange.
pull/22/head
leonnicolas 4 years ago
parent ca96923d3c
commit 5011a29be8
No known key found for this signature in database
GPG Key ID: 088D0743E2B65C07

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

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

@ -35,6 +35,33 @@ export function genDateRange (struct: any) {
delete struct.to; 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. * Can be used in resolvers to specify, if entry is locked by other user.
* Returns true if locked by other user. * Returns true if locked by other user.

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

@ -137,8 +137,6 @@ export default {
return new GraphQLError('Insufficient Permissions'); 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 }) { timeFrames (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) {
if (req.permissions.includes(Permission.ReadTimeFrame)) { if (req.permissions.includes(Permission.ReadTimeFrame)) {
return dataSources.lendingStationAPI.timeFramesByCargoBikeId(parent.id); return dataSources.lendingStationAPI.timeFramesByCargoBikeId(parent.id);
@ -159,8 +157,50 @@ export default {
} else { } else {
return new GraphQLError('Insufficient Permissions'); 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: { Equipment: {
cargoBike (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) { cargoBike (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) {

@ -259,13 +259,16 @@ export default gql`
hasCoverBox: Boolean! hasCoverBox: Boolean!
"cover box can be locked" "cover box can be locked"
lockable: Boolean! lockable: Boolean!
boxLength: Float! minBoxLength: Float
boxWidth: Float! maxBoxLength: Float
boxHeight: Float! minBoxWidth: Float
maxWeightBox: Float! maxBoxWidth: Float
maxWeightLuggageRack: Float! minBoxHeight: Float
maxWeightTotal: Float! maxBoxHeight: Float
bikeLength: Float! maxWeightBox: Float
maxWeightLuggageRack: Float
maxWeightTotal: Float
bikeLength: Float
bikeWidth: Float bikeWidth: Float
bikeHeight: Float bikeHeight: Float
bikeWeight: Float bikeWeight: Float
@ -274,13 +277,16 @@ export default gql`
input DimensionsAndLoadCreateInput { input DimensionsAndLoadCreateInput {
hasCoverBox: Boolean! hasCoverBox: Boolean!
lockable: Boolean! lockable: Boolean!
boxLength: Float! minBoxLength: Float
boxWidth: Float! maxBoxLength: Float
boxHeight: Float! minBoxWidth: Float
maxWeightBox: Float! maxBoxWidth: Float
maxWeightLuggageRack: Float! minBoxHeight: Float
maxWeightTotal: Float! maxBoxHeight: Float
bikeLength: Float! maxWeightBox: Float
maxWeightLuggageRack: Float
maxWeightTotal: Float
bikeLength: Float
bikeWidth: Float bikeWidth: Float
bikeHeight: Float bikeHeight: Float
bikeWeight: Float bikeWeight: Float
@ -289,9 +295,12 @@ export default gql`
input DimensionsAndLoadUpdateInput { input DimensionsAndLoadUpdateInput {
hasCoverBox: Boolean hasCoverBox: Boolean
lockable: Boolean lockable: Boolean
boxLength: Float minBoxLength: Float
boxWidth: Float maxBoxLength: Float
boxHeight: Float minBoxWidth: Float
maxBoxWidth: Float
minBoxHeight: Float
maxBoxHeight: Float
maxWeightBox: Float maxWeightBox: Float
maxWeightLuggageRack: Float maxWeightLuggageRack: Float
maxWeightTotal: Float maxWeightTotal: Float

Loading…
Cancel
Save