Skip to content

Commit

Permalink
feat: delete person trigger & func
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Sep 12, 2024
1 parent 761f682 commit 550eb22
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions views/011_users.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
---
CREATE OR REPLACE FUNCTION delete_person(person_id uuid)
RETURNS void AS $$
BEGIN
DELETE FROM casbin_rule WHERE v0 = person_id::TEXT;

UPDATE people SET deleted_at = CURRENT_TIMESTAMP WHERE id = person_id;
END;
$$
LANGUAGE plpgsql;

-- Insert identities in people table
CREATE OR REPLACE FUNCTION sync_identity_to_people ()
RETURNS TRIGGER AS $$
Expand All @@ -10,6 +21,8 @@ BEGIN
name = concat(NEW.traits::json->'name'->>'first', ' ', NEW.traits::json->'name'->>'last'),
email = NEW.traits::json->>'email'
WHERE id = NEW.id;
ELSIF TG_OP = 'DELETE' THEN
PERFORM delete_person(OLD.id);
END IF;

RETURN NEW;
Expand All @@ -20,11 +33,12 @@ DO $$
BEGIN
IF EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'identities') THEN
CREATE OR REPLACE TRIGGER identity_to_people
AFTER INSERT OR UPDATE ON identities
AFTER INSERT OR UPDATE OR DELETE ON identities
FOR EACH ROW
EXECUTE PROCEDURE sync_identity_to_people();
END IF;
END $$;
---

CREATE OR REPLACE VIEW
people_roles AS
Expand All @@ -37,4 +51,4 @@ FROM
people
INNER JOIN casbin_rule cr ON cr.v0 = people.id::VARCHAR
GROUP BY
people.id;
people.id;

0 comments on commit 550eb22

Please sign in to comment.