89 lines
3.1 KiB
TypeScript
89 lines
3.1 KiB
TypeScript
import { z } from 'zod';
|
|
import { createDatasetV2 } from '@stock-bot/research/datasets';
|
|
import type { CanonicalDatasetPreparation } from '@stock-bot/stock-config/canonical-research';
|
|
import type { HistoricalDatasetInputV2, HistoricalDatasetV2 } from '@stock-bot/types';
|
|
import {
|
|
retainResearchDataset,
|
|
type RetainedDatasetStore,
|
|
} from '../../../orchestrator/src/data/retained-research-dataset';
|
|
import { canonicalDatasetUnavailableReason } from './canonical-research.service';
|
|
|
|
export class DatasetPreparationError extends Error {
|
|
constructor(
|
|
readonly status: 400 | 409,
|
|
message: string
|
|
) {
|
|
super(message);
|
|
}
|
|
}
|
|
|
|
const previewBody = z.object({ input: z.unknown() }).strict();
|
|
const retainBody = previewBody
|
|
.extend({ expectedHash: z.string().regex(/^[a-f0-9]{64}$/) })
|
|
.strict();
|
|
|
|
function normalize(input: unknown): HistoricalDatasetV2 {
|
|
try {
|
|
// createDatasetV2 performs the complete strict runtime parse; no metadata is inferred here.
|
|
return createDatasetV2(input as HistoricalDatasetInputV2);
|
|
} catch (error) {
|
|
if (error instanceof z.ZodError) {
|
|
const issue = error.issues[0];
|
|
throw new DatasetPreparationError(
|
|
400,
|
|
`Invalid source dataset at ${issue?.path.join('.') || 'input'} (${issue?.code ?? 'invalid'})`
|
|
);
|
|
}
|
|
// The normalizer's ordinary Error messages describe invariant failures without source values.
|
|
// Platform errors (for example an invalid Intl time zone) may contain submitted values.
|
|
throw new DatasetPreparationError(
|
|
400,
|
|
error instanceof Error && error.constructor === Error
|
|
? `Invalid source dataset: ${error.message}`
|
|
: 'Source dataset requires valid complete v2 metadata and bars'
|
|
);
|
|
}
|
|
}
|
|
|
|
function describe(dataset: HistoricalDatasetV2): CanonicalDatasetPreparation {
|
|
const reason = canonicalDatasetUnavailableReason(dataset);
|
|
return {
|
|
version: 'canonical-dataset-preparation/v1',
|
|
hash: dataset.manifest.hash,
|
|
manifest: dataset.manifest,
|
|
available: reason === null,
|
|
reason,
|
|
};
|
|
}
|
|
|
|
/** Pure preview: declared source evidence is normalized, never fetched or independently certified. */
|
|
export function previewDatasetPreparation(body: unknown): CanonicalDatasetPreparation {
|
|
const parsed = previewBody.safeParse(body);
|
|
if (!parsed.success) {
|
|
throw new DatasetPreparationError(400, 'Expected a source dataset input');
|
|
}
|
|
return describe(normalize(parsed.data.input));
|
|
}
|
|
|
|
export async function retainDatasetPreparation(
|
|
database: RetainedDatasetStore,
|
|
body: unknown
|
|
): Promise<CanonicalDatasetPreparation> {
|
|
const parsed = retainBody.safeParse(body);
|
|
if (!parsed.success) {
|
|
throw new DatasetPreparationError(
|
|
400,
|
|
'Expected a source dataset input and lowercase SHA256 expectedHash'
|
|
);
|
|
}
|
|
const dataset = normalize(parsed.data.input);
|
|
if (dataset.manifest.hash !== parsed.data.expectedHash) {
|
|
throw new DatasetPreparationError(
|
|
409,
|
|
'Source dataset differs from the preview; preview it again'
|
|
);
|
|
}
|
|
// Keep storage failures separate from malformed input, including conflicting corrupt retained rows.
|
|
await retainResearchDataset(database, dataset);
|
|
return describe(dataset);
|
|
}
|