File size: 2,422 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
-- name: IntKeyGetServiceID :one
SELECT
    service_id
FROM
    integration_keys
WHERE
    id = $1
    AND type = $2;

-- name: IntKeyCreate :exec
INSERT INTO integration_keys(id, name, type, service_id, external_system_name)
    VALUES ($1, $2, $3, $4, $5);

-- name: IntKeyFindOne :one
SELECT
    id,
    name,
    type,
    service_id,
    external_system_name
FROM
    integration_keys
WHERE
    id = $1;

-- name: IntKeyFindByService :many
SELECT
    id,
    name,
    type,
    service_id,
    external_system_name
FROM
    integration_keys
WHERE
    service_id = $1;

-- name: IntKeyDelete :exec
DELETE FROM integration_keys
WHERE id = ANY (@ids::uuid[]);

-- name: IntKeyGetConfig :one
SELECT
    config
FROM
    uik_config
WHERE
    id = $1
FOR UPDATE;

-- name: IntKeySetConfig :exec
INSERT INTO uik_config(id, config)
    VALUES ($1, $2)
ON CONFLICT (id)
    DO UPDATE SET
        config = $2;

-- name: IntKeyDeleteConfig :exec
DELETE FROM uik_config
WHERE id = $1;

-- name: IntKeyGetType :one
SELECT
    type
FROM
    integration_keys
WHERE
    id = $1;

-- name: IntKeyPromoteSecondary :one
UPDATE
    uik_config
SET
    primary_token = secondary_token,
    primary_token_hint = secondary_token_hint,
    secondary_token = NULL,
    secondary_token_hint = NULL
WHERE
    id = $1
RETURNING
    primary_token_hint;

-- name: IntKeyTokenHints :one
SELECT
    primary_token_hint,
    secondary_token_hint
FROM
    uik_config
WHERE
    id = $1;

-- name: IntKeySetPrimaryToken :one
UPDATE
    uik_config
SET
    primary_token = $2,
    primary_token_hint = $3
WHERE
    id = $1
    AND primary_token IS NULL
RETURNING
    id;

-- name: IntKeySetSecondaryToken :one
UPDATE
    uik_config
SET
    secondary_token = $2,
    secondary_token_hint = $3
WHERE
    id = $1
    AND secondary_token IS NULL
    AND primary_token IS NOT NULL
RETURNING
    id;

-- name: IntKeyUIKValidateService :one
SELECT
    k.service_id
FROM
    uik_config c
    JOIN integration_keys k ON k.id = c.id
WHERE
    c.id = sqlc.arg(key_id)
    AND k.type = 'universal'
    AND (c.primary_token = sqlc.arg(token_id)
        OR c.secondary_token = sqlc.arg(token_id));

-- name: IntKeyDeleteSecondaryToken :exec
UPDATE
    uik_config
SET
    secondary_token = NULL,
    secondary_token_hint = NULL
WHERE
    id = $1;

-- name: IntKeyInsertSignalMessage :exec
INSERT INTO pending_signals(dest_id, service_id, params)
    VALUES ($1, $2, $3);