Skip to content

Commit 185e20d

Browse files
authored
Update server.ts (#39)
1 parent 2117a1f commit 185e20d

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

server.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,20 @@ app.get("/results/json", async (req, res) => {
191191
// Create New Link if it doesn't exist otherwise update
192192
app.post(`/link/${env_config.RANDOM}`, async (req, res) => {
193193
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
194197
await Db_Wrapper.update(
195-
{alias}, {$set: {url, 'hits': 0}},
196-
{upsert: true},
198+
{alias, 'active': true}, {$set: {'active': false, 'end': Date().toString()}},
199+
{},
197200
'links'
198201
)
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+
199208
res.status(200).send('OK')
200209
})
201210

@@ -206,10 +215,15 @@ app.get(`/link/${env_config.RANDOM}/:alias`, async (req, res) => {
206215
// Redirection To Mturk URL and increments count number
207216
app.get("/r/:alias", async (req, res) => {
208217
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+
}
211224
Db_Wrapper.update(
212-
{alias}, {$set: {'hits': hits + 1}},
225+
{alias, active},
226+
{$set: {'hits': hits + 1}},
213227
{},
214228
'links'
215229
)

0 commit comments

Comments
 (0)