Files
mostovik-backend/tools/openapi/source-export-catalog.mjs
Aleksandr Meshchryakov f0a0266495
All checks were successful
Mostovik Backend CI/CD / Tests and lint (push) Successful in 4m1s
Mostovik Backend CI/CD / Build linux/amd64 release images (push) Successful in 4m9s
Mostovik Backend CI/CD / Deploy and verify internal main (push) Has been skipped
Mostovik Backend CI/CD / Deploy dev (push) Successful in 3m5s
feat: configure export year window and publish ZIP archives under 99 MB
2026-11-03 10:02:04 +01:00

82 lines
8.2 KiB
JavaScript

/** Typed all-available contract, composed with generated legacy DRF operations. */
export function addSourceExportCatalog(document) {
const schemas = document.components.schemas;
const ref = name => ({ $ref: `#/components/schemas/${name}` });
const string = { type: 'string' };
const uuid = { type: 'string', format: 'uuid' };
const date = { type: 'string', format: 'date-time' };
const integer = { type: 'integer', minimum: 0, maximum: 9007199254740991 };
const positive = { ...integer, minimum: 1 };
const year = { type: 'integer', minimum: 1, maximum: 9999, nullable: true };
const bool = { type: 'boolean' };
const enumeration = values => ({ type: 'string', enum: values });
const format = enumeration(['xlsx', 'csv', 'json']);
const nullable = schema => ({ ...schema, nullable: true });
const nil = { type: 'object', nullable: true, enum: [null] };
const array = items => ({ type: 'array', items });
const object = properties => ({ type: 'object', additionalProperties: false, properties, required: Object.keys(properties) });
const group = structuredClone(schemas.OrganizationSourceRecordExportRequest.properties.sources.items);
schemas.SourceExportBuildSummary = object({ state: enumeration(['idle', 'queued', 'running', 'success', 'failed']), started_at: nullable(date), finished_at: nullable(date), error_code: nullable(string) });
const snapshot = {
source_group: group, generation_id: nullable(uuid),
state: enumeration(['ready', 'empty', 'not_ready', 'building', 'unavailable']),
unavailable_reason: nullable(enumeration(['disabled', 'adapter_missing', 'build_failed', 'storage_unavailable'])),
generated_at: nullable(date), data_as_of: nullable(date), data_scope: enumeration(['all_available']), partitioning: enumeration(['year', 'none']),
};
schemas.SourceExportSourceSummary = object({ ...snapshot, source_slug: string, title: string, order: integer, export_enabled: bool,
records_count: nullable(integer), exported_records_count: nullable(integer), parts_count: nullable(integer), supported_formats: { ...array(format), uniqueItems: true },
default_format: nullable(format), is_stale: bool, last_build: ref('SourceExportBuildSummary') });
const envelope = (data, meta = nil) => object({ success: { type: 'boolean', enum: [true] }, data, errors: nil, meta });
schemas.SourceExportCatalogResponse = envelope(object({ schema_version: enumeration(['2']), catalog_revision: string, sources: array(ref('SourceExportSourceSummary')) }));
schemas.SourceExportDescription = object({ composition: string, origin: string, fields: string, example_dataset_key: nullable(string), example_is_synthetic: bool,
example_record: { type: 'object', additionalProperties: true, nullable: true } });
schemas.SourceExportDataset = object({ key: string, year, title: string, export_schema_version: string, records_count: integer, parts_count: integer });
schemas.SourceExportArtifact = object({ artifact_id: uuid, format, file_name: { type: 'string', pattern: '^[A-Za-z0-9._-]+$' }, mime_type: string,
size_bytes: positive, sha256: { type: 'string', pattern: '^[0-9a-f]{64}$' } });
schemas.SourceExportPart = object({ part_id: uuid, year, dataset_key: string, title: string, part_number: positive, parts_count: positive, records_count: positive,
default_format: format, variants: { ...array(ref('SourceExportArtifact')), minItems: 1 } });
schemas.SourceExportPagination = object({ page: positive, page_size: { ...positive, maximum: 200 }, total_count: integer, total_pages: positive, has_next: bool, has_previous: bool });
schemas.SourceExportPolicy = object({ years_back: integer, min_year: positive, max_year: positive, archive_max_bytes: positive });
schemas.SourceExportArchive = object({ archive_id: uuid, artifact_id: uuid, archive_number: positive, archives_count: positive, format,
years: array(positive), file_name: string, mime_type: string, size_bytes: positive, sha256: string, part_ids: array(uuid) });
schemas.SourceExportSourceDetail = object({ ...snapshot, current_generation_id: nullable(uuid), export_policy: nullable(ref('SourceExportPolicy')), archives: array(ref('SourceExportArchive')), description: ref('SourceExportDescription'), datasets: array(ref('SourceExportDataset')), files: array(ref('SourceExportPart')) });
schemas.SourceExportSourceDetailResponse = envelope(ref('SourceExportSourceDetail'), object({ pagination: ref('SourceExportPagination') }));
schemas.SourceExportAuthenticationError = object({ detail: string });
schemas.SourceExportError = object({ detail: string, code: string, field: nullable(string), retryable: bool, request_id: uuid });
schemas.SourceExportFileTicketRequest = object({ selection_kind: enumeration(['file']), source_group: group, generation_id: uuid, part_id: uuid, format });
schemas.SourceExportArchiveTicketRequest = object({ selection_kind: enumeration(['source_archive']), source_group: group, generation_id: uuid, format });
schemas.SourceExportNumberedArchiveTicketRequest = object({ selection_kind: enumeration(['source_archive']), source_group: group, generation_id: uuid, archive_id: uuid, format });
schemas.OrganizationSourceRecordExportRequest.additionalProperties = false;
schemas.SourceRecordExportTicket.additionalProperties = false;
schemas.SourceRecordExportTicket.required = Object.keys(schemas.SourceRecordExportTicket.properties);
schemas.SourceExportSelectionTicket = object({ ticket: { type: 'string', minLength: 47, maxLength: 47 }, file_name: string, expires_in: { type: 'integer', enum: [300] },
selection_kind: enumeration(['file', 'source_archive']), source_group: group, generation_id: uuid, format, mime_type: string, size_bytes: nullable(positive), parts_count: positive });
schemas.SourceRecordExportDownloadRequest = object({ ticket: { type: 'string', minLength: 1, maxLength: 128 } });
const base = '/api/v2/organization-source-records/';
const json = schema => ({ 'application/json': { schema } });
const response = (schema, description = 'Success') => ({ description, content: json(schema) });
const errorResponses = Object.fromEntries([400,403,404,409,410,429,503,500].map(code => [code, response(ref('SourceExportError'), 'Export request failed')]));
for (const [endpoint, schema] of [['export-catalog/', 'SourceExportCatalogResponse'], ['export-catalog/{source_group}/', 'SourceExportSourceDetailResponse']]) {
const operation = document.paths[base + endpoint].get;
operation.responses = { ...errorResponses, 200: response(ref(schema)), 401: response(ref('SourceExportAuthenticationError'), 'Authentication required') };
operation.parameters = endpoint.includes('{') ? [
{ name: 'source_group', in: 'path', required: true, schema: group },
{ name: 'generation_id', in: 'query', schema: uuid },
{ name: 'page', in: 'query', schema: { ...positive, default: 1 } },
{ name: 'page_size', in: 'query', schema: { ...positive, maximum: 200, default: 50 } },
] : [];
}
const ticket = document.paths[base + 'export-ticket/'].post;
ticket.requestBody = { required: true, content: json({ oneOf: ['OrganizationSourceRecordExportRequest', 'SourceExportFileTicketRequest', 'SourceExportArchiveTicketRequest', 'SourceExportNumberedArchiveTicketRequest'].map(ref) }) };
ticket.responses = { ...errorResponses, 201: response({ oneOf: ['SourceRecordExportTicket', 'SourceExportSelectionTicket'].map(ref) }), 401: response(ref('SourceExportAuthenticationError'), 'Authentication required') };
const download = document.paths[base + 'export-download/'].post;
download.requestBody = { required: true, content: { 'application/x-www-form-urlencoded': { schema: ref('SourceRecordExportDownloadRequest') } } };
download.responses = { ...errorResponses, 200: { description: 'Pinned artifact or streaming ZIP64', content: Object.fromEntries([
'application/zip', 'application/json', 'text/csv', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
].map(mime => [mime, { schema: { type: 'string', format: 'binary' } }])) } };
for (const [status, legacy] of [[400, 'SourceRecordExportInvalidTicket'], [410, 'SourceRecordExportTicketExpired'], [503, 'SourceRecordExportUnavailable']]) {
download.responses[status] = response({ oneOf: [ref(legacy), ref('SourceExportError')] }, 'Legacy or all-available export failure');
}
}