Skip to content

Instantly share code, notes, and snippets.

@saifulapm
Last active August 16, 2021 19:10
Show Gist options
  • Select an option

  • Save saifulapm/f5e9964b5df089e4cbc3e843e0774cfe to your computer and use it in GitHub Desktop.

Select an option

Save saifulapm/f5e9964b5df089e4cbc3e843e0774cfe to your computer and use it in GitHub Desktop.
var dataProducts = [];
var totalProducts = 0;
var dataLoaded = false;
var totalPage = 0;
var totalUpdated = 0;
var maxLimit = 13000;
function getCategory(type, total = 0) {
var brand = $("#brand option:selected").val();
var year = $("#years option:selected").val();
var make = $("#make option:selected").val();
var model = $("#model option:selected").val();
var subModel = $("#subModel option:selected").val();
var engine = $("#engine option:selected").val();
var categoryId = $("#category option:selected").val();
var partId = $("#parts option:selected").val();
if (type == 'next') {
if (total != 0) {
var No = total;
} else {
var No = dataProducts['0']['Properties']['endingRecord'];
}
} else {
var No = "";
}
var recordsPerPage = $('#optionTopPartsPerPage').val();
var skuName = $('#textSearch').val().trim();
var skuName = encodeURIComponent(skuName.toUpperCase());
var value = $('#value').val();
var now = new Date();
var n = now.getTime();
$.ajax({
type: 'post',
url: 'api/makeEndecaCall/ts=' + n + '',
async: true,
cache: false,
headers: {
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'
},
data: {
recordsPerPage: 50,
year: year,
make: make,
model: model,
subModel: subModel,
engine: engine,
brand: brand,
No: No,
skuName: skuName,
categoryId: categoryId,
partId: partId,
type: "category"
},
statusCode: {
500: function() {
alert("Something Went Wrong");
}
},
success: function(data) {
totalProducts = data['0']['Properties']['totalNumberOfRecords'];
dataProducts = data;
if (total != 0) {
config.count = 0;
}
dataLoaded = true;
},
});
}
var config = {
count: 0
};
function syncStore(callback) {
if (dataProducts[config.count] !== undefined && dataLoaded && dataProducts['0']['Properties']['endingRecord'] <= maxLimit) {
var item = dataProducts[config.count];
var total = parseInt(item['Properties']['GPTStock']) + parseInt(item['Properties']['ILStock']) + parseInt(item['Properties']['NVStock']) + parseInt(item['Properties']['PERUStock']) + parseInt(item['Properties']['VAStock']);
var local_data = {
'action': 'syncNow',
'quantity': total,
'sku': item['sku']
};
$.ajax({
type: 'POST',
url: "https://portal.rancynautoparts.com/import",
dataType: 'json',
data: local_data,
success: function(data) {
//console.log(data);
config.count = config.count + 1;
totalUpdated = totalUpdated + 1;
setTimeout(callback, 500);
},
error: function() {
var local_data = {
'action': 'addError',
'sku': item['sku']
};
$.ajax({
type: 'POST',
url: "https://portal.rancynautoparts.com/errors",
dataType: 'json',
data: local_data
}).done(function(json) {
config.count = config.count + 1;
setTimeout(callback, 500);
});
}
});
} else if(dataLoaded) {
if(totalProducts > dataProducts['0']['Properties']['endingRecord'] && dataProducts['0']['Properties']['endingRecord'] <= maxLimit) {
dataLoaded = false;
totalPage = totalPage + 1;
getCategory('next');
config.count = 0;
setTimeout(callback, 500);
} else {
console.log("Completed " + maxLimit);
}
} else {
setTimeout(callback, 500);
}
}
// Change Brand Here
$('#brand').val('Replacement').trigger('change');
(function syncNow() {
syncStore(syncNow);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment