1
1
import { Argument , Command } from 'commander' ;
2
+ import { generate as generatePassword } from 'generate-password' ;
2
3
3
4
import { getCsvContent } from '../shared/csv' ;
4
- import { insertUsers , register } from '../shared/db' ;
5
+ import { getUsersByRole , insertUsers , register } from '../shared/db' ;
5
6
import { validateEnv } from '../shared/env' ;
7
+ import { createPasswordsList } from '../shared/files' ;
6
8
import { createLogger } from '../shared/logger' ;
7
9
import { ParticipantCsvRow , RegisterDTO , User , userRoles } from '../shared/models' ;
8
10
import { transformToMatchClass } from '../shared/object' ;
@@ -15,34 +17,47 @@ export const registerParticipants = (program: Command) => {
15
17
. description ( 'Creates accounts for CodersCamp participants listed in the CSV file' )
16
18
. addArgument ( new Argument ( '<csv-path>' , 'Path to the CSV file' ) )
17
19
. action ( async ( csvPath : string ) => {
20
+ const participants : User [ ] = [ ] ;
21
+ const passwords = createPasswordsList ( ) ;
22
+
18
23
try {
19
24
await validateEnv ( ) ;
20
25
21
26
const rows = await getCsvContent ( csvPath ) ;
22
27
const participantsRows = await Promise . all ( rows . map ( transformToMatchClass ( ParticipantCsvRow ) ) ) ;
23
-
24
- const participants : User [ ] = [ ] ;
28
+ const currentParticipants = await getUsersByRole ( userRoles . participant ) ;
29
+ const currentParticipantsEmails = currentParticipants . map ( ( { email } ) => email ) ;
25
30
26
31
logger . debug ( 'Iterating through parsed rows' ) ;
27
32
28
33
for ( const { email, firstName, lastName } of participantsRows ) {
29
- const registerDto = await transformToMatchClass ( RegisterDTO ) ( { email } ) ;
30
- const userId = await register ( registerDto ) ;
31
- const participant = await transformToMatchClass ( User ) ( {
32
- ...registerDto ,
33
- id : userId ,
34
- name : `${ firstName } ${ lastName } ` ,
35
- role : userRoles . participant ,
36
- } ) ;
37
-
38
- participants . push ( participant ) ;
34
+ if ( ! currentParticipantsEmails . includes ( email ) ) {
35
+ const password = generatePassword ( { length : 16 , numbers : true , symbols : true } ) ;
36
+ const registerDto = await transformToMatchClass ( RegisterDTO ) ( { email, password } ) ;
37
+ const userId = await register ( registerDto ) ;
38
+ const participant = await transformToMatchClass ( User ) ( {
39
+ ...registerDto ,
40
+ id : userId ,
41
+ firstName,
42
+ lastName,
43
+ role : userRoles . participant ,
44
+ } ) ;
45
+
46
+ participants . push ( participant ) ;
47
+ passwords . add ( { email, password } ) ;
48
+ } else {
49
+ logger . debug ( `Participant with email ${ email } already exists in the database` ) ;
50
+ }
39
51
}
40
52
41
53
logger . debug ( 'Iteration through parsed rows finished' ) ;
42
54
43
55
await insertUsers ( participants ) ;
44
56
} catch ( ex ) {
45
57
logger . error ( ex ) ;
58
+ logger . info ( 'Already registered participants' , participants ) ;
59
+ } finally {
60
+ await passwords . save ( ) ;
46
61
}
47
62
} ) ;
48
63
} ;
0 commit comments