From 0754fd5f74e7b18bbffe1114c5b967bedec38f1a Mon Sep 17 00:00:00 2001 From: leonnicolas Date: Sat, 28 Nov 2020 21:56:09 +0100 Subject: [PATCH] src/*: use type daterange for timeframes Instead of to and from, no type dataRange is used. Type DateRange still has to and from. --- src/datasources/db/lendingstationAPI.ts | 5 +---- src/datasources/db/utils.ts | 15 ++++++------- src/resolvers/lendingStationResolvers.ts | 12 ++++++----- src/schema/type-defs.ts | 27 +++++++++++++++++------- 4 files changed, 34 insertions(+), 25 deletions(-) diff --git a/src/datasources/db/lendingstationAPI.ts b/src/datasources/db/lendingstationAPI.ts index a0f84c6..52c08ad 100644 --- a/src/datasources/db/lendingstationAPI.ts +++ b/src/datasources/db/lendingstationAPI.ts @@ -183,10 +183,7 @@ export class LendingStationAPI extends DataSource { async createTimeFrame (timeFrame: any) { return await this.connection.transaction(async (entityManager: EntityManager) => { - if (timeFrame.to === undefined) { - timeFrame.to = ''; - } - timeFrame.dateRange = '[' + timeFrame.from + ',' + timeFrame.to + ')'; + genDateRange(timeFrame); // checking for overlapping time frames const overlapping = await entityManager.getRepository(TimeFrame) .createQueryBuilder('timeframe') diff --git a/src/datasources/db/utils.ts b/src/datasources/db/utils.ts index 0f62e60..69318dc 100644 --- a/src/datasources/db/utils.ts +++ b/src/datasources/db/utils.ts @@ -23,16 +23,15 @@ import { ActionLog, Actions } from '../../model/ActionLog'; import { UserInputError } from 'apollo-server-express'; export function genDateRange (struct: any) { - if (struct.to === undefined) { - struct.to = ''; - } - struct.dateRange = '[' + struct.from + ',' + struct.to + ')'; - if (struct.from === undefined) { + if (!struct.dateRange || !struct.dateRange.from) { delete struct.dateRange; + return; + } else if (!struct.dateRange?.to) { + struct.dateRange.to = ''; + } else if (struct.dateRange.to === struct.dateRange.from) { + throw new UserInputError('Date Range can not be empty, provide different dates.'); } - // delete these keys, so the struct can be used to update the engagement entity - delete struct.from; - delete struct.to; + struct.dateRange = '[' + struct.dateRange.from + ',' + struct.dateRange.to + ')'; } /** diff --git a/src/resolvers/lendingStationResolvers.ts b/src/resolvers/lendingStationResolvers.ts index eb26f62..8cc3467 100644 --- a/src/resolvers/lendingStationResolvers.ts +++ b/src/resolvers/lendingStationResolvers.ts @@ -104,14 +104,16 @@ export default { return parent.loanTimes ? parent.loanTimes : []; } }, - TimeFrame: { + DateRange: { from (parent: any) { - return (parent.dateRange as string).split(',')[0].replace('[', ''); + return (parent as string).split(',')[0].replace('[', ''); }, - to (parent: any) { - const str = (parent.dateRange as string).split(',')[1].replace(')', ''); + to (parent: string) { + const str = (parent as string).split(',')[1].replace(')', ''); return (str.length > 0) ? str : null; - }, + } + }, + TimeFrame: { cargoBike (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) { if (req.permissions.includes(Permission.ReadBike)) { return dataSources.cargoBikeAPI.cargoBikeByTimeFrameId(parent.id); diff --git a/src/schema/type-defs.ts b/src/schema/type-defs.ts index e5847cc..ff66e76 100644 --- a/src/schema/type-defs.ts +++ b/src/schema/type-defs.ts @@ -935,13 +935,26 @@ export default gql` loanTimes: [String!] } - "(dt. Zeitscheibe) When was a bike where" - type TimeFrame { - id: ID! - "format YYYY-MM-dd" + type DateRange{ from: Date! + "will be infinity of not omitted" + to: Date + } + + input DateRangeInput { "format YYYY-MM-dd" + from: Date! + """ + format YYYY-MM-dd + will be infinity of not omitted + """ to: Date + } + + "(dt. Zeitscheibe) When was a bike where" + type TimeFrame { + id: ID! + dateRange: DateRange! note: String lendingStation: LendingStation! cargoBike: CargoBike! @@ -953,8 +966,7 @@ export default gql` } input TimeFrameCreateInput { - from: Date! - to: Date + dateRange: DateRangeInput! note: String lendingStationId: ID! cargoBikeId: ID! @@ -962,8 +974,7 @@ export default gql` input TimeFrameUpdateInput { id: ID! - from: Date - to: Date + dateRange: DateRangeInput note: String lendingStationId: ID cargoBikeId: ID