feat: implemented untested changeAccount logic

This commit is contained in:
Roberto Tonino 2021-06-02 16:46:20 +02:00
parent 10e1346521
commit c7f74ed6ce
3 changed files with 48 additions and 0 deletions

View File

@ -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)
})
})

View File

@ -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

View File

@ -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,