11import { Inject , Injectable } from '@nestjs/common' ;
2- import { Cron , CronExpression } from '@nestjs/schedule' ;
32import AdmZip from 'adm-zip' ;
43import { Buffer } from 'buffer' ;
54import 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 ;
0 commit comments