From c7f74ed6ce2276c226298361bc7b6aa9e06e8d90 Mon Sep 17 00:00:00 2001 From: Roberto Tonino Date: Wed, 2 Jun 2021 16:46:20 +0200 Subject: [PATCH] feat: implemented untested changeAccount logic --- .../src/routes/api/get/changeAccount.spec.ts | 15 +++++++++ server/src/routes/api/get/changeAccount.ts | 31 +++++++++++++++++++ server/src/routes/api/get/index.ts | 2 ++ 3 files changed, 48 insertions(+) create mode 100644 server/src/routes/api/get/changeAccount.spec.ts diff --git a/server/src/routes/api/get/changeAccount.spec.ts b/server/src/routes/api/get/changeAccount.spec.ts new file mode 100644 index 0000000..11c039a --- /dev/null +++ b/server/src/routes/api/get/changeAccount.spec.ts @@ -0,0 +1,15 @@ +import { appSendGet } from '../../../../tests/utils' + +describe('analyzeLink requests', () => { + it('should respond 200 to calls with supported child number', async () => { + const res = await appSendGet('/api/changeAccount/?child=1') + + expect(res.status).toBe(200) + }) + + it('should respond 400 to calls with not supported child number', async () => { + const res = await appSendGet('/api/changeAccount/') + + expect(res.status).toBe(400) + }) +}) diff --git a/server/src/routes/api/get/changeAccount.ts b/server/src/routes/api/get/changeAccount.ts index e69de29..9bb90f2 100644 --- a/server/src/routes/api/get/changeAccount.ts +++ b/server/src/routes/api/get/changeAccount.ts @@ -0,0 +1,31 @@ +import { RequestHandler } from 'express' +// @ts-expect-error +import { Deezer } from 'deezer-js' + +import { ApiHandler } from '../../../types' +import { sessionDZ } from '../../../main' + +const path: ApiHandler['path'] = '/changeAccount' + +interface ChangeAccountQuery { + child: number +} + +const handler: RequestHandler<{}, {}, {}, ChangeAccountQuery> = (req, res) => { + if (!req.query || !req.query.child) { + return res.status(400).send({ errorMessage: 'No child specified', errorCode: 'CA01' }) + } + + const { child: accountNum } = req.query + + if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() + const dz = sessionDZ[req.session.id] + + const accountData = dz.change_account(accountNum) + + return res.status(200).send(accountData) +} + +const apiHandler: ApiHandler = { path, handler } + +export default apiHandler diff --git a/server/src/routes/api/get/index.ts b/server/src/routes/api/get/index.ts index 1ece60c..25239d4 100644 --- a/server/src/routes/api/get/index.ts +++ b/server/src/routes/api/get/index.ts @@ -1,4 +1,5 @@ import analyzeLink from './analyzeLink' +import changeAccount from './changeAccount' import getHome from './getHome' import getCharts from './getCharts' import mainSearch from './mainSearch' @@ -17,6 +18,7 @@ import getQueue from './getQueue' export default [ albumSearch, + changeAccount, analyzeLink, getHome, getCharts,