From 56be416a3443f21c7452ed17ec151856339edfdd Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 5 Nov 2024 21:43:55 +0200 Subject: [PATCH 1/5] feat: moved fetching research to remix backend --- .../Content/Common/ResearchCategorySelect.tsx | 6 +- src/pages/Research/Content/ResearchList.tsx | 20 +- src/pages/Research/research.service.test.ts | 198 ++++++++---------- src/pages/Research/research.service.ts | 188 ++++------------- src/routes/api.research.categories.ts | 27 +++ src/routes/api.research.drafts.count.ts | 36 ++++ src/routes/api.research.ts | 176 ++++++++++++++++ 7 files changed, 381 insertions(+), 270 deletions(-) create mode 100644 src/routes/api.research.categories.ts create mode 100644 src/routes/api.research.drafts.count.ts create mode 100644 src/routes/api.research.ts diff --git a/src/pages/Research/Content/Common/ResearchCategorySelect.tsx b/src/pages/Research/Content/Common/ResearchCategorySelect.tsx index 8144e2e736..2a986eb0e6 100644 --- a/src/pages/Research/Content/Common/ResearchCategorySelect.tsx +++ b/src/pages/Research/Content/Common/ResearchCategorySelect.tsx @@ -13,11 +13,7 @@ const ResearchFieldCategory = () => { useEffect(() => { const getCategories = async () => { const categories = await researchService.getResearchCategories() - setOptions( - categories - .filter((x) => !x._deleted) - .map((x) => ({ label: x.label, value: x })), - ) + setOptions(categories.map((x) => ({ label: x.label, value: x }))) } getCategories() diff --git a/src/pages/Research/Content/ResearchList.tsx b/src/pages/Research/Content/ResearchList.tsx index 303317e5c1..423037b25f 100644 --- a/src/pages/Research/Content/ResearchList.tsx +++ b/src/pages/Research/Content/ResearchList.tsx @@ -10,14 +10,13 @@ import DraftButton from 'src/pages/common/Drafts/DraftButton' import useDrafts from 'src/pages/common/Drafts/useDrafts' import { Box, Flex, Heading, useThemeUI } from 'theme-ui' -import { ITEMS_PER_PAGE, RESEARCH_EDITOR_ROLES } from '../constants' +import { RESEARCH_EDITOR_ROLES } from '../constants' import { listing } from '../labels' import { researchService } from '../research.service' import { ResearchFilterHeader } from './ResearchFilterHeader' import ResearchListItem from './ResearchListItem' import { ResearchSearchParams } from './ResearchSearchParams' -import type { DocumentData, QueryDocumentSnapshot } from 'firebase/firestore' import type { IResearch, ResearchStatus } from 'oa-shared' import type { ThemeWithName } from 'oa-themes' import type { ResearchSortOption } from '../ResearchSortOptions' @@ -34,9 +33,7 @@ const ResearchList = observer(() => { getDrafts: researchService.getDrafts, }) const [total, setTotal] = useState(0) - const [lastVisible, setLastVisible] = useState< - QueryDocumentSnapshot | undefined - >(undefined) + const [lastId, setLastId] = useState(undefined) const [searchParams, setSearchParams] = useSearchParams() const q = searchParams.get(ResearchSearchParams.q) || '' @@ -63,9 +60,7 @@ const ResearchList = observer(() => { } }, [q, category, status, sort]) - const fetchResearchItems = async ( - skipFrom?: QueryDocumentSnapshot, - ) => { + const fetchResearchItems = async (lastDocId?: string) => { setIsFetching(true) try { @@ -76,18 +71,17 @@ const ResearchList = observer(() => { category, sort, status, - skipFrom, - ITEMS_PER_PAGE, + lastDocId, ) - if (skipFrom) { + if (lastDocId) { // if skipFrom is set, means we are requesting another page that should be appended setResearchItems((items) => [...items, ...result.items]) } else { setResearchItems(result.items) } - setLastVisible(result.lastVisible) + setLastId(result.items[result.items.length - 1]._id) setTotal(result.total) } catch (error) { @@ -187,7 +181,7 @@ const ResearchList = observer(() => { > diff --git a/src/pages/Research/research.service.test.ts b/src/pages/Research/research.service.test.ts index 348edfcbd7..016a6f542d 100644 --- a/src/pages/Research/research.service.test.ts +++ b/src/pages/Research/research.service.test.ts @@ -1,120 +1,108 @@ -import '@testing-library/jest-dom/vitest' - -import { ResearchStatus } from 'oa-shared' import { describe, expect, it, vi } from 'vitest' - -import { exportedForTesting } from './research.service' - -const mockWhere = vi.fn() -const mockOrderBy = vi.fn() -const mockLimit = vi.fn() -vi.mock('firebase/firestore', () => ({ - collection: vi.fn(), - query: vi.fn(), - and: vi.fn(), - where: (path, op, value) => mockWhere(path, op, value), - limit: (limit) => mockLimit(limit), - orderBy: (field, direction) => mockOrderBy(field, direction), -})) - -vi.mock('../../stores/databaseV2/endpoints', () => ({ - DB_ENDPOINTS: { - research: 'research', - researchCategories: 'researchCategories', - }, -})) - -vi.mock('../../config/config', () => ({ - getConfigurationOption: vi.fn(), - FIREBASE_CONFIG: { - apiKey: 'AIyChVN', - databaseURL: 'https://test.firebaseio.com', - projectId: 'test', - storageBucket: 'test.appspot.com', - }, - localStorage: vi.fn(), - SITE: 'unit-tests', -})) - -describe('research.search', () => { - it('searches for text', () => { - // prepare - const words = ['test', 'text'] - - // act - exportedForTesting.createSearchQuery(words, '', 'MostRelevant', null) - - // assert - expect(mockWhere).toHaveBeenCalledWith( - 'keywords', - 'array-contains-any', - words, - ) +import { researchService } from './research.service' + +describe('research.service', () => { + describe('search', () => { + it('fetches research articles based on search criteria', async () => { + // Mock successful fetch response + global.fetch = vi.fn().mockResolvedValue({ + json: () => + Promise.resolve({ + items: [{ id: '1', title: 'Sample Research' }], + total: 1, + }), + }) + + // Call search with mock parameters + const result = await researchService.search( + ['sample'], + 'science', + 'Newest', + null, + ) + + // Assert results + expect(result).toEqual({ + items: [{ id: '1', title: 'Sample Research' }], + total: 1, + }) + }) + + it('handles errors in search', async () => { + global.fetch = vi.fn().mockRejectedValue('error') + + const result = await researchService.search( + ['sample'], + 'science', + 'Newest', + null, + ) + + expect(result).toEqual({ items: [], total: 0 }) + }) }) - it('filters by category', () => { - // prepare - const category = 'cat1' + describe('getResearchCategories', () => { + it('fetches research categories', async () => { + global.fetch = vi.fn().mockResolvedValue({ + json: () => + Promise.resolve({ categories: [{ id: 'cat1', name: 'Science' }] }), + }) - // act - exportedForTesting.createSearchQuery([], category, 'MostRelevant', null) + const result = await researchService.getResearchCategories() - // assert - expect(mockWhere).toHaveBeenCalledWith( - 'researchCategory._id', - '==', - category, - ) - }) + expect(result).toEqual([{ id: 'cat1', name: 'Science' }]) + }) + + it('handles errors in fetching research categories', async () => { + global.fetch = vi.fn().mockRejectedValue('error') - it('should not call orderBy if sorting by most relevant', () => { - // act - exportedForTesting.createSearchQuery(['test'], '', 'MostRelevant', null) + const result = await researchService.getResearchCategories() - // assert - expect(mockOrderBy).toHaveBeenCalledTimes(0) + expect(result).toEqual([]) + }) }) - it('should call orderBy when sorting is not MostRelevant', () => { - // act - exportedForTesting.createSearchQuery(['test'], '', 'Newest', null) + describe('getDraftCount', () => { + it('fetches draft count for a user', async () => { + global.fetch = vi.fn().mockResolvedValue({ + json: () => Promise.resolve({ total: 5 }), + }) - // assert - expect(mockOrderBy).toHaveBeenLastCalledWith('_created', 'desc') - }) + const result = await researchService.getDraftCount('user123') + + expect(result).toBe(5) + }) + + it('handles errors in fetching draft count', async () => { + global.fetch = vi.fn().mockRejectedValue('error') - it('should filter by research status', () => { - // act - exportedForTesting.createSearchQuery( - ['test'], - '', - 'Newest', - ResearchStatus.COMPLETED, - ) - - // assert - expect(mockWhere).toHaveBeenCalledWith( - 'researchStatus', - '==', - ResearchStatus.COMPLETED, - ) + const result = await researchService.getDraftCount('user123') + + expect(result).toBe(0) + }) }) - it('should limit results', () => { - // prepare - const take = 12 - - // act - exportedForTesting.createSearchQuery( - ['test'], - '', - 'Newest', - null, - undefined, - take, - ) - - // assert - expect(mockLimit).toHaveBeenLastCalledWith(take) + describe('getDrafts', () => { + it('fetches research drafts for a user', async () => { + global.fetch = vi.fn().mockResolvedValue({ + json: () => + Promise.resolve({ + items: [{ id: 'draft1', title: 'Draft Research' }], + }), + }) + + const result = await researchService.getDrafts('user123') + + expect(result).toEqual([{ id: 'draft1', title: 'Draft Research' }]) + }) + + it('handles errors in fetching drafts', async () => { + global.fetch = vi.fn().mockRejectedValue('error') + + const result = await researchService.getDrafts('user123') + + expect(result).toEqual([]) + }) }) }) diff --git a/src/pages/Research/research.service.ts b/src/pages/Research/research.service.ts index 0c38104a62..00c6145f78 100644 --- a/src/pages/Research/research.service.ts +++ b/src/pages/Research/research.service.ts @@ -1,26 +1,5 @@ -import { - and, - collection, - getCountFromServer, - getDocs, - limit, - orderBy, - query, - startAfter, - where, -} from 'firebase/firestore' -import { IModerationStatus } from 'oa-shared' -import { DB_ENDPOINTS } from 'src/models/dbEndpoints' - -import { firestore } from '../../utils/firebase' - -import type { - DocumentData, - QueryDocumentSnapshot, - QueryFilterConstraint, - QueryNonFilterConstraint, -} from 'firebase/firestore' -import type { ICategory, IResearch, ResearchStatus } from 'oa-shared' +import { logger } from 'src/logger' +import type { IResearch, ICategory, ResearchStatus } from 'oa-shared' import type { ResearchSortOption } from './ResearchSortOptions' const search = async ( @@ -28,141 +7,60 @@ const search = async ( category: string, sort: ResearchSortOption, status: ResearchStatus | null, - snapshot?: QueryDocumentSnapshot, - take: number = 10, + lastDocId?: string, ) => { - const { itemsQuery, countQuery } = createSearchQuery( - words, - category, - sort, - status, - snapshot, - take, - ) - - const documentSnapshots = await getDocs(itemsQuery) - const lastVisible = documentSnapshots.docs - ? documentSnapshots.docs[documentSnapshots.docs.length - 1] - : undefined - - const items = documentSnapshots.docs - ? documentSnapshots.docs.map((x) => x.data() as IResearch.Item) - : [] - const total = (await getCountFromServer(countQuery)).data().count - - return { items, total, lastVisible } -} - -const createSearchQuery = ( - words: string[], - category: string, - sort: ResearchSortOption, - status: ResearchStatus | null, - snapshot?: QueryDocumentSnapshot, - take: number = 10, -) => { - const collectionRef = collection(firestore, DB_ENDPOINTS.research) - let filters: QueryFilterConstraint[] = [ - and( - where('_deleted', '!=', true), - where('moderation', '==', IModerationStatus.ACCEPTED), - ), - ] - let constraints: QueryNonFilterConstraint[] = [] - - if (words?.length > 0) { - filters = [...filters, and(where('keywords', 'array-contains-any', words))] - } - - if (category) { - filters = [...filters, where('researchCategory._id', '==', category)] - } - - if (status) { - filters = [...filters, where('researchStatus', '==', status)] - } - - if (sort) { - const sortConstraint = getSort(sort) - - if (sortConstraint) { - constraints = [...constraints, sortConstraint] + try { + const response = await fetch( + `/api/research?words=${words.join(',')}&category=${category}&sort=${sort}&status=${status ?? ''}&last_doc_id=${lastDocId ?? ''}`, + ) + const { items, total } = (await response.json()) as { + items: IResearch.Item[] + total: number } - } - - const countQuery = query(collectionRef, and(...filters), ...constraints) - if (snapshot) { - constraints = [...constraints, startAfter(snapshot)] + return { items, total } + } catch (error) { + logger.error('Failed to fetch research articles', { error }) + return { items: [], total: 0 } } - - const itemsQuery = query( - collectionRef, - and(...filters), - ...constraints, - limit(take), - ) - - return { countQuery, itemsQuery } } const getResearchCategories = async () => { - const collectionRef = collection(firestore, DB_ENDPOINTS.researchCategories) - - return (await getDocs(query(collectionRef))).docs.map( - (x) => x.data() as ICategory, - ) -} + try { + const response = await fetch('/api/research/categories') + const { categories } = (await response.json()) as { + categories: ICategory[] + } -const getSort = (sort: ResearchSortOption) => { - switch (sort) { - case 'MostComments': - return orderBy('totalCommentCount', 'desc') - case 'MostUpdates': - return orderBy('totalUpdates', 'desc') - case 'MostUseful': - return orderBy('totalUsefulVotes', 'desc') - case 'Newest': - return orderBy('_created', 'desc') - case 'LatestUpdated': - return orderBy('_contentModifiedTimestamp', 'desc') + return categories + } catch (error) { + logger.error('Failed to fetch draft count', { error }) + return [] } } -const createDraftQuery = (userId: string) => { - const collectionRef = collection(firestore, DB_ENDPOINTS.research) - const filters = and( - where('_createdBy', '==', userId), - where('moderation', 'in', [ - IModerationStatus.AWAITING_MODERATION, - IModerationStatus.DRAFT, - IModerationStatus.IMPROVEMENTS_NEEDED, - IModerationStatus.REJECTED, - ]), - where('_deleted', '!=', true), - ) - - const countQuery = query(collectionRef, filters) - const itemsQuery = query( - collectionRef, - filters, - orderBy('_contentModifiedTimestamp', 'desc'), - ) - - return { countQuery, itemsQuery } -} - const getDraftCount = async (userId: string) => { - const { countQuery } = createDraftQuery(userId) - - return (await getCountFromServer(countQuery)).data().count + try { + const response = await fetch(`/api/research/drafts/count?user_id=${userId}`) + const { total } = (await response.json()) as { total: number } + + return total + } catch (error) { + logger.error('Failed to fetch draft count', { error }) + return 0 + } } const getDrafts = async (userId: string) => { - const { itemsQuery } = createDraftQuery(userId) - const docs = await getDocs(itemsQuery) - - return docs.docs ? docs.docs.map((x) => x.data() as IResearch.Item) : [] + try { + const response = await fetch(`/api/research?drafts=true&user_id=${userId}`) + const { items } = (await response.json()) as { items: IResearch.Item[] } + + return items + } catch (error) { + logger.error('Failed to fetch research draft articles', { error }) + return [] + } } export const researchService = { @@ -171,7 +69,3 @@ export const researchService = { getDrafts, getDraftCount, } - -export const exportedForTesting = { - createSearchQuery, -} diff --git a/src/routes/api.research.categories.ts b/src/routes/api.research.categories.ts new file mode 100644 index 0000000000..015361fb22 --- /dev/null +++ b/src/routes/api.research.categories.ts @@ -0,0 +1,27 @@ +import { json } from '@remix-run/node' +import { collection, getDocs, query, where } from 'firebase/firestore' +import Keyv from 'keyv' +import { DB_ENDPOINTS } from 'src/models/dbEndpoints' +import { firestore } from 'src/utils/firebase' + +import type { ICategory } from 'oa-shared' + +const cache = new Keyv({ ttl: 600000 }) // ttl: 10 minutes + +// runs on the server +export const loader = async () => { + const cachedCategories = await cache.get('researchCategories') + + // check if cached map pins are availbe, if not - load from db and cache them + if (cachedCategories) return json({ categories: cachedCategories }) + + const collectionRef = collection(firestore, DB_ENDPOINTS.researchCategories) + const categoriesQuery = query(collectionRef, where('_deleted', '!=', true)) + + const categories: ICategory[] = (await getDocs(categoriesQuery)).docs.map( + (x) => x.data() as ICategory, + ) + + cache.set('researchCategories', categories) + return json({ categories }) +} diff --git a/src/routes/api.research.drafts.count.ts b/src/routes/api.research.drafts.count.ts new file mode 100644 index 0000000000..171d25617d --- /dev/null +++ b/src/routes/api.research.drafts.count.ts @@ -0,0 +1,36 @@ +import { json } from '@remix-run/node' +import { + and, + collection, + getCountFromServer, + query, + where, +} from 'firebase/firestore' +import { DB_ENDPOINTS } from 'src/models/dbEndpoints' +import { firestore } from 'src/utils/firebase' + +import { IModerationStatus } from 'oa-shared' + +// runs on the server +export const loader = async ({ request }) => { + const url = new URL(request.url) + const searchParams = url.searchParams + const userId: string | null = searchParams.get('user_id') + + const collectionRef = collection(firestore, DB_ENDPOINTS.research) + const filters = and( + where('_createdBy', '==', userId), + where('moderation', 'in', [ + IModerationStatus.AWAITING_MODERATION, + IModerationStatus.DRAFT, + IModerationStatus.IMPROVEMENTS_NEEDED, + IModerationStatus.REJECTED, + ]), + where('_deleted', '!=', true), + ) + + const countQuery = query(collectionRef, filters) + const total = (await getCountFromServer(countQuery)).data().count + + return json({ total }) +} diff --git a/src/routes/api.research.ts b/src/routes/api.research.ts new file mode 100644 index 0000000000..66b80fe032 --- /dev/null +++ b/src/routes/api.research.ts @@ -0,0 +1,176 @@ +import { json } from '@remix-run/node' +import { + and, + doc, + getDoc, + collection, + getCountFromServer, + getDocs, + limit, + orderBy, + query, + startAfter, + where, + QueryFilterConstraint, + QueryNonFilterConstraint, +} from 'firebase/firestore' +import { DB_ENDPOINTS } from 'src/models/dbEndpoints' +import { firestore } from 'src/utils/firebase' +import { ITEMS_PER_PAGE } from 'src/pages/Research/constants' + +import type { IResearch, ResearchStatus } from 'oa-shared' +import { IModerationStatus } from 'oa-shared' +import type { ResearchSortOption } from 'src/pages/Research/ResearchSortOptions.ts' + +// runs on the server +export const loader = async ({ request }) => { + const url = new URL(request.url) + const searchParams = url.searchParams + const words: string[] = + searchParams.get('words') != '' + ? searchParams.get('words')?.split(',') ?? [] + : [] + const category: string | null = searchParams.get('category') + const sort: ResearchSortOption = (searchParams.get('sort') ?? + 'LatestUpdated') as ResearchSortOption + const status: ResearchStatus | null = searchParams.get( + 'status', + ) as ResearchStatus + const lastDocId: string | null = searchParams.get('last_doc_id') + const drafts: boolean = searchParams.get('drafts') != undefined + const userId: string | null = searchParams.get('user_id') + + const { itemsQuery, countQuery } = await createSearchQuery( + words, + category, + sort, + status, + lastDocId, + ITEMS_PER_PAGE, + drafts, + userId, + ) + + const documentSnapshots = await getDocs(itemsQuery) + const items = documentSnapshots.docs + ? documentSnapshots.docs.map((x) => x.data() as IResearch.Item) + : [] + + let total: number | undefined = undefined + // get total only if not requesting drafts + if (!drafts || !userId) + total = (await getCountFromServer(countQuery)).data().count + + return json({ items, total }) +} + +export const action = async ({ request }) => { + const method = request.method + switch (method) { + case 'POST': + // Create new research + return json({ message: 'Created a research' }) + case 'PUT': + // Edit existing research + return json({ message: 'Updated a research' }) + case 'DELETE': + // Delete a research + return json({ message: 'Deleted a research' }) + default: + return json({ message: 'Method Not Allowed' }, { status: 405 }) + } +} + +const createSearchQuery = async ( + words: string[], + category: string | null, + sort: ResearchSortOption, + status: ResearchStatus | null, + lastDocId: string | null, + page_size: number, + drafts: boolean, + userId: string | null, +) => { + let filters: QueryFilterConstraint[] = [] + if (drafts && userId) { + filters = [ + and( + where('_createdBy', '==', userId), + where('moderation', 'in', [ + IModerationStatus.AWAITING_MODERATION, + IModerationStatus.DRAFT, + IModerationStatus.IMPROVEMENTS_NEEDED, + IModerationStatus.REJECTED, + ]), + where('_deleted', '!=', true), + ), + ] + } else { + filters = [ + and( + where('_deleted', '!=', true), + where('moderation', '==', IModerationStatus.ACCEPTED), + ), + ] + } + + let constraints: QueryNonFilterConstraint[] = [] + + const sortByField = getSortByField(sort) + constraints = [orderBy(sortByField, 'desc')] // TODO - add sort by _id to act as a tie breaker + + if (words?.length > 0) { + filters = [...filters, and(where('keywords', 'array-contains-any', words))] + } + + if (category) { + filters = [...filters, where('researchCategory._id', '==', category)] + } + + if (status) { + filters = [...filters, where('researchStatus', '==', status)] + } + + const collectionRef = collection(firestore, DB_ENDPOINTS.research) + const countQuery = query(collectionRef, and(...filters), ...constraints) + + // add pagination only to itemsQuery, not countQuery + if (lastDocId) { + const lastDocSnapshot = await getDoc( + doc(collection(firestore, DB_ENDPOINTS.research), lastDocId), + ) + + if (!lastDocSnapshot.exists) { + throw new Error('Document with the provided ID does not exist.') + } + const lastDocData = lastDocSnapshot.data() as IResearch.Item + + constraints.push(startAfter(lastDocData[sortByField])) // TODO - add startAfter by _id to act as a tie breaker + } + + const itemsQuery = query( + collectionRef, + and(...filters), + ...constraints, + limit(page_size), + ) + + return { countQuery, itemsQuery } +} + +const getSortByField = (sort: ResearchSortOption) => { + switch (sort) { + case 'MostComments': + return 'totalCommentCount' + case 'MostUpdates': + return 'totalUpdates' + case 'MostUseful': + return 'totalUsefulVotes' + case 'Newest': + return '_created' + case 'LatestUpdated': + return '_contentModifiedTimestamp' + default: + return '_contentModifiedTimestamp' + } +} From 647687371b20e67b29768619154fca6ac33fe0df Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 5 Nov 2024 21:51:52 +0200 Subject: [PATCH 2/5] fix: fixed import lint issues --- src/pages/Research/research.service.test.ts | 1 + src/pages/Research/research.service.ts | 3 ++- src/routes/api.research.drafts.count.ts | 3 +-- src/routes/api.research.ts | 13 +++++++------ 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/pages/Research/research.service.test.ts b/src/pages/Research/research.service.test.ts index 016a6f542d..e576aa6a05 100644 --- a/src/pages/Research/research.service.test.ts +++ b/src/pages/Research/research.service.test.ts @@ -1,4 +1,5 @@ import { describe, expect, it, vi } from 'vitest' + import { researchService } from './research.service' describe('research.service', () => { diff --git a/src/pages/Research/research.service.ts b/src/pages/Research/research.service.ts index 00c6145f78..3e97896513 100644 --- a/src/pages/Research/research.service.ts +++ b/src/pages/Research/research.service.ts @@ -1,5 +1,6 @@ import { logger } from 'src/logger' -import type { IResearch, ICategory, ResearchStatus } from 'oa-shared' + +import type { ICategory, IResearch, ResearchStatus } from 'oa-shared' import type { ResearchSortOption } from './ResearchSortOptions' const search = async ( diff --git a/src/routes/api.research.drafts.count.ts b/src/routes/api.research.drafts.count.ts index 171d25617d..3aae4956df 100644 --- a/src/routes/api.research.drafts.count.ts +++ b/src/routes/api.research.drafts.count.ts @@ -6,11 +6,10 @@ import { query, where, } from 'firebase/firestore' +import { IModerationStatus } from 'oa-shared' import { DB_ENDPOINTS } from 'src/models/dbEndpoints' import { firestore } from 'src/utils/firebase' -import { IModerationStatus } from 'oa-shared' - // runs on the server export const loader = async ({ request }) => { const url = new URL(request.url) diff --git a/src/routes/api.research.ts b/src/routes/api.research.ts index 66b80fe032..22072377e8 100644 --- a/src/routes/api.research.ts +++ b/src/routes/api.research.ts @@ -1,25 +1,26 @@ import { json } from '@remix-run/node' import { and, - doc, - getDoc, collection, + doc, getCountFromServer, + getDoc, getDocs, limit, orderBy, query, startAfter, where, - QueryFilterConstraint, - QueryNonFilterConstraint, } from 'firebase/firestore' +import { IModerationStatus } from 'oa-shared' import { DB_ENDPOINTS } from 'src/models/dbEndpoints' -import { firestore } from 'src/utils/firebase' import { ITEMS_PER_PAGE } from 'src/pages/Research/constants' +import { firestore } from 'src/utils/firebase' +import type { + QueryFilterConstraint, + QueryNonFilterConstraint} from 'firebase/firestore'; import type { IResearch, ResearchStatus } from 'oa-shared' -import { IModerationStatus } from 'oa-shared' import type { ResearchSortOption } from 'src/pages/Research/ResearchSortOptions.ts' // runs on the server From c79155bfaac8995fdc57e89cc3a29f8e012022df Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 5 Nov 2024 21:57:23 +0200 Subject: [PATCH 3/5] fix: fix lint issues --- src/routes/api.research.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/routes/api.research.ts b/src/routes/api.research.ts index 22072377e8..1db9017b22 100644 --- a/src/routes/api.research.ts +++ b/src/routes/api.research.ts @@ -19,7 +19,8 @@ import { firestore } from 'src/utils/firebase' import type { QueryFilterConstraint, - QueryNonFilterConstraint} from 'firebase/firestore'; + QueryNonFilterConstraint, +} from 'firebase/firestore' import type { IResearch, ResearchStatus } from 'oa-shared' import type { ResearchSortOption } from 'src/pages/Research/ResearchSortOptions.ts' From ea6c691fec212f6a777ef59c81dd16d38eae11a3 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 8 Nov 2024 12:07:46 +0200 Subject: [PATCH 4/5] fix: fix merge with research SSR --- src/pages/Research/research.service.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/pages/Research/research.service.ts b/src/pages/Research/research.service.ts index 0b58d6ae25..d73cfdc763 100644 --- a/src/pages/Research/research.service.ts +++ b/src/pages/Research/research.service.ts @@ -1,6 +1,15 @@ +import { collection, getDocs, query, where } from 'firebase/firestore' import { logger } from 'src/logger' - -import type { ICategory, IResearch, ResearchStatus } from 'oa-shared' +import { DB_ENDPOINTS } from 'src/models/dbEndpoints' +import { firestore } from 'src/utils/firebase' +import { changeUserReferenceToPlainText } from 'src/utils/mentions.utils' + +import type { + ICategory, + IResearch, + IResearchDB, + ResearchStatus, +} from 'oa-shared' import type { ResearchSortOption } from './ResearchSortOptions' const search = async ( From 8760cc630cc5fad6e9797bb143be2e2a0f25cf7f Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 10 Nov 2024 22:47:15 +0200 Subject: [PATCH 5/5] fix: fix PR reviews --- src/pages/Research/research.service.ts | 15 ++++++++++----- src/routes/api.research.categories.ts | 4 ++-- src/routes/api.research.drafts.count.ts | 2 +- src/routes/api.research.ts | 4 ++-- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/pages/Research/research.service.ts b/src/pages/Research/research.service.ts index d73cfdc763..0456b28741 100644 --- a/src/pages/Research/research.service.ts +++ b/src/pages/Research/research.service.ts @@ -20,9 +20,14 @@ const search = async ( lastDocId?: string, ) => { try { - const response = await fetch( - `/api/research?words=${words.join(',')}&category=${category}&sort=${sort}&status=${status ?? ''}&last_doc_id=${lastDocId ?? ''}`, - ) + const url = new URL('/api/research', window.location.origin) + url.searchParams.append('words', words.join(',')) + url.searchParams.append('category', category) + url.searchParams.append('sort', sort) + url.searchParams.append('status', status ?? '') + url.searchParams.append('lastDocId', lastDocId ?? '') + + const response = await fetch(url) const { items, total } = (await response.json()) as { items: IResearch.Item[] total: number @@ -51,7 +56,7 @@ const getResearchCategories = async () => { const getDraftCount = async (userId: string) => { try { - const response = await fetch(`/api/research/drafts/count?user_id=${userId}`) + const response = await fetch(`/api/research/drafts/count?userId=${userId}`) const { total } = (await response.json()) as { total: number } return total @@ -63,7 +68,7 @@ const getDraftCount = async (userId: string) => { const getDrafts = async (userId: string) => { try { - const response = await fetch(`/api/research?drafts=true&user_id=${userId}`) + const response = await fetch(`/api/research?drafts=true&userId=${userId}`) const { items } = (await response.json()) as { items: IResearch.Item[] } return items diff --git a/src/routes/api.research.categories.ts b/src/routes/api.research.categories.ts index 015361fb22..93ff86de71 100644 --- a/src/routes/api.research.categories.ts +++ b/src/routes/api.research.categories.ts @@ -6,13 +6,13 @@ import { firestore } from 'src/utils/firebase' import type { ICategory } from 'oa-shared' -const cache = new Keyv({ ttl: 600000 }) // ttl: 10 minutes +const cache = new Keyv({ ttl: 3600000 }) // ttl: 60 minutes // runs on the server export const loader = async () => { const cachedCategories = await cache.get('researchCategories') - // check if cached map pins are availbe, if not - load from db and cache them + // check if cached categories are available, if not - load from db and cache them if (cachedCategories) return json({ categories: cachedCategories }) const collectionRef = collection(firestore, DB_ENDPOINTS.researchCategories) diff --git a/src/routes/api.research.drafts.count.ts b/src/routes/api.research.drafts.count.ts index 3aae4956df..b90094f1a5 100644 --- a/src/routes/api.research.drafts.count.ts +++ b/src/routes/api.research.drafts.count.ts @@ -14,7 +14,7 @@ import { firestore } from 'src/utils/firebase' export const loader = async ({ request }) => { const url = new URL(request.url) const searchParams = url.searchParams - const userId: string | null = searchParams.get('user_id') + const userId: string | null = searchParams.get('userId') const collectionRef = collection(firestore, DB_ENDPOINTS.research) const filters = and( diff --git a/src/routes/api.research.ts b/src/routes/api.research.ts index 1db9017b22..f7d64c8337 100644 --- a/src/routes/api.research.ts +++ b/src/routes/api.research.ts @@ -38,9 +38,9 @@ export const loader = async ({ request }) => { const status: ResearchStatus | null = searchParams.get( 'status', ) as ResearchStatus - const lastDocId: string | null = searchParams.get('last_doc_id') + const lastDocId: string | null = searchParams.get('lastDocId') const drafts: boolean = searchParams.get('drafts') != undefined - const userId: string | null = searchParams.get('user_id') + const userId: string | null = searchParams.get('userId') const { itemsQuery, countQuery } = await createSearchQuery( words,