Skip to content

Commit

Permalink
Rework Hasura responses
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephVolosin committed Nov 21, 2024
1 parent 30967c3 commit 3ccd817
Showing 1 changed file with 8 additions and 44 deletions.
52 changes: 8 additions & 44 deletions src/packages/external-source/external-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
GetExternalEventTypeAttributeSchemaResponse,
UploadExternalSourceJSON,

Check failure on line 10 in src/packages/external-source/external-source.ts

View workflow job for this annotation

GitHub Actions / lint

'UploadExternalSourceJSON' is declared but never used.
} from '../../types/external-source.js';
import type { ExternalEventInsertInput, UploadAttributeJSON } from '../../types/external-event.js';
import type { ExternalEventInsertInput, ExternalEventJson, UploadAttributeJSON } from '../../types/external-event.js';

Check failure on line 12 in src/packages/external-source/external-source.ts

View workflow job for this annotation

GitHub Actions / lint

'ExternalEventJson' is declared but never used.
import Ajv from 'ajv';
import { getEnv } from '../../env.js';
import getLogger from '../../logger.js';
Expand Down Expand Up @@ -140,30 +140,6 @@ async function uploadExternalSource(req: Request, res: Response) {
'x-hasura-user-id': userHeader ? `${userHeader}` : '',
};

const uploadedExternalSource = await parseJSONFile<UploadExternalSourceJSON>(file);

// Format all source times, validate that they're logical
const startTimeFormatted: string | undefined = convertDoyToYmd(
uploadedExternalSource.source.period.start_time.replaceAll('Z', ''),
)?.replace('Z', '+00:00');
const endTimeFormatted: string | undefined = convertDoyToYmd(
uploadedExternalSource.source.period.end_time.replaceAll('Z', ''),
)?.replace('Z', '+00:00');
const validAtFormatted: string | undefined = convertDoyToYmd(
uploadedExternalSource.source.valid_at.replaceAll('Z', ''),
)?.replace('Z', '+00:00');
if (!startTimeFormatted || !endTimeFormatted || !validAtFormatted) {
const errorMsg = `Parsing failed - parsing dates in input failed. ${uploadedExternalSource.source.period.start_time}, ${uploadedExternalSource.source.period.end_time}, ${uploadedExternalSource.source.valid_at}`;
res.status(500).send({ message: errorMsg });
return;
}

if (new Date(startTimeFormatted) > new Date(endTimeFormatted)) {
const errorMsg = `Parsing failed - start time ${startTimeFormatted} after end time ${endTimeFormatted}.`;
res.status(500).send({ message: errorMsg });
return;
}

logger.info(`POST /uploadExternalSource: Uploading External Source: ${key}`);

// Verify that this is a valid external source
Expand Down Expand Up @@ -193,7 +169,7 @@ async function uploadExternalSource(req: Request, res: Response) {
let sourceAttributesAreValid: boolean = false;
let sourceSchema: Ajv.ValidateFunction | undefined = undefined;
const sourceTypeResponseJSON = await sourceAttributeSchema.json();
const getExternalSourceTypeAttributeSchemaResponse = sourceTypeResponseJSON as
const getExternalSourceTypeAttributeSchemaResponse = sourceTypeResponseJSON.data as
| GetExternalSourceTypeAttributeSchemaResponse
| HasuraError;
if (
Expand Down Expand Up @@ -226,23 +202,12 @@ async function uploadExternalSource(req: Request, res: Response) {
return;
}

// Get the attribute schema(s) for all external event types used by the source's events
// get list of all used event types
const usedExternalEventTypes = parsedExternalEvents
.map((externalEvent: ExternalEventInsertInput) => externalEvent.event_type_name)
.reduce((acc: string[], externalEventType: string) => {
if (!acc.includes(externalEventType)) {
acc.push(externalEventType);
}
externalEventsCreated.push({
attributes: externalEvent.attributes,
duration: externalEvent.duration,
event_type_name: externalEvent.event_type,
key: externalEvent.key,
start_time: externalEvent.start_time,
});
const usedExternalEventTypes = parsedExternalEvents.reduce((acc: string[], externalEvent: ExternalEventInsertInput) => {
if (!acc.includes(externalEvent.event_type_name)) {
acc.push(externalEvent.event_type_name);
}
}
return acc;
}, []);

const usedExternalEventTypesAttributesSchemas: Record<string, Ajv.ValidateFunction> = {};
for (const eventType of usedExternalEventTypes) {
Expand All @@ -257,10 +222,9 @@ async function uploadExternalSource(req: Request, res: Response) {
method: 'POST',
});
const eventTypeJSONResponse = await eventAttributeSchema.json();
const getExternalEventTypeAttributeSchemaResponse = eventTypeJSONResponse as
const getExternalEventTypeAttributeSchemaResponse = eventTypeJSONResponse.data as
| GetExternalEventTypeAttributeSchemaResponse
| HasuraError;

if (
(getExternalEventTypeAttributeSchemaResponse as GetExternalEventTypeAttributeSchemaResponse)
.external_event_type_by_pk?.attribute_schema !== null
Expand Down

0 comments on commit 3ccd817

Please sign in to comment.