@@ -6,14 +6,26 @@ import { Repository } from 'typeorm';
6
6
import { ICacheKeys } from 'src/static/interfaces/cache.interfaces' ;
7
7
import { LocalCacheService } from 'src/cache/local-cache.service' ;
8
8
import { ExceptionMessages } from 'src/static/enums/messages.enums' ;
9
+ import { readFileSync , existsSync } from 'fs' ;
10
+ import { ConfigService } from '@nestjs/config' ;
9
11
10
12
@Injectable ( )
11
13
export class ToysService {
12
14
constructor (
13
15
@InjectRepository ( ToyEntity )
14
16
private readonly repository : Repository < ToyEntity > ,
15
17
private readonly cacheService : LocalCacheService ,
16
- ) { }
18
+ private readonly configService : ConfigService ,
19
+ ) {
20
+ setTimeout ( ( ) => {
21
+ if ( this . configService . get ( 'UPDATE_TOYS_FROM_FILE' ) !== undefined ) {
22
+ console . log ( 'UPDATE TOYS FROM FILE STARTED...' ) ;
23
+ this . updateToysFromFile ( ) . then ( ( ) => {
24
+ console . log ( 'UPDATE TOYS FROM FILE END.' ) ;
25
+ } ) ;
26
+ }
27
+ } , 1000 ) ;
28
+ }
17
29
18
30
readonly cacheKeys : ICacheKeys = this . cacheService . cacheKeys ( ) ;
19
31
@@ -98,4 +110,26 @@ export class ToysService {
98
110
99
111
return true ;
100
112
}
113
+
114
+ async updateToysFromFile ( ) : Promise < void > {
115
+ if ( existsSync ( '../toys-orders-backend/result.json' ) ) {
116
+ try {
117
+ const toysFromFile : { code : number ; name : string } [ ] = JSON . parse (
118
+ readFileSync ( '../toys-orders-backend/result.json' ) . toString ( ) ,
119
+ ) ;
120
+ await this . repository . save (
121
+ toysFromFile . map ( ( toy ) => {
122
+ return {
123
+ code : toy . code . toString ( ) ,
124
+ partName : toy . name . toString ( ) ,
125
+ } ;
126
+ } ) ,
127
+ ) ;
128
+ } catch ( e ) {
129
+ console . error ( e ) ;
130
+ }
131
+ } else {
132
+ console . error ( 'FILE NOT FOUND!' ) ;
133
+ }
134
+ }
101
135
}
0 commit comments