Skip to content

Commit 094a392

Browse files
authored
Merge pull request #59 from CinCoders/main
upmerge
2 parents f297776 + 1ed31fe commit 094a392

6 files changed

Lines changed: 54 additions & 70 deletions

File tree

package-lock.json

Lines changed: 2 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "research-back",
3-
"version": "1.6.3",
3+
"version": "1.6.4",
44
"description": "Research Dashboard API - System to display information extracted from professors' Lattes curriculum.",
55
"license": "MIT",
66
"author": "CInCoders <cincoders@cin.ufpe.br> (https://cincoders.cin.ufpe.br)",
@@ -46,7 +46,6 @@
4646
"iconv-lite": "^0.7.0",
4747
"nest-keycloak-connect": "^1.9.1",
4848
"nestjs-soap": "^3.0.4",
49-
"node-cron": "^4.2.1",
5049
"pg": "^8.7.1",
5150
"rimraf": "^3.0.2",
5251
"string-similarity": "^4.0.4",

src/import-xml/import-xml.controller.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { Controller, Get, Param, Post, Query, Res, UploadedFiles, UseInterceptors } from '@nestjs/common';
1+
import { Controller, Get, HttpException, HttpStatus, Param, Post, Query, Res, UploadedFiles, UseInterceptors } from '@nestjs/common';
22
import { AnyFilesInterceptor } from '@nestjs/platform-express';
33
import { ApiOAuth2, ApiResponse, ApiTags } from '@nestjs/swagger';
44
import { Response } from 'express';
55
import { AuthenticatedUser, Roles } from 'nest-keycloak-connect';
66
import { extname } from 'path';
7+
import { ProfessorService } from 'src/professor/professor.service';
78
import { SystemRoles } from 'src/types/enums';
89
import { Page } from '../types/page.dto';
910
import { ImportXmlDto } from './dto/import-xml.dto';
@@ -14,7 +15,7 @@ import { ImportXmlService } from './import-xml.service';
1415
@Controller('import-xml')
1516
@ApiOAuth2([])
1617
export class ImportXmlController {
17-
constructor(private readonly importXmlService: ImportXmlService) {}
18+
constructor(private readonly importXmlService: ImportXmlService, private readonly professorService: ProfessorService) {}
1819

1920
@Post()
2021
@UseInterceptors(
@@ -79,8 +80,15 @@ export class ImportXmlController {
7980
@Post('professors/lattes/import')
8081
async importAllProfessors(@AuthenticatedUser() user: any, @Res() res: Response) {
8182
const username = `${user.name} (${user.email})`;
82-
await this.importXmlService.importAllProfessors(username);
83-
return res.sendStatus(200);
83+
const professorsCount = await this.professorService.count();
84+
85+
if (professorsCount === 0) {
86+
throw new HttpException('Nenhum professor encontrado', HttpStatus.NOT_FOUND);
87+
}
88+
89+
this.importXmlService.executeBackgroundProfessorsUpdate(username);
90+
91+
return res.status(200).json({ professorsCount });
8492
}
8593

8694
@ApiResponse({

src/import-xml/import-xml.service.ts

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Inject, Injectable } from '@nestjs/common';
2-
import { Cron, CronExpression } from '@nestjs/schedule';
32
import AdmZip from 'adm-zip';
43
import { Buffer } from 'buffer';
54
import extract from 'extract-zip';
@@ -1506,7 +1505,11 @@ export class ImportXmlService {
15061505
return !lastImport || latestUpdate > new Date(lastImport.includedAt);
15071506
}
15081507

1509-
async processProfessorData(identifier: string, username?: string): Promise<void> {
1508+
async processProfessorData(identifier: string, username?: string): Promise<Express.Multer.File> {
1509+
if (!identifier) {
1510+
throw new Error('Professor identifier is undefined');
1511+
}
1512+
15101513
const args: WsCurriculoGetCurriculoCompactado = { id: identifier };
15111514
const [result] = await this.lattesSoapClient.getCurriculoCompactadoAsync(args);
15121515
const base64Zip = result.return;
@@ -1524,10 +1527,6 @@ export class ImportXmlService {
15241527
xmlCustomEncoding && iconv.encodingExists(xmlCustomEncoding)
15251528
? iconv.decode(xmlData, xmlCustomEncoding)
15261529
: xmlContentTemp;
1527-
1528-
if (!identifier) {
1529-
throw new Error('Professor identifier is undefined');
1530-
}
15311530
const filePath = this.generateFilePath(identifier);
15321531

15331532
await fs.promises.mkdir(this.XML_PATH, { recursive: true });
@@ -1547,58 +1546,46 @@ export class ImportXmlService {
15471546
stream: new Readable(),
15481547
};
15491548

1550-
await this.enqueueFiles([tempFile], username || 'atualizado automaticamente');
1549+
return tempFile;
15511550
}
15521551

1553-
@Cron(CronExpression.EVERY_WEEK)
1554-
async getCurriculum(): Promise<void> {
1555-
try {
1556-
const professors = await this.professorService.findAll();
1552+
// @Cron(CronExpression.EVERY_WEEK)
1553+
// async getCurriculum(): Promise<void> {
1554+
// try {
1555+
// const professors = await this.professorService.findAll();
15571556

1558-
for (const professorTableDto of professors) {
1559-
const professor = await this.professorService.findOne(undefined, professorTableDto.identifier);
1557+
// for (const professorTableDto of professors) {
1558+
// const professor = await this.professorService.findOne(undefined, professorTableDto.identifier);
15601559

1561-
if (!professor || !professor.identifier) continue;
1560+
// if (!professor || !professor.identifier) continue;
15621561

1563-
const hasUpdates = await this.hasProfessorUpdates(professor);
1562+
// const hasUpdates = await this.hasProfessorUpdates(professor);
15641563

1565-
if (hasUpdates) {
1566-
await this.processProfessorData(professor.identifier);
1567-
}
1568-
}
1569-
} catch (error) {
1570-
await logErrorToDatabase(error, EntityType.IMPORT);
1571-
}
1572-
}
1564+
// if (hasUpdates) {
1565+
// await this.processProfessorData(professor.identifier);
1566+
// }
1567+
// }
1568+
// } catch (error) {
1569+
// await logErrorToDatabase(error, EntityType.IMPORT);
1570+
// }
1571+
// }
15731572

1574-
async importAllProfessors(username: string): Promise<void> {
1573+
async executeBackgroundProfessorsUpdate(username: string): Promise<void> {
15751574
try {
15761575
const professors = await this.professorService.findAll();
1577-
let currentIndex = 0;
1578-
1579-
const worker = async () => {
1580-
while (true) {
1581-
const index = currentIndex++;
1576+
const tempFiles: Express.Multer.File[] = [];
15821577

1583-
if (index >= professors.length) break;
1584-
1585-
const professor = professors[index];
1586-
const prof = await this.professorService.findOne(undefined, professor.identifier);
1587-
1588-
if (prof) {
1589-
try {
1590-
if (!prof.identifier) throw new Error('Professor identifier is undefined');
1578+
for (const { identifier, professorId } of professors) {
1579+
try {
1580+
if (!identifier) throw new Error('Professor identifier is undefined');
15911581

1592-
await this.processProfessorData(prof.identifier, username);
1593-
} catch (err) {
1594-
await logErrorToDatabase(err, EntityType.IMPORT, professor.identifier);
1595-
}
1596-
}
1582+
tempFiles.push(await this.processProfessorData(identifier, username));
1583+
} catch (err) {
1584+
await logErrorToDatabase(err, EntityType.IMPORT, String(professorId));
15971585
}
1598-
};
1586+
}
15991587

1600-
const workers = Array.from({ length: 10 }, () => worker());
1601-
await Promise.all(workers);
1588+
await this.enqueueFiles(tempFiles, username || 'atualizado automaticamente')
16021589
} catch (error) {
16031590
await logErrorToDatabase(error, EntityType.IMPORT);
16041591
throw error;
@@ -1607,7 +1594,8 @@ export class ImportXmlService {
16071594

16081595
async importProfessorById(identifier: string, username: string): Promise<void> {
16091596
try {
1610-
await this.processProfessorData(identifier, username);
1597+
const tempFile = await this.processProfessorData(identifier, username);
1598+
await this.enqueueFiles([tempFile], username || 'atualizado automaticamente');
16111599
} catch (error) {
16121600
await logErrorToDatabase(error, EntityType.IMPORT);
16131601
throw error;

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ async function bootstrap() {
5858

5959
await AppDataSource.initialize()
6060
.then(() => console.log('LOG [Typeorm] Success connection'))
61-
.catch((error) => console.log(error));
61+
.catch(error => console.log(error));
6262

6363
const document = SwaggerModule.createDocument(app, config);
6464

src/professor/professor.service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ export class ProfessorService {
3333
return professor;
3434
}
3535

36+
async count(): Promise<number> {
37+
return AppDataSource.createQueryBuilder().select('p').from(Professor, 'p').getCount();
38+
}
39+
3640
async findAll(): Promise<ProfessorTableDto[]> {
3741
const professors = await AppDataSource.createQueryBuilder()
3842
.select(['p.id as id', 'p.identifier as identifier', 'p.name as name'])

0 commit comments

Comments
 (0)