Skip to content

Commit

Permalink
fixed spinning wheel, PUT procedure and DELETE procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
vmanawat committed Dec 12, 2024
1 parent ac6b0ae commit 2062687
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 62 deletions.
76 changes: 53 additions & 23 deletions backend/src/aqi_api/aqi_api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export class AqiApiService {
);
return response.data.id;
} catch (err) {
console.log(body);
this.logger.error(
"API CALL TO POST Field Visits failed: ",
err.response.data.message,
Expand Down Expand Up @@ -510,12 +509,13 @@ export class AqiApiService {
}

mergeErrorMessages(localErrors: any[], remoteErrors: any[]) {
const map = new Map<number, any>();
const map = new Map<string, any>();

const mergeItem = (item: any) => {
const exists = map.get(item.rowNum);
const key = `${item.rowNum}-${item.type}`
const exists = map.get(key);
map.set(
item.rowNum,
key,
exists
? { ...exists, message: { ...exists.message, ...item.message } }
: item,
Expand Down Expand Up @@ -577,27 +577,45 @@ export class AqiApiService {
},
});

console.log(guidsToDelete)

// Delete all the observations from the list of imported guids
// Delete all the observations in AQI that are in the list of imported guids
if (guidsToDelete[0].imported_guids.observations.length > 0) {
try {
let deletion = await axios.delete(
`${process.env.AQI_BASE_URL}/v2/observations?ids=${guidsToDelete[0].imported_guids.observations}`,
{
headers: {
Authorization: `token ${process.env.AQI_ACCESS_TOKEN}`,
"x-api-key": process.env.AQI_ACCESS_TOKEN,
},
},
const batchSize = 50;
const observationBatches = [];
for (
let i = 0;
i < guidsToDelete[0].imported_guids.observations.length;
i += batchSize
) {
observationBatches.push(
guidsToDelete[0].imported_guids.observations.slice(
i,
i + batchSize,
),
);
this.logger.log("AQI OBS DELETION: " + deletion.data);
} catch (err) {
this.logger.error(`API call to delete AQI observation failed: `, err);
}

observationBatches.forEach(async (batch) => {
try {
let deletion = await axios.delete(
`${process.env.AQI_BASE_URL}/v2/observations?ids=${batch}`,
{
headers: {
Authorization: `token ${process.env.AQI_ACCESS_TOKEN}`,
"x-api-key": process.env.AQI_ACCESS_TOKEN,
},
},
);
this.logger.log("AQI OBS DELETION: " + deletion);
} catch (err) {
this.logger.error(
`API call to delete AQI observation failed: `,
err,
);
}
});
}

// Delete all the specimens for the activities imported from AQI and the PSQL db
// Delete all the specimens that were imported for the file from AQI and the PSQL db
if (guidsToDelete[0].imported_guids.specimens.length > 0) {
for (const specimen of guidsToDelete[0].imported_guids.specimens) {
try {
Expand All @@ -620,7 +638,11 @@ export class AqiApiService {
});
this.logger.log("DB SPECIMEN DELETION: " + dbDeletion);
} catch (err) {
this.logger.error(`API call to delete DB specimen failed: `, err);
if (err.code === 'P2025'){
this.logger.log(`Record with ID ${specimen} not found in DB. Record was deleted in AQI but skipping deletion from DB.`);
}else{
this.logger.error(`API call to delete DB specimen failed: `, err);
}
}
} catch (err) {
this.logger.error(`API call to delete AQI specimen failed: `, err);
Expand Down Expand Up @@ -651,7 +673,11 @@ export class AqiApiService {
});
this.logger.log("DB ACTIVITY DELETION: " + dbDeletion);
} catch (err) {
this.logger.error(`API call to delete DB activity failed: `, err);
if (err.code === 'P2025'){
this.logger.log(`Record with ID ${activity} not found in DB. Record was deleted in AQI but skipping deletion from DB.`);
} else{
this.logger.error(`API call to delete DB activity failed: `, err);
}
}
} catch (err) {
this.logger.error(`API call to delete AQI activity failed: `, err);
Expand Down Expand Up @@ -683,7 +709,11 @@ export class AqiApiService {
});
this.logger.log("DB VISIT DELETION: " + dbDeletion);
} catch (err) {
this.logger.error(`API call to delete DB visits failed: `, err);
if (err.code === 'P2025'){
this.logger.log(`Records with IDs ${guidsToDelete[0].imported_guids.visits} not found in DB. Records were deleted in AQI but skipping deletion from DB.`);
} else{
this.logger.error(`API call to delete DB visits failed: `, err);
}
}
} catch (err) {
this.logger.error(`API call to delete AQI visit failed: `, err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -717,23 +717,6 @@ export class FileParseValidateService {
}

getUniqueWithCounts(data: any[]) {
// const map = new Map<
// string,
// { rec: any; count: number; positions: number[] }
// >();

// data.forEach((obj, index) => {
// const key = JSON.stringify(obj);
// if (map.has(key)) {
// const entry = map.get(key)!;
// entry.count++;
// entry.positions.push(index);
// } else {
// map.set(key, { rec: obj, count: 1, positions: [index] });
// }
// });
// const dupeCount = Array.from(map.values());
// return dupeCount;
const seen = new Map();
const duplicateDetails = [];

Expand Down Expand Up @@ -858,6 +841,15 @@ export class FileParseValidateService {
}
});

if (record.hasOwnProperty("Depth Unit")){
if (record["Depth Upper"]){
if (record["Depth Unit"] != "metre"){
let errorLog = `{"rowNum": ${index + 2}, "type": "ERROR", "message": {"Depth_Unit": "${record["Depth Unit"]} is not valid unit for Depth. Only 'Metre' is allowed"}}`;
errorLogs.push(JSON.parse(errorLog));
}
}
}

if (record.hasOwnProperty("Project")) {
const present = await this.aqiService.databaseLookup(
"aqi_projects",
Expand Down Expand Up @@ -1024,7 +1016,7 @@ export class FileParseValidateService {
);
if (activityExists !== null && activityExists !== undefined) {
existingGUIDS["activity"] = activityExists;
let errorLog = `{"rowNum": ${index + 2}, "type": "WARN", "message": {"Activity": "Activity Name ${record.ActivityName} for Field Visit at Start Time ${record.FieldVisitStartTime} already exists in AQI Activities"}}`;
let errorLog = `{"rowNum": ${index + 2}, "type": "ERROR", "message": {"Activity": "Activity Name ${record.ActivityName} for Field Visit at Start Time ${record.FieldVisitStartTime} already exists in AQI Activities"}}`;
errorLogs.push(JSON.parse(errorLog));
}

Expand All @@ -1037,7 +1029,7 @@ export class FileParseValidateService {
]);
if (specimenExists !== null && specimenExists !== undefined) {
existingGUIDS["specimen"] = specimenExists;
let errorLog = `{"rowNum": ${index + 2}, "type": "WARN", "message": {"Specimen": "Specimen Name ${record.SpecimenName} for that Acitivity at Start Time ${record.ObservedDateTime} already exists in AQI Specimen"}}`;
let errorLog = `{"rowNum": ${index + 2}, "type": "ERROR", "message": {"Specimen": "Specimen Name ${record.SpecimenName} for that Acitivity at Start Time ${record.ObservedDateTime} already exists in AQI Specimen"}}`;
errorLogs.push(JSON.parse(errorLog));
}

Expand Down Expand Up @@ -1270,7 +1262,11 @@ export class FileParseValidateService {
file_operation_code,
);

if (localValidationResults[0].some((item) => item.type === "ERROR")) {
const hasError = localValidationResults[0].some((item) => item.type === "ERROR")
const hasWarn = localValidationResults[0].some((item) => item.type === "WARN")


if (hasError) {
/*
* If there are any errors then
* Set the file status to 'REJECTED'
Expand All @@ -1291,9 +1287,9 @@ export class FileParseValidateService {
* If there are no errors then
* Check to see if there are any WARNINGS
* If WARNINGS
* Proceed with the PATCH logic
* Proceed with the PUT logic
*/
if (localValidationResults[0].some((item) => item.type === "WARN")) {
if (hasWarn) {
let visitInfo = [],
expandedVisitInfo = [];
let activityInfo = [],
Expand Down Expand Up @@ -1413,6 +1409,7 @@ export class FileParseValidateService {
const uniqueSpecimensWithIDsAndCounts = this.getUniqueWithCounts(
allSpecimensWithGUIDS,
);

specimenInfo = await this.specimensJson(
uniqueSpecimensWithIDsAndCounts,
"put",
Expand All @@ -1424,7 +1421,7 @@ export class FileParseValidateService {
return { ...obj2, ...obj1 };
});
const uniqueSpecimensWithCounts =
this.getUniqueWithCounts(allSpecimens);
this.getUniqueWithCounts(allSpecimens).filter(item => item.rec.SpecimenName !== "");
specimenInfo = await this.specimensJson(
uniqueSpecimensWithCounts,
"post",
Expand Down Expand Up @@ -1479,7 +1476,7 @@ export class FileParseValidateService {
active_ind: false,
},
});
} else {
} else if (!hasError && !hasWarn) {
// If there are no errors or warnings
await this.fileSubmissionsService.updateFileStatus(
file_submission_id,
Expand Down
35 changes: 20 additions & 15 deletions frontend/src/pages/FileUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -318,23 +318,28 @@ function FileUpload() {
</Typography>
</Button>
) : (
<Button style={{ color: "orange" }}>
<CircularProgress color="secondary" />
</Button>
""
)}

<Button
id={"delete-file-" + index}
style={{
color: "black",
outline: "none !important",
}}
onClick={() => {
handleOpen(index);
}}
>
<DeleteRounded />
</Button>
{fileStatusCodes.items[index] == "ACCEPTED" ||
fileStatusCodes.items[index] == "REJECTED" ||
fileStatusCodes.items[index] == "INPROGRESS" ||
fileStatusCodes.items[index] == "QUEUED" ? (
""
) : (
<Button
id={"delete-file-" + index}
style={{
color: "black",
outline: "none !important",
}}
onClick={() => {
handleOpen(index);
}}
>
<DeleteRounded />
</Button>
)}
</ButtonGroup>
</Grid>
<Grid item xs={4}>
Expand Down

0 comments on commit 2062687

Please sign in to comment.