Skip to content

Commit

Permalink
Merge pull request #17 from cedricWebsenso/main
Browse files Browse the repository at this point in the history
v1.2 - Import by instance + customize route type by structure
  • Loading branch information
websenso-team authored Jul 17, 2023
2 parents 5f9d690 + ebf8269 commit 274a020
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 72 deletions.
38 changes: 18 additions & 20 deletions config/configImport_GEOTREK.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,35 +80,33 @@ exports.geotrekInstance = {
}
}
},
/*1 : {
geotrekUrl : 'https://admin.cheminsdesparcs.fr/api/v2x',
1 : {
geotrekUrl : 'https://admin.sportsnature.hauteloire.fr/api/v2',
structures : {
3568: { // MULTI MEMBER COOKING
specialId: 'ENT1RandoEcrins',
name: 'Parc national des Écrins',
1: { // CD43
specialId: 'HauteLoire1',
name: 'Maison du Tourisme de la Haute-Loire',
address1: null,
address2: null,
city: '1813',
insee: '05061',
specialIdSitra: '1856',
city: '16901',
insee: '43157',
specialIdSitra: '4633537',
statusImport: 2,
memberId : 1856,
production: false,
memberId : 1158,
production: true,
activity : {
0: 3333,
1: 3284,
2: 3283,
3: 3313,
4: 1992,
5: 5147,
4: 3333,
6: 4201,
7: 3333,
8: 4201,
9: 3333,
10: 3333,
11: 3333,
}
8: 3302
},
itineraireType : {
1: 'BOUCLE',
3: 'ALLER_ITINERANCE',
}
},
}
}*/
}
};
2 changes: 1 addition & 1 deletion modules/products/client/config/products.client.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ angular.module('products').run([
Menus.addSubMenuItem('topbar', 'products', {
title: 'Import Geotrek API',
state: 'products.import',
stateParams: { importType: 'geotrek-api' }
stateParams: { importType: 'geotrek-api', importInstance: '0' }
});
}
]);
2 changes: 1 addition & 1 deletion modules/products/client/config/products.client.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ angular.module('products').config([
templateUrl: 'modules/products/views/list-products.client.view.html'
})
.state('products.import', {
url: '/import/:importType',
url: '/import/:importType/:importInstance/',
templateUrl: 'modules/products/views/import-products.client.view.html'
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ angular.module('products').controller('ProductsController', [

$scope.import = function () {
var url = '/api/products/import';
if ($stateParams.importType) {
url += '?type=' + $stateParams.importType;
if ($stateParams.importType && $stateParams.importInstance) {
url += '?type=' + $stateParams.importType + '&instance=' + $stateParams.importInstance;
}
$http.get(url).then(
function (response) {
Expand Down
14 changes: 11 additions & 3 deletions modules/products/server/controllers/products.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var path = require('path'),
'./modules/core/server/controllers/errors.server.controller'
)),
config = require(__dirname + '/../../../../config/config.js'),
configImportGEOTREK = require(path.resolve('config/configImport_GEOTREK.js')),
_ = require('lodash');

/**
Expand All @@ -36,17 +37,24 @@ exports.list = async function (req, res) {
* @param {object} res
*/
exports.import = function (req, res) {
var type = req.query && req.query.type ? req.query.type : null;
const type = req.query && req.query.type ? req.query.type : null,
instance = req.query && req.query.instance ? req.query.instance : null;

if (config.debug && config.debug.logs)
console.log('Begin import auto for', type);

if (!type) {
throw 'Unable to determine type';
}

if (configImportGEOTREK.geotrekInstance[instance] === undefined)
{
throw 'Instance not found';
}

try {
res.json({ message: 'Importing ' + type + ' flux in progress...' });
Product.import(type);
res.json({ message: 'Importing ' + type + ' flux in progress from ' + configImportGEOTREK.geotrekInstance[instance].geotrekUrl });
Product.import(type, instance);
} catch (err) {
console.log('err = ', err);
}
Expand Down
4 changes: 2 additions & 2 deletions modules/products/server/models/exportSitra.server.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4024,7 +4024,7 @@ function __buildLegalEntity(product, root, rootFieldList) {

finalLegalEntity.informations.structureGestion = {
type: legalEntityObj.product.type,
id: legalEntityObj.product.specialIdSitra
id: (process.env.NODE_ENV == 'production') ? legalEntityObj.product.specialIdSitra : config.specialIdSitra
};
rootFieldList.push('informations.structureGestion');
break;
Expand All @@ -4036,7 +4036,7 @@ function __buildLegalEntity(product, root, rootFieldList) {

finalLegalEntity.informations.structureInformation = {
type: legalEntityObj.product.type,
id: legalEntityObj.product.specialIdSitra
id: (process.env.NODE_ENV == 'production') ? legalEntityObj.product.specialIdSitra : config.specialIdSitra
};
break;

Expand Down
12 changes: 8 additions & 4 deletions modules/products/server/models/import.server.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ var mongoose = require('mongoose'),
/**
* Import
* @param {String} type
* @param {String} instance
* @param {function} callback
*/
exports.import = function (type, callback) {
__importProductsbyApi(type, () => {
exports.import = function (type, instance, callback) {
console.log('> import.server.model ', type, instance);
__importProductsbyApi(type, instance, () => {
if (callback) {
callback();
}
Expand All @@ -22,10 +24,11 @@ exports.import = function (type, callback) {
/**
* Import product from sql
* @param {String} type
* @param {String} instance
* @param {function} callback
* @private
*/
function __importProductsbyApi(type, callback) {
function __importProductsbyApi(type, instance, callback) {
var User = mongoose.model('User');
User.findOne({
username: 'admin'
Expand All @@ -40,7 +43,8 @@ function __importProductsbyApi(type, callback) {
importObj = new ImportClass({
user: user,
lang: 'fr',
importType: type
importType: type,
importInstance: instance
});

importObj.start(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,12 @@ class ImportGenericGeotrekApi extends Import {
: null;
this.user = options.user ? options.user : null;
this.lang = options.lang ? options.lang : 'fr';

/*this.baseURL = 'https://geotrek-admin.ecrins-parcnational.fr/api/v2';
this.instanceApi = axios.create({
baseURL: this.baseURL,
validateStatus(status) {
return status < 500;
}
});*/
this.importInstance = options.importInstance ? parseInt(options.importInstance) : 0;
}

import(data, next) {
if (config.debug && config.debug.logs)
console.log('ImportGenericGeotrekApi.prototype.import');
console.log('ImportGenericGeotrekApi.prototype.import', this.importInstance);

const me = this,
nbInstanceGeotrek = Object.keys(
Expand All @@ -53,28 +46,31 @@ class ImportGenericGeotrekApi extends Import {
Object.keys(configImportGEOTREK.geotrekInstance).forEach(function (
structure
) {
console.log(
'Instance = ',
structure,
' - GeoAdmin URL = ',
configImportGEOTREK.geotrekInstance[structure].geotrekUrl
);

me.executeQuery(
0,
configImportGEOTREK.geotrekInstance[structure].geotrekUrl,
structure
).catch((err) => {
if (structure == me.importInstance)
{
console.log(
chalk.red(
'>>>>>>>>>>>>>>>>>>>>>>>> ERROR = ',
err,
'For instance = ',
structure
)
'Instance = ',
structure,
' - GeoAdmin URL = ',
configImportGEOTREK.geotrekInstance[structure].geotrekUrl
);
return false;
});

me.executeQuery(
0,
configImportGEOTREK.geotrekInstance[structure].geotrekUrl,
structure
).catch((err) => {
console.log(
chalk.red(
'>>>>>>>>>>>>>>>>>>>>>>>> OBJ NOT FOUND IN = ',
err,
'For instance = ',
structure
)
);
return false;
});
}
});
}

Expand All @@ -99,7 +95,7 @@ class ImportGenericGeotrekApi extends Import {
return status < 500;
}
});

console.log(chalk.green("Axiox Connex = ", geoTrekPath + urlNext));
const { data, status } = await this.instanceApi.get(geoTrekPath + urlNext);
if (status === 200) {
this.doUpsertAsync = await pify(importUtils.doUpsert);
Expand Down Expand Up @@ -203,9 +199,10 @@ class ImportGenericGeotrekApi extends Import {
break;
}*/

/*if (process.env.NODE_ENV == 'development' && config.debug != undefined) {
this.member = element.structure = 3568;
}*/
if (process.env.NODE_ENV == 'development' && config.debug != undefined) {
this.member = /*element.structure =*/ 3568;
console.log(chalk.green('DEV MODE - Member = ', this.member));
}

/* if (configImportGEOTREK.geotrekInstance[structure].structures[element.structure].production === false) {
if (
Expand All @@ -215,6 +212,8 @@ class ImportGenericGeotrekApi extends Import {
)
console.log('ImportGenericGeotrekApi.prototype.importProduct structure instance not in production', structure, element.structure);
} else {*/

console.log(chalk.green('Config = ', configImportGEOTREK.geotrekInstance[structure].structures[element.structure]));

if (this.member && configImportGEOTREK.geotrekInstance[structure].structures[element.structure].production) {
this.configData = {
Expand Down Expand Up @@ -282,7 +281,7 @@ class ImportGenericGeotrekApi extends Import {
complementNl: this.getComplement(element, 'nl'),
localization: this.getLocalization(element),
price: this.getPrice(element),
itinerary: await this.getItinerary(element),
itinerary: await this.getItinerary(element, structure),
perimetreGeographique: this.getPerimetreGeographique(element),
description: this.getDescription(element, 'fr'),
descriptionEn: this.getDescription(element, 'en'),
Expand Down Expand Up @@ -443,7 +442,7 @@ class ImportGenericGeotrekApi extends Import {
};
}

async getItinerary(element) {
async getItinerary(element, structure) {
const itineraire = {
dailyDuration: null,
distance: null,
Expand Down Expand Up @@ -471,8 +470,12 @@ class ImportGenericGeotrekApi extends Import {
itineraire.negative = DataString.convertNegative(element.descent);
}
if (element.route) {
itineraire.itineraireType =
configImportGEOTREK.itineraireType[element.route];
if (configImportGEOTREK.geotrekInstance[structure].structures[element.structure].itineraireType != undefined)
{
itineraire.itineraireType = configImportGEOTREK.geotrekInstance[structure].structures[element.structure].itineraireType[element.route];
} else {
itineraire.itineraireType = configImportGEOTREK.itineraireType[element.route];
}
}
if (element.slug) {
const slugCategory = this.getSlugCategory(element);
Expand Down
5 changes: 3 additions & 2 deletions modules/products/server/models/product.server.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,13 @@ ProductSchema.statics.getSITRAInternalCriteriaReference =
* Import
*
* @param {String} type
* @param {String} instance
* @param {function} callback
*/
ProductSchema.statics.import = function (type, callback) {
ProductSchema.statics.import = function (type, instance, callback) {
if (config.debug && config.debug.logs)
console.log('ProductSchema.statics.import');
ImportModel.import(type, callback);
ImportModel.import(type, instance, callback);
};

/**
Expand Down

0 comments on commit 274a020

Please sign in to comment.