| | 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 | {{{ |
| | 40 | CREATE OR REPLACE FUNCTION project.block_double_promo() |
| | 41 | RETURNS TRIGGER AS $$ |
| | 42 | DECLARE |
| | 43 | used_points INTEGER; |
| | 44 | discounted_product_exists BOOLEAN; |
| | 45 | BEGIN |
| | 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; |
| | 68 | END; |
| | 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 | {{{ |
| | 77 | DROP TRIGGER IF EXISTS trg_block_double_promo_order_products |
| | 78 | ON project.order_products; |
| | 79 | |
| | 80 | CREATE TRIGGER trg_block_double_promo_order_products |
| | 81 | BEFORE INSERT OR UPDATE ON project.order_products |
| | 82 | FOR EACH ROW |
| | 83 | EXECUTE 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 | {{{ |
| | 91 | CREATE OR REPLACE FUNCTION project.block_points_on_discounted_order() |
| | 92 | RETURNS TRIGGER AS $$ |
| | 93 | DECLARE |
| | 94 | discounted_product_exists BOOLEAN; |
| | 95 | BEGIN |
| | 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; |
| | 115 | END; |
| | 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 | {{{ |
| | 124 | CREATE OR REPLACE FUNCTION project.block_points_on_discounted_order() |
| | 125 | RETURNS TRIGGER AS $$ |
| | 126 | DECLARE |
| | 127 | discounted_product_exists BOOLEAN; |
| | 128 | BEGIN |
| | 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; |
| | 148 | END; |
| | 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 | }}} |