-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(modules): create display, qr modules
- Create display module, move RotatingLinksController into it - Create qr module, move QrCodeService and assets into it - Extract QrCodeController from `api/qr` into `modules/qr` - Inject config, existing inversify dependencies into classes wherever possible
- Loading branch information
Showing
18 changed files
with
154 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
src/server/controllers/interfaces/RotatingLinksControllerInterface.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import Express from 'express' | ||
import { inject, injectable } from 'inversify' | ||
import { DependencyIds } from '../../constants' | ||
|
||
@injectable() | ||
export class RotatingLinksController { | ||
private linksToRotate?: string | ||
|
||
constructor(@inject(DependencyIds.linksToRotate) linksToRotate?: string) { | ||
this.linksToRotate = linksToRotate | ||
} | ||
|
||
getRotatingLinks: (_: Express.Request, res: Express.Response) => void = ( | ||
_, | ||
res, | ||
) => { | ||
res.send(this.linksToRotate) | ||
} | ||
} | ||
|
||
export default RotatingLinksController |
7 changes: 4 additions & 3 deletions
7
...ntrollers/RotatingLinksController.test.ts → ...__tests__/RotatingLinksController.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
import httpMocks from 'node-mocks-http' | ||
import { RotatingLinksController } from '../../../src/server/controllers/RotatingLinksController' | ||
import { RotatingLinksController } from '../RotatingLinksController' | ||
|
||
describe('RotatingLinksController tests', () => { | ||
const linksToRotate = 'testlink1,testlink2,testlink3' | ||
const controller = new RotatingLinksController(linksToRotate) | ||
it('Should return rotating links defined in the application configurations', () => { | ||
const controller = new RotatingLinksController() | ||
const { req, res } = httpMocks.createMocks() | ||
jest.spyOn(res, 'send') | ||
controller.getRotatingLinks(req, res) | ||
|
||
expect(res.send).toBeCalledWith('testlink1,testlink2,testlink3') | ||
expect(res.send).toBeCalledWith(linksToRotate) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export { | ||
RotatingLinksController, | ||
RotatingLinksController as default, | ||
} from './RotatingLinksController' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Request, Response } from 'express' | ||
import { inject, injectable } from 'inversify' | ||
import { UrlRepositoryInterface } from '../../repositories/interfaces/UrlRepositoryInterface' | ||
|
||
import ImageFormat from '../../../shared/util/image-format' | ||
import { DependencyIds } from '../../constants' | ||
|
||
import { QrCodeService } from './interfaces' | ||
|
||
@injectable() | ||
export class QrCodeController { | ||
private ogUrl: string | ||
|
||
private qrCodeService: QrCodeService | ||
|
||
private urlRepository: UrlRepositoryInterface | ||
|
||
constructor( | ||
@inject(DependencyIds.ogUrl) ogUrl: string, | ||
@inject(DependencyIds.qrCodeService) qrCodeService: QrCodeService, | ||
@inject(DependencyIds.urlRepository) urlRepository: UrlRepositoryInterface, | ||
) { | ||
this.ogUrl = ogUrl | ||
this.qrCodeService = qrCodeService | ||
this.urlRepository = urlRepository | ||
} | ||
|
||
private shortUrlExists: (shortUrl: string) => Promise<boolean> = async ( | ||
shortUrl, | ||
) => { | ||
return !!(await this.urlRepository.findByShortUrl(shortUrl)) | ||
} | ||
|
||
createGoQrCode: (req: Request, res: Response) => Promise<void> = async ( | ||
req, | ||
res, | ||
): Promise<void> => { | ||
const url = req.query.url as string | ||
const format = req.query.format as ImageFormat | ||
|
||
if (!(await this.shortUrlExists(url))) { | ||
res.status(400).send('Short link does not exist') | ||
return | ||
} | ||
|
||
// Append base url to short link before creating the qr. | ||
const goShortLink = `${this.ogUrl}/${url}` | ||
|
||
// Creates the QR code and sends it to the client. | ||
const buffer = await this.qrCodeService.createGoQrCode(goShortLink, format) | ||
// Provides callee a proposed filename for image. | ||
res.set('Filename', goShortLink) | ||
res.contentType(format) | ||
res.end(buffer) | ||
} | ||
} | ||
|
||
export default QrCodeController |
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export { | ||
QrCodeController, | ||
QrCodeController as default, | ||
} from './QrCodeController' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import ImageFormat from '../../../../shared/util/image-format' | ||
|
||
export interface QrCodeService { | ||
createGoQrCode: (url: string, format: ImageFormat) => Promise<Buffer> | ||
} | ||
|
||
export default QrCodeService |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { QrCodeService, QrCodeService as default } from './QrCodeService' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/server/modules/qr/services/__tests__/QrCodeService.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import jsQR from 'jsqr' | ||
import png from 'upng-js' | ||
|
||
import ImageFormat from '../../../../../shared/util/image-format' | ||
|
||
import { QrCodeService } from '..' | ||
|
||
const testUrl = 'https://github.com/opengovsg/GoGovSG' | ||
|
||
describe('GoGovSg QR code', () => { | ||
describe('generates accurately', () => { | ||
const qrCodeService = new QrCodeService() | ||
|
||
test('png string', async () => { | ||
const buffer = (await qrCodeService.createGoQrCode( | ||
testUrl, | ||
ImageFormat.PNG, | ||
)) as Buffer | ||
const data = png.decode(buffer) | ||
const out = { | ||
data: new Uint8ClampedArray(png.toRGBA8(data)[0]), | ||
height: data.height, | ||
width: data.width, | ||
} | ||
const code = jsQR(out.data, out.width, out.height) | ||
expect(code).not.toBeNull() | ||
expect(code!.data).toEqual(testUrl) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { QrCodeService, QrCodeService as default } from './QrCodeService' |
Oops, something went wrong.