Skip to content

Commit f945d4c

Browse files
committed
fix: do not update as JSON object yet
1 parent 705b25f commit f945d4c

File tree

4 files changed

+82
-41
lines changed

4 files changed

+82
-41
lines changed

src/server/modules/redirect/__tests__/RedirectController.test.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -520,13 +520,15 @@ describe('redirect API tests', () => {
520520
test('url does exists in cache but not db', async () => {
521521
const req = createRequestWithShortUrl('Aaa')
522522
const res = httpMocks.createResponse()
523-
const redirectDestination: RedirectDestination = {
524-
longUrl: 'aa',
525-
isFile: false,
526-
safeBrowsingExpiry: new Date(Date.now() + 1000).toISOString(),
527-
}
528-
529-
redisMockClient.set('aaa', JSON.stringify(redirectDestination))
523+
// FIXME: Update to use the new RedirectDestination type
524+
// const redirectDestination: RedirectDestination = {
525+
// longUrl: 'aa',
526+
// isFile: false,
527+
// safeBrowsingExpiry: new Date(Date.now() + 1000).toISOString(),
528+
// }
529+
530+
// redisMockClient.set('aaa', JSON.stringify(redirectDestination))
531+
redisMockClient.set('aaa', 'aa')
530532
mockDbEmpty()
531533

532534
await container
@@ -540,14 +542,16 @@ describe('redirect API tests', () => {
540542
test('url in cache and db is down', async () => {
541543
const req = createRequestWithShortUrl('Aaa')
542544
const res = httpMocks.createResponse()
543-
const redirectDestination: RedirectDestination = {
544-
longUrl: 'aa',
545-
isFile: false,
546-
safeBrowsingExpiry: new Date(Date.now() + 1000).toISOString(),
547-
}
545+
// FIXME: Update to use the new RedirectDestination type
546+
// const redirectDestination: RedirectDestination = {
547+
// longUrl: 'aa',
548+
// isFile: false,
549+
// safeBrowsingExpiry: new Date(Date.now() + 1000).toISOString(),
550+
// }
548551

549552
mockDbDown()
550-
redisMockClient.set('aaa', JSON.stringify(redirectDestination))
553+
// redisMockClient.set('aaa', JSON.stringify(redirectDestination))
554+
redisMockClient.set('aaa', 'aa')
551555

552556
await container
553557
.get<RedirectController>(DependencyIds.redirectController)

src/server/modules/redirect/services/RedirectService.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ export class RedirectService {
6767
const { longUrl, isFile, safeBrowsingExpiry } =
6868
await this.urlRepository.getLongUrl(shortUrl)
6969

70+
console.log(
71+
JSON.stringify({
72+
shortUrl,
73+
longUrl,
74+
isFile,
75+
safeBrowsingExpiry,
76+
}),
77+
)
78+
7079
// Validate that the longUrl is not a malicious link
7180
const isSafeBrowsingResultExpired =
7281
!isFile &&

src/server/repositories/UrlRepository.ts

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,10 @@ export class UrlRepository implements UrlRepositoryInterface {
208208
} catch {
209209
// Cache failed, look in database
210210
const redirectDestination = await this.getLongUrlFromDatabase(shortUrl)
211-
this.cacheShortUrl(shortUrl, redirectDestination).catch((error) =>
212-
logger.error(`Unable to cache short URL: ${error}`),
211+
// FIXME: Cache the entire RedirectDestination object once all app
212+
// clients are updated to use the new RedirectDestination type.
213+
this.cacheShortUrl(shortUrl, redirectDestination.longUrl).catch(
214+
(error) => logger.error(`Unable to cache short URL: ${error}`),
213215
)
214216
return redirectDestination
215217
}
@@ -533,11 +535,22 @@ export class UrlRepository implements UrlRepositoryInterface {
533535
const redirectDestination = JSON.parse(cacheLongUrl)
534536
resolve(redirectDestination)
535537
} catch (_) {
536-
reject(
537-
new NotFoundError(
538-
`longUrl not found in cache:\tshortUrl=${shortUrl}`,
539-
),
538+
logger.info(
539+
`Cache lookup returned a string instead of an object:\tshortUrl=${shortUrl}`,
540540
)
541+
resolve({
542+
longUrl: cacheLongUrl,
543+
isFile: false,
544+
safeBrowsingExpiry: null,
545+
})
546+
547+
// FIXME: Throw NotFoundError once all app clients are updated to
548+
// use the new RedirectDestination type.
549+
// reject(
550+
// new NotFoundError(
551+
// `longUrl not found in cache:\tshortUrl=${shortUrl}`,
552+
// ),
553+
// )
541554
}
542555
}
543556
}),
@@ -549,23 +562,35 @@ export class UrlRepository implements UrlRepositoryInterface {
549562
* @param {string} shortUrl Short url.
550563
* @param {string} longUrl Long url.
551564
*/
552-
private cacheShortUrl: (
553-
shortUrl: string,
554-
redirectDestination: RedirectDestination,
555-
) => Promise<void> = (shortUrl, redirectDestination) => {
556-
return new Promise((resolve, reject) => {
557-
redirectClient.set(
558-
shortUrl,
559-
JSON.stringify(redirectDestination),
560-
'EX',
561-
redirectExpiry,
562-
(err) => {
565+
private cacheShortUrl: (shortUrl: string, longUrl: string) => Promise<void> =
566+
(shortUrl, longUrl) => {
567+
return new Promise((resolve, reject) => {
568+
redirectClient.set(shortUrl, longUrl, 'EX', redirectExpiry, (err) => {
563569
if (err) reject(err)
564570
else resolve()
565-
},
566-
)
567-
})
568-
}
571+
})
572+
})
573+
}
574+
575+
// FIXME: This is the future implementation of the cacheShortUrl method to be
576+
// used once all app clients are updated to use the new RedirectDestination
577+
// private cacheShortUrl: (
578+
// shortUrl: string,
579+
// redirectDestination: RedirectDestination,
580+
// ) => Promise<void> = (shortUrl, redirectDestination) => {
581+
// return new Promise((resolve, reject) => {
582+
// redirectClient.set(
583+
// shortUrl,
584+
// JSON.stringify(redirectDestination),
585+
// 'EX',
586+
// redirectExpiry,
587+
// (err) => {
588+
// if (err) reject(err)
589+
// else resolve()
590+
// },
591+
// )
592+
// })
593+
// }
569594

570595
/**
571596
* Generates the ranking algorithm to be used in the ORDER BY clause in the

test/server/repositories/UrlRepository.test.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -606,13 +606,16 @@ describe('UrlRepository', () => {
606606

607607
it('should return from cache when cache is filled', async () => {
608608
// Arrange
609-
const cacheUrl = {
610-
longUrl: 'aaa',
611-
isFile: false,
612-
safeBrowsingExpiry: null,
613-
}
614-
redisMockClient.set('a', JSON.stringify(cacheUrl))
615-
await expect(repository.getLongUrl('a')).resolves.toEqual(cacheUrl)
609+
// FIXME: Update to use the new RedirectDestination type
610+
// const cacheUrl = {
611+
// longUrl: 'aaa',
612+
// isFile: false,
613+
// safeBrowsingExpiry: null,
614+
// }
615+
// redisMockClient.set('a', JSON.stringify(cacheUrl))
616+
// await expect(repository.getLongUrl('a')).resolves.toEqual(cacheUrl)
617+
redisMockClient.set('a', 'aaa')
618+
await expect(repository.getLongUrl('a')).resolves.toBe('aaa')
616619
})
617620

618621
it('should return from db when cache is down', async () => {

0 commit comments

Comments
 (0)