feat: roles, permissions
This commit is contained in:
@@ -43,7 +43,42 @@ CREATE TABLE IF NOT EXISTS b2b_translations (
|
||||
CONSTRAINT fk_translations_component FOREIGN KEY (component_id) REFERENCES b2b_components(id) ON DELETE RESTRICT ON UPDATE RESTRICT
|
||||
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;
|
||||
|
||||
CREATE TABLE `b2b_roles` (
|
||||
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
||||
`name` VARCHAR(63) NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
);
|
||||
CREATE UNIQUE INDEX `IX_b2b_roles_id`
|
||||
ON `b2b_roles` (
|
||||
`id` ASC
|
||||
);
|
||||
|
||||
CREATE TABLE b2b_permissions (
|
||||
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
||||
`name` VARCHAR(255) NOT NULL UNIQUE
|
||||
);
|
||||
|
||||
CREATE TABLE b2b_role_permissions (
|
||||
role_id BIGINT UNSIGNED NOT NULL,
|
||||
permission_id BIGINT UNSIGNED NOT NULL,
|
||||
|
||||
PRIMARY KEY (role_id, permission_id),
|
||||
|
||||
CONSTRAINT fk_role_permissions_role
|
||||
FOREIGN KEY (role_id)
|
||||
REFERENCES b2b_roles(id)
|
||||
ON DELETE CASCADE,
|
||||
|
||||
CONSTRAINT fk_role_permissions_permission
|
||||
FOREIGN KEY (permission_id)
|
||||
REFERENCES b2b_permissions(id)
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE TABLE `b2b_top_menu_roles` (
|
||||
`top_menu_id` BIGINT UNSIGNED NOT NULL,
|
||||
`role_id` BIGINT UNSIGNED NOT NULL
|
||||
);
|
||||
|
||||
-- customers
|
||||
CREATE TABLE IF NOT EXISTS b2b_customers (
|
||||
@@ -52,7 +87,7 @@ CREATE TABLE IF NOT EXISTS b2b_customers (
|
||||
password VARCHAR(255) NULL,
|
||||
first_name VARCHAR(100) NULL,
|
||||
last_name VARCHAR(100) NULL,
|
||||
role VARCHAR(20) NULL DEFAULT 'user',
|
||||
role_id BIGINT UNSIGNED NOT NULL DEFAULT 1,
|
||||
provider VARCHAR(20) NULL DEFAULT 'local',
|
||||
provider_id VARCHAR(255) NULL,
|
||||
avatar_url VARCHAR(500) NULL,
|
||||
@@ -77,6 +112,9 @@ ON b2b_customers (email);
|
||||
CREATE INDEX IF NOT EXISTS idx_customers_deleted_at
|
||||
ON b2b_customers (deleted_at);
|
||||
|
||||
ALTER TABLE b2b_customers
|
||||
ADD CONSTRAINT fk_customer_role
|
||||
FOREIGN KEY (role_id) REFERENCES b2b_roles(id);
|
||||
|
||||
-- customer_carts
|
||||
CREATE TABLE IF NOT EXISTS b2b_customer_carts (
|
||||
@@ -175,16 +213,7 @@ CREATE TABLE b2b_specific_price (
|
||||
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
|
||||
is_active BOOLEAN DEFAULT TRUE
|
||||
) ENGINE = InnoDB;
|
||||
CREATE INDEX idx_b2b_scope ON b2b_specific_price(scope);
|
||||
CREATE INDEX idx_b2b_customer ON b2b_specific_price(b2b_id_customer);
|
||||
@@ -344,6 +373,60 @@ END$$
|
||||
|
||||
DELIMITER ;
|
||||
|
||||
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;
|
||||
|
||||
@@ -321,19 +321,36 @@ AND (
|
||||
)
|
||||
)
|
||||
|
||||
ORDER BY
|
||||
/* 🔥 STRICT PRIORITY */
|
||||
ORDER BY
|
||||
/* 🔥 SCOPE PRIORITY */
|
||||
bsp.scope = 'product' DESC,
|
||||
bsp.scope = 'category' DESC,
|
||||
bsp.scope = 'shop' DESC,
|
||||
|
||||
bsp.b2b_id_customer DESC,
|
||||
bsp.b2b_id_country DESC,
|
||||
/* 🔥 CUSTOMER PRIORITY */
|
||||
(
|
||||
EXISTS (
|
||||
SELECT 1 FROM b2b_specific_price_customer c
|
||||
WHERE c.b2b_specific_price_id = bsp.id
|
||||
AND c.b2b_id_customer = p_id_customer
|
||||
)
|
||||
) DESC,
|
||||
|
||||
/* 🔥 COUNTRY PRIORITY */
|
||||
(
|
||||
EXISTS (
|
||||
SELECT 1 FROM b2b_specific_price_country ctry
|
||||
WHERE ctry.b2b_specific_price_id = bsp.id
|
||||
AND ctry.b2b_id_country = b2b_id_country
|
||||
)
|
||||
) DESC,
|
||||
|
||||
/* GLOBAL fallback (no restrictions) naturally goes last */
|
||||
|
||||
bsp.from_quantity DESC,
|
||||
bsp.id DESC
|
||||
|
||||
LIMIT 1
|
||||
LIMIT 1
|
||||
) bsp ON 1=1
|
||||
LEFT JOIN b2b_currency_rates br_bsp
|
||||
ON br_bsp.b2b_id_currency = bsp.b2b_id_currency
|
||||
|
||||
Reference in New Issue
Block a user