1
+ const { stat } = require ( 'fs' )
2
+ const path = require ( 'path' )
3
+ const fs = require ( 'fs' ) . promises
4
+
5
+ async function readFile ( filename ) {
6
+
7
+ const filePath = path . join ( __dirname , filename )
8
+
9
+ let fileData = await fs . readFile ( filePath )
10
+ //Generate a list with all rows in the file
11
+ dataList = fileData . toString ( ) . split ( "\n" )
12
+ //Remove header
13
+ return dataList . slice ( 1 , dataList . length )
14
+ }
15
+
16
+
17
+ async function main ( ) {
18
+ try {
19
+ fileData = await readFile ( 'file.csv' )
20
+ let active_users = [ ]
21
+ for ( element of fileData ) {
22
+ let [ id , email , status ] = element . split ( "|" )
23
+ status = status . trim ( ) . toLowerCase ( )
24
+ if ( status == "activo" ) {
25
+ active_users . push ( { "id" :id , "email" :email , "status" :status } )
26
+ }
27
+ }
28
+
29
+ if ( active_users . length >= 3 ) {
30
+ //Get subscription winner
31
+ index = Math . floor ( Math . random ( ) * active_users . length )
32
+ console . log ( "Subscription winner " + active_users [ index ] . email + " with id: " + active_users [ index ] . id )
33
+ active_users . splice ( index , 1 ) //Remove selected user
34
+ //Get discount winner
35
+
36
+ index = Math . floor ( Math . random ( ) * active_users . length )
37
+ console . log ( "Discount winner " + active_users [ index ] . email + " with id: " + active_users [ index ] . id )
38
+ active_users . splice ( index , 1 ) //Remove selected user
39
+ // Get book winner
40
+ index = Math . floor ( Math . random ( ) * active_users . length )
41
+ console . log ( "Book winner " + active_users [ index ] . email + " with id: " + active_users [ index ] . id )
42
+ active_users . splice ( index , 1 )
43
+ }
44
+ else {
45
+ console . log ( "There are least than 3 active users" )
46
+ }
47
+
48
+
49
+ } catch ( error ) {
50
+ console . log ( "Error opening the file: " + error . message )
51
+ }
52
+
53
+
54
+
55
+ }
56
+
57
+ main ( )
0 commit comments