@@ -4,7 +4,6 @@ with audit_logs as (
4
4
select
5
5
source_relation,
6
6
source_id as user_id,
7
- source_label as user_name,
8
7
created_at,
9
8
lower (change_description) as change_description
10
9
from {{ ref(' stg_zendesk__audit_log' ) }}
@@ -21,9 +20,7 @@ with audit_logs as (
21
20
select
22
21
source_relation,
23
22
user_id,
24
- user_name,
25
23
created_at,
26
- change_description,
27
24
-- extract and split change description for the support role
28
25
trim ({{ dbt .split_part (zendesk .extract_support_role_changes (' change_description' ), " ' to '" , 1 ) }}) as from_role,
29
26
trim ({{ dbt .split_part (zendesk .extract_support_role_changes (' change_description' ), " ' to '" , 2 ) }}) as to_role,
@@ -32,30 +29,38 @@ with audit_logs as (
32
29
min (created_at) over (partition by source_relation, user_id) as min_created_at_per_user
33
30
from audit_logs
34
31
32
+ ), split_to_from_deduped as (
33
+ select
34
+ source_relation,
35
+ user_id,
36
+ created_at,
37
+ from_role,
38
+ to_role,
39
+ min_created_at_per_user
40
+ from split_to_from
41
+ where from_role != to_role
42
+ {{ dbt_utils .group_by (6 ) }}
43
+
35
44
-- Isolates the first "from" role as the base
36
45
), first_roles as (
37
46
select
38
47
source_relation,
39
48
user_id,
40
- user_name,
41
- change_description,
42
49
cast(null as {{ dbt .type_timestamp () }}) as valid_starting_at, -- fill in with created_at of user later
43
50
created_at as valid_ending_at, -- this it the created_at of the audit log entry
44
51
from_role as role
45
- from split_to_from
52
+ from split_to_from_deduped
46
53
where created_at = min_created_at_per_user
47
54
48
55
-- Captures all subsequent "to" roles
49
56
), role_changes as (
50
57
select
51
58
source_relation,
52
59
user_id,
53
- user_name,
54
- change_description,
55
60
created_at as valid_starting_at,
56
61
lead(created_at) over (partition by source_relation, user_id order by created_at asc ) as valid_ending_at,
57
62
to_role as role
58
- from split_to_from
63
+ from split_to_from_deduped
59
64
60
65
), unioned as (
61
66
select *
@@ -74,7 +79,6 @@ with audit_logs as (
74
79
lower (coalesce(unioned .role , users .role )) as role,
75
80
coalesce(unioned .valid_starting_at , users .created_at , cast(' 1970-01-01' as {{ dbt .type_timestamp () }})) as valid_starting_at,
76
81
coalesce(unioned .valid_ending_at , {{ dbt .current_timestamp () }}) as valid_ending_at,
77
- unioned .change_description ,
78
82
-- include these in case they're needed for the internal_user_criteria
79
83
users .external_id ,
80
84
users .email ,
@@ -101,7 +105,6 @@ with audit_logs as (
101
105
role,
102
106
valid_starting_at,
103
107
valid_ending_at,
104
- change_description,
105
108
106
109
{% if var(' internal_user_criteria' , false) - %} -- apply the filter to historical roles if provided
107
110
role in (' admin' , ' agent' ) or {{ var(' internal_user_criteria' , false) }} as is_internal_role
0 commit comments