2 Commits

Author SHA1 Message Date
dd806bbb1e feat: create procedure for retrieving products 2026-03-19 15:44:42 +01:00
Daniel Goc
b6bf6ed5c6 small debug 2026-03-19 10:39:27 +01:00
3 changed files with 230 additions and 5 deletions

View File

@@ -610,10 +610,10 @@ func (s *AuthService) GetLangISOCode(langID uint) (string, error) {
var lang string
if langID == 0 { // retrieve the default lang
err := db.DB.Table("b2b_language").Where("is_default = ?", 1).First(lang).Error
err := db.DB.Table("b2b_language").Where("is_default = ?", 1).Select("iso_code").Scan(&lang).Error
return lang, err
} else {
err := db.DB.Table("b2b_language").Where("id = ?", langID).Where("active = ?", 1).First(lang).Error
err := db.DB.Table("b2b_language").Where("id = ?", langID).Where("active = ?", 1).Select("iso_code").Scan(&lang).Error
return lang, err
}
}

View File

@@ -72,7 +72,8 @@ CREATE TABLE IF NOT EXISTS b2b_customers (
password_reset_expires DATETIME(6) NULL,
last_password_reset_request DATETIME(6) NULL,
last_login_at DATETIME(6) NULL,
lang VARCHAR(10) NULL DEFAULT 'en',
lang_id BIGINT NULL DEFAULT 2,
country_id BIGINT NULL DEFAULT 2,
created_at DATETIME(6) NULL,
updated_at DATETIME(6) NULL,
deleted_at DATETIME(6) NULL
@@ -114,9 +115,9 @@ CREATE INDEX IF NOT EXISTS idx_refresh_tokens_customer_id ON b2b_refresh_tokens
-- insert sample admin user admin@ma-al.com/Maal12345678
INSERT IGNORE INTO b2b_customers (id, email, password, first_name, last_name, role, provider, provider_id, avatar_url, is_active, email_verified, email_verification_token, email_verification_expires, password_reset_token, password_reset_expires, last_password_reset_request, last_login_at, lang, created_at, updated_at, deleted_at)
INSERT IGNORE INTO b2b_customers (id, email, password, first_name, last_name, role, provider, provider_id, avatar_url, is_active, email_verified, email_verification_token, email_verification_expires, password_reset_token, password_reset_expires, last_password_reset_request, last_login_at, lang_id, country_id, created_at, updated_at, deleted_at)
VALUES
(1, 'admin@ma-al.com', '$2a$10$Owy9DjrS0l3Fz4XoOvh5pulgmOMqdwXmb7hYE9BovnSuWS2plGr82', 'Super', 'Admin', 'admin', 'local', '', '', 1, 1, NULL, NULL, '', NULL, NULL, NULL, 'pl', '2026-03-02 16:55:10.252740', '2026-03-02 16:55:10.252740', NULL);
(1, 'admin@ma-al.com', '$2a$10$Owy9DjrS0l3Fz4XoOvh5pulgmOMqdwXmb7hYE9BovnSuWS2plGr82', 'Super', 'Admin', 'admin', 'local', '', '', 1, 1, NULL, NULL, '', NULL, NULL, NULL, 1, 1, '2026-03-02 16:55:10.252740', '2026-03-02 16:55:10.252740', NULL);
ALTER TABLE b2b_customers AUTO_INCREMENT = 1;
-- countries

View File

@@ -0,0 +1,224 @@
-- +goose Up
DELIMITER //
DROP PROCEDURE IF EXISTS get_full_product //
CREATE PROCEDURE get_full_product(
IN p_id_product INT UNSIGNED,
IN p_id_shop INT UNSIGNED,
IN p_id_lang INT UNSIGNED,
IN p_id_customer INT UNSIGNED,
IN p_id_group INT UNSIGNED,
IN p_id_currency INT UNSIGNED,
IN p_id_country INT UNSIGNED,
IN p_quantity INT UNSIGNED
)
BEGIN
DECLARE v_tax_rate DECIMAL(10,4) DEFAULT 0;
SELECT COALESCE(t.rate, 0.0000) INTO v_tax_rate
FROM ps_tax_rule tr
INNER JOIN ps_tax t
ON t.id_tax = tr.id_tax
WHERE tr.id_tax_rules_group = (
SELECT ps.id_tax_rules_group
FROM ps_product_shop ps
WHERE ps.id_product = p_id_product
AND ps.id_shop = p_id_shop
LIMIT 1
)
AND tr.id_country = p_id_country
ORDER BY
tr.id_state DESC,
tr.zipcode_from != '' DESC,
tr.id_tax_rule DESC
LIMIT 1;
/* FINAL JSON */
SELECT JSON_OBJECT(
/* ================= PRODUCT ================= */
'id_product', p.id_product,
'reference', p.reference,
'name', pl.name,
'description', pl.description,
'short_description', pl.description_short,
/* ================= PRICE ================= */
'price', JSON_OBJECT(
'base', COALESCE(ps.price, p.price),
'final_tax_excl',
(
COALESCE(ps.price, p.price)
- IFNULL(
CASE
WHEN sp.reduction_type = 'amount' THEN sp.reduction
WHEN sp.reduction_type = 'percentage' THEN COALESCE(ps.price, p.price) * sp.reduction
ELSE 0
END, 0
)
),
'final_tax_incl',
(
(
COALESCE(ps.price, p.price)
- IFNULL(
CASE
WHEN sp.reduction_type = 'amount' THEN sp.reduction
WHEN sp.reduction_type = 'percentage' THEN COALESCE(ps.price, p.price) * sp.reduction
ELSE 0
END, 0
)
) * (1 + v_tax_rate / 100)
)
),
/* ================= META ================= */
'active', COALESCE(ps.active, p.active),
'visibility', COALESCE(ps.visibility, p.visibility),
'manufacturer', m.name,
'category', cl.name,
/* ================= IMAGE ================= */
'cover_image', JSON_OBJECT(
'id', i.id_image,
'legend', il.legend
),
/* ================= FEATURES ================= */
'features', (
SELECT JSON_ARRAYAGG(
JSON_OBJECT(
'name', fl.name,
'value', fvl.value
)
)
FROM ps_feature_product fp
JOIN ps_feature_lang fl
ON fl.id_feature = fp.id_feature AND fl.id_lang = p_id_lang
JOIN ps_feature_value_lang fvl
ON fvl.id_feature_value = fp.id_feature_value AND fvl.id_lang = p_id_lang
WHERE fp.id_product = p.id_product
),
/* ================= COMBINATIONS ================= */
'combinations', (
SELECT JSON_ARRAYAGG(
JSON_OBJECT(
'id_product_attribute', pa.id_product_attribute,
'reference', pa.reference,
'price', JSON_OBJECT(
'impact', COALESCE(pas.price, pa.price),
'final_tax_excl',
(
COALESCE(ps.price, p.price)
+ COALESCE(pas.price, pa.price)
),
'final_tax_incl',
(
(
COALESCE(ps.price, p.price)
+ COALESCE(pas.price, pa.price)
) * (1 + v_tax_rate / 100)
)
),
'stock', IFNULL(sa.quantity, 0),
'default_on', pas.default_on,
/* ATTRIBUTES JSON */
'attributes', (
SELECT JSON_ARRAYAGG(
JSON_OBJECT(
'group', agl.name,
'attribute', al.name
)
)
FROM ps_product_attribute_combination pac
JOIN ps_attribute a ON a.id_attribute = pac.id_attribute
JOIN ps_attribute_lang al
ON al.id_attribute = a.id_attribute AND al.id_lang = p_id_lang
JOIN ps_attribute_group_lang agl
ON agl.id_attribute_group = a.id_attribute_group AND agl.id_lang = p_id_lang
WHERE pac.id_product_attribute = pa.id_product_attribute
),
/* IMAGES */
'images', (
SELECT JSON_ARRAYAGG(img.id_image)
FROM ps_product_attribute_image pai
JOIN ps_image img ON img.id_image = pai.id_image
WHERE pai.id_product_attribute = pa.id_product_attribute
)
)
)
FROM ps_product_attribute pa
JOIN ps_product_attribute_shop pas
ON pas.id_product_attribute = pa.id_product_attribute
AND pas.id_shop = p_id_shop
LEFT JOIN ps_stock_available sa
ON sa.id_product = pa.id_product
AND sa.id_product_attribute = pa.id_product_attribute
AND sa.id_shop = p_id_shop
WHERE pa.id_product = p.id_product
)
) AS product_json
FROM ps_product p
LEFT JOIN ps_product_shop ps
ON ps.id_product = p.id_product AND ps.id_shop = p_id_shop
LEFT JOIN ps_product_lang pl
ON pl.id_product = p.id_product
AND pl.id_lang = p_id_lang
AND pl.id_shop = p_id_shop
LEFT JOIN ps_category_lang cl
ON cl.id_category = COALESCE(ps.id_category_default, p.id_category_default)
AND cl.id_lang = p_id_lang
AND cl.id_shop = p_id_shop
LEFT JOIN ps_manufacturer m
ON m.id_manufacturer = p.id_manufacturer
LEFT JOIN ps_image i
ON i.id_product = p.id_product AND i.cover = 1
LEFT JOIN ps_image_lang il
ON il.id_image = i.id_image AND il.id_lang = p_id_lang
/* SPECIFIC PRICE */
LEFT JOIN (
SELECT sp1.*
FROM ps_specific_price sp1
WHERE sp1.id_product = p_id_product
AND (sp1.id_customer = 0 OR sp1.id_customer = p_id_customer)
AND (sp1.id_group = 0 OR sp1.id_group = p_id_group)
AND (sp1.id_currency = 0 OR sp1.id_currency = p_id_currency)
AND sp1.from_quantity <= p_quantity
ORDER BY
sp1.id_customer DESC,
sp1.id_group DESC,
sp1.from_quantity DESC,
sp1.id_specific_price DESC
LIMIT 1
) sp ON sp.id_product = p.id_product
WHERE p.id_product = p_id_product
LIMIT 1;
END //
DELIMITER ;
-- +goose Down