| Current Path : /home/deltalab/PMS/pim-connector/logic/akeneo/ |
| Current File : //home/deltalab/PMS/pim-connector/logic/akeneo/products.js |
// DEPENDENCIES ================================================
const axios = require('axios');
const auth = require('./auth');
//const { ProductModel } = require('./../../models/graphql/schemas/product');
class ProductModel{
sku;
barcode;
title;
description;
}
/**
* Return the product details for the product with the given barcode
* @param {*} id the barcode of the product
* @returns a ProductModel object populated with the matching result or null if no product could be found
*/
async function getProductDetails(id){
// Get the akeneo auth token
const authResponse = await auth.authToAkeneo();
// Sanity check
if(authResponse.result !== "success"){
console.log(`getProductDetails(${id}): authentication error ${authResponse.message}`);
return null;
}
// The authentication token
const authToken = { "Authorization": "Bearer "+authResponse.authToken }
// Build the search string
const requestUrl = process.env.AKENEO_URL + '/api/rest/v1/products?search={"barcode":[{"operator":"=","value":"'+id+'"}]}';
// Perform the request
let toReturn = null;
await axios.get(requestUrl, { headers: authToken } )
.then(success =>{
if(Object.keys(success.data._embedded.items).length > 0){
toReturn = new ProductModel();
// Filling the model according to the Akeneo response
toReturn.sku = success.data._embedded.items[0].identifier;
toReturn.title = success.data._embedded.items[0].values.name[0].data;
toReturn.description = success.data._embedded.items[0].values.description[0].data
}
}, reject =>{
console.log("ProductFetchError: " + JSON.stringify(reject.response.data));
});
console.log(`getProductDetails(${id}): ${(toReturn!==null)}`);
return toReturn;
}
async function getProductSuggestions(hint, type){
const authResponse = await auth.authToAkeneo();
// Sanity check
if(authResponse.result !== "success"){
console.log(`getProductSuggestions(${hint},${type}): authentication error ${authResponse.message}`);
return null;
}
// The authentication token
const authToken = { "Authorization": "Bearer "+authResponse.authToken }
// Build the search string
const requestUrl = process.env.AKENEO_URL + '/api/rest/v1/products?search={"'+type+'":[{"operator":"CONTAINS","value":"'+hint+'"}]}';
// Perform the request to akeneo
let toReturn = null;
await axios.get(requestUrl, { headers: authToken })
.then(success =>{
toReturn = null;
if(Object.keys(success.data._embedded.items).length > 0){
toReturn = new Array<ProductModel>(Object.keys(success.data._embedded.items).length);
var i=0;
success.data._embedded.items.forEach(item => {
toReturn[i] = new ProductModel();
// Filling the model according to the Akeneo response
toReturn[i].sku = item.identifier;
toReturn[i].title = item.values.name[0].data;
toReturn[i].description = item.values.description[0].data;
i++;
});
}
}, reject =>{
console.log("ProductFetchError: " + JSON.stringify(reject.response.data));
toReturn = null;
});
return toReturn;
}
exports.getProductDetails = getProductDetails;
exports.getProductSuggestions = getProductSuggestions