mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Save search term in search history and update timestamp
This commit is contained in:
parent
8f96c167f8
commit
d4f32f7bdd
3 changed files with 40 additions and 5 deletions
|
|
@ -5,10 +5,12 @@ import {
|
|||
JoinColumn,
|
||||
ManyToOne,
|
||||
PrimaryGeneratedColumn,
|
||||
Unique,
|
||||
} from 'typeorm'
|
||||
import { User } from './user'
|
||||
|
||||
@Entity({ name: 'search_history' })
|
||||
@Unique('search_history_user_id_term_key', ['user', 'term'])
|
||||
export class SearchHistory {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id!: string
|
||||
|
|
|
|||
|
|
@ -16,11 +16,16 @@ export const saveSearchHistory = async (
|
|||
userId: string,
|
||||
term: string
|
||||
): Promise<void> => {
|
||||
const searchHistory = new SearchHistory()
|
||||
searchHistory.user.id = userId
|
||||
searchHistory.term = term
|
||||
searchHistory.createdAt = new Date()
|
||||
await getRepository(SearchHistory).save(searchHistory)
|
||||
await getRepository(SearchHistory).upsert(
|
||||
{
|
||||
user: { id: userId },
|
||||
term,
|
||||
createdAt: new Date(),
|
||||
},
|
||||
{
|
||||
conflictPaths: ['user', 'term'],
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
export const deleteSearchHistory = async (userId: string): Promise<void> => {
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ import {
|
|||
} from '../../src/elastic/pages'
|
||||
import { addHighlightToPage } from '../../src/elastic/highlights'
|
||||
import { refreshIndex } from '../../src/elastic'
|
||||
import { SearchHistory } from '../../src/entity/search_history'
|
||||
|
||||
chai.use(chaiString)
|
||||
|
||||
|
|
@ -842,6 +843,33 @@ describe('Article API', () => {
|
|||
|
||||
after(async () => {
|
||||
await deletePagesByParam({ userId: user.id }, ctx)
|
||||
await getRepository(SearchHistory).delete({ user: { id: user.id } })
|
||||
})
|
||||
|
||||
context('when we search for a keyword', () => {
|
||||
before(() => {
|
||||
keyword = 'search'
|
||||
})
|
||||
|
||||
it('saves the term in search history', async () => {
|
||||
await graphqlRequest(query, authToken).expect(200)
|
||||
const searchHistories = await getRepository(SearchHistory).findBy({
|
||||
user: { id: user.id },
|
||||
})
|
||||
expect(searchHistories.length).to.eq(1)
|
||||
expect(searchHistories[0].term).to.eq(keyword)
|
||||
const searchHistory = searchHistories[0]
|
||||
|
||||
// Check that the search history is updated
|
||||
await graphqlRequest(query, authToken).expect(200)
|
||||
const newSearchHistories = await getRepository(SearchHistory).findBy({
|
||||
user: { id: user.id },
|
||||
})
|
||||
expect(newSearchHistories.length).to.eq(1)
|
||||
expect(newSearchHistories[0].createdAt).to.be.greaterThan(
|
||||
searchHistory.createdAt
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
context('when type:highlights is not in the query', () => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue