Advertisement
chrissharp123

user delete SQL

Sep 23rd, 2024
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. select au.id
  2. into actor.usrs_to_delete_2024_09_20
  3. from actor.usr au
  4. join permission.grp_tree grp on (au.profile = grp.id)
  5. where
  6. ( -- begin OR
  7. grp.name in (
  8. 'Digital Only',
  9. 'GLS',
  10. 'Homebound',
  11. 'NonResident',
  12. 'OutOfState',
  13. 'Patron',
  14. 'Quipu',
  15. 'Restricted',
  16. 'Temp',
  17. 'TempRes12',
  18. 'TempRes6',
  19. 'Trustee'
  20. )
  21.  
  22. and date(au.expire_date) < date(now()) - '4 years'::interval
  23. -- no owed money in either direction and no payment within the last 4 years
  24. and not exists (
  25.     select 1
  26.          from money.materialized_billable_xact_summary mmbxs
  27.          where mmbxs.usr = au.id
  28.          and (
  29.                  balance_owed <> '0.00' or (now() - last_payment_ts) < '4 years'::interval)
  30.     )
  31. -- no activity entries within the last 4 years
  32. and not exists (
  33.     select 1
  34.          from actor.usr_activity aua
  35.          where aua.usr = au.id
  36.          and (now() - event_time) < '4 years'::interval
  37.     )
  38. -- no accounts created within the last 4 years
  39. and date(au.create_date) < date(now()) - '4 years'::interval
  40. ) OR (
  41. -- PLAY Cards are deleted one year after expire date\
  42. -- They can't owe any money.
  43. (
  44. grp.name = 'PLAY Card'
  45. and not exists (
  46.     select 1
  47.          from money.materialized_billable_xact_summary mmbxs
  48.          where mmbxs.usr = au.id
  49.          and balance_owed <> '0.00'
  50.     )
  51. and date(au.expire_date) < date(now()) - '1 year'::interval
  52. )
  53. ) -- end OR
  54. and au.deleted = false
  55. -- consider open circs
  56. and not exists (
  57.         select 1
  58.         from action.circulation circ
  59.         where circ.usr = au.id
  60.         and xact_finish is null
  61. )
  62. -- not in collections - PATRON_IN_COLLECTIONS standing penalty
  63. and not exists (
  64.         select 1
  65.         from actor.usr_standing_penalty usp
  66.         where usp.usr = au.id
  67.         and usp.standing_penalty = 30
  68. )
  69. ;
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement