feat(places): phone, e-mail and opening hours by hand when the search has none (#2472, #2356)

This commit is contained in:
Maurice
2026-10-01 13:42:44 +02:00
parent 8c37597bd7
commit d61d8ca9d7
45 changed files with 726 additions and 22 deletions
+33
View File
@@ -0,0 +1,33 @@
import { describe, expect, it } from 'vitest';
import { hasPlaceHours, parsePlaceHours, placeEmailField, placeOpeningHoursField } from './place-hours';
import { placeCreateRequestSchema, placeUpdateRequestSchema } from './place.schema';
const WEEK = '[{"closed":false,"open":"09:00","close":"17:00"},{"closed":false},{"closed":false},{"closed":false},{"closed":false},{"closed":true},{"closed":true}]';
describe('place hours and e-mail (#2472)', () => {
it('parses seven days and refuses anything else', () => {
expect(parsePlaceHours(WEEK)).toHaveLength(7);
expect(parsePlaceHours('[]')).toBeNull();
expect(parsePlaceHours('{nope')).toBeNull();
expect(parsePlaceHours('[{"closed":false,"open":"25:00"},{},{},{},{},{},{}]')).toBeNull();
expect(parsePlaceHours('')).toBeNull();
expect(parsePlaceHours(null)).toBeNull();
});
it('tells a week that says something from an empty one', () => {
expect(hasPlaceHours(parsePlaceHours(WEEK))).toBe(true);
expect(hasPlaceHours(Array.from({ length: 7 }, () => ({ closed: false })))).toBe(false);
expect(hasPlaceHours(null)).toBe(false);
});
it('validates both fields on the request bodies, empty meaning clear', () => {
expect(placeOpeningHoursField.safeParse(WEEK).success).toBe(true);
expect(placeOpeningHoursField.safeParse('').success).toBe(true);
expect(placeOpeningHoursField.safeParse('[1,2]').success).toBe(false);
expect(placeEmailField.safeParse('a@b.co').success).toBe(true);
expect(placeEmailField.safeParse('').success).toBe(true);
expect(placeEmailField.safeParse('nope').success).toBe(false);
expect(placeCreateRequestSchema.safeParse({ name: 'X', email: 'nope' }).success).toBe(false);
expect(placeUpdateRequestSchema.safeParse({ opening_hours: WEEK, email: null }).success).toBe(true);
});
});
+58
View File
@@ -0,0 +1,58 @@
import { z } from 'zod';
/**
* A place's own opening hours and e-mail, typed in by hand (#2472). The search
* fills phone and website when it knows them; when it does not, or for a place
* nobody has listed, these are how the traveller adds what they found out.
*
* Hours are stored as JSON text on the place: seven days, Monday first, the
* same order Google's weekday lines come in, so the inspector shows either the
* same way.
*/
export const PLACE_HOURS_DAYS = 7;
const TIME = /^([01]\d|2[0-3]):[0-5]\d$/;
export const placeHoursDaySchema = z.object({
closed: z.boolean(),
open: z.string().regex(TIME).optional(),
close: z.string().regex(TIME).optional(),
});
export type PlaceHoursDay = z.infer<typeof placeHoursDaySchema>;
export const placeOpeningHoursSchema = z.array(placeHoursDaySchema).length(PLACE_HOURS_DAYS);
export type PlaceOpeningHours = z.infer<typeof placeOpeningHoursSchema>;
/** The stored text as seven days, or null when it is empty or not hours at all. */
export function parsePlaceHours(raw: unknown): PlaceOpeningHours | null {
if (typeof raw !== 'string' || raw.trim() === '') return null;
try {
const parsed = placeOpeningHoursSchema.safeParse(JSON.parse(raw));
return parsed.success ? parsed.data : null;
} catch {
return null;
}
}
/** Whether a week says anything: a day that is closed or has a time. */
export function hasPlaceHours(hours: PlaceOpeningHours | null): boolean {
return !!hours && hours.some(d => d.closed || !!d.open || !!d.close);
}
/** The request field: the JSON text, empty or null to clear it. */
export const placeOpeningHoursField = z
.string()
.max(1000)
.refine(v => v === '' || parsePlaceHours(v) !== null, { message: 'opening_hours must be seven days, Monday first' })
.nullable()
.optional();
const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
/** An e-mail address, or empty to clear it. */
export const placeEmailField = z
.string()
.max(254)
.refine(v => v === '' || EMAIL.test(v.trim()), { message: 'email must be an e-mail address' })
.nullable()
.optional();
+9
View File
@@ -1,5 +1,6 @@
import { tagSchema } from '../tag/tag.schema';
import { PLACE_WEBSITE_MAX_LENGTH, normalizePlaceWebsite } from './place-website';
import { placeEmailField, placeOpeningHoursField } from './place-hours';
import { z } from 'zod';
@@ -178,6 +179,10 @@ export const placeSchema = z.object({
fill_percent: fillPercentSchema,
website: z.string().nullable().optional(),
phone: z.string().nullable().optional(),
/** Typed in by hand (#2472). */
email: z.string().nullable().optional(),
/** Hand-kept hours as JSON text, seven days Monday first (#2472); see place-hours.ts. */
opening_hours: z.string().nullable().optional(),
transport_mode: z.string().nullable().optional(),
created_at: z.string().optional(),
updated_at: z.string().optional(),
@@ -251,6 +256,10 @@ export type AssignmentPlace = z.infer<typeof assignmentPlaceSchema>;
* service already reads it that way rather than as "leave alone".
*/
const stopTypeField = z.object({
// Named on the open body like the two below: an e-mail and the hand-kept hours have a
// fixed shape, and a malformed one would otherwise sit on the place unseen (#2472).
email: placeEmailField,
opening_hours: placeOpeningHoursField,
stop_type: roadtripStopTypeSchema.nullable().optional(),
// Named for the same reason: a bounded vocabulary on an otherwise open body. Zero is
// outside it on purpose — a stop that fills nothing is not a stop, and letting one