@bytebrand/export-cars 1.21.73 → 1.21.74

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.73",
3
+ "version": "1.21.74",
4
4
  "description": "",
5
5
  "author": "bytebrand",
6
6
  "license": "UNLICENSED",
@@ -1,4 +1,4 @@
1
- import { get, isEmpty, isNil, set, toString, upperFirst } from 'lodash';
1
+ import { get, isEmpty, isNil, isNumber, set, toString, upperFirst } from 'lodash';
2
2
 
3
3
  import { Injectable } from '@nestjs/common';
4
4
 
@@ -50,9 +50,21 @@ export class SantanderTransformer {
50
50
  const transformedCarData = {};
51
51
  const rules = await this.mappingService.getRules();
52
52
 
53
- console.log('rules', JSON.stringify(rules));
53
+ const isNew =
54
+ (!carData.mainData.firstRegistration || this.monthDiffLess(carData, 8)) &&
55
+ (!carData.mainData.mileage || carData.mainData.mileage < 1000);
56
+ if (isNew) {
57
+ console.log('carData', carData._id);
58
+ console.log('rules', JSON.stringify(rules));
59
+ }
54
60
 
55
61
  for (const rule of rules) {
62
+ const importFieldNumber = Number(rule.importField.match(/\d+/)[0]);
63
+
64
+ if (!isNew && this.checkFieldsOnlyForNew(importFieldNumber)) {
65
+ continue;
66
+ }
67
+
56
68
  if (rule.custom === 'yes') {
57
69
  const transformedField = await this.customTransformers(rule, carData, images);
58
70
  if (transformedField) {
@@ -64,10 +76,12 @@ export class SantanderTransformer {
64
76
  set(transformedCarData, `${rule.importField}`, await this.customTransformers(rule, carData, images));
65
77
  } else {
66
78
  if (!isNil(fieldValue) || ["['8']"].includes(rule.importField)) {
67
- const transformedField = await this.transformField(fieldValue, rule);
79
+ if (!this.checkFieldsOnlyForNew(importFieldNumber)) {
80
+ const transformedField = await this.transformField(fieldValue, rule);
68
81
 
69
- if (!isNil(transformedField)) {
70
- set(transformedCarData, `${rule.importField}`, transformedField);
82
+ if (!isNil(transformedField)) {
83
+ set(transformedCarData, `${rule.importField}`, transformedField);
84
+ }
71
85
  }
72
86
  }
73
87
  }
@@ -85,6 +99,15 @@ export class SantanderTransformer {
85
99
  return transformedCarData;
86
100
  }
87
101
 
102
+ private async checkFieldsOnlyForNew(importFieldNumber: number) {
103
+ const fieldsOnlyForNewCar = new Set<number>([
104
+ 336, 337, 338, 339, 313, 320, 405, 468, 469, 470, 471, 472, 473, 474, 475,
105
+ 513, 514, 515, 516, 517, 520, 521, 522, 523, 524, 525, 526, 527
106
+ ]);
107
+
108
+ return fieldsOnlyForNewCar.has(importFieldNumber);
109
+ }
110
+
88
111
  private async transformField(fieldValue: any, rule: MappingRules): Promise<string | void> {
89
112
  const transformProcedureName = `${rule.autodeFieldType}To${upperFirst(rule.importFieldType)}`;
90
113
 
@@ -144,6 +167,36 @@ export class SantanderTransformer {
144
167
  }
145
168
  }
146
169
 
170
+ private monthDiffLess(car: DataSchemes.Car, month: number) {
171
+ const fieldDate = new Date(this.convertToSeconds(car.mainData.firstRegistration)).getTime();
172
+ const conditionDate = new Date().getTime();
173
+ if (!isNumber(fieldDate)) {
174
+ return false;
175
+ }
176
+
177
+ const monthsDifference = (fieldDate - conditionDate) / (1000 * 60 * 60 * 24 * 30);
178
+
179
+ return Math.round(Math.abs(monthsDifference)) <= month;
180
+ }
181
+
182
+ private convertToSeconds(fieldValue: string | number | Date | null): any {
183
+ if (fieldValue === null) {
184
+ return null;
185
+ }
186
+
187
+ let timestamp = 0;
188
+
189
+ if (typeof fieldValue === 'number') {
190
+ if (fieldValue > 1e12) {
191
+ timestamp = fieldValue;
192
+ } else {
193
+ timestamp = fieldValue * 1000;
194
+ }
195
+ }
196
+
197
+ return Math.floor(timestamp);
198
+ }
199
+
147
200
  private async offerStringToString(): Promise<string> {
148
201
  const dealerId = get(this.currentCar, 'metaData.ownerId');
149
202
  const descriptionSchema = await this.userService.getDefaultSummaryByDealerId(dealerId);