Files
Jyotisha/frontend/src/lib/server-owned-birth-profile.ts
T
jesse-ux b85c4a686a
Independent Staging Quality Gate / validate (push) Successful in 13m27s
Independent Staging Quality Gate / publish (push) Failing after 1h0m1s
fix(rectification): anchor candidate windows to civil dates across midnight
Carry explicit local date intervals instead of inferring the day from clock
order. Cluster width, delivery, adoption, and reports keep the actual civil
date; adopted date is stored separately from the reported birth_date.

Algorithm identity is scoring-9 / spec-v5. Scoring weights, confirmation
thresholds, and Skill version are unchanged. Isolated Linux final-3 gates
passed; four pre-existing Python failures remain. This is not a production
release.
2026-09-21 02:55:00 +08:00

128 lines
5.3 KiB
TypeScript

import type { GlobalBirthProfile } from "./global-birth-payloads.ts";
import { resolveAyanamsa } from "./ayanamsa.ts";
import { effectiveBirthDate } from "./effective-birth-date.ts";
const usableActiveStatuses = new Set(["accepted", "confirmed"]);
export const ACCOUNT_BIRTH_SELECT =
"name,birth_date,active_birth_date,active_birth_timezone_offset,active_birth_provenance,reported_birth_time,active_birth_time,birth_time,birth_time_status,birth_time_source,country_code,province_code,city_code,district_code,latitude,longitude,timezone_offset,timezone_id,ayanamsa,birth_place_label,uncertainty_before_minutes,uncertainty_after_minutes,declared_window_start,declared_window_end" as const;
export type AccountBirthRow = Readonly<{
name?: unknown;
birth_date?: unknown;
active_birth_date?: unknown;
active_birth_timezone_offset?: unknown;
active_birth_provenance?: unknown;
reported_birth_time?: unknown;
active_birth_time?: unknown;
birth_time?: unknown;
birth_time_status?: unknown;
birth_time_source?: unknown;
birth_place_label?: unknown;
uncertainty_before_minutes?: unknown;
uncertainty_after_minutes?: unknown;
declared_window_start?: unknown;
declared_window_end?: unknown;
country_code?: unknown;
province_code?: unknown;
city_code?: unknown;
district_code?: unknown;
latitude?: unknown;
longitude?: unknown;
timezone_offset?: unknown;
timezone_id?: unknown;
ayanamsa?: unknown;
}>;
function asAccountBirthRow(row: unknown): AccountBirthRow {
if (!row || typeof row !== "object" || Array.isArray(row)) return {};
return row as AccountBirthRow;
}
function text(value: unknown): string | undefined {
if (typeof value !== "string") return undefined;
const trimmed = value.trim();
return trimmed.length > 0 ? trimmed : undefined;
}
function clock(value: unknown): string | undefined {
const raw = text(value);
const match = raw ? /^(\d{1,2}):(\d{2})/.exec(raw) : null;
if (!match) return undefined;
const hour = Number.parseInt(match[1], 10);
const minute = Number.parseInt(match[2], 10);
if (hour < 0 || hour > 23 || minute < 0 || minute > 59) return undefined;
return `${String(hour).padStart(2, "0")}:${String(minute).padStart(2, "0")}`;
}
function finiteNumber(value: unknown): number | undefined {
if (typeof value === "number" && Number.isFinite(value)) return value;
if (typeof value === "string" && value.trim()) {
const parsed = Number(value);
return Number.isFinite(parsed) ? parsed : undefined;
}
return undefined;
}
function selectedClock(row: AccountBirthRow): string | undefined {
const status = text(row.birth_time_status) ?? "";
const active = clock(row.active_birth_time) ?? clock(row.birth_time);
const reported = clock(row.reported_birth_time);
if (usableActiveStatuses.has(status) && active) return active;
return reported ?? active;
}
export function globalBirthProfileFromAccountRow(row: unknown): GlobalBirthProfile & {
birthTimeStatus?: string;
} {
const accountRow = asAccountBirthRow(row);
if (usableActiveStatuses.has(text(accountRow.birth_time_status) ?? "")
&& accountRow.active_birth_date != null
&& finiteNumber(accountRow.active_birth_timezone_offset) === undefined) {
throw new Error("adopted_birth_calculation_incomplete");
}
return {
name: text(accountRow.name),
date: effectiveBirthDate(accountRow),
time: selectedClock(accountRow),
countryCode: text(accountRow.country_code),
provinceCode: text(accountRow.province_code),
cityCode: text(accountRow.city_code),
districtCode: text(accountRow.district_code),
latitude: finiteNumber(accountRow.latitude) ?? null,
longitude: finiteNumber(accountRow.longitude) ?? null,
timezoneOffset: usableActiveStatuses.has(text(accountRow.birth_time_status) ?? "")
? finiteNumber(accountRow.active_birth_timezone_offset) ?? finiteNumber(accountRow.timezone_offset) ?? null
: finiteNumber(accountRow.timezone_offset) ?? null,
timezoneId: text(accountRow.timezone_id),
ayanamsa: resolveAyanamsa(accountRow),
birthTimeStatus: text(accountRow.birth_time_status),
};
}
export function globalBirthProfileFromStoredChart(value: unknown): GlobalBirthProfile | null {
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
const record = value as Record<string, unknown>;
const profile = globalBirthProfileFromAccountRow({
name: record.name,
birth_date: record.date ?? record.birth_date,
active_birth_date: record.activeDate ?? record.active_birth_date,
active_birth_timezone_offset: record.activeTimezoneOffset ?? record.active_birth_timezone_offset,
reported_birth_time: record.reportedTime ?? record.reported_birth_time,
active_birth_time: record.time ?? record.active_birth_time,
birth_time: record.birth_time,
birth_time_status: record.birthTimeStatus ?? record.birth_time_status,
country_code: record.countryCode ?? record.country_code,
province_code: record.provinceCode ?? record.province_code,
city_code: record.cityCode ?? record.city_code,
district_code: record.districtCode ?? record.district_code,
latitude: record.latitude,
longitude: record.longitude,
timezone_offset: record.timezoneOffset ?? record.timezone_offset,
timezone_id: record.timezoneId ?? record.timezone_id,
ayanamsa: record.ayanamsa,
});
if (!profile.date || !profile.time) return null;
return profile;
}