Add recentSearches API implementation and test

This commit is contained in:
Hongbo Wu 2022-10-12 12:20:48 +08:00
parent fd1d43c104
commit 1725f96d82
4 changed files with 114 additions and 1 deletions

View file

@ -100,6 +100,7 @@ import {
generateUploadFilePathName,
} from '../utils/uploads'
import { getPageByParam } from '../elastic/pages'
import { recentSearchesResolver } from './recent_searches'
/* eslint-disable @typescript-eslint/naming-convention */
type ResultResolveType = {
@ -196,6 +197,7 @@ export const functionResolvers = {
typeaheadSearch: typeaheadSearchResolver,
updatesSince: updatesSinceResolver,
integrations: integrationsResolver,
recentSearches: recentSearchesResolver,
},
User: {
async sharedArticles(
@ -604,4 +606,5 @@ export const functionResolvers = {
...resultResolveTypeResolver('SetIntegration'),
...resultResolveTypeResolver('Integrations'),
...resultResolveTypeResolver('DeleteIntegration'),
...resultResolveTypeResolver('RecentSearches'),
}

View file

@ -0,0 +1,36 @@
import { authorized } from '../../utils/helpers'
import {
RecentSearchesError,
RecentSearchesErrorCode,
RecentSearchesSuccess,
} from '../../generated/graphql'
import { analytics } from '../../utils/analytics'
import { env } from '../../env'
import { getRepository } from '../../entity/utils'
import { User } from '../../entity/user'
import { getRecentSearches } from '../../services/search_history'
export const recentSearchesResolver = authorized<
RecentSearchesSuccess,
RecentSearchesError
>(async (_obj, _params, { claims: { uid }, log }) => {
log.info('recentSearches')
analytics.track({
userId: uid,
event: 'recentSearches',
properties: {
env: env.server.apiEnv,
},
})
const user = await getRepository(User).findOneBy({ id: uid })
if (!user) {
return { errorCodes: [RecentSearchesErrorCode.Unauthorized] }
}
const searches = await getRecentSearches(uid)
return {
searches,
}
})

View file

@ -17,7 +17,7 @@ export const saveSearchHistory = async (
term: string
): Promise<void> => {
const searchHistory = new SearchHistory()
searchHistory.user = { id: userId } as any
searchHistory.user.id = userId
searchHistory.term = term
searchHistory.createdAt = new Date()
await getRepository(SearchHistory).save(searchHistory)

View file

@ -0,0 +1,74 @@
import 'mocha'
import { expect } from 'chai'
import { User } from '../../src/entity/user'
import { PageContext } from '../../src/elastic/types'
import { createTestUser, deleteTestUser } from '../db'
import { graphqlRequest, request } from '../util'
import { createPubSubClient } from '../../src/datalayer/pubsub'
import { getRepository } from '../../src/entity/utils'
import { SearchHistory } from '../../src/entity/search_history'
describe('recent_searches resolver', () => {
let user: User
let authToken: string
let ctx: PageContext
before(async () => {
// create fake user and login
user = await createTestUser('fakeUser')
const res = await request
.post('/local/debug/fake-user-login')
.send({ fakeEmail: user.email })
authToken = res.body.authToken
ctx = {
pubsub: createPubSubClient(),
refresh: true,
uid: user.id,
}
})
after(async () => {
// clean up
await deleteTestUser(user.name)
})
describe('recentSearches API', () => {
const recentSearchesQuery = `
query {
recentSearches {
... on RecentSearchesSuccess {
searches {
term
}
}
}
}
`
before(async () => {
// create fake recent searches
await getRepository(SearchHistory).save([
{
user: { id: user.id },
term: 'test1',
},
{
user: { id: user.id },
term: 'test2',
},
])
})
after(async () => {
await getRepository(SearchHistory).delete({ user: { id: user.id } })
})
it('returns recent searches', async () => {
const response = await graphqlRequest(
recentSearchesQuery,
authToken
).expect(200)
expect(response.body.data.recentSearches.searches).to.be.lengthOf(2)
})
})
})