Skip to content

Commit 274a020

Browse files
Merge pull request #17 from cedricWebsenso/main
v1.2 - Import by instance + customize route type by structure
2 parents 5f9d690 + ebf8269 commit 274a020

File tree

9 files changed

+86
-72
lines changed

9 files changed

+86
-72
lines changed

config/configImport_GEOTREK.js

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,35 +80,33 @@ exports.geotrekInstance = {
8080
}
8181
}
8282
},
83-
/*1 : {
84-
geotrekUrl : 'https://admin.cheminsdesparcs.fr/api/v2x',
83+
1 : {
84+
geotrekUrl : 'https://admin.sportsnature.hauteloire.fr/api/v2',
8585
structures : {
86-
3568: { // MULTI MEMBER COOKING
87-
specialId: 'ENT1RandoEcrins',
88-
name: 'Parc national des Écrins',
86+
1: { // CD43
87+
specialId: 'HauteLoire1',
88+
name: 'Maison du Tourisme de la Haute-Loire',
8989
address1: null,
9090
address2: null,
91-
city: '1813',
92-
insee: '05061',
93-
specialIdSitra: '1856',
91+
city: '16901',
92+
insee: '43157',
93+
specialIdSitra: '4633537',
9494
statusImport: 2,
95-
memberId : 1856,
96-
production: false,
95+
memberId : 1158,
96+
production: true,
9797
activity : {
98-
0: 3333,
9998
1: 3284,
10099
2: 3283,
101100
3: 3313,
102-
4: 1992,
103-
5: 5147,
101+
4: 3333,
104102
6: 4201,
105-
7: 3333,
106-
8: 4201,
107-
9: 3333,
108-
10: 3333,
109-
11: 3333,
110-
}
103+
8: 3302
104+
},
105+
itineraireType : {
106+
1: 'BOUCLE',
107+
3: 'ALLER_ITINERANCE',
108+
}
111109
},
112110
}
113-
}*/
111+
}
114112
};

modules/products/client/config/products.client.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ angular.module('products').run([
1818
Menus.addSubMenuItem('topbar', 'products', {
1919
title: 'Import Geotrek API',
2020
state: 'products.import',
21-
stateParams: { importType: 'geotrek-api' }
21+
stateParams: { importType: 'geotrek-api', importInstance: '0' }
2222
});
2323
}
2424
]);

modules/products/client/config/products.client.routes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ angular.module('products').config([
1616
templateUrl: 'modules/products/views/list-products.client.view.html'
1717
})
1818
.state('products.import', {
19-
url: '/import/:importType',
19+
url: '/import/:importType/:importInstance/',
2020
templateUrl: 'modules/products/views/import-products.client.view.html'
2121
});
2222
}

modules/products/client/controllers/products.client.controller.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ angular.module('products').controller('ProductsController', [
9191

9292
$scope.import = function () {
9393
var url = '/api/products/import';
94-
if ($stateParams.importType) {
95-
url += '?type=' + $stateParams.importType;
94+
if ($stateParams.importType && $stateParams.importInstance) {
95+
url += '?type=' + $stateParams.importType + '&instance=' + $stateParams.importInstance;
9696
}
9797
$http.get(url).then(
9898
function (response) {

modules/products/server/controllers/products.server.controller.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var path = require('path'),
1010
'./modules/core/server/controllers/errors.server.controller'
1111
)),
1212
config = require(__dirname + '/../../../../config/config.js'),
13+
configImportGEOTREK = require(path.resolve('config/configImport_GEOTREK.js')),
1314
_ = require('lodash');
1415

1516
/**
@@ -36,17 +37,24 @@ exports.list = async function (req, res) {
3637
* @param {object} res
3738
*/
3839
exports.import = function (req, res) {
39-
var type = req.query && req.query.type ? req.query.type : null;
40+
const type = req.query && req.query.type ? req.query.type : null,
41+
instance = req.query && req.query.instance ? req.query.instance : null;
42+
4043
if (config.debug && config.debug.logs)
4144
console.log('Begin import auto for', type);
4245

4346
if (!type) {
4447
throw 'Unable to determine type';
4548
}
49+
50+
if (configImportGEOTREK.geotrekInstance[instance] === undefined)
51+
{
52+
throw 'Instance not found';
53+
}
4654

4755
try {
48-
res.json({ message: 'Importing ' + type + ' flux in progress...' });
49-
Product.import(type);
56+
res.json({ message: 'Importing ' + type + ' flux in progress from ' + configImportGEOTREK.geotrekInstance[instance].geotrekUrl });
57+
Product.import(type, instance);
5058
} catch (err) {
5159
console.log('err = ', err);
5260
}

modules/products/server/models/exportSitra.server.model.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4024,7 +4024,7 @@ function __buildLegalEntity(product, root, rootFieldList) {
40244024

40254025
finalLegalEntity.informations.structureGestion = {
40264026
type: legalEntityObj.product.type,
4027-
id: legalEntityObj.product.specialIdSitra
4027+
id: (process.env.NODE_ENV == 'production') ? legalEntityObj.product.specialIdSitra : config.specialIdSitra
40284028
};
40294029
rootFieldList.push('informations.structureGestion');
40304030
break;
@@ -4036,7 +4036,7 @@ function __buildLegalEntity(product, root, rootFieldList) {
40364036

40374037
finalLegalEntity.informations.structureInformation = {
40384038
type: legalEntityObj.product.type,
4039-
id: legalEntityObj.product.specialIdSitra
4039+
id: (process.env.NODE_ENV == 'production') ? legalEntityObj.product.specialIdSitra : config.specialIdSitra
40404040
};
40414041
break;
40424042

modules/products/server/models/import.server.model.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ var mongoose = require('mongoose'),
99
/**
1010
* Import
1111
* @param {String} type
12+
* @param {String} instance
1213
* @param {function} callback
1314
*/
14-
exports.import = function (type, callback) {
15-
__importProductsbyApi(type, () => {
15+
exports.import = function (type, instance, callback) {
16+
console.log('> import.server.model ', type, instance);
17+
__importProductsbyApi(type, instance, () => {
1618
if (callback) {
1719
callback();
1820
}
@@ -22,10 +24,11 @@ exports.import = function (type, callback) {
2224
/**
2325
* Import product from sql
2426
* @param {String} type
27+
* @param {String} instance
2528
* @param {function} callback
2629
* @private
2730
*/
28-
function __importProductsbyApi(type, callback) {
31+
function __importProductsbyApi(type, instance, callback) {
2932
var User = mongoose.model('User');
3033
User.findOne({
3134
username: 'admin'
@@ -40,7 +43,8 @@ function __importProductsbyApi(type, callback) {
4043
importObj = new ImportClass({
4144
user: user,
4245
lang: 'fr',
43-
importType: type
46+
importType: type,
47+
importInstance: instance
4448
});
4549

4650
importObj.start(() => {

modules/products/server/models/import/geotrek-api/importGeneric.server.model.js

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,12 @@ class ImportGenericGeotrekApi extends Import {
3131
: null;
3232
this.user = options.user ? options.user : null;
3333
this.lang = options.lang ? options.lang : 'fr';
34-
35-
/*this.baseURL = 'https://geotrek-admin.ecrins-parcnational.fr/api/v2';
36-
this.instanceApi = axios.create({
37-
baseURL: this.baseURL,
38-
validateStatus(status) {
39-
return status < 500;
40-
}
41-
});*/
34+
this.importInstance = options.importInstance ? parseInt(options.importInstance) : 0;
4235
}
4336

4437
import(data, next) {
4538
if (config.debug && config.debug.logs)
46-
console.log('ImportGenericGeotrekApi.prototype.import');
39+
console.log('ImportGenericGeotrekApi.prototype.import', this.importInstance);
4740

4841
const me = this,
4942
nbInstanceGeotrek = Object.keys(
@@ -53,28 +46,31 @@ class ImportGenericGeotrekApi extends Import {
5346
Object.keys(configImportGEOTREK.geotrekInstance).forEach(function (
5447
structure
5548
) {
56-
console.log(
57-
'Instance = ',
58-
structure,
59-
' - GeoAdmin URL = ',
60-
configImportGEOTREK.geotrekInstance[structure].geotrekUrl
61-
);
62-
63-
me.executeQuery(
64-
0,
65-
configImportGEOTREK.geotrekInstance[structure].geotrekUrl,
66-
structure
67-
).catch((err) => {
49+
if (structure == me.importInstance)
50+
{
6851
console.log(
69-
chalk.red(
70-
'>>>>>>>>>>>>>>>>>>>>>>>> ERROR = ',
71-
err,
72-
'For instance = ',
73-
structure
74-
)
52+
'Instance = ',
53+
structure,
54+
' - GeoAdmin URL = ',
55+
configImportGEOTREK.geotrekInstance[structure].geotrekUrl
7556
);
76-
return false;
77-
});
57+
58+
me.executeQuery(
59+
0,
60+
configImportGEOTREK.geotrekInstance[structure].geotrekUrl,
61+
structure
62+
).catch((err) => {
63+
console.log(
64+
chalk.red(
65+
'>>>>>>>>>>>>>>>>>>>>>>>> OBJ NOT FOUND IN = ',
66+
err,
67+
'For instance = ',
68+
structure
69+
)
70+
);
71+
return false;
72+
});
73+
}
7874
});
7975
}
8076

@@ -99,7 +95,7 @@ class ImportGenericGeotrekApi extends Import {
9995
return status < 500;
10096
}
10197
});
102-
98+
console.log(chalk.green("Axiox Connex = ", geoTrekPath + urlNext));
10399
const { data, status } = await this.instanceApi.get(geoTrekPath + urlNext);
104100
if (status === 200) {
105101
this.doUpsertAsync = await pify(importUtils.doUpsert);
@@ -203,9 +199,10 @@ class ImportGenericGeotrekApi extends Import {
203199
break;
204200
}*/
205201

206-
/*if (process.env.NODE_ENV == 'development' && config.debug != undefined) {
207-
this.member = element.structure = 3568;
208-
}*/
202+
if (process.env.NODE_ENV == 'development' && config.debug != undefined) {
203+
this.member = /*element.structure =*/ 3568;
204+
console.log(chalk.green('DEV MODE - Member = ', this.member));
205+
}
209206

210207
/* if (configImportGEOTREK.geotrekInstance[structure].structures[element.structure].production === false) {
211208
if (
@@ -215,6 +212,8 @@ class ImportGenericGeotrekApi extends Import {
215212
)
216213
console.log('ImportGenericGeotrekApi.prototype.importProduct structure instance not in production', structure, element.structure);
217214
} else {*/
215+
216+
console.log(chalk.green('Config = ', configImportGEOTREK.geotrekInstance[structure].structures[element.structure]));
218217

219218
if (this.member && configImportGEOTREK.geotrekInstance[structure].structures[element.structure].production) {
220219
this.configData = {
@@ -282,7 +281,7 @@ class ImportGenericGeotrekApi extends Import {
282281
complementNl: this.getComplement(element, 'nl'),
283282
localization: this.getLocalization(element),
284283
price: this.getPrice(element),
285-
itinerary: await this.getItinerary(element),
284+
itinerary: await this.getItinerary(element, structure),
286285
perimetreGeographique: this.getPerimetreGeographique(element),
287286
description: this.getDescription(element, 'fr'),
288287
descriptionEn: this.getDescription(element, 'en'),
@@ -443,7 +442,7 @@ class ImportGenericGeotrekApi extends Import {
443442
};
444443
}
445444

446-
async getItinerary(element) {
445+
async getItinerary(element, structure) {
447446
const itineraire = {
448447
dailyDuration: null,
449448
distance: null,
@@ -471,8 +470,12 @@ class ImportGenericGeotrekApi extends Import {
471470
itineraire.negative = DataString.convertNegative(element.descent);
472471
}
473472
if (element.route) {
474-
itineraire.itineraireType =
475-
configImportGEOTREK.itineraireType[element.route];
473+
if (configImportGEOTREK.geotrekInstance[structure].structures[element.structure].itineraireType != undefined)
474+
{
475+
itineraire.itineraireType = configImportGEOTREK.geotrekInstance[structure].structures[element.structure].itineraireType[element.route];
476+
} else {
477+
itineraire.itineraireType = configImportGEOTREK.itineraireType[element.route];
478+
}
476479
}
477480
if (element.slug) {
478481
const slugCategory = this.getSlugCategory(element);

modules/products/server/models/product.server.model.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,13 @@ ProductSchema.statics.getSITRAInternalCriteriaReference =
341341
* Import
342342
*
343343
* @param {String} type
344+
* @param {String} instance
344345
* @param {function} callback
345346
*/
346-
ProductSchema.statics.import = function (type, callback) {
347+
ProductSchema.statics.import = function (type, instance, callback) {
347348
if (config.debug && config.debug.logs)
348349
console.log('ProductSchema.statics.import');
349-
ImportModel.import(type, callback);
350+
ImportModel.import(type, instance, callback);
350351
};
351352

352353
/**

0 commit comments

Comments
 (0)