src/*: use type daterange for timeframes

Instead of to and from, no type dataRange is used.
Type DateRange still has to and from.
pull/23/head
leonnicolas 4 years ago
parent 321dc72123
commit 0754fd5f74
No known key found for this signature in database
GPG Key ID: 088D0743E2B65C07

@ -183,10 +183,7 @@ export class LendingStationAPI extends DataSource {
async createTimeFrame (timeFrame: any) { async createTimeFrame (timeFrame: any) {
return await this.connection.transaction(async (entityManager: EntityManager) => { return await this.connection.transaction(async (entityManager: EntityManager) => {
if (timeFrame.to === undefined) { genDateRange(timeFrame);
timeFrame.to = '';
}
timeFrame.dateRange = '[' + timeFrame.from + ',' + timeFrame.to + ')';
// checking for overlapping time frames // checking for overlapping time frames
const overlapping = await entityManager.getRepository(TimeFrame) const overlapping = await entityManager.getRepository(TimeFrame)
.createQueryBuilder('timeframe') .createQueryBuilder('timeframe')

@ -23,16 +23,15 @@ import { ActionLog, Actions } from '../../model/ActionLog';
import { UserInputError } from 'apollo-server-express'; import { UserInputError } from 'apollo-server-express';
export function genDateRange (struct: any) { export function genDateRange (struct: any) {
if (struct.to === undefined) { if (!struct.dateRange || !struct.dateRange.from) {
struct.to = '';
}
struct.dateRange = '[' + struct.from + ',' + struct.to + ')';
if (struct.from === undefined) {
delete struct.dateRange; 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 struct.dateRange = '[' + struct.dateRange.from + ',' + struct.dateRange.to + ')';
delete struct.from;
delete struct.to;
} }
/** /**

@ -104,14 +104,16 @@ export default {
return parent.loanTimes ? parent.loanTimes : []; return parent.loanTimes ? parent.loanTimes : [];
} }
}, },
TimeFrame: { DateRange: {
from (parent: any) { from (parent: any) {
return (parent.dateRange as string).split(',')[0].replace('[', ''); return (parent as string).split(',')[0].replace('[', '');
}, },
to (parent: any) { to (parent: string) {
const str = (parent.dateRange as string).split(',')[1].replace(')', ''); const str = (parent as string).split(',')[1].replace(')', '');
return (str.length > 0) ? str : null; return (str.length > 0) ? str : null;
}, }
},
TimeFrame: {
cargoBike (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) { cargoBike (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) {
if (req.permissions.includes(Permission.ReadBike)) { if (req.permissions.includes(Permission.ReadBike)) {
return dataSources.cargoBikeAPI.cargoBikeByTimeFrameId(parent.id); return dataSources.cargoBikeAPI.cargoBikeByTimeFrameId(parent.id);

@ -935,13 +935,26 @@ export default gql`
loanTimes: [String!] loanTimes: [String!]
} }
"(dt. Zeitscheibe) When was a bike where" type DateRange{
type TimeFrame {
id: ID!
"format YYYY-MM-dd"
from: Date! from: Date!
"will be infinity of not omitted"
to: Date
}
input DateRangeInput {
"format YYYY-MM-dd" "format YYYY-MM-dd"
from: Date!
"""
format YYYY-MM-dd
will be infinity of not omitted
"""
to: Date to: Date
}
"(dt. Zeitscheibe) When was a bike where"
type TimeFrame {
id: ID!
dateRange: DateRange!
note: String note: String
lendingStation: LendingStation! lendingStation: LendingStation!
cargoBike: CargoBike! cargoBike: CargoBike!
@ -953,8 +966,7 @@ export default gql`
} }
input TimeFrameCreateInput { input TimeFrameCreateInput {
from: Date! dateRange: DateRangeInput!
to: Date
note: String note: String
lendingStationId: ID! lendingStationId: ID!
cargoBikeId: ID! cargoBikeId: ID!
@ -962,8 +974,7 @@ export default gql`
input TimeFrameUpdateInput { input TimeFrameUpdateInput {
id: ID! id: ID!
from: Date dateRange: DateRangeInput
to: Date
note: String note: String
lendingStationId: ID lendingStationId: ID
cargoBikeId: ID cargoBikeId: ID

Loading…
Cancel
Save