({
accountId,
logger,
}: {
accountId: string;
logger: Logger;
})
| 15 | import { env } from './utils/env'; |
| 16 | |
| 17 | export async function setup({ |
| 18 | accountId, |
| 19 | logger, |
| 20 | }: { |
| 21 | accountId: string; |
| 22 | logger: Logger; |
| 23 | }) { |
| 24 | const account = await prisma.accounts.findUnique({ |
| 25 | where: { |
| 26 | id: accountId, |
| 27 | }, |
| 28 | }); |
| 29 | |
| 30 | if (!account) { |
| 31 | throw new Error(`account not found: ${accountId}`); |
| 32 | } |
| 33 | |
| 34 | const isPublic = account.type === 'PUBLIC'; |
| 35 | |
| 36 | const key = isPublic |
| 37 | ? createAccountKey({ |
| 38 | keyWithSearchPermissions: env.TYPESENSE_SEARCH_ONLY, |
| 39 | accountId: account.id, |
| 40 | }) |
| 41 | : undefined; |
| 42 | |
| 43 | const searchSettings: SerializedSearchSettings = { |
| 44 | engine: 'typesense', |
| 45 | scope: isPublic ? 'public' : 'private', |
| 46 | apiKey: key?.value || 'private', |
| 47 | apiKeyExpiresAt: key?.expires_at, |
| 48 | }; |
| 49 | |
| 50 | let cursor = 0; |
| 51 | do { |
| 52 | const threads = await queryThreads({ |
| 53 | where: { |
| 54 | ...threadsWhere({ accountId }), |
| 55 | incrementId: { gt: cursor }, |
| 56 | }, |
| 57 | orderBy: { incrementId: 'asc' }, |
| 58 | take: 50, |
| 59 | }); |
| 60 | |
| 61 | if (!threads.length) { |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | cursor = threads.at(threads.length - 1)?.incrementId!; |
| 66 | |
| 67 | await pushToTypesense({ |
| 68 | threads, |
| 69 | is_restrict: searchSettings.scope === 'private', |
| 70 | logger, |
| 71 | anonymize: account.anonymizeUsers |
| 72 | ? (account.anonymize as AnonymizeType) |
| 73 | : undefined, |
| 74 | }); |
no test coverage detected