Create an import from integration cloud task

This commit is contained in:
Hongbo Wu
2023-02-27 18:27:17 +08:00
parent b62b769604
commit c8ec84562b
3 changed files with 45 additions and 5 deletions

View File

@ -22,7 +22,11 @@ import { Integration, IntegrationType } from '../../entity/integration'
import { analytics } from '../../utils/analytics'
import { env } from '../../env'
import { getIntegrationService } from '../../services/integrations'
import { deleteTask, enqueueSyncWithIntegration } from '../../utils/createTask'
import {
deleteTask,
enqueueImportFromIntegration,
enqueueSyncWithIntegration,
} from '../../utils/createTask'
export const setIntegrationResolver = authorized<
SetIntegrationSuccess,
@ -239,8 +243,8 @@ export const importFromIntegrationResolver = authorized<
}
}
const integrationService = getIntegrationService(integration.name)
const count = await integrationService.import(integration)
// create a task to import all the pages
await enqueueImportFromIntegration(uid, integration.id)
analytics.track({
userId: uid,
@ -251,7 +255,7 @@ export const importFromIntegrationResolver = authorized<
})
return {
count,
success: true,
}
} catch (error) {
log.error(error)

View File

@ -441,4 +441,37 @@ export const enqueueRecommendation = async (
return createdTasks[0].name
}
export const enqueueImportFromIntegration = async (
userId: string,
integrationId: string
): Promise<string> => {
const { GOOGLE_CLOUD_PROJECT } = process.env
// use pubsub data format to send the userId to the task handler
const payload = {
userId,
integrationId,
}
// If there is no Google Cloud Project Id exposed, it means that we are in local environment
if (env.dev.isLocal || !GOOGLE_CLOUD_PROJECT) {
return nanoid()
}
const createdTasks = await createHttpTaskWithToken({
project: GOOGLE_CLOUD_PROJECT,
payload,
taskHandlerUrl: `${env.queue.integrationTaskHandlerUrl}/import`,
priority: 'low',
})
if (!createdTasks || !createdTasks[0].name) {
logger.error(`Unable to get the name of the task`, {
payload,
createdTasks,
})
throw new CreateTaskError(`Unable to get the name of the task`)
}
return createdTasks[0].name
}
export default createHttpTaskWithToken

View File

@ -129,7 +129,10 @@ export default function Integrations(): JSX.Element {
}
}
if (!router.isReady) return
if (router.query.state == 'pocketAuthorizationFinished') {
if (
router.query.state == 'pocketAuthorizationFinished' &&
!pocketConnected
) {
connectToPocket()
}
}, [router])