Fix data coming in correctly when copying template from an existing product, automatically strip out deals and black friday categories
This commit is contained in:
@@ -526,7 +526,7 @@ router.get('/field-options', async (req, res) => {
|
||||
|
||||
// Fetch tax categories
|
||||
const [taxCategories] = await connection.query(`
|
||||
SELECT tax_code_id as value, name as label
|
||||
SELECT CAST(tax_code_id AS CHAR) as value, name as label
|
||||
FROM product_tax_codes
|
||||
ORDER BY tax_code_id = 0 DESC, name
|
||||
`);
|
||||
@@ -820,6 +820,8 @@ router.get('/search-products', async (req, res) => {
|
||||
s.companyname AS vendor,
|
||||
sid.supplier_itemnumber AS vendor_reference,
|
||||
sid.notions_itemnumber AS notions_reference,
|
||||
sid.supplier_id AS supplier,
|
||||
sid.notions_case_pack AS case_qty,
|
||||
pc1.name AS brand,
|
||||
p.company AS brand_id,
|
||||
pc2.name AS line,
|
||||
@@ -839,7 +841,10 @@ router.get('/search-products', async (req, res) => {
|
||||
p.country_of_origin,
|
||||
ci.totalsold AS total_sold,
|
||||
p.datein AS first_received,
|
||||
pls.date_sold AS date_last_sold
|
||||
pls.date_sold AS date_last_sold,
|
||||
IF(p.tax_code IS NULL, '', CAST(p.tax_code AS CHAR)) AS tax_code,
|
||||
CAST(p.size_cat AS CHAR) AS size_cat,
|
||||
CAST(p.shipping_restrictions AS CHAR) AS shipping_restrictions
|
||||
FROM products p
|
||||
LEFT JOIN product_current_prices pcp ON p.pid = pcp.pid AND pcp.active = 1
|
||||
LEFT JOIN supplier_item_data sid ON p.pid = sid.pid
|
||||
@@ -893,6 +898,21 @@ router.get('/search-products', async (req, res) => {
|
||||
|
||||
const [results] = await connection.query(query, queryParams);
|
||||
|
||||
// Debug log to check values
|
||||
if (results.length > 0) {
|
||||
console.log('Product search result sample fields:', {
|
||||
pid: results[0].pid,
|
||||
tax_code: results[0].tax_code,
|
||||
tax_code_type: typeof results[0].tax_code,
|
||||
tax_code_value: `Value: '${results[0].tax_code}'`,
|
||||
size_cat: results[0].size_cat,
|
||||
shipping_restrictions: results[0].shipping_restrictions,
|
||||
supplier: results[0].supplier,
|
||||
case_qty: results[0].case_qty,
|
||||
moq: results[0].moq
|
||||
});
|
||||
}
|
||||
|
||||
res.json(results);
|
||||
} catch (error) {
|
||||
console.error('Error searching products:', error);
|
||||
@@ -1008,7 +1028,7 @@ router.get('/product-categories/:pid', async (req, res) => {
|
||||
|
||||
// Query to get categories for a specific product
|
||||
const query = `
|
||||
SELECT pc.cat_id, pc.name, pc.type, pc.combined_name
|
||||
SELECT pc.cat_id, pc.name, pc.type, pc.combined_name, pc.master_cat_id
|
||||
FROM product_category_index pci
|
||||
JOIN product_categories pc ON pci.cat_id = pc.cat_id
|
||||
WHERE pci.pid = ?
|
||||
@@ -1023,8 +1043,61 @@ router.get('/product-categories/:pid', async (req, res) => {
|
||||
console.log(`Product ${pid} has ${rows.length} categories with types: ${uniqueTypes.join(', ')}`);
|
||||
console.log('Categories:', rows.map(row => ({ id: row.cat_id, name: row.name, type: row.type })));
|
||||
|
||||
// Check for parent categories to filter out deals and black friday
|
||||
const sectionQuery = `
|
||||
SELECT pc.cat_id, pc.name
|
||||
FROM product_categories pc
|
||||
WHERE pc.type = 10 AND (LOWER(pc.name) LIKE '%deal%' OR LOWER(pc.name) LIKE '%black friday%')
|
||||
`;
|
||||
|
||||
const [dealSections] = await connection.query(sectionQuery);
|
||||
const dealSectionIds = dealSections.map(section => section.cat_id);
|
||||
|
||||
console.log('Filtering out categories from deal sections:', dealSectionIds);
|
||||
|
||||
// Filter out categories from deals and black friday sections
|
||||
const filteredCategories = rows.filter(category => {
|
||||
// Direct check for top-level deal sections
|
||||
if (category.type === 10) {
|
||||
return !dealSectionIds.some(id => id === category.cat_id);
|
||||
}
|
||||
|
||||
// For categories (type 11), check if their parent is a deal section
|
||||
if (category.type === 11) {
|
||||
return !dealSectionIds.some(id => id === category.master_cat_id);
|
||||
}
|
||||
|
||||
// For subcategories (type 12), get their parent category first
|
||||
if (category.type === 12) {
|
||||
const parentId = category.master_cat_id;
|
||||
// Find the parent category in our rows
|
||||
const parentCategory = rows.find(c => c.cat_id === parentId);
|
||||
// If parent not found or parent's parent is not a deal section, keep it
|
||||
return !parentCategory || !dealSectionIds.some(id => id === parentCategory.master_cat_id);
|
||||
}
|
||||
|
||||
// For subsubcategories (type 13), check their hierarchy manually
|
||||
if (category.type === 13) {
|
||||
const parentId = category.master_cat_id;
|
||||
// Find the parent subcategory
|
||||
const parentSubcategory = rows.find(c => c.cat_id === parentId);
|
||||
if (!parentSubcategory) return true;
|
||||
|
||||
// Find the grandparent category
|
||||
const grandparentId = parentSubcategory.master_cat_id;
|
||||
const grandparentCategory = rows.find(c => c.cat_id === grandparentId);
|
||||
// If grandparent not found or grandparent's parent is not a deal section, keep it
|
||||
return !grandparentCategory || !dealSectionIds.some(id => id === grandparentCategory.master_cat_id);
|
||||
}
|
||||
|
||||
// Keep all other category types
|
||||
return true;
|
||||
});
|
||||
|
||||
console.log(`Filtered out ${rows.length - filteredCategories.length} deal/black friday categories`);
|
||||
|
||||
// Format the response to match the expected format in the frontend
|
||||
const categories = rows.map(category => ({
|
||||
const categories = filteredCategories.map(category => ({
|
||||
value: category.cat_id.toString(),
|
||||
label: category.name,
|
||||
type: category.type,
|
||||
|
||||
Reference in New Issue
Block a user