Skip to content

Commit 036b822

Browse files
authored
chore: minor code cleanup and internal improvements (#142)
1 parent 722efef commit 036b822

File tree

8 files changed

+58
-59
lines changed

8 files changed

+58
-59
lines changed

src/lib/import/airtrail.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const AirTrailFile = z.object({
3636
.string()
3737
.min(3, { message: 'Username must be at least 3 characters long' })
3838
.max(20, { message: 'Username must be at most 20 characters long' })
39-
.regex(/^[a-zA-Z0-9_]+$/, {
39+
.regex(/^\w+$/, {
4040
message:
4141
'Username can only contain letters, numbers, and underscores',
4242
}),

src/lib/import/aita.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const AITA_SEAT_CLASS_MAP: Record<string, (typeof SeatClasses)[number]> = {
1818

1919
export const processAITAFile = (input: string, options: PlatformOptions) => {
2020
const tripPattern =
21-
/^Ownership\.([\w]+);[^\r\n]*\sflights:\s([\s\S]+?)\shotels:/gm;
21+
/^Ownership\.(\w+);[^\r\n]*\sflights:\s([\s\S]+?)\shotels:/gm;
2222
const flightPattern =
2323
/^([^;\n\r]*);(\w*);.*?;(\w{2,4};\d{2,4});(\w*);(\w{3});(\w{3});([\d\-T:]+);([\d\-T:]+);([\d\-T:]+);([\d\-T:]+);(.*)/gm;
2424

src/lib/import/jetlog.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,27 @@ const JETLOG_FLIGHT_CLASS_MAP: Record<string, Seat['seatClass']> = {
1919
};
2020

2121
const nullTransformer = (v: string) => (v === '' ? null : v);
22+
const dateRegex = /^\d{4}-\d{2}-\d{2}$/;
23+
const optionalTimePrimitive = z
24+
.string()
25+
.refine((v) => v === '' || v.match(/^\d{2}:\d{2}$/), {
26+
message: 'Invalid time format',
27+
})
28+
.transform(nullTransformer);
29+
const optionalDatePrimitive = z
30+
.string()
31+
.refine((v) => v === '' || v.match(dateRegex), {
32+
message: 'Invalid date format',
33+
})
34+
.transform(nullTransformer);
2235

2336
const JetLogFlight = z.object({
24-
date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/),
37+
date: z.string().regex(dateRegex),
2538
origin: z.string(),
2639
destination: z.string(),
27-
departure_time: z
28-
.string()
29-
.regex(/^\d{2}:\d{2}$|/)
30-
.transform(nullTransformer),
31-
arrival_time: z
32-
.string()
33-
.regex(/^\d{2}:\d{2}$|/)
34-
.transform(nullTransformer),
35-
arrival_date: z
36-
.string()
37-
.regex(/^\d{4}-\d{2}-\d{2}$|/)
38-
.transform(nullTransformer),
40+
departure_time: optionalTimePrimitive,
41+
arrival_time: optionalTimePrimitive,
42+
arrival_date: optionalDatePrimitive,
3943
seat: z.enum(['window', 'middle', 'aisle', '']).transform(nullTransformer),
4044
ticket_class: z.string().transform(nullTransformer),
4145
duration: z.string().transform(nullTransformer),

src/lib/server/utils/oauth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export const getOAuthClient = async () => {
5454

5555
const { enabled, clientId, clientSecret, issuerUrl } = config.oauth;
5656
if (!enabled || !clientId || !clientSecret || !issuerUrl) {
57-
throw new Error('OAuth is not enabled');
57+
throw new Error('OAuth is not enabled or configured properly');
5858
}
5959

6060
try {

src/lib/utils/data/data.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,20 @@ export const formatSeat = (f: FlightData) => {
217217
const s = f.seats.find((seat) => seat.userId === userId);
218218
if (!s) return null;
219219

220-
return s.seat && s.seatNumber && s.seatClass
221-
? `${t(s.seatClass)} (${s.seat} ${s.seatNumber})`
222-
: s.seat && s.seatNumber
223-
? `${s.seat} ${s.seatNumber}`
224-
: s.seat && s.seatClass
225-
? `${t(s.seatClass)} (${s.seat})`
226-
: s.seatClass
227-
? t(s.seatClass)
228-
: s.seat
229-
? t(s.seat)
230-
: null;
220+
if (s.seat && s.seatNumber && s.seatClass) {
221+
return `${t(s.seatClass)} (${s.seat} ${s.seatNumber})`;
222+
}
223+
if (s.seat && s.seatNumber) {
224+
return `${s.seat} ${s.seatNumber}`;
225+
}
226+
if (s.seat && s.seatClass) {
227+
return `${t(s.seatClass)} (${s.seat})`;
228+
}
229+
if (s.seatClass) {
230+
return t(s.seatClass);
231+
}
232+
if (s.seat) {
233+
return t(s.seat);
234+
}
235+
return null;
231236
};

src/lib/zod/flight.ts

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,25 @@ import { z } from 'zod';
22

33
import { FlightReasons, SeatClasses, SeatTypes } from '$lib/db/types';
44

5-
// |^$ is for empty string in the case where the user deletes the input
6-
const regex24h =
7-
/^([01]?[0-9]|2[0-3])(?::|\.|)[0-5][0-9](?:\s?(?:am|pm))?$|^$/i;
5+
const regex24h = /^([01]?\d|2[0-3])(?::|\.|)[0-5]\d(?:\s?(?:am|pm))?$/i;
86
const regex12hLike = /^\d{1,2}(?::|\.|)\d{2}\s?(?:am|pm)$/i;
9-
const regex12h = /^([1-9]|1[0-2])(?::|\.|)[0-5][0-9]\s?(?:am|pm)$/i;
7+
const regex12h = /^([1-9]|1[0-2])(?::|\.|)[0-5]\d\s?(?:am|pm)$/i;
8+
9+
const timePrimitive = z
10+
.string()
11+
.refine((value) => {
12+
// Always allow empty string (to allow the user to delete the input)
13+
if (value === '') return true;
14+
15+
return regex24h.test(value);
16+
}, 'Invalid 24-hour format')
17+
.refine((value) => {
18+
// Skip 12-hour check if empty or not possibly 12-hour format (caught by the previous refine)
19+
if (value === '' || !regex12hLike.test(value)) return true;
20+
21+
return regex12h.test(value);
22+
}, 'Invalid 12-hour format')
23+
.nullable();
1024

1125
export const flightAirportsSchema = z.object({
1226
from: z.string().min(1, 'Select an origin'),
@@ -19,30 +33,12 @@ export const flightDateTimeSchema = z.object({
1933
.datetime({ offset: true, message: 'Select a departure date' })
2034
.nullable()
2135
.refine((value) => value !== null, 'Select a departure date'),
22-
departureTime: z
23-
.string()
24-
.refine((value) => regex24h.test(value), 'Invalid 24-hour format')
25-
.refine((value) => {
26-
if (regex12hLike.test(value)) {
27-
return regex12h.test(value);
28-
}
29-
return true; // If it's not in 12-hour format, just return true (it'll be caught by the previous refine)
30-
}, 'Invalid 12-hour format')
31-
.nullable(),
36+
departureTime: timePrimitive,
3237
arrival: z
3338
.string()
3439
.datetime({ offset: true, message: 'Select an arrival date' })
3540
.nullable(),
36-
arrivalTime: z
37-
.string()
38-
.refine((value) => regex24h.test(value), 'Invalid 24-hour format')
39-
.refine((value) => {
40-
if (regex12hLike.test(value)) {
41-
return regex12h.test(value);
42-
}
43-
return true; // If it's not in 12-hour format, just return true (it'll be caught by the previous refine)
44-
}, 'Invalid 12-hour format')
45-
.nullable(),
41+
arrivalTime: timePrimitive,
4642
});
4743

4844
export const flightSeatInformationSchema = z.object({

src/lib/zod/user.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const userSchema = z.object({
55
.string()
66
.min(3, { message: 'Username must be at least 3 characters long' })
77
.max(20, { message: 'Username must be at most 20 characters long' })
8-
.regex(/^[a-zA-Z0-9_]+$/, {
8+
.regex(/^\w+$/, {
99
message: 'Username can only contain letters, numbers, and underscores',
1010
}),
1111
password: z.string().min(8),

src/routes/api/flight/save/+server.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import type { TZDate } from '@date-fns/tz';
2-
import {
3-
differenceInSeconds,
4-
format,
5-
formatISO,
6-
isBefore,
7-
isValid,
8-
} from 'date-fns';
2+
import { differenceInSeconds, format, formatISO, isBefore } from 'date-fns';
93
import { actionResult, setError, superValidate } from 'sveltekit-superforms';
104
import { zod } from 'sveltekit-superforms/adapters';
115

0 commit comments

Comments
 (0)