Added promotion pricing calculation in panel

This commit is contained in:
Pinga 2023-12-15 07:57:01 +02:00
parent a8a4c44590
commit be28985948
3 changed files with 150 additions and 72 deletions

View file

@ -308,8 +308,16 @@ document.addEventListener("DOMContentLoaded", function() {
const priceValue = document.getElementById('domainPrice'); const priceValue = document.getElementById('domainPrice');
function extractTLD(domain) { function extractTLD(domain) {
const match = domain.match(/\.[a-zA-Z0-9]+$/); const parts = domain.split('.');
return match ? match[0].toLowerCase() : null;
// If the domain has more than two segments (e.g., 'test.com.test'), return the last two segments
if (parts.length > 2) {
return parts.slice(-2).join('.').toLowerCase();
}
// If the domain has two or fewer segments (e.g., 'test.test' or 'test'), return the last segment
else {
return parts[parts.length - 1].toLowerCase();
}
} }
function getDomainPrice(domain, years) { function getDomainPrice(domain, years) {
@ -318,30 +326,48 @@ document.addEventListener("DOMContentLoaded", function() {
return Promise.reject("Invalid TLD"); return Promise.reject("Invalid TLD");
} }
// Call your API to get the domain price based on its TLD. // Regular expression for exact TLD match
return fetch(`/api/records/domain_price?join=domain_tld`) const tldRegex = new RegExp(`${tld.toLowerCase()}$`);
.then(response => {
if (response.ok) { // Fetch both promotional pricing and regular pricing
return response.json(); return Promise.all([
} else { fetch(`/api/records/promotion_pricing?join=domain_tld`).then(response => response.json()),
return Promise.reject("Failed to fetch domain prices"); fetch(`/api/records/domain_price?join=domain_tld`).then(response => response.json())
} ])
}) .then(([promoData, pricingData]) => {
.then(data => { const today = new Date();
// Find the domain TLD and then get its price for the "create" command
const tldData = data.records.find(record => // Check for a valid promotion
record.tldid && record.tldid.tld.toLowerCase() === tld.toLowerCase() && const promo = promoData.records.find(record =>
record.command === 'create' record.tld_id && tldRegex.test(record.tld_id.tld.toLowerCase()) &&
); new Date(record.start_date) <= today &&
if (tldData) { new Date(record.end_date) >= today &&
const priceField = `m${years * 12}`; (!record.years_of_promotion || record.years_of_promotion >= years)
const price = parseFloat(tldData[priceField]); );
if (!isNaN(price)) {
return price; // Find the regular price for the TLD
const tldData = pricingData.records.find(record =>
record.tldid && tldRegex.test(record.tldid.tld.toLowerCase()) &&
record.command === 'create'
);
if (tldData) {
const priceField = `m${years * 12}`;
let price = parseFloat(tldData[priceField]);
if (!isNaN(price)) {
if (promo) {
// Apply the promotion discount
price -= (price * parseFloat(promo.discount_percentage) / 100);
} }
return price;
} }
return Promise.reject("TLD price not found"); }
}); return Promise.reject("TLD price not found");
})
.catch(error => {
console.error("Error fetching pricing data:", error);
return Promise.reject("Error fetching pricing data");
});
} }
function formatPrice(price) { function formatPrice(price) {

View file

@ -105,8 +105,16 @@ document.addEventListener("DOMContentLoaded", function() {
const priceValue = document.getElementById('domainPrice'); const priceValue = document.getElementById('domainPrice');
function extractTLD(domain) { function extractTLD(domain) {
const match = domain.match(/\.[a-zA-Z0-9]+$/); const parts = domain.split('.');
return match ? match[0].toLowerCase() : null;
// If the domain has more than two segments (e.g., 'test.com.test'), return the last two segments
if (parts.length > 2) {
return parts.slice(-2).join('.').toLowerCase();
}
// If the domain has two or fewer segments (e.g., 'test.test' or 'test'), return the last segment
else {
return parts[parts.length - 1].toLowerCase();
}
} }
function getDomainPrice(domain, years) { function getDomainPrice(domain, years) {
@ -115,30 +123,48 @@ document.addEventListener("DOMContentLoaded", function() {
return Promise.reject("Invalid TLD"); return Promise.reject("Invalid TLD");
} }
// Call your API to get the domain price based on its TLD. // Regular expression for exact TLD match
return fetch(`/api/records/domain_price?join=domain_tld`) const tldRegex = new RegExp(`${tld.toLowerCase()}$`);
.then(response => {
if (response.ok) { // Fetch both promotional pricing and regular pricing
return response.json(); return Promise.all([
} else { fetch(`/api/records/promotion_pricing?join=domain_tld`).then(response => response.json()),
return Promise.reject("Failed to fetch domain prices"); fetch(`/api/records/domain_price?join=domain_tld`).then(response => response.json())
} ])
}) .then(([promoData, pricingData]) => {
.then(data => { const today = new Date();
// Find the domain TLD and then get its price for the "renew" command
const tldData = data.records.find(record => // Check for a valid promotion
record.tldid && record.tldid.tld.toLowerCase() === tld.toLowerCase() && const promo = promoData.records.find(record =>
record.command === 'renew' record.tld_id && tldRegex.test(record.tld_id.tld.toLowerCase()) &&
); new Date(record.start_date) <= today &&
if (tldData) { new Date(record.end_date) >= today &&
const priceField = `m${years * 12}`; (!record.years_of_promotion || record.years_of_promotion >= years)
const price = parseFloat(tldData[priceField]); );
if (!isNaN(price)) {
return price; // Find the regular price for the TLD
const tldData = pricingData.records.find(record =>
record.tldid && tldRegex.test(record.tldid.tld.toLowerCase()) &&
record.command === 'renew'
);
if (tldData) {
const priceField = `m${years * 12}`;
let price = parseFloat(tldData[priceField]);
if (!isNaN(price)) {
if (promo) {
// Apply the promotion discount
price -= (price * parseFloat(promo.discount_percentage) / 100);
} }
return price;
} }
return Promise.reject("TLD price not found"); }
}); return Promise.reject("TLD price not found");
})
.catch(error => {
console.error("Error fetching pricing data:", error);
return Promise.reject("Error fetching pricing data");
});
} }
function formatPrice(price) { function formatPrice(price) {

View file

@ -110,8 +110,16 @@ document.addEventListener("DOMContentLoaded", function() {
const priceValue = document.getElementById('domainPrice'); const priceValue = document.getElementById('domainPrice');
function extractTLD(domain) { function extractTLD(domain) {
const match = domain.match(/\.[a-zA-Z0-9]+$/); const parts = domain.split('.');
return match ? match[0].toLowerCase() : null;
// If the domain has more than two segments (e.g., 'test.com.test'), return the last two segments
if (parts.length > 2) {
return parts.slice(-2).join('.').toLowerCase();
}
// If the domain has two or fewer segments (e.g., 'test.test' or 'test'), return the last segment
else {
return parts[parts.length - 1].toLowerCase();
}
} }
function getDomainPrice(domain, years) { function getDomainPrice(domain, years) {
@ -120,30 +128,48 @@ document.addEventListener("DOMContentLoaded", function() {
return Promise.reject("Invalid TLD"); return Promise.reject("Invalid TLD");
} }
// Call your API to get the domain price based on its TLD. // Regular expression for exact TLD match
return fetch(`/api/records/domain_price?join=domain_tld`) const tldRegex = new RegExp(`${tld.toLowerCase()}$`);
.then(response => {
if (response.ok) { // Fetch both promotional pricing and regular pricing
return response.json(); return Promise.all([
} else { fetch(`/api/records/promotion_pricing?join=domain_tld`).then(response => response.json()),
return Promise.reject("Failed to fetch domain prices"); fetch(`/api/records/domain_price?join=domain_tld`).then(response => response.json())
} ])
}) .then(([promoData, pricingData]) => {
.then(data => { const today = new Date();
// Find the domain TLD and then get its price for the "transfer" command
const tldData = data.records.find(record => // Check for a valid promotion
record.tldid && record.tldid.tld.toLowerCase() === tld.toLowerCase() && const promo = promoData.records.find(record =>
record.command === 'transfer' record.tld_id && tldRegex.test(record.tld_id.tld.toLowerCase()) &&
); new Date(record.start_date) <= today &&
if (tldData) { new Date(record.end_date) >= today &&
const priceField = `m${years * 12}`; (!record.years_of_promotion || record.years_of_promotion >= years)
const price = parseFloat(tldData[priceField]); );
if (!isNaN(price)) {
return price; // Find the regular price for the TLD
const tldData = pricingData.records.find(record =>
record.tldid && tldRegex.test(record.tldid.tld.toLowerCase()) &&
record.command === 'transfer'
);
if (tldData) {
const priceField = `m${years * 12}`;
let price = parseFloat(tldData[priceField]);
if (!isNaN(price)) {
if (promo) {
// Apply the promotion discount
price -= (price * parseFloat(promo.discount_percentage) / 100);
} }
return price;
} }
return Promise.reject("TLD price not found"); }
}); return Promise.reject("TLD price not found");
})
.catch(error => {
console.error("Error fetching pricing data:", error);
return Promise.reject("Error fetching pricing data");
});
} }
function formatPrice(price) { function formatPrice(price) {