@@ -38,33 +38,42 @@ async function updateAccessToken(endpoint: types.HealthLinkEndpoint) {
3838
3939export  const  DbLinks  =  { 
4040  create ( config : types . HealthLinkConfig )  { 
41+     let  {  userId,  sessionId,  ...configSansUserAndSession  }  =  config ; 
42+ 
4143    const  link  =  { 
42-       config, 
44+       config :  configSansUserAndSession , 
4345      id : randomStringWithEntropy ( 32 ) , 
46+       userId : userId , 
47+       sessionId : sessionId , 
4448      managementToken : randomStringWithEntropy ( 32 ) , 
49+       created : new  Date ( ) . getTime ( )  /  1000 , 
4550      active : true , 
4651    } ; 
4752    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)` , 
5055      { 
5156        id : link . id , 
57+         userId : link . userId , 
58+         sessionId : link . sessionId , 
5259        managementToken : link . managementToken , 
5360        active : link . active , 
61+         created : link . created , 
5462        exp : link . config . exp , 
5563        passcode : link . config . passcode , 
56-       } , 
64+       } 
5765    ) ; 
5866
5967    return  link ; 
6068  } , 
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` , 
6371    { 
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+     } ) ; 
6877    return  true ; 
6978  } , 
7079  deactivate ( shl : types . HealthLink )  { 
@@ -83,19 +92,48 @@ export const DbLinks = {
8392      id : linkRow . id  as  string , 
8493      passcodeFailuresRemaining : linkRow . passcode_failures_remaining  as  number , 
8594      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 , 
8698      managementToken : linkRow . management_token  as  string , 
8799      config : { 
88100        exp : linkRow . config_exp  as  number , 
89101        passcode : linkRow . config_passcode  as  string , 
90102      } , 
91103    } ; 
92104  } , 
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+   } , 
93128  getShlInternal ( linkId : string ) : types . HealthLink  { 
94129    const  linkRow  =  db . prepareQuery ( `SELECT * from shlink where id=?` ) . oneEntry ( [ linkId ] ) ; 
95130    return  { 
96131      id : linkRow . id  as  string , 
97132      passcodeFailuresRemaining : linkRow . passcode_failures_remaining  as  number , 
98133      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 , 
99137      managementToken : linkRow . management_token  as  string , 
100138      config : { 
101139        exp : linkRow . config_exp  as  number , 
@@ -142,6 +180,17 @@ export const DbLinks = {
142180
143181    return  true ; 
144182  } , 
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+   } , 
145194  async  addEndpoint ( linkId : string ,  endpoint : types . HealthLinkEndpoint ) : Promise < string >  { 
146195    const  id  =  randomStringWithEntropy ( 32 ) ; 
147196
0 commit comments