@@ -4,18 +4,28 @@ import {
44 FilenameEnum ,
55 IFileDTO ,
66 IFileService ,
7+ log ,
78} from "$common" ;
89import { FileRespository } from "$repositories" ;
910import { PersonService } from "$service/person/PersonService.ts" ;
11+ import { IObjectS3DTO , IPersonDTO } from '$interfaces' ;
12+ import { SimpleQueueService } from "$component/AWS/sqs.component.ts" ;
1013
1114export class FileService implements IFileService {
12- private s3 : typeof S3 ;
1315 private fileRepository : typeof FileRespository ;
14- private personService : PersonService = new PersonService ( )
16+ private personService : PersonService ;
17+ private s3 : typeof S3 ;
18+ private sQs : SimpleQueueService ;
19+ private status = {
20+ "PENDING" : "PENDING" ,
21+ "LAUNCHED" : "LAUNCHED"
22+ } ;
1523
1624 constructor ( ) {
1725 this . s3 = S3 ;
26+ this . sQs = new SimpleQueueService ( ) ;
1827 this . fileRepository = FileRespository ;
28+ this . personService = new PersonService ( ) ;
1929 }
2030
2131 public async handlerFilesPerson ( files : Array < FormDataFile > ) : Promise < string > {
@@ -57,40 +67,76 @@ export class FileService implements IFileService {
5767 return this . fileRepository . list ( ) ;
5868 }
5969
60- public async listenFiles ( ) {
61- const files = await this . fileRepository . pendingFiles ( ) ;
70+ public async listenFilesFromDB ( ) {
71+ const filesPending = await this . fileRepository . pendingFiles ( ) ;
6272
63- if ( ! files ) {
73+ if ( ! filesPending ) {
6474 return ;
6575 }
6676
67- const pendings = files . filter (
68- file => file . status === FilenameEnum . PENDING
69- ) ;
77+ for ( const filePending of filesPending ) {
78+ await this . personService . listenAndCreatePerson ( filePending . name ) ;
79+ await this . fileRepository . updatedAfterListenAll ( filePending . id )
7080
71- for ( const pending of pendings ) {
72- if ( pending . status === FilenameEnum . PENDING ) {
73- this . personService . listenAndCreatePerson ( pending . name ) ;
74- await this . fileRepository . updatedAfterListenAll ( pending . id )
75- continue ;
76- } ;
81+ continue ;
7782 }
7883
79- return files ;
84+ return filesPending ;
8085 }
8186
82- public async listAllObjectsFromBucket ( ) {
87+ public async listAllObjectsFromBucket ( ) : Promise < Array < IObjectS3DTO > > {
8388 const objects = await this . s3 . listObjects ( ) as Array < _Object > ;
8489
85- const objectsFiltered = objects . map ( object => {
86- return {
87- name : object . Key ,
88- size : object . Size ,
89- }
90+ const objectsFiltered : Array < IObjectS3DTO > = objects . map ( object => {
91+ return {
92+ name : object . Key ,
93+ size : object . Size ,
94+ }
9095 } ) ;
9196
9297 return objectsFiltered ;
9398 }
99+
100+ public async handlerPersonFromObjectIntoSQS ( ) {
101+ const objects = await this . listAllObjectsFromBucket ( ) ;
102+
103+ const personList : Partial < IPersonDTO > [ ] = [ ]
104+
105+ for ( const object of objects ) {
106+ const pendingsFiles = await this . fileRepository . pendingFilesByName ( object . name as string ) ;
107+
108+ if ( ! pendingsFiles ) {
109+ return ;
110+ }
111+
112+ pendingsFiles . forEach ( async file => {
113+ if ( String ( file . status ) !== this . status . PENDING ) {
114+ log . success ( "Done process with success!" ) ;
115+ return ;
116+ }
117+
118+ const getS3Object = await this . s3 . readFileFromS3 ( object . name as string ) as Array < string > ;
119+
120+ for ( const result of getS3Object ) {
121+ const [ name , age , sex , size , weight ] = result . split ( ',' ) ;
122+ const person : Partial < IPersonDTO > = {
123+ name,
124+ age : Number ( age ) ,
125+ sex,
126+ size : Number ( size ) ,
127+ weight : Number ( weight )
128+ }
129+
130+ await this . sQs . handleQueue ( person ) ;
131+ continue ;
132+ }
133+
134+ await this . fileRepository . updatedAfterListenAll ( file . id as string ) ;
135+ } ) ;
136+ }
137+
138+ return personList ;
139+ }
94140}
95141
96142export default new FileService ( )
0 commit comments