Skip to content

Commit 7bcb754

Browse files
committed
toys from file feature
1 parent 0edc4ac commit 7bcb754

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# sklad json
2+
result.json
3+
14
# dotenv
25
.env
36

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"start:dev": "nest start --watch",
1313
"start:debug": "nest start --debug --watch",
1414
"start:prod": "nest build && node dist/main",
15+
"start:withupdatetoys": "nest build && node dist/main --UPDATE_TOYS_FROM_FILE=true",
1516
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
1617
"test": "jest",
1718
"test:watch": "jest --watch",

src/toys/toys.service.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,26 @@ import { Repository } from 'typeorm';
66
import { ICacheKeys } from 'src/static/interfaces/cache.interfaces';
77
import { LocalCacheService } from 'src/cache/local-cache.service';
88
import { ExceptionMessages } from 'src/static/enums/messages.enums';
9+
import { readFileSync, existsSync } from 'fs';
10+
import { ConfigService } from '@nestjs/config';
911

1012
@Injectable()
1113
export class ToysService {
1214
constructor(
1315
@InjectRepository(ToyEntity)
1416
private readonly repository: Repository<ToyEntity>,
1517
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+
}
1729

1830
readonly cacheKeys: ICacheKeys = this.cacheService.cacheKeys();
1931

@@ -98,4 +110,26 @@ export class ToysService {
98110

99111
return true;
100112
}
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+
}
101135
}

0 commit comments

Comments
 (0)