feat: creat main products query

This commit is contained in:
2026-03-26 15:59:13 +01:00
parent df14eb5ae4
commit bd97ed1a3b
17 changed files with 959 additions and 229 deletions

View File

@@ -1,7 +1,7 @@
-- +goose Up
CREATE TABLE IF NOT EXISTS b2b_language (
id INT AUTO_INCREMENT PRIMARY KEY,
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
created_at DATETIME(6) NOT NULL,
updated_at DATETIME(6) NULL,
deleted_at DATETIME(6) NULL,
@@ -19,29 +19,22 @@ CREATE TABLE IF NOT EXISTS b2b_language (
CREATE INDEX IF NOT EXISTS idx_language_deleted_at
ON b2b_language (deleted_at);
INSERT IGNORE INTO b2b_language
(id, created_at, updated_at, deleted_at, name, iso_code, lang_code, date_format, date_format_short, rtl, is_default, active, flag)
VALUES
(1, '2022-09-16 17:10:02.837', '2026-03-02 21:24:36.779730', NULL, 'Polski', 'pl', 'pl', '__-__-____', '__-__', 0, 0, 1, '🇵🇱'),
(2, '2022-09-16 17:10:02.852', '2026-03-02 21:24:36.779730', NULL, 'English', 'en', 'en', '__-__-____', '__-__', 0, 1, 1, '🇬🇧'),
(3, '2022-09-16 17:10:02.852', '2026-03-02 21:24:36.779730', NULL, 'Deutsch', 'de', 'de', '__-__-____', '__-__', 0, 0, 1, '🇩🇪');
CREATE TABLE IF NOT EXISTS b2b_components (
id INT AUTO_INCREMENT PRIMARY KEY,
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;
CREATE UNIQUE INDEX IF NOT EXISTS uk_components_name ON b2b_components (name, id);
-- scopes
CREATE TABLE IF NOT EXISTS b2b_scopes (
id INT AUTO_INCREMENT PRIMARY KEY,
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;
CREATE UNIQUE INDEX IF NOT EXISTS uk_scopes_name ON b2b_scopes (name);
-- translations
CREATE TABLE IF NOT EXISTS b2b_translations (
lang_id INT NOT NULL,
scope_id INT NOT NULL,
component_id INT NOT NULL,
lang_id INT UNSIGNED NOT NULL,
scope_id INT UNSIGNED NOT NULL,
component_id INT UNSIGNED NOT NULL,
`key` VARCHAR(255) NOT NULL,
data TEXT NULL,
PRIMARY KEY (lang_id, scope_id, component_id, `key`),
@@ -71,8 +64,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_id BIGINT NULL DEFAULT 2,
country_id BIGINT NULL DEFAULT 2,
lang_id INT NULL DEFAULT 2,
country_id INT NULL DEFAULT 2,
created_at DATETIME(6) NULL,
updated_at DATETIME(6) NULL,
deleted_at DATETIME(6) NULL
@@ -87,7 +80,7 @@ ON b2b_customers (deleted_at);
-- customer_carts
CREATE TABLE IF NOT EXISTS b2b_customer_carts (
cart_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
cart_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
user_id BIGINT UNSIGNED NOT NULL,
name VARCHAR(255) NULL,
CONSTRAINT fk_customer_carts_customers FOREIGN KEY (user_id) REFERENCES b2b_customers(id) ON DELETE CASCADE ON UPDATE CASCADE
@@ -97,10 +90,10 @@ CREATE INDEX IF NOT EXISTS idx_customer_carts_user_id ON b2b_customer_carts (use
-- carts_products
CREATE TABLE IF NOT EXISTS b2b_carts_products (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
cart_id BIGINT UNSIGNED NOT NULL,
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
cart_id INT UNSIGNED NOT NULL,
product_id INT UNSIGNED NOT NULL,
product_attribute_id BIGINT NULL,
product_attribute_id INT NULL,
amount INT UNSIGNED NOT NULL,
CONSTRAINT fk_carts_products_customer_carts FOREIGN KEY (cart_id) REFERENCES b2b_customer_carts (cart_id) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT fk_carts_products_product FOREIGN KEY (product_id) REFERENCES ps_product (id_product) ON DELETE CASCADE ON UPDATE CASCADE
@@ -110,7 +103,7 @@ CREATE INDEX IF NOT EXISTS idx_carts_products_cart_id ON b2b_carts_products (car
-- refresh_tokens
CREATE TABLE IF NOT EXISTS b2b_refresh_tokens (
id INT AUTO_INCREMENT PRIMARY KEY,
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
customer_id BIGINT UNSIGNED NOT NULL,
token_hash VARCHAR(64) NOT NULL,
expires_at DATETIME(6) NOT NULL,
@@ -120,24 +113,146 @@ CREATE TABLE IF NOT EXISTS b2b_refresh_tokens (
CREATE UNIQUE INDEX IF NOT EXISTS uk_refresh_tokens_token_hash ON b2b_refresh_tokens (token_hash);
CREATE INDEX IF NOT EXISTS idx_refresh_tokens_customer_id ON b2b_refresh_tokens (customer_id);
CREATE TABLE `b2b_currencies` (
`id` BIGINT UNSIGNED AUTO_INCREMENT NOT NULL,
`ps_id_currency` INT UNSIGNED NOT NULL,
`is_default` TINYINT NOT NULL,
`is_active` TINYINT NOT NULL,
PRIMARY KEY (`id`)
)
ENGINE = InnoDB;
ALTER TABLE `b2b_currencies` ADD CONSTRAINT `FK_b2b_currencies_ps_id_currency` FOREIGN KEY (`ps_id_currency`) REFERENCES `ps_currency` (`id_currency`) ON DELETE CASCADE ON UPDATE CASCADE;
CREATE INDEX `fk_b2b_currencies_ps_currency`
ON `b2b_currencies` (
`ps_id_currency` ASC
);
CREATE TABLE `b2b_currency_rates` (
`id` BIGINT UNSIGNED AUTO_INCREMENT NOT NULL,
`b2b_id_currency` BIGINT UNSIGNED NOT NULL,
`created_at` DATETIME NOT NULL,
`conversion_rate` DECIMAL(13,6) NULL DEFAULT NULL ,
PRIMARY KEY (`id`)
)
ENGINE = InnoDB;
ALTER TABLE `b2b_currency_rates` ADD CONSTRAINT `FK_b2b_currency_rates_b2b_id_currency` FOREIGN KEY (`b2b_id_currency`) REFERENCES `b2b_currencies` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
CREATE INDEX `fk_b2b_currency_rates_b2b_currencies`
ON `b2b_currency_rates` (
`b2b_id_currency` ASC
);
-- countries
CREATE TABLE IF NOT EXISTS b2b_countries (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(128) NOT NULL,
currency_id INT UNSIGNED NOT NULL,
flag VARCHAR(16) NOT NULL,
CONSTRAINT fk_countries_currency FOREIGN KEY (currency_id) REFERENCES ps_currency(id_currency) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
`id` BIGINT UNSIGNED AUTO_INCREMENT NOT NULL,
`flag` VARCHAR(16) NOT NULL,
`ps_id_country` INT UNSIGNED NOT NULL,
`b2b_id_currency` BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk_b2b_countries_ps_country` FOREIGN KEY (`ps_id_country`) REFERENCES `ps_country` (`id_country`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `FK_b2b_countries_b2b_id_currency` FOREIGN KEY (`b2b_id_currency`) REFERENCES `b2b_currencies` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
)
ENGINE = InnoDB;
CREATE INDEX `fk_b2b_countries_ps_country`
ON `b2b_countries` (
`ps_id_country` ASC
);
INSERT IGNORE INTO b2b_countries
(id, name, currency_id, flag)
VALUES
(1, 'Polska', 1, '🇵🇱'),
(2, 'England', 2, '🇬🇧'),
(3, 'Čeština', 2, '🇨🇿'),
(4, 'Deutschland', 2, '🇩🇪');
CREATE TABLE b2b_specific_price (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
created_at DATETIME NULL,
updated_at DATETIME NULL,
deleted_at DATETIME NULL,
scope ENUM('shop', 'category', 'product') NOT NULL,
valid_from DATETIME NULL,
valid_till DATETIME NULL,
has_expiration_date BOOLEAN DEFAULT FALSE,
reduction_type ENUM('amount', 'percentage') NOT NULL,
price DECIMAL(10, 2) NULL,
b2b_id_currency BIGINT UNSIGNED NULL, -- specifies which currency is used for the price
percentage_reduction DECIMAL(5, 2) NULL,
from_quantity INT UNSIGNED DEFAULT 1,
is_active BOOLEAN DEFAULT TRUE,
CONSTRAINT fk_b2b_specific_price_country FOREIGN KEY (b2b_id_country) REFERENCES b2b_countries(id) ON DELETE
SET
NULL ON UPDATE CASCADE,
CONSTRAINT fk_b2b_specific_price_customer FOREIGN KEY (b2b_id_customer) REFERENCES b2b_customers(id) ON DELETE
SET
NULL ON UPDATE CASCADE,
CONSTRAINT fk_b2b_specific_price_currency FOREIGN KEY (b2b_id_currency) REFERENCES b2b_currencies(id) ON DELETE
SET
NULL ON UPDATE CASCADE
) ENGINE = InnoDB;
CREATE INDEX idx_b2b_scope ON b2b_specific_price(scope);
CREATE INDEX idx_b2b_customer ON b2b_specific_price(b2b_id_customer);
CREATE INDEX idx_b2b_country ON b2b_specific_price(b2b_id_country);
CREATE INDEX idx_b2b_active_dates ON b2b_specific_price(is_active, valid_from, valid_till);
CREATE INDEX idx_b2b_lookup
ON b2b_specific_price (
scope,
is_active,
b2b_id_customer,
b2b_id_country,
from_quantity
);
CREATE TABLE b2b_specific_price_product (
b2b_specific_price_id BIGINT UNSIGNED,
id_product INT UNSIGNED,
PRIMARY KEY (b2b_specific_price_id, id_product),
FOREIGN KEY (b2b_specific_price_id) REFERENCES b2b_specific_price(id) ON DELETE CASCADE,
FOREIGN KEY (id_product) REFERENCES ps_product(id_product) ON DELETE CASCADE
);
CREATE TABLE b2b_specific_price_category (
b2b_specific_price_id BIGINT UNSIGNED,
id_category INT UNSIGNED,
PRIMARY KEY (b2b_specific_price_id, id_category),
FOREIGN KEY (b2b_specific_price_id) REFERENCES b2b_specific_price(id) ON DELETE CASCADE,
FOREIGN KEY (id_category) REFERENCES ps_category(id_category) ON DELETE CASCADE
);
CREATE TABLE b2b_specific_price_product_attribute (
b2b_specific_price_id BIGINT UNSIGNED,
id_product_attribute INT UNSIGNED,
PRIMARY KEY (b2b_specific_price_id, id_product_attribute),
FOREIGN KEY (b2b_specific_price_id) REFERENCES b2b_specific_price(id) ON DELETE CASCADE,
FOREIGN KEY (id_product_attribute) REFERENCES ps_product_attribute(id_product_attribute) ON DELETE CASCADE
);
CREATE TABLE b2b_specific_price_customer (
b2b_specific_price_id BIGINT UNSIGNED,
b2b_id_customer BIGINT UNSIGNED,
PRIMARY KEY (b2b_specific_price_id, b2b_id_customer),
FOREIGN KEY (b2b_specific_price_id) REFERENCES b2b_specific_price(id) ON DELETE CASCADE,
FOREIGN KEY (b2b_id_customer) REFERENCES b2b_customers(id) ON DELETE CASCADE
);
CREATE TABLE b2b_specific_price_country (
b2b_specific_price_id BIGINT UNSIGNED,
b2b_id_country BIGINT UNSIGNED,
PRIMARY KEY (b2b_specific_price_id, b2b_id_country),
FOREIGN KEY (b2b_specific_price_id) REFERENCES b2b_specific_price(id) ON DELETE CASCADE,
FOREIGN KEY (b2b_id_country) REFERENCES b2b_countries(id) ON DELETE CASCADE
);
CREATE INDEX idx_b2b_product_rel
ON b2b_specific_price_product (id_product);
CREATE INDEX idx_b2b_category_rel
ON b2b_specific_price_category (id_category);
CREATE INDEX idx_b2b_product_attribute_rel
ON b2b_specific_price_product_attribute (id_product_attribute);
CREATE INDEX idx_bsp_customer
ON b2b_specific_price_customer (b2b_specific_price_id, b2b_id_customer);
CREATE INDEX idx_bsp_country
ON b2b_specific_price_country (b2b_specific_price_id, b2b_id_country);
-- +goose Down
DROP TABLE IF EXISTS b2b_countries;
@@ -147,3 +262,9 @@ DROP TABLE IF EXISTS b2b_scopes;
DROP TABLE IF EXISTS b2b_translations;
DROP TABLE IF EXISTS b2b_customers;
DROP TABLE IF EXISTS b2b_refresh_tokens;
DROP TABLE IF EXISTS b2b_currencies;
DROP TABLE IF EXISTS b2b_currency_rates;
DROP TABLE IF EXISTS b2b_specific_price;
DROP TABLE IF EXISTS b2b_specific_price_product;
DROP TABLE IF EXISTS b2b_specific_price_category;
DROP TABLE IF EXISTS b2b_specific_price_product_attribute;