Skip to content

Commit

Permalink
Merge branch 'main' into siyangcao/eng-1730-add-mutations-and-queries…
Browse files Browse the repository at this point in the history
…-into-the-front-end
  • Loading branch information
csiyang authored Feb 3, 2025
2 parents 5b80272 + ad75f2e commit 0b50e14
Show file tree
Hide file tree
Showing 8 changed files with 586 additions and 96 deletions.
11 changes: 10 additions & 1 deletion apps/api-journeys/src/__generated__/gql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 32 additions & 37 deletions apps/api-media/src/workers/algolia/service/service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import algoliasearch from 'algoliasearch'
import { algoliasearch } from 'algoliasearch'
import clone from 'lodash/clone'

import { VideoVariant } from '.prisma/api-media-client'
Expand All @@ -14,10 +14,6 @@ const saveObjectsSpy = jest
.fn()
.mockReturnValue({ wait: jest.fn().mockResolvedValue({}) })

const initIndexSpy = jest.fn().mockReturnValue({
saveObjects: saveObjectsSpy
})

jest.mock('@apollo/client', () => {
const originalModule = jest.requireActual('@apollo/client')
return {
Expand Down Expand Up @@ -54,17 +50,13 @@ jest.mock('@apollo/client', () => {
}
})

jest.mock('algoliasearch', () => {
return jest.fn().mockImplementation(() => {
return {
initIndex: initIndexSpy
}
})
})
jest.mock('algoliasearch', () => ({
algoliasearch: jest.fn().mockImplementation(() => ({
saveObjects: saveObjectsSpy
}))
}))

const mockAlgoliaSearch = algoliasearch as jest.MockedFunction<
typeof algoliasearch
>
const mockAlgoliaSearch = algoliasearch

describe('algolia/service', () => {
const originalEnv = clone(process.env)
Expand Down Expand Up @@ -145,28 +137,31 @@ describe('algolia/service', () => {
}
})
expect(mockAlgoliaSearch).toHaveBeenCalledWith('id', 'key')
expect(initIndexSpy).toHaveBeenCalledWith('video-variants')
expect(saveObjectsSpy).toHaveBeenCalledWith([
{
childrenCount: 1,
description: ['description'],
duration: 100,
image: `https://imagedelivery.net/${
process.env.CLOUDFLARE_IMAGE_ACCOUNT ?? 'testAccount'
}/imageId/f=jpg,w=1280,h=600,q=95`,
imageAlt: 'imageAlt',
label: 'label',
languageId: '21754',
languageEnglishName: 'Chinese, Simplified',
languagePrimaryName: '简体中文',
objectID: 'id',
slug: 'slug',
subtitles: ['21754'],
titles: ['title2', 'title'],
videoId: 'videoId',
manualRanking: 1
}
])
expect(saveObjectsSpy).toHaveBeenCalledWith({
indexName: 'video-variants',
objects: [
{
childrenCount: 1,
description: ['description'],
duration: 100,
image: `https://imagedelivery.net/${
process.env.CLOUDFLARE_IMAGE_ACCOUNT ?? 'testAccount'
}/imageId/f=jpg,w=1280,h=600,q=95`,
imageAlt: 'imageAlt',
label: 'label',
languageId: '21754',
languageEnglishName: 'Chinese, Simplified',
languagePrimaryName: '简体中文',
objectID: 'id',
slug: 'slug',
subtitles: ['21754'],
titles: ['title2', 'title'],
videoId: 'videoId',
manualRanking: 1
}
],
waitForTasks: true
})
})

it('should sync all videos to Algolia when prd', async () => {
Expand Down
9 changes: 6 additions & 3 deletions apps/api-media/src/workers/algolia/service/service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApolloClient, InMemoryCache, createHttpLink } from '@apollo/client'
import algoliasearch from 'algoliasearch'
import { algoliasearch } from 'algoliasearch'
import { Logger } from 'pino'

import { graphql } from '../../../lib/graphql/gatewayGraphql'
Expand Down Expand Up @@ -158,9 +158,12 @@ export async function service(logger?: Logger): Promise<void> {
}
})

const index = client.initIndex(appIndex)
try {
await index.saveObjects(transformedVideos).wait()
await client.saveObjects({
indexName: appIndex,
objects: transformedVideos,
waitForTasks: true
})
logger?.info(`exported ${offset} videos to algolia`)
} catch (error) {
logger?.error(error, 'unable to export videos to algolia')
Expand Down
19 changes: 12 additions & 7 deletions apps/arclight/src/app/v2/resources/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import algoliasearch from 'algoliasearch'
import { algoliasearch } from 'algoliasearch'
import { ResultOf, graphql } from 'gql.tada'
import { NextRequest } from 'next/server'

Expand Down Expand Up @@ -88,17 +88,21 @@ async function searchAlgolia(term: string) {
}
const client = algoliasearch(appID, apiKey)

const { results } = await client.search<AlgoliaHit>([
const { results } = await client.searchSingleIndex<AlgoliaHit>(
{
indexName,
query: term,
params: {
filters: `languageId:529`,
searchParams: {
query: term,
filters: `languageId:529`
}
},
{
queryParameters: {
highlightPreTag: '<>',
highlightPostTag: '<>'
}
}
])
)

if (!results[0] || !('hits' in results[0])) {
throw new Error('Unexpected Algolia response format')
Expand Down Expand Up @@ -201,7 +205,8 @@ export async function GET(request: NextRequest): Promise<Response> {
),
alternateLanguages: [],
mediaComponents: transformedVideos.map(
(video) => video.mediaComponentId
(video: { mediaComponentId: string }) =>
video.mediaComponentId
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import algoliasearch, { SearchClient } from 'algoliasearch'
import { SearchClient, algoliasearch } from 'algoliasearch'
import { ReactElement, ReactNode, createContext, useContext } from 'react'

const searchClient = algoliasearch(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import algoliasearch from 'algoliasearch'
import { algoliasearch } from 'algoliasearch'
import type { UiState } from 'instantsearch.js'
import type { ReactElement, ReactNode } from 'react'
import { InstantSearch } from 'react-instantsearch'
Expand Down
Loading

0 comments on commit 0b50e14

Please sign in to comment.