exercises

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

inventory_seed.sql (1541B)


      1 -- ============================================================
      2 -- Inventory seed data
      3 -- ============================================================
      4 -- products_test_data.sql only populates the products schema. The inventory
      5 -- service reads inventory.product_inventory / inventory.product_pricing, so
      6 -- without this backfill every reserve_stock() call returns FALSE ("Insufficient
      7 -- stock available") because the product has no inventory row at all.
      8 
      9 SET search_path TO inventory, public;
     10 
     11 INSERT INTO product_inventory (
     12   product_uuid, product_asin, stock_quantity, reserved_quantity,
     13   stock_status, last_restocked_at
     14 )
     15 SELECT
     16   p.uuid,
     17   p.asin,
     18   COALESCE(p.stock_quantity, 0),
     19   0,
     20   CASE
     21     WHEN COALESCE(p.stock_quantity, 0) = 0 THEN 'out_of_stock'
     22     WHEN p.stock_quantity <= 10 THEN 'low_stock'
     23     ELSE 'in_stock'
     24   END,
     25   CURRENT_TIMESTAMP
     26 FROM products.products p
     27 WHERE p.is_active AND p.deleted_at IS NULL
     28 ON CONFLICT (product_uuid) DO NOTHING;
     29 
     30 INSERT INTO product_pricing (
     31   product_uuid, product_asin, final_price, initial_price, currency,
     32   discount_percentage, discount_amount, is_active
     33 )
     34 SELECT
     35   p.uuid,
     36   p.asin,
     37   p.price,
     38   p.initial_price,
     39   COALESCE(p.currency, 'USD'),
     40   -- products.discount is text like '13%'
     41   NULLIF(regexp_replace(COALESCE(p.discount, ''), '[^0-9.]', '', 'g'), '')::numeric,
     42   CASE WHEN p.initial_price IS NOT NULL THEN ROUND(p.initial_price - p.price, 2) END,
     43   true
     44 FROM products.products p
     45 WHERE p.is_active AND p.deleted_at IS NULL AND p.price IS NOT NULL
     46 ON CONFLICT DO NOTHING;