Skip to content

Commit

Permalink
Merge pull request #5 from CinCoders/feature/RES-11
Browse files Browse the repository at this point in the history
Update xml imports to save the last one
  • Loading branch information
dcruzb authored Oct 23, 2023
2 parents 79f3f60 + d3cedac commit 1601da3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
4 changes: 4 additions & 0 deletions docs/USAGE.MD
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ Then, you aditionally will put the client_id of your Keycloak realm (there's no
### Being enabled to test the API

After that, you will have the authorization to make requests and check the responses.

## Checking the downloaded XML paths

Update the current XML_PATH to the desired folder
48 changes: 36 additions & 12 deletions src/import-xml/import-xml.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ export class ImportXmlService {
}

async updateXMLStatus(
filename: string,
id: string,
filename: string | undefined,
status: string,
professorName: string | undefined,
) {
Expand All @@ -117,7 +118,11 @@ export class ImportXmlService {
break;
}
case Status.PROGRESS: {
importXml = { status: status, professorName: professorName };
importXml = {
status: status,
professorName: professorName,
name: filename,
};
break;
}
case Status.CONCLUDED:
Expand All @@ -129,7 +134,7 @@ export class ImportXmlService {
await AppDataSource.createQueryBuilder()
.update(ImportXml)
.set(importXml)
.where('id=:name', { name: filename })
.where('id=:name', { name: id })
.execute();
}

Expand All @@ -146,10 +151,8 @@ export class ImportXmlService {
);
} else {
try {
await rename(
file.path,
this.XML_PATH + '/' + file.originalname.split('.')[0],
);
await rename(file.path, this.XML_PATH + '/' + file.originalname);
file.path = this.XML_PATH + '/' + file.originalname;
} catch (error) {
if (error instanceof Error) {
throw Error(
Expand All @@ -159,10 +162,9 @@ export class ImportXmlService {
throw Error('The file could not be renamed.');
}

xmlData = await readFile(
this.XML_PATH + '/' + file.originalname.split('.')[0],
{ encoding: 'latin1' },
);
xmlData = await readFile(this.XML_PATH + '/' + file.originalname, {
encoding: 'latin1',
});
}
const object = await parseStringPromise(xmlData);
return JSON.parse(JSON.stringify(object));
Expand Down Expand Up @@ -1052,6 +1054,9 @@ export class ImportXmlService {
this.XML_PATH + '/' + 'curriculo.xml',
this.XML_PATH + '/' + file.originalname.split('.')[0] + '.xml',
);
unlink(zipPath);
file.path =
this.XML_PATH + '/' + file.originalname.split('.')[0] + '.xml';
} catch (err) {
await logErrorToDatabase(err, EntityType.UNZIP, undefined);
}
Expand Down Expand Up @@ -1099,6 +1104,8 @@ export class ImportXmlService {
const importXml = new ImportXml();
importXml.id = files[i].filename;
importXml.name = files[i].originalname;
// importXml.originalfilename = files[i].originalname;
// importXml.filename = '';
importXml.user = username;
importXml.status = Status.PENDING;
importXml.startedAt = undefined;
Expand Down Expand Up @@ -1139,7 +1146,12 @@ export class ImportXmlService {
for (let i = 0; i < files.length; i++) {
await queryRunner.startTransaction();
try {
this.updateXMLStatus(files[i].filename, Status.LOADING, undefined);
this.updateXMLStatus(
files[i].filename,
undefined,
Status.LOADING,
undefined,
);
let importXmlLog = this.createImportLog(
files[i],
username,
Expand All @@ -1151,8 +1163,18 @@ export class ImportXmlService {

// se o professor não existir, criamos, se existir podemos usá-lo
professorDto = this.getProfessorData(json);
// renomear com o lattes do professor

await rename(
files[i].path,
this.XML_PATH + '/' + professorDto.identifier + '.xml',
);

const filename = professorDto.identifier + '.xml';

this.updateXMLStatus(
files[i].filename,
filename,
Status.PROGRESS,
professorDto.name,
);
Expand Down Expand Up @@ -1203,13 +1225,15 @@ export class ImportXmlService {
importXmlLog.message += 'SUCCESS';
this.updateXMLStatus(
files[i].filename,
filename,
Status.CONCLUDED,
professorDto.name,
);
} catch (err) {
importXmlLog.message += 'FAILED';
this.updateXMLStatus(
files[i].filename,
undefined,
Status.NOT_IMPORTED,
professorDto?.name,
);
Expand Down

0 comments on commit 1601da3

Please sign in to comment.