Skip to content

Commit

Permalink
chore: Improve workTimes typings
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnns committed Feb 21, 2023
1 parent 0dd1c22 commit e378cac
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 91 deletions.
7 changes: 6 additions & 1 deletion src/lib/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { faker } from "@faker-js/faker";
import { IsoDate, IsoUtcDateTime } from "../models/dateTime.js";

export const ONE_DAY = 24 * 60 * 60 * 1000;
export const ONE_YEAR = 356 * ONE_DAY;
Expand All @@ -10,7 +11,11 @@ export const isoDateFromDateTime = (dateTime: Date) => {
dateTime.getFullYear(),
String(dateTime.getMonth() + 1).padStart(2, "0"),
String(dateTime.getDate()).padStart(2, "0"),
].join("-");
].join("-") as IsoDate;
};

export const isoUtcDateTimeFromDateTime = (dateTime: Date) => {
return dateTime.toJSON().replace(/\.\d{3}Z$/, "Z") as IsoUtcDateTime;
};

export const isoMonthFromDateTime = (dateTime: Date) => {
Expand Down
116 changes: 58 additions & 58 deletions src/models/__snapshots__/workTimes.mocks.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ Array [
Object {
"changes": Array [
Object {
"timeSince": "2020-03-24T15:24:20.000Z",
"timeUntil": "2020-03-24T16:25:46.000Z",
"type": 1,
"timeSince": "2020-03-24T15:24:20Z",
"timeUntil": "2020-03-24T16:25:46Z",
"type": 2,
},
Object {
"timeSince": "2020-03-24T17:28:08.000Z",
"timeUntil": "2020-03-24T17:43:43.000Z",
"type": 1,
"timeSince": "2020-03-24T17:28:08Z",
"timeUntil": "2020-03-24T17:43:43Z",
"type": 2,
},
Object {
"timeSince": "2020-03-24T18:43:19.000Z",
"timeUntil": "2020-03-24T22:24:41.000Z",
"type": 1,
"timeSince": "2020-03-24T18:43:19Z",
"timeUntil": "2020-03-24T22:24:41Z",
"type": 2,
},
],
"date": "2020-03-24",
Expand All @@ -27,19 +27,19 @@ Array [
Object {
"changes": Array [
Object {
"timeSince": "2020-04-14T12:05:31.000Z",
"timeUntil": "2020-04-14T15:35:44.000Z",
"type": 0,
"timeSince": "2020-04-14T12:05:31Z",
"timeUntil": "2020-04-14T15:35:44Z",
"type": 1,
},
Object {
"timeSince": "2020-04-14T16:43:44.000Z",
"timeUntil": "2020-04-14T18:10:26.000Z",
"type": 1,
"timeSince": "2020-04-14T16:43:44Z",
"timeUntil": "2020-04-14T18:10:26Z",
"type": 2,
},
Object {
"timeSince": "2020-04-14T18:17:50.000Z",
"timeUntil": "2020-04-14T19:31:25.000Z",
"type": 0,
"timeSince": "2020-04-14T18:17:50Z",
"timeUntil": "2020-04-14T19:31:25Z",
"type": 1,
},
],
"date": "2020-04-14",
Expand All @@ -49,14 +49,14 @@ Array [
Object {
"changes": Array [
Object {
"timeSince": "2020-06-05T13:59:37.000Z",
"timeUntil": "2020-06-05T14:13:14.000Z",
"type": 1,
"timeSince": "2020-06-05T13:59:37Z",
"timeUntil": "2020-06-05T14:13:14Z",
"type": 2,
},
Object {
"timeSince": "2020-06-05T14:39:21.000Z",
"timeUntil": "2020-06-05T19:05:46.000Z",
"type": 1,
"timeSince": "2020-06-05T14:39:21Z",
"timeUntil": "2020-06-05T19:05:46Z",
"type": 2,
},
],
"date": "2020-06-05",
Expand All @@ -66,14 +66,14 @@ Array [
Object {
"changes": Array [
Object {
"timeSince": "2020-09-11T12:29:09.000Z",
"timeUntil": "2020-09-11T15:04:18.000Z",
"type": 0,
"timeSince": "2020-09-11T12:29:09Z",
"timeUntil": "2020-09-11T15:04:18Z",
"type": 1,
},
Object {
"timeSince": "2020-09-11T21:25:45.000Z",
"timeUntil": "2020-09-11T21:41:56.000Z",
"type": 0,
"timeSince": "2020-09-11T21:25:45Z",
"timeUntil": "2020-09-11T21:41:56Z",
"type": 1,
},
],
"date": "2020-09-11",
Expand All @@ -83,14 +83,14 @@ Array [
Object {
"changes": Array [
Object {
"timeSince": "2020-09-17T13:19:29.000Z",
"timeUntil": "2020-09-17T15:40:51.000Z",
"type": 1,
"timeSince": "2020-09-17T13:19:29Z",
"timeUntil": "2020-09-17T15:40:51Z",
"type": 2,
},
Object {
"timeSince": "2020-09-17T18:34:06.000Z",
"timeUntil": "2020-09-17T21:15:06.000Z",
"type": 1,
"timeSince": "2020-09-17T18:34:06Z",
"timeUntil": "2020-09-17T21:15:06Z",
"type": 2,
},
],
"date": "2020-09-17",
Expand All @@ -106,16 +106,16 @@ Array [
"date": "2020-03-24",
"intervals": Array [
Object {
"timeSince": "2020-03-24T15:24:20.000Z",
"timeUntil": "2020-03-24T16:25:46.000Z",
"timeSince": "2020-03-24T15:24:20Z",
"timeUntil": "2020-03-24T16:25:46Z",
},
Object {
"timeSince": "2020-03-24T17:28:08.000Z",
"timeUntil": "2020-03-24T17:43:43.000Z",
"timeSince": "2020-03-24T17:28:08Z",
"timeUntil": "2020-03-24T17:43:43Z",
},
Object {
"timeSince": "2020-03-24T18:43:19.000Z",
"timeUntil": "2020-03-24T22:24:41.000Z",
"timeSince": "2020-03-24T18:43:19Z",
"timeUntil": "2020-03-24T22:24:41Z",
},
],
"offset": 0,
Expand All @@ -125,12 +125,12 @@ Array [
"date": "2020-04-14",
"intervals": Array [
Object {
"timeSince": "2020-04-14T12:05:31.000Z",
"timeUntil": "2020-04-14T13:03:41.000Z",
"timeSince": "2020-04-14T12:05:31Z",
"timeUntil": "2020-04-14T13:03:41Z",
},
Object {
"timeSince": "2020-04-14T15:35:44.000Z",
"timeUntil": "2020-04-14T18:10:26.000Z",
"timeSince": "2020-04-14T15:35:44Z",
"timeUntil": "2020-04-14T18:10:26Z",
},
],
"offset": 0,
Expand All @@ -140,16 +140,16 @@ Array [
"date": "2020-06-05",
"intervals": Array [
Object {
"timeSince": "2020-06-05T13:50:29.000Z",
"timeUntil": "2020-06-05T14:13:14.000Z",
"timeSince": "2020-06-05T13:50:29Z",
"timeUntil": "2020-06-05T14:13:14Z",
},
Object {
"timeSince": "2020-06-05T14:39:21.000Z",
"timeUntil": "2020-06-05T14:43:25.000Z",
"timeSince": "2020-06-05T14:39:21Z",
"timeUntil": "2020-06-05T14:43:25Z",
},
Object {
"timeSince": "2020-06-05T16:25:38.000Z",
"timeUntil": "2020-06-05T19:44:36.000Z",
"timeSince": "2020-06-05T16:25:38Z",
"timeUntil": "2020-06-05T19:44:36Z",
},
],
"offset": 0,
Expand All @@ -159,12 +159,12 @@ Array [
"date": "2020-09-11",
"intervals": Array [
Object {
"timeSince": "2020-09-11T10:25:10.000Z",
"timeUntil": "2020-09-11T11:46:26.000Z",
"timeSince": "2020-09-11T10:25:10Z",
"timeUntil": "2020-09-11T11:46:26Z",
},
Object {
"timeSince": "2020-09-11T16:09:25.000Z",
"timeUntil": "2020-09-11T22:37:45.000Z",
"timeSince": "2020-09-11T16:09:25Z",
"timeUntil": "2020-09-11T22:37:45Z",
},
],
"offset": 0,
Expand All @@ -174,8 +174,8 @@ Array [
"date": "2020-09-17",
"intervals": Array [
Object {
"timeSince": "2020-09-17T14:53:25.000Z",
"timeUntil": "2020-09-17T19:53:41.000Z",
"timeSince": "2020-09-17T14:53:25Z",
"timeUntil": "2020-09-17T19:53:41Z",
},
],
"offset": 0,
Expand Down
4 changes: 2 additions & 2 deletions src/models/workTimes.mocks.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
createChangeRequestMocks,
createWorkTimeChangeRequestMocks,
createWorkTimeDayMocks,
} from "./workTimes.mocks.js";

Expand All @@ -14,7 +14,7 @@ describe("workTimesMocks", () => {

test("createChangeRequestMocks", () => {
expect(
createChangeRequestMocks({
createWorkTimeChangeRequestMocks({
count: 5,
})
).toMatchSnapshot();
Expand Down
33 changes: 18 additions & 15 deletions src/models/workTimes.mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import {
generateRandomDates,
generateWithMaxDuplicates,
isoDateFromDateTime,
isoUtcDateTimeFromDateTime,
startOfDay,
} from "../lib/mocks.js";
import {
ChangeRequest,
ChangeRequestTimeInterval,
ChangeRequestTimeIntervalType,
WorkTimeChangeRequest,
WorkTimeChangeRequestInterval,
WorkTimeChangeRequestIntervalType,
WorkTimeDay,
WorkTimeDayTimeInterval,
WorkTimeDayInterval,
} from "./workTimes.js";

const DEFAULT_FROM = new Date(2020, 0);
Expand All @@ -27,7 +28,7 @@ const generateIntervals = ({
}: {
count?: number;
date: Date;
}): Array<WorkTimeDayTimeInterval> => {
}): Array<WorkTimeDayInterval> => {
const pointsInTime = generateWithMaxDuplicates({
// Wir stellen sicher, dass immer ein Start- und ein Endpunkt existieren
count: count * 2,
Expand All @@ -50,15 +51,17 @@ const generateIntervals = ({
},
}).sort();

return pointsInTime.reduce<Array<WorkTimeDayTimeInterval>>(
return pointsInTime.reduce<Array<WorkTimeDayInterval>>(
(acc, pointInTime, index) => {
// Endpunkte werden nicht selbst verarbeitet, sondern wurden bereits
// in der Iteration zuvor verwendet
if (index % 2 === 1) return acc;

const interval: WorkTimeDayTimeInterval = {
timeSince: new Date(pointInTime).toISOString(),
timeUntil: new Date(pointsInTime[index + 1]).toISOString(),
const interval: WorkTimeDayInterval = {
timeSince: isoUtcDateTimeFromDateTime(new Date(pointInTime)),
timeUntil: isoUtcDateTimeFromDateTime(
new Date(pointsInTime[index + 1])
),
};

return [...acc, interval];
Expand Down Expand Up @@ -106,14 +109,14 @@ export const createWorkTimeDayMocks = ({
const generateChangeRequestChanges = ({
count = 1,
date = DEFAULT_FROM,
}): Array<ChangeRequestTimeInterval> => {
}): Array<WorkTimeChangeRequestInterval> => {
return generateIntervals({ count, date }).map((interval, index) => {
return {
...interval,
timeUntil: interval.timeUntil!,
type: faker.datatype.boolean()
? ChangeRequestTimeIntervalType.Add
: ChangeRequestTimeIntervalType.Remove,
? WorkTimeChangeRequestIntervalType.Add
: WorkTimeChangeRequestIntervalType.Remove,
};
});
};
Expand All @@ -124,7 +127,7 @@ const createChangeRequest = ({
}: {
date: Date;
id: number;
}): ChangeRequest => {
}): WorkTimeChangeRequest => {
const changes = generateChangeRequestChanges({
count: faker.datatype.number({ min: 1, max: 4 }),
date,
Expand All @@ -138,7 +141,7 @@ const createChangeRequest = ({
};
};

export const createChangeRequestMocks = ({
export const createWorkTimeChangeRequestMocks = ({
count = 1,
dateBetween: [from, to] = [DEFAULT_FROM, DEFAULT_TO],
}: {
Expand All @@ -150,7 +153,7 @@ export const createChangeRequestMocks = ({
return generateRandomDates({
count,
between: [from, to],
}).map((timestamp): ChangeRequest => {
}).map((timestamp): WorkTimeChangeRequest => {
id = id + 1;
const date = startOfDay(new Date(timestamp));

Expand Down
32 changes: 17 additions & 15 deletions src/models/workTimes.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import { IsoDate, IsoUtcDateTime } from "./dateTime.js";

export type WorkTimeDay = {
date: string;
date: IsoDate;
usersId: number;
intervals: Array<WorkTimeDayTimeInterval>;
intervals: Array<WorkTimeDayInterval>;
offset: number;
};

export type WorkTimeDayTimeInterval = {
timeSince: string;
timeUntil: null | string;
export type WorkTimeDayInterval = {
timeSince: IsoUtcDateTime;
timeUntil: null | IsoUtcDateTime;
};

export type ChangeRequest = {
export type WorkTimeChangeRequest = {
id: number;
date: string;
date: IsoDate;
usersId: number;
changes: Array<ChangeRequestTimeInterval>;
changes: Array<WorkTimeChangeRequestInterval>;
};

export type ChangeRequestTimeInterval = {
type: ChangeRequestTimeIntervalType;
timeSince: string;
timeUntil: string;
export type WorkTimeChangeRequestInterval = {
type: WorkTimeChangeRequestIntervalType;
timeSince: IsoUtcDateTime;
timeUntil: IsoUtcDateTime;
};

export enum ChangeRequestTimeIntervalType {
Add,
Remove,
export enum WorkTimeChangeRequestIntervalType {
Add = 1,
Remove = 2,
}

0 comments on commit e378cac

Please sign in to comment.