Skip to content

Commit

Permalink
Refactor createExternalSource to send parsed JSON as form data
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephVolosin committed Nov 21, 2024
1 parent 5745f17 commit e54c8a4
Showing 1 changed file with 12 additions and 29 deletions.
41 changes: 12 additions & 29 deletions src/utilities/effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ import type {
import type {
DerivationGroup,
DerivationGroupInsertInput,
ExternalSourceInsertInput,
ExternalSourcePkey,
ExternalSourceSlim,
PlanDerivationGroup,
Expand Down Expand Up @@ -915,12 +914,6 @@ const effects = {
creatingExternalSource.set(true);
createExternalSourceError.set(null);

// Create mutation inputs for Hasura
const derivationGroupInsert: DerivationGroupInsertInput = {
name: derivationGroupName !== '' ? derivationGroupName : `${externalSourceTypeName} Default`,
source_type_name: externalSourceTypeName,
};

// Convert all times, validate they exist or else throw a failure
const startTimeFormatted: string | undefined = convertDoyToYmd(startTime.replaceAll('Z', ''))?.replace(
'Z',
Expand All @@ -943,24 +936,8 @@ const effects = {
return;
}

// Create external source mutation input for Hasura
const externalSourceInsert: ExternalSourceInsertInput = {
external_events: [], // updated after this map is created
source: {
attributes: externalSourceAttributes,
derivation_group_name: derivationGroupInsert.name,
key: externalSourceKey,
period: {
end_time: endTimeFormatted,
start_time: startTimeFormatted,
},
source_type_name: externalSourceTypeName,
valid_at: validAtFormatted,
},
};

// Create external events + external event types mutation inputs for Hasura
let externalEventsCreated: ExternalEventInsertInput[] = [];
const externalEventsCreated: ExternalEventInsertInput[] = [];
for (const externalEvent of externalEvents) {
// Ensure the duration is valid
try {
Expand Down Expand Up @@ -1002,11 +979,17 @@ const effects = {
}
}

externalSourceInsert.external_events = externalEventsCreated;
externalEventsCreated = [];

const body = JSON.stringify(externalSourceInsert);
const reqResponse = await reqGateway(`/uploadExternalSource`, 'POST', body, user, false);
const body = new FormData();
body.append('attributes', JSON.stringify(externalSourceAttributes));
body.append('derivation_group_name', derivationGroupName);
body.append('key', externalSourceKey);
body.append('end_time', endTimeFormatted);
body.append('start_time', startTimeFormatted);
body.append('source_type_name', externalSourceTypeName);
body.append('valid_at', validAtFormatted);
body.append('external_events', JSON.stringify(externalEventsCreated));

const reqResponse = await reqGateway(`/uploadExternalSource`, 'POST', body, user, true);
if (reqResponse?.errors === undefined) {
showSuccessToast('External Source Created Successfully');
} else {
Expand Down

0 comments on commit e54c8a4

Please sign in to comment.