@@ -191,11 +191,20 @@ app.get("/results/json", async (req, res) => {
191
191
// Create New Link if it doesn't exist otherwise update
192
192
app . post ( `/link/${ env_config . RANDOM } ` , async ( req , res ) => {
193
193
const { alias, url } = req . body
194
+
195
+ // Set old link if it exists to inactive
196
+ // With current logic there can only be one active link
194
197
await Db_Wrapper . update (
195
- { alias} , { $set : { url , 'hits ' : 0 } } ,
196
- { upsert : true } ,
198
+ { alias, 'active' : true } , { $set : { 'active' : false , 'end ' : Date ( ) . toString ( ) } } ,
199
+ { } ,
197
200
'links'
198
201
)
202
+
203
+ // create new alias if post request has been sent
204
+ // Only active link with this alias
205
+ await Db_Wrapper . insert ( { alias, url, 'hits' : 0 , 'active' : true , 'start' : Date ( ) . toString ( ) , 'end' : null } , 'links' )
206
+
207
+
199
208
res . status ( 200 ) . send ( 'OK' )
200
209
} )
201
210
@@ -206,10 +215,15 @@ app.get(`/link/${env_config.RANDOM}/:alias`, async (req, res) => {
206
215
// Redirection To Mturk URL and increments count number
207
216
app . get ( "/r/:alias" , async ( req , res ) => {
208
217
try {
209
- const body = await Db_Wrapper . find ( { 'alias' : req . params . alias } , 'links' )
210
- const { alias, url, hits} = body [ 0 ]
218
+ const active = true
219
+ const body = await Db_Wrapper . find ( { 'alias' : req . params . alias , active} , 'links' )
220
+ let { alias, url, hits} = body [ 0 ]
221
+ if ( hits === null ) {
222
+ hits = 0
223
+ }
211
224
Db_Wrapper . update (
212
- { alias} , { $set : { 'hits' : hits + 1 } } ,
225
+ { alias, active} ,
226
+ { $set : { 'hits' : hits + 1 } } ,
213
227
{ } ,
214
228
'links'
215
229
)
0 commit comments