@bytebrand/export-cars 1.21.61 → 1.21.63

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bytebrand/export-cars",
3
- "version": "1.21.61",
3
+ "version": "1.21.63",
4
4
  "description": "",
5
5
  "author": "bytebrand",
6
6
  "license": "UNLICENSED",
@@ -1,4 +1,4 @@
1
- import { escape, get, join, round, toSafeInteger, toString, trim, truncate } from 'lodash';
1
+ import { escape, get, isNumber, join, round, toSafeInteger, toString, trim, truncate } from 'lodash';
2
2
  import { CarDescriptionFactory, DescriptionTypeEnum } from '@bytebrand/car-description';
3
3
  import { DataSchemes } from '@bytebrand/car-schema';
4
4
  import { TranslatorExtended } from '@bytebrand/schema-tools';
@@ -77,7 +77,7 @@ export class GoogleTransformer {
77
77
  model: this.transformService.model(car),
78
78
  trim: this.getTrim(car),
79
79
  year: this.getYear(car),
80
- mileage: `${this.mileage(car)}km`,
80
+ mileage: `${this.getMileage(car)}km`,
81
81
  color: this.getColor(car),
82
82
  vehicle_option: this.getExtrasField(car),
83
83
  body_style: this.mapBodyStyle(car),
@@ -347,8 +347,42 @@ export class GoogleTransformer {
347
347
  return truncate(trim, { length: GOOGLE_MAX_TRIM_LENGTH, separator: ' ' });
348
348
  }
349
349
 
350
- mileage(car: DataSchemes.Car): number {
351
- const mileage = get(car, 'mainData.mileage', 0);
350
+ private convertToSeconds(fieldValue: string | number | Date | null): any {
351
+ if (fieldValue === null) {
352
+ return null;
353
+ }
354
+
355
+ let timestamp = 0;
356
+
357
+ if (typeof fieldValue === 'number') {
358
+ if (fieldValue > 1e12) {
359
+ timestamp = fieldValue;
360
+ } else {
361
+ timestamp = fieldValue * 1000;
362
+ }
363
+ }
364
+
365
+ return Math.floor(timestamp);
366
+ }
367
+
368
+ private monthDiffLess(car: DataSchemes.Car, month: number) {
369
+ const fieldDate = new Date(this.convertToSeconds(car.mainData.firstRegistration)).getTime();
370
+ const conditionDate = new Date().getTime();
371
+ if (!isNumber(fieldDate)) {
372
+ return false;
373
+ }
374
+
375
+ const monthsDifference = (fieldDate - conditionDate) / (1000 * 60 * 60 * 24 * 30);
376
+
377
+ return Math.abs(monthsDifference) <= month;
378
+ }
379
+
380
+ private getMileage(car: DataSchemes.Car): number {
381
+ let mileage = get(car, 'mainData.mileage', 0);
382
+
383
+ if ((!car.mainData.firstRegistration || this.monthDiffLess(car, 8)) && mileage < 1000)
384
+ mileage = 1001
385
+
352
386
  return !mileage ? 10 : mileage;
353
387
  }
354
388