diff --git a/packages/api/src/services/search_history.ts b/packages/api/src/services/search_history.ts new file mode 100644 index 000000000..2521ea64a --- /dev/null +++ b/packages/api/src/services/search_history.ts @@ -0,0 +1,38 @@ +import { SearchHistory } from '../entity/search_history' +import { getRepository } from '../entity/utils' + +export const getRecentSearches = async ( + userId: string +): Promise => { + // get top 10 recent searches + return getRepository(SearchHistory).find({ + where: { user: { id: userId } }, + order: { createdAt: 'DESC' }, + take: 10, + }) +} + +export const saveSearchHistory = async ( + userId: string, + term: string +): Promise => { + const searchHistory = new SearchHistory() + searchHistory.user = { id: userId } as any + searchHistory.term = term + searchHistory.createdAt = new Date() + await getRepository(SearchHistory).save(searchHistory) +} + +export const deleteSearchHistory = async (userId: string): Promise => { + await getRepository(SearchHistory).delete({ user: { id: userId } }) +} + +export const deleteSearchHistoryById = async ( + userId: string, + searchHistoryId: string +): Promise => { + await getRepository(SearchHistory).delete({ + user: { id: userId }, + id: searchHistoryId, + }) +}