Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. /*
  2. * helpers types
  3. */
  4. create type tier_details as (tier_id int, duration text, name text, price money);
  5. create type card_details as (card_brand text, card_last_four text, card_exp_mo text, card_exp_yr text);
  6.  
  7. /*
  8. * helper functions
  9. */
  10. -- tier_id returns the tier_id for an email_address
  11. create function tier_id(_email_address text) returns int as $$
  12. select tier_id from users
  13. where email_address = _email_address;
  14. $$ language sql;
  15.  
  16. -- tier_info returns the tier_details for an email_address
  17. create function tier_info(_email_address text) returns tier_details as $$
  18. select tier_id, duration, name, price from tiers
  19. where tier_id = tier_id(_email_address);
  20. $$ language sql;
  21.  
  22. -- card_info returns the card_details for an email_address
  23. create function card_info(_email_address text) returns card_details as $$
  24. select card_brand, card_last_four, card_exp_mo, card_exp_yr from users
  25. where email_address = _email_address;
  26. $$ language sql;
  27.  
  28. -- id returns the id for an email_address
  29. create function id(_email_address text) returns uuid as $$
  30. select id from users
  31. where email_address = _email_address;
  32. $$ language sql;
  33.  
  34. -- display_id returns the display id for an email_address
  35. create function display_id(_email_address text) returns text as $$
  36. select display_id from settings
  37. where id = id(_email_address);
  38. $$ language sql;
  39.  
  40. -- email_address_unique returns whether or not an email_address is unique
  41. create function email_address_unique(_email_address text) returns bool as $$
  42. select exists(
  43. select from users
  44. where email_address = _email_address
  45. );
  46. $$ language sql;
  47.  
  48. -- dispatch_receipt dispatches a receipt for an email_address
  49. create function dispatch_receipt(_email_address text) returns uuid as $$
  50. declare
  51. t tier_details := tier_info(_email_address);
  52. c card_details := card_info(_email_address);
  53. begin
  54. insert into receipts (id, tier_duration, tier_name, tier_price, card_brand, card_last_four, card_exp_mo, card_exp_yr)
  55. values (id(_email_address), t.duration, t.name, t.price, c.card_brand, c.card_last_four, c.card_exp_mo, c.card_exp_yr)
  56. returning receipt_id;
  57. end;
  58. $$ language plpgsql;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement