Skip to content

Commit

Permalink
Merge pull request #30 from OpenSourcePolitics/impersonation_poc
Browse files Browse the repository at this point in the history
Impersonation poc
  • Loading branch information
hellpe authored Nov 6, 2024
2 parents c8e8f93 + a3aa09e commit adef85b
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
WITH proposals_under_impersonation AS (
SELECT
proposals.id AS proposal_id
FROM {{ ref('proposals') }} AS proposals
LEFT JOIN {{ ref('stg_decidim_impersonation_logs') }} AS impersonations ON proposals.first_author_id = impersonations.user_id
WHERE proposals.created_at BETWEEN impersonations.started_at AND impersonations.ended_at
ORDER BY proposals.id
)

SELECT
*,
(CASE WHEN proposals_under_impersonation.proposal_id IS NOT NULL THEN true ELSE false END) AS authored_under_impersonation
FROM {{ ref('proposals') }} AS proposals
LEFT JOIN proposals_under_impersonation ON proposals.id = proposals_under_impersonation.proposal_id
24 changes: 24 additions & 0 deletions projects/marseille/models/marts/schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#test disabled because of ARRAY incompatibility (dbt limitation)
#unit_tests:
# - name: date_match_test
# description: "Check that proposals with creation dates outside impersonation ranges are properly flagged as not impersonated"
# model: proposals_with_impersonation_info
# given:
# - input: ref('proposals')
# format: dict
# rows:
# - {id: 1, first_author_id: 42, created_at: "2021-08-31 14:12:33.144731"}
# - {id: 1, first_author_id: 43, created_at: "2021-08-31 14:12:34.144731"}
# - {id: 1, first_author_id: 44, created_at: "2021-08-31 14:12:31.144731"}
# - input: ref('decidim_impersonation_logs')
# format: dict
# rows:
# - {user_id: 42, started_at: "2021-08-31 14:12:32", ended_at: "2021-08-31 14:12:34"}
# - {user_id: 43, started_at: "2021-08-31 14:12:32", ended_at: "2021-08-31 14:12:34"}
# - {user_id: 44, started_at: "2021-08-31 14:12:32", ended_at: "2021-08-31 14:12:34"}
# expect:
# format: dict
# rows:
# - {authored_under_impersonation: true}
# - {authored_under_impersonation: false}
# - {authored_under_impersonation: false}
22 changes: 22 additions & 0 deletions projects/marseille/models/marts/users_with_promote_info.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
WITH promote_actions AS (
SELECT
DISTINCT ON (users.id)
users.id AS user_id,
decidim_action_logs.id AS log_id,
action
FROM {{ ref('stg_decidim_action_logs') }} AS decidim_action_logs
LEFT JOIN {{ ref('users') }} AS users ON users.id = decidim_action_logs.resource_id
WHERE resource_type = 'Decidim::User'
AND action = 'promote'
)

SELECT
users.*,
(CASE
WHEN promote_actions.action IS NOT NULL AND users.managed = false THEN 'Confirmée'
WHEN promote_actions.action IS NOT NULL AND users.managed = true THEN 'En attente'
WHEN promote_actions.action IS NULL AND users.managed = false THEN 'Utilisateur non représenté'
ELSE 'Non proposée'
END) AS promotion_status
FROM {{ ref('users') }} AS users
LEFT JOIN promote_actions ON users.id = promote_actions.user_id
9 changes: 9 additions & 0 deletions projects/marseille/models/staging/decidim/schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: 2

sources:
- name: decidim
database: "{{ env_var('DBNAME') }}"
schema: public
tables:
- name: decidim_impersonation_logs
- name: decidim_action_logs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
WITH source AS (
SELECT * FROM {{ source('decidim', 'decidim_action_logs') }}
)

SELECT
id,
created_at,
updated_at,
decidim_user_id,
action,
resource_type,
resource_id
FROM source
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
WITH source AS (
SELECT * FROM {{ source('decidim', 'decidim_impersonation_logs') }}
)

SELECT
id,
decidim_user_id AS user_id,
reason,
started_at,
ended_at
FROM source

0 comments on commit adef85b

Please sign in to comment.