@bytebrand/export-cars 1.0.9 → 1.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/dist/config/as24.config.js +1 -0
  2. package/dist/config/as24.config.js.map +1 -1
  3. package/dist/config/autoboerse.config.js +1 -0
  4. package/dist/config/autoboerse.config.js.map +1 -1
  5. package/dist/config/config.d.ts +3 -0
  6. package/dist/config/config.js +5 -0
  7. package/dist/config/config.js.map +1 -1
  8. package/dist/config/eauto.config.js +1 -0
  9. package/dist/config/eauto.config.js.map +1 -1
  10. package/dist/config/marktde.config.js +1 -0
  11. package/dist/config/marktde.config.js.map +1 -1
  12. package/dist/config/yescar.config.js +1 -0
  13. package/dist/config/yescar.config.js.map +1 -1
  14. package/dist/export.schema.d.ts +6 -0
  15. package/dist/export.schema.js +6 -0
  16. package/dist/export.schema.js.map +1 -1
  17. package/dist/exporters/as24.exporter.js +13 -0
  18. package/dist/exporters/as24.exporter.js.map +1 -1
  19. package/dist/exporters/mobile.exporter.js +3 -4
  20. package/dist/exporters/mobile.exporter.js.map +1 -1
  21. package/dist/interfaces/params.interface.d.ts +1 -0
  22. package/dist/main.js.map +1 -1
  23. package/dist/services/mapping.service.d.ts +4 -3
  24. package/dist/services/mapping.service.js +23 -14
  25. package/dist/services/mapping.service.js.map +1 -1
  26. package/dist/tsconfig.build.tsbuildinfo +1 -1
  27. package/package.json +2 -2
  28. package/src/config/as24.config.ts +1 -0
  29. package/src/config/autoboerse.config.ts +1 -0
  30. package/src/config/config.ts +5 -0
  31. package/src/config/eauto.config.ts +1 -0
  32. package/src/config/marktde.config.ts +1 -0
  33. package/src/config/yescar.config.ts +1 -0
  34. package/src/export.schema.ts +6 -0
  35. package/src/exporters/as24.exporter.ts +17 -13
  36. package/src/exporters/mobile.exporter.ts +3 -4
  37. package/src/interfaces/params.interface.ts +1 -0
  38. package/src/main.ts +6 -1
  39. package/src/services/mapping.service.ts +25 -27
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bytebrand/export-cars",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
4
4
  "description": "",
5
5
  "author": "bytebrand",
6
6
  "license": "UNLICENSED",
@@ -64,5 +64,5 @@
64
64
  "engines": {
65
65
  "node": ">=14.0.0"
66
66
  },
67
- "gitHead": "8f6de1762a4b742afd2e9020cf9a27d4fe0d57e3"
67
+ "gitHead": "76f936636e6e646ed55feb87f618d492d1cad69a"
68
68
  }
@@ -6,5 +6,6 @@ export class As24Config extends Config {
6
6
  super(params);
7
7
  this.pathesConfig.localConvertedDirectory = 'resources/as24-export';
8
8
  this.dealersList = params.dealers.split(',');
9
+ this.mappingSpreadsheetTitle = 'mobile';
9
10
  }
10
11
  }
@@ -8,5 +8,6 @@ export class AutoboerseConfig extends Config {
8
8
  this.pathesConfig.localConvertedDirectory = 'resources/autoboerse-export';
9
9
  this.imageUrl = `https://images.${this.url}/carimage`;
10
10
  this.downloadImages = true;
11
+ this.mappingSpreadsheetTitle = 'mobile_yescar';
11
12
  }
12
13
  }
@@ -32,6 +32,9 @@ export class Config {
32
32
  cpc: string;
33
33
  ownerId: string;
34
34
  downloadImages: boolean;
35
+ googleSheetsApiKey: string;
36
+ mappingSpreadsheetId: string;
37
+ mappingSpreadsheetTitle: string;
35
38
 
36
39
  protected environmentConfig = {
37
40
  [Environment.LOCAL]: {
@@ -68,6 +71,8 @@ export class Config {
68
71
  this.fileName = params.fileName || `${params.environment}_${params.exportType}`;
69
72
  this.criteoId = this.environmentConfig[params.environment].criteoId;
70
73
  this.url = this.environmentConfig[params.environment].url;
74
+ this.googleSheetsApiKey = params.googleSheetsApiKey;
75
+ this.mappingSpreadsheetId = '147qz9sTgs1wi13ua8YKa0IgfxAn1X_qH6LkGnAnzeyY';
71
76
  this.userActionsUrl = `https://www.${this.url}/service/user-actions`;
72
77
  this.imboUrl = `https://www.${this.url}/service/image-imbo`;
73
78
  this.imageUrl = `https://images.${this.url}`;
@@ -8,5 +8,6 @@ export class EautoConfig extends Config {
8
8
  this.pathesConfig.localConvertedDirectory = 'resources/eauto-export';
9
9
  this.fileName = '416';
10
10
  this.imageUrl = `https://images.${this.url}/carimage`;
11
+ this.mappingSpreadsheetTitle = 'mobile_yescar';
11
12
  }
12
13
  }
@@ -7,5 +7,6 @@ export class MarktdeConfig extends Config {
7
7
  this.campaign = 'marktde';
8
8
  this.pathesConfig.localConvertedDirectory = 'resources/marktde-export';
9
9
  this.imageUrl = `https://images.${this.url}/carimage`;
10
+ this.mappingSpreadsheetTitle = 'mobile_yescar';
10
11
  }
11
12
  }
@@ -7,5 +7,6 @@ export class YescarConfig extends Config {
7
7
  this.campaign = 'YesAuto';
8
8
  this.pathesConfig.localConvertedDirectory = 'resources/yescar-export';
9
9
  this.imageUrl = `https://images.${this.url}/carimage`;
10
+ this.mappingSpreadsheetTitle = 'mobile_yescar';
10
11
  }
11
12
  }
@@ -1,3 +1,4 @@
1
+ import { As24Config } from './config/as24.config';
1
2
  import { Auto24Config } from './config/auto24.config';
2
3
  import { AutoboerseConfig } from './config/autoboerse.config';
3
4
  import { AutouncleConfig } from './config/autouncle.config';
@@ -10,6 +11,7 @@ import { MeinstadtConfig } from './config/meinstadt.config';
10
11
  import { OoyyoConfig } from './config/ooyyo.config';
11
12
  import { YescarConfig } from './config/yescar.config';
12
13
  import { ExportType } from './enums/export-type.enum';
14
+ import { As24Exporter } from './exporters/as24.exporter';
13
15
  import { Auto24Exporter } from './exporters/auto24.exporter';
14
16
  import { AutouncleExporter } from './exporters/autouncle.exporter';
15
17
  import { ByOwnerExporter } from './exporters/by-owner.exporter';
@@ -64,4 +66,8 @@ export const exportSchema = {
64
66
  config: AutouncleConfig,
65
67
  exporter: AutouncleExporter,
66
68
  },
69
+ [ExportType.AS24]: {
70
+ config: As24Config,
71
+ exporter: As24Exporter,
72
+ },
67
73
  };
@@ -7,9 +7,11 @@ import { Injectable } from '@nestjs/common';
7
7
  import { ModuleRef } from '@nestjs/core';
8
8
  import { As24Transformer } from '../transformers/as24.transformer';
9
9
  import { UserService } from '../services/user.service';
10
- import { get } from 'lodash';
10
+ import { get, union } from 'lodash';
11
11
  import { convertToCSV } from '../utils/converter.utils';
12
12
  import { writeFile } from 'fs/promises';
13
+ import { normalize } from 'path';
14
+ import { Zipper } from '@bytebrand/zipper';
13
15
 
14
16
  @Injectable()
15
17
  export class As24Exporter implements Exporter {
@@ -31,6 +33,8 @@ export class As24Exporter implements Exporter {
31
33
  descriptionSchemas[dealerId] = await this.userService.getDefaultSummaryByDealerId(dealerId);
32
34
  }
33
35
 
36
+ const processedDealers = [];
37
+
34
38
  await this.loaderService.process(async (chunk: FsChunkInterface) => {
35
39
  if (chunk && chunk.data) {
36
40
  for (let i = 0; i < chunk.data.length; i += 1) {
@@ -43,28 +47,28 @@ export class As24Exporter implements Exporter {
43
47
  ) {
44
48
  const transformedData = await as24Transformer.transform(data.car, descriptionSchemas);
45
49
  const transformedDataCSV = `${convertToCSV(transformedData)}\n`;
50
+
46
51
  await writeFile(
47
52
  `${this.configService.config.pathesConfig.localConvertedDirectory}/${ownerId}.csv`,
48
53
  Buffer.from(transformedDataCSV),
49
54
  { flag: 'a' },
50
55
  );
56
+
57
+ union([ownerId], processedDealers);
51
58
  }
52
59
  }
53
60
  chunk.data = null;
54
61
  }
55
62
  });
56
63
 
57
- // TODO: filter dealers and zip files
58
-
59
- // const fileZIP = normalize(
60
- // `${this.configService.config.pathesConfig.localConvertedDirectory}/${this.configService.config.fileName}.zip`,
61
- // );
62
- // const fileCSV = normalize(
63
- // `${this.configService.config.pathesConfig.localConvertedDirectory}/${this.configService.config.fileName}.csv`,
64
- // );
65
-
66
- // if (await CronicleUtils.exists(fileCSV)) {
67
- // await Zipper.zip({ fileList: [fileCSV], dest: fileZIP });
68
- // }
64
+ for (const dealer of processedDealers) {
65
+ const now = new Date();
66
+ const d = `${now.getFullYear()}${now.getMonth() + 1}${now.getDate()}`;
67
+ const fileCSV = normalize(`${this.configService.config.pathesConfig.localConvertedDirectory}/${dealer}.csv`);
68
+ const fileZIP = normalize(`${this.configService.config.pathesConfig.localConvertedDirectory}/${d}_${dealer}.zip`);
69
+ if (await CronicleUtils.exists(fileCSV)) {
70
+ await Zipper.zip({ fileList: [fileCSV], dest: fileZIP });
71
+ }
72
+ }
69
73
  }
70
74
  }
@@ -16,6 +16,7 @@ import { lastValueFrom } from 'rxjs';
16
16
  import { createWriteStream } from 'fs';
17
17
  import { get } from 'lodash';
18
18
  import { RulesService } from '../services/rules.service';
19
+ import { pipeline } from 'stream/promises';
19
20
 
20
21
  @Injectable()
21
22
  export class MobileExporter implements Exporter {
@@ -93,7 +94,7 @@ export class MobileExporter implements Exporter {
93
94
  responseType: 'stream',
94
95
  }),
95
96
  )
96
- .then(response => {
97
+ .then(({ data }) => {
97
98
  const writer = createWriteStream(
98
99
  join(
99
100
  this.configService.config.pathesConfig.localConvertedDirectory,
@@ -101,9 +102,7 @@ export class MobileExporter implements Exporter {
101
102
  `${vehicleId}_${index + 1}${extname(imageUrl) || '.jpg'}`,
102
103
  ),
103
104
  );
104
- response.data.pipe(writer);
105
- writer.on('finish', resolve);
106
- writer.on('error', reject);
105
+ pipeline(data, writer).then(resolve).catch(reject);
107
106
  })
108
107
  .catch(reject);
109
108
  });
@@ -16,4 +16,5 @@ export interface Params {
16
16
  cpc?: string;
17
17
  ownerId?: string;
18
18
  fileName?: string;
19
+ googleSheetsApiKey?: string;
19
20
  }
package/src/main.ts CHANGED
@@ -3,6 +3,12 @@ import { CommandFactory } from 'nest-commander';
3
3
  import { AppModule } from './app.module';
4
4
 
5
5
  async function bootstrap() {
6
+ // setInterval(() => {
7
+ // Object.entries(process.memoryUsage()).forEach(item =>
8
+ // console.log(`${item[0]}: ${(item[1] / 1024 / 1024).toFixed(4)} MB`),
9
+ // );
10
+ // console.log('\n');
11
+ // }, 10000);
6
12
  try {
7
13
  await CommandFactory.run(AppModule, console);
8
14
  LoggerService.complete();
@@ -10,5 +16,4 @@ async function bootstrap() {
10
16
  LoggerService.exitError(e.message || e);
11
17
  }
12
18
  }
13
-
14
19
  bootstrap();
@@ -1,47 +1,45 @@
1
1
  import { Injectable } from '@nestjs/common';
2
- import { CronicleUtils } from '@bytebrand/cronicle-helper';
3
2
  import { MappingRules } from '../interfaces/mapping-rules.interface';
4
- import { HttpService } from '@nestjs/axios';
5
- import { lastValueFrom } from 'rxjs';
3
+ import { GoogleSpreadsheet, GoogleSpreadsheetRow } from 'google-spreadsheet';
4
+ import { ConfigService } from './config.service';
6
5
 
7
6
  @Injectable()
8
7
  export class MappingService {
9
- constructor(private httpService: HttpService) {}
8
+ constructor(private configService: ConfigService) {}
10
9
  private rules: MappingRules[];
11
10
 
12
11
  async getRules(): Promise<MappingRules[]> {
13
12
  if (!this.rules) {
14
- const { data } = await lastValueFrom(
15
- this.httpService.get(
16
- 'https://docs.google.com/spreadsheets/d/e/2PACX-1vRh14TrYZDtk2efsnVsbfKypPWGdtGSSjgJRVlY2qrqHybaQZWP4HptLb3RQhiKfBsxQUdtltCRnwHU/pub?gid=1580940856&single=true&output=csv',
17
- ),
18
- );
19
- const rows: MappingRules[] = await CronicleUtils.parseCSV(data);
13
+ const spreadsheet = new GoogleSpreadsheet(this.configService.config.mappingSpreadsheetId);
14
+ spreadsheet.useApiKey(this.configService.config.googleSheetsApiKey);
15
+ await spreadsheet.loadInfo();
16
+ const rows = await spreadsheet.sheetsByTitle[this.configService.config.mappingSpreadsheetTitle].getRows();
17
+ const mappingRules = this.rowsToRules(rows);
20
18
 
21
- rows.map((row, _i, selfRows) => {
22
- this.removeEmptyKeys(row);
23
- if (row.importFieldType && row.importFieldType.includes('selector')) {
24
- this.groupSelectors(row, selfRows);
19
+ mappingRules.map((rule, _i, selfRules) => {
20
+ this.removeEmptyKeys(rule);
21
+ if (rule.importFieldType && rule.importFieldType.includes('selector')) {
22
+ this.groupSelectors(rule, selfRules);
25
23
  }
26
24
  });
27
25
 
28
- this.rules = this.filterResultedRows(rows);
26
+ this.rules = this.filterResultedRows(mappingRules);
29
27
  }
30
28
  return this.rules;
31
29
  }
32
30
 
33
- // private rowsToRules(rows: GoogleSpreadsheetRow[]): MappingRulesInterface[] {
34
- // return rows.map(row =>
35
- // Object.assign(
36
- // {},
37
- // ...Object.entries(Object.getOwnPropertyDescriptors(row))
38
- // .filter(([_key, descriptor]) => typeof descriptor.get === 'function')
39
- // .map(([key, descriptor]) => ({
40
- // [key]: descriptor.get() || '',
41
- // })),
42
- // ),
43
- // );
44
- // }
31
+ private rowsToRules(rows: GoogleSpreadsheetRow[]): MappingRules[] {
32
+ return rows.map(row =>
33
+ Object.assign(
34
+ {},
35
+ ...Object.entries(Object.getOwnPropertyDescriptors(row))
36
+ .filter(([_key, descriptor]) => typeof descriptor.get === 'function')
37
+ .map(([key, descriptor]) => ({
38
+ [key]: descriptor.get() || '',
39
+ })),
40
+ ),
41
+ );
42
+ }
45
43
 
46
44
  private filterResultedRows(rows: MappingRules[]): MappingRules[] {
47
45
  return rows.filter((value, idx, self) => {