Changes between Version 6 and Version 7 of AdvancedDatabaseDevelopment


Ignore:
Timestamp:
06/05/26 15:32:31 (6 days ago)
Author:
232012
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • AdvancedDatabaseDevelopment

    v6 v7  
    3131* **Description:** This keeps customers from using multiple promotions in a single order. If someone tries to buy a product that is already on sale, this trigger stops them from using their store loyalty points on that same order. It forces the system to only allow one discount type at a time so the store doesn't lose money.
    3232}}}
     33* **Implementation:**
     34
     35{{{#!div style="text-align: justify; width: 100%;"
     36  First, we implement a trigger function that checks if an order combines loyalty points with a discounted product and blocks it if it does.
     37}}}
     38
     39{{{
     40CREATE OR REPLACE FUNCTION project.block_double_promo()
     41RETURNS TRIGGER AS $$
     42DECLARE
     43    used_points INTEGER;
     44    discounted_product_exists BOOLEAN;
     45BEGIN
     46    SELECT o.points_used
     47    INTO used_points
     48    FROM project.orders o
     49    WHERE o.order_id = NEW.order_id;
     50
     51    SELECT EXISTS (
     52        SELECT 1
     53        FROM project.modification_products mp
     54        JOIN project.modifications m
     55            ON m.modification_id = mp.modification_id
     56        WHERE mp.product_id = NEW.product_id
     57          AND m.type_of_modification = 'DISCOUNT'
     58          AND m.discount IS NOT NULL
     59    )
     60    INTO discounted_product_exists;
     61
     62    IF used_points > 0 AND discounted_product_exists THEN
     63        RAISE EXCEPTION
     64            'Double promotion is not allowed: loyalty points cannot be used on an order containing discounted products.';
     65    END IF;
     66
     67    RETURN NEW;
     68END;
     69$$ LANGUAGE plpgsql;
     70}}}
     71
     72{{{#!div style="text-align: justify; width: 100%;"
     73  We also create a trigger that runs when a product is added to an order.
     74}}}
     75
     76{{{
     77DROP TRIGGER IF EXISTS trg_block_double_promo_order_products
     78ON project.order_products;
     79
     80CREATE TRIGGER trg_block_double_promo_order_products
     81BEFORE INSERT OR UPDATE ON project.order_products
     82FOR EACH ROW
     83EXECUTE FUNCTION project.block_double_promo();
     84}}}
     85
     86{{{#!div style="text-align: justify; width: 100%;"
     87  Then we add another trigger function that checks if points are being added to an order that already contains discounted products.
     88}}}
     89
     90{{{
     91CREATE OR REPLACE FUNCTION project.block_points_on_discounted_order()
     92RETURNS TRIGGER AS $$
     93DECLARE
     94    discounted_product_exists BOOLEAN;
     95BEGIN
     96    SELECT EXISTS (
     97        SELECT 1
     98        FROM project.order_products op
     99        JOIN project.modification_products mp
     100            ON mp.product_id = op.product_id
     101        JOIN project.modifications m
     102            ON m.modification_id = mp.modification_id
     103        WHERE op.order_id = NEW.order_id
     104          AND m.type_of_modification = 'DISCOUNT'
     105          AND m.discount IS NOT NULL
     106    )
     107    INTO discounted_product_exists;
     108
     109    IF NEW.points_used > 0 AND discounted_product_exists THEN
     110        RAISE EXCEPTION
     111            'Double promotion is not allowed: loyalty points cannot be used on an order containing discounted products.';
     112    END IF;
     113
     114    RETURN NEW;
     115END;
     116$$ LANGUAGE plpgsql;
     117}}}
     118
     119{{{#!div style="text-align: justify; width: 100%;"
     120  Finally, we implement a trigger that runs when loyalty points are added or changed on an order.
     121}}}
     122
     123{{{
     124CREATE OR REPLACE FUNCTION project.block_points_on_discounted_order()
     125RETURNS TRIGGER AS $$
     126DECLARE
     127    discounted_product_exists BOOLEAN;
     128BEGIN
     129    SELECT EXISTS (
     130        SELECT 1
     131        FROM project.order_products op
     132        JOIN project.modification_products mp
     133            ON mp.product_id = op.product_id
     134        JOIN project.modifications m
     135            ON m.modification_id = mp.modification_id
     136        WHERE op.order_id = NEW.order_id
     137          AND m.type_of_modification = 'DISCOUNT'
     138          AND m.discount IS NOT NULL
     139    )
     140    INTO discounted_product_exists;
     141
     142    IF NEW.points_used > 0 AND discounted_product_exists THEN
     143        RAISE EXCEPTION
     144            'Double promotion is not allowed: loyalty points cannot be used on an order containing discounted products.';
     145    END IF;
     146
     147    RETURN NEW;
     148END;
     149$$ LANGUAGE plpgsql;
     150}}}
     151
     152==== Price Drop Alert
     153
     154{{{#!div style="text-align: justify; width: 100%;"
     155* **Description:** When a product manager drops the price of a product, this trigger would notify users that have that same product in their wishlists. It scans the wishlists of all users to see who has been eyeing that specific item. For every enthusiast found, it automatically writes a notification record into the system tables so the application knows exactly who to alert or email about the price cut.
     156}}}
    33157* **Implementation:** 
    34158
    35 ==== Price Drop Alert
    36 
    37 {{{#!div style="text-align: justify; width: 100%;"
    38 * **Description:** When a product manager drops the price of a product, this trigger would notify users that have that same product in their wishlists. It scans the wishlists of all users to see who has been eyeing that specific item. For every enthusiast found, it automatically writes a notification record into the system tables so the application knows exactly who to alert or email about the price cut.
    39 }}}
    40 * **Implementation:** 
    41 
     159{{{#!div style="text-align: justify; width: 100%;"
    42160  First, we need to create a table that will be used to store information about users who should be notified when a product's price decreases.
     161}}}
    43162
    44163{{{
     
    59178}}}
    60179
     180{{{#!div style="text-align: justify; width: 100%;"
    61181  Then, we need to create a trigger function that defines the logic that automatically identifies interested users and creates notification records whenever a product becomes cheaper.
     182}}}
    62183
    63184{{{
     
    95216}}}
    96217
     218{{{#!div style="text-align: justify; width: 100%;"
    97219  Finally, the trigger attaches the notification logic to the products table so it executes automatically after a price reduction occurs.
     220}}}
    98221
    99222{{{