feat: complete minute birth-time rectification flow

This commit is contained in:
Jesse_Chen
2026-07-25 01:15:48 +08:00
parent f90aba217f
commit 3ca30ed7de
93 changed files with 8347 additions and 1646 deletions
@@ -1,5 +1,6 @@
import { NextResponse } from "next/server";
import { chinaLocations } from "@/data/china-locations";
import { finiteBirthNumber, resolveMissingBirthTimezoneOffset } from "@/lib/birth-profile-timezone";
type Profile = {
date?: string;
@@ -7,24 +8,32 @@ type Profile = {
provinceCode?: string;
cityCode?: string;
districtCode?: string;
latitude?: number | null;
longitude?: number | null;
timezoneId?: string;
timezoneOffset?: number | null;
};
const jyotishApiBase = process.env.JYOTISH_API_BASE ?? "http://127.0.0.1:5200";
function payloadFromProfile(profile: Profile) {
export async function payloadFromProfile(profile: Profile) {
if (!profile.date || !profile.time) return null;
const resolved = await resolveMissingBirthTimezoneOffset(profile, { preferredTime: profile.time }) as Profile;
const province = chinaLocations.country.provinces.find((item) => item.code === profile.provinceCode);
const city = province?.cities.find((item) => item.code === profile.cityCode);
const district = city?.districts.find((item) => item.code === profile.districtCode);
const location = district ?? city;
if (!location) return null;
const lat = finiteBirthNumber(resolved.latitude) ?? location?.center[1] ?? null;
const lon = finiteBirthNumber(resolved.longitude) ?? location?.center[0] ?? null;
const tz = finiteBirthNumber(resolved.timezoneOffset) ?? (location ? chinaLocations.country.timezone : null);
if (lat === null || lon === null || tz === null) return null;
return {
birth_time: `${profile.date} ${profile.time}`,
uncertainty_minutes: 30,
step_minutes: 2,
lat: location.center[1],
lon: location.center[0],
tz: chinaLocations.country.timezone,
lat,
lon,
tz,
};
}
@@ -48,7 +57,7 @@ async function fetchQuestionnaire(payload: Record<string, unknown>) {
export async function POST(request: Request) {
const body = await request.json().catch(() => null) as { profile?: Profile } | null;
const payload = payloadFromProfile(body?.profile ?? {});
const payload = await payloadFromProfile(body?.profile ?? {}).catch(() => null);
if (!payload) {
return NextResponse.json({
status: "blocked",