@@ -38,33 +38,42 @@ async function updateAccessToken(endpoint: types.HealthLinkEndpoint) {
38
38
39
39
export const DbLinks = {
40
40
create ( config : types . HealthLinkConfig ) {
41
+ let { userId, sessionId, ...configSansUserAndSession } = config ;
42
+
41
43
const link = {
42
- config,
44
+ config : configSansUserAndSession ,
43
45
id : randomStringWithEntropy ( 32 ) ,
46
+ userId : userId ,
47
+ sessionId : sessionId ,
44
48
managementToken : randomStringWithEntropy ( 32 ) ,
49
+ created : new Date ( ) . getTime ( ) / 1000 ,
45
50
active : true ,
46
51
} ;
47
52
db . query (
48
- `INSERT INTO shlink (id, management_token, active, config_exp, config_passcode)
49
- values (:id, :managementToken, :active, :exp, :passcode)` ,
53
+ `INSERT INTO shlink (id, user_id, session_id, management_token, active, created , config_exp, config_passcode)
54
+ values (:id, :userId, :sessionId, : managementToken, :active, :created , :exp, :passcode)` ,
50
55
{
51
56
id : link . id ,
57
+ userId : link . userId ,
58
+ sessionId : link . sessionId ,
52
59
managementToken : link . managementToken ,
53
60
active : link . active ,
61
+ created : link . created ,
54
62
exp : link . config . exp ,
55
63
passcode : link . config . passcode ,
56
- } ,
64
+ }
57
65
) ;
58
66
59
67
return link ;
60
68
} ,
61
- updateConfig ( shl : types . HealthLink ) {
62
- db . query ( `UPDATE shlink set config_passcode=:passcode, config_exp=:exp where id=:id` ,
69
+ updateConfig ( linkId : string , config : types . HealthLinkConfig ) {
70
+ db . query ( `UPDATE shlink set config_passcode=:passcode, config_exp=:exp, session_id=:sessionId where id=:id` ,
63
71
{
64
- id : shl . id ,
65
- exp : shl . config . exp ,
66
- passcode : shl . config . passcode
67
- } )
72
+ id : linkId ,
73
+ exp : config . exp ,
74
+ passcode : config . passcode ,
75
+ sessionId : config . sessionId
76
+ } ) ;
68
77
return true ;
69
78
} ,
70
79
deactivate ( shl : types . HealthLink ) {
@@ -83,19 +92,48 @@ export const DbLinks = {
83
92
id : linkRow . id as string ,
84
93
passcodeFailuresRemaining : linkRow . passcode_failures_remaining as number ,
85
94
active : Boolean ( linkRow . active ) as boolean ,
95
+ userId : linkRow . user_id as string ,
96
+ sessionId : linkRow . session_id as string ,
97
+ created : linkRow . created as string ,
86
98
managementToken : linkRow . management_token as string ,
87
99
config : {
88
100
exp : linkRow . config_exp as number ,
89
101
passcode : linkRow . config_passcode as string ,
90
102
} ,
91
103
} ;
92
104
} ,
105
+ getUserShl ( userId : string ) : types . HealthLink | undefined {
106
+ try {
107
+ const linkRow = db
108
+ . prepareQuery ( `SELECT * from shlink where user_id=? and active=1 order by created desc limit 1` )
109
+ . oneEntry ( [ userId ] ) ;
110
+ return {
111
+ id : linkRow . id as string ,
112
+ passcodeFailuresRemaining : linkRow . passcode_failures_remaining as number ,
113
+ active : Boolean ( linkRow . active ) as boolean ,
114
+ userId : linkRow . user_id as string ,
115
+ sessionId : linkRow . session_id as string ,
116
+ created : linkRow . created as string ,
117
+ managementToken : linkRow . management_token as string ,
118
+ config : {
119
+ exp : linkRow . config_exp as number ,
120
+ passcode : linkRow . config_passcode as string ,
121
+ } ,
122
+ } ;
123
+ } catch ( e ) {
124
+ console . warn ( e ) ;
125
+ return undefined ;
126
+ }
127
+ } ,
93
128
getShlInternal ( linkId : string ) : types . HealthLink {
94
129
const linkRow = db . prepareQuery ( `SELECT * from shlink where id=?` ) . oneEntry ( [ linkId ] ) ;
95
130
return {
96
131
id : linkRow . id as string ,
97
132
passcodeFailuresRemaining : linkRow . passcode_failures_remaining as number ,
98
133
active : Boolean ( linkRow . active ) as boolean ,
134
+ userId : linkRow . user_id as string ,
135
+ sessionId : linkRow . session_id as string ,
136
+ created : linkRow . created as string ,
99
137
managementToken : linkRow . management_token as string ,
100
138
config : {
101
139
exp : linkRow . config_exp as number ,
@@ -142,6 +180,17 @@ export const DbLinks = {
142
180
143
181
return true ;
144
182
} ,
183
+ async deleteAllFiles ( linkId : string ) {
184
+
185
+ db . query (
186
+ `delete from shlink_file where shlink = :linkId` ,
187
+ {
188
+ linkId
189
+ }
190
+ ) ;
191
+
192
+ return true ;
193
+ } ,
145
194
async addEndpoint ( linkId : string , endpoint : types . HealthLinkEndpoint ) : Promise < string > {
146
195
const id = randomStringWithEntropy ( 32 ) ;
147
196
0 commit comments