Skip to content

Commit

Permalink
update process for when an error is caught
Browse files Browse the repository at this point in the history
  • Loading branch information
TaylorFries committed Aug 22, 2024
1 parent 9a59e07 commit bd3fc99
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions express-api/src/services/properties/propertiesServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ export type BulkUploadRowResult = {
reason?: string;
};

const checkForHeaders = (sheetObj: Record<string, any>[]) => {
const checkForHeaders = (sheetObj: Record<string, any>[], columnArray: any) => {
const requiredHeaders = [
'PropertyType',
'PID',
Expand All @@ -590,7 +590,7 @@ const checkForHeaders = (sheetObj: Record<string, any>[]) => {
}
}
for (let rowNum = 0; rowNum < requiredHeaders.length; rowNum++) {
if (!Object.keys(sheetObj[0]).includes(requiredHeaders[rowNum])) {
if (!columnArray.includes(requiredHeaders[rowNum])) {
throw new ErrorWithCode('Please ensure all required headers are present', 400);
}
}
Expand All @@ -611,9 +611,10 @@ const importPropertiesAsJSON = async (
roles: string[],
resultId: number,
) => {
const columnsArray = xlsx.utils.sheet_to_json(worksheet, { header: 1 })[0];
const sheetObj: Record<string, any>[] = xlsx.utils.sheet_to_json(worksheet);

checkForHeaders(sheetObj);
checkForHeaders(sheetObj, columnsArray);

const classifications = await AppDataSource.getRepository(PropertyClassification).find({
select: { Name: true, Id: true },
Expand Down Expand Up @@ -913,18 +914,25 @@ const processFile = async (filePath: string, resultRowId: number, user: User, ro
const worksheet = file.Sheets[sheetName];

results = await propertyServices.importPropertiesAsJSON(worksheet, user, roles, resultRowId);
await AppDataSource.getRepository(ImportResult).save({
Id: resultRowId,
CompletionPercentage: 1.0,
Results: results,
UpdatedById: user.Id,
UpdatedOn: new Date(),
});
return results; // Note that this return still works with finally as long as return is not called from finally block.
} catch (e) {
parentPort.postMessage('Aborting file upload: ' + e.message);
parentPort.postMessage('Aborting stack: ' + e.stack);
} finally {
await AppDataSource.getRepository(ImportResult).save({
Id: resultRowId,
CompletionPercentage: 1.0,
CompletionPercentage: -1.0,
Results: results,
UpdatedById: user.Id,
UpdatedOn: new Date(),
});
} finally {
await AppDataSource.destroy(); //Not sure whether this is necessary but seems like the safe thing to do.
}
};
Expand Down

0 comments on commit bd3fc99

Please sign in to comment.