From 992ae61432d02eb464aa8dfe2184d46cd9a171c2 Mon Sep 17 00:00:00 2001 From: hmacr Date: Fri, 7 Mar 2025 01:30:42 +0530 Subject: [PATCH] create import --- app/components/Dialogs.tsx | 5 +- app/stores/DialogsStore.ts | 4 ++ plugins/notion/client/Imports.tsx | 33 +++++++++-- .../notion/client/components/ImportDialog.tsx | 49 ++++++++++++--- .../20250306181804-create-imports.js | 56 ++++++++++++++++++ server/models/Import.ts | 59 +++++++++++++++++++ server/models/index.ts | 2 + server/routes/api/imports/imports.ts | 36 +++++++++++ server/routes/api/imports/index.ts | 1 + server/routes/api/imports/schema.ts | 20 +++++++ server/routes/api/index.ts | 2 + shared/types.ts | 12 ++++ 12 files changed, 264 insertions(+), 15 deletions(-) create mode 100644 server/migrations/20250306181804-create-imports.js create mode 100644 server/models/Import.ts create mode 100644 server/routes/api/imports/imports.ts create mode 100644 server/routes/api/imports/index.ts create mode 100644 server/routes/api/imports/schema.ts diff --git a/app/components/Dialogs.tsx b/app/components/Dialogs.tsx index e7f4d05589..d1af42168c 100644 --- a/app/components/Dialogs.tsx +++ b/app/components/Dialogs.tsx @@ -23,7 +23,10 @@ function Dialogs() { key={id} isOpen={modal.isOpen} fullscreen={modal.fullscreen ?? false} - onRequestClose={() => dialogs.closeModal(id)} + onRequestClose={() => { + modal.onClose?.(); + dialogs.closeModal(id); + }} title={modal.title} style={modal.style} > diff --git a/app/stores/DialogsStore.ts b/app/stores/DialogsStore.ts index d39be23ddb..d8956ddb12 100644 --- a/app/stores/DialogsStore.ts +++ b/app/stores/DialogsStore.ts @@ -8,6 +8,7 @@ type DialogDefinition = { isOpen: boolean; fullscreen?: boolean; style?: React.CSSProperties; + onClose?: () => void; }; export default class DialogsStore { @@ -50,6 +51,7 @@ export default class DialogsStore { fullscreen, replace, style, + onClose, }: { id?: string; title: string; @@ -57,6 +59,7 @@ export default class DialogsStore { content: React.ReactNode; style?: React.CSSProperties; replace?: boolean; + onClose?: () => void; }) => { setTimeout( action(() => { @@ -70,6 +73,7 @@ export default class DialogsStore { fullscreen, style, isOpen: true, + onClose, }); }), 0 diff --git a/plugins/notion/client/Imports.tsx b/plugins/notion/client/Imports.tsx index 100e27fc8e..400735f3d5 100644 --- a/plugins/notion/client/Imports.tsx +++ b/plugins/notion/client/Imports.tsx @@ -1,6 +1,7 @@ import { observer } from "mobx-react"; import React from "react"; import { useTranslation } from "react-i18next"; +import { useHistory, useLocation } from "react-router-dom"; import { toast } from "sonner"; import env from "@shared/env"; import { IntegrationService } from "@shared/types"; @@ -16,23 +17,45 @@ export const Notion = observer(() => { const { t } = useTranslation(); const { dialogs } = useStores(); const team = useCurrentTeam(); - const appName = env.APP_NAME; + const history = useHistory(); + const location = useLocation(); + const queryParams = useQuery(); + const appName = env.APP_NAME; const authUrl = NotionUtils.authUrl({ state: { teamId: team.id } }); - const queryParams = useQuery(); const service = queryParams.get("service"); const oauthSuccess = queryParams.get("success") === ""; const oauthError = queryParams.get("error"); + const integrationId = queryParams.get("integrationId"); + + const clearQueryParams = React.useCallback(() => { + history.replace({ + pathname: location.pathname, + search: "", + }); + }, [history, location]); + + const handleSubmit = React.useCallback(() => { + dialogs.closeAllModals(); + clearQueryParams(); + }, [dialogs, clearQueryParams]); React.useEffect(() => { - if (oauthSuccess && service === IntegrationService.Notion) { + if ( + integrationId && + oauthSuccess && + service === IntegrationService.Notion + ) { dialogs.openModal({ title: t("Import data"), - content: , + content: ( + + ), + onClose: clearQueryParams, }); } - }, [t, dialogs, oauthSuccess, service]); + }, [t, dialogs, oauthSuccess, service, clearQueryParams]); React.useEffect(() => { if (!oauthError) { diff --git a/plugins/notion/client/components/ImportDialog.tsx b/plugins/notion/client/components/ImportDialog.tsx index 96844241d4..c8082f0873 100644 --- a/plugins/notion/client/components/ImportDialog.tsx +++ b/plugins/notion/client/components/ImportDialog.tsx @@ -3,13 +3,16 @@ import { useTranslation } from "react-i18next"; import { toast } from "sonner"; import styled from "styled-components"; import { s } from "@shared/styles"; -import { CollectionPermission } from "@shared/types"; +import { + CollectionPermission, + ImportData, + IntegrationService, +} from "@shared/types"; import Button from "~/components/Button"; import { Emoji } from "~/components/Emoji"; import Flex from "~/components/Flex"; import InputSelectPermission from "~/components/InputSelectPermission"; import Text from "~/components/Text"; -import useQuery from "~/hooks/useQuery"; import useRequest from "~/hooks/useRequest"; import { EmptySelectValue } from "~/types"; import { client } from "~/utils/ApiClient"; @@ -19,10 +22,13 @@ type PageWithPermission = Page & { permission?: CollectionPermission; }; -export function ImportDialog() { +type Props = { + integrationId: string; + onSubmit: () => void; +}; + +export function ImportDialog({ integrationId, onSubmit }: Props) { const { t } = useTranslation(); - const queryParams = useQuery(); - const integrationId = queryParams.get("integrationId"); // integration id will be available here since dialog renders only in success flow. const [pagesWithPermission, setPagesWithPermission] = React.useState(); @@ -53,9 +59,30 @@ export function ImportDialog() { [] ); - const handleStartImport = React.useCallback(() => { - console.log("start import"); - }, []); + const handleStartImport = React.useCallback(async () => { + const data: ImportData = { + collection: pagesWithPermission!.map((page) => ({ + externalId: page.id, + permission: page.permission, + })), + }; + + try { + await client.post("/imports.create", { + integrationId, + service: IntegrationService.Notion, + data, + }); + + toast.success( + t("Your import is being processed, you can safely leave this page") + ); + + onSubmit(); + } catch (err) { + toast.error(err.message); + } + }, [pagesWithPermission, onSubmit]); React.useEffect(() => { if (pages?.length) { @@ -74,10 +101,14 @@ export function ImportDialog() { return
Loading data
; } + if (pagesWithPermission.length === 0) { + return
No pages available for import
; + } + return (
- {pagesWithPermission?.map((page) => ( + {pagesWithPermission.map((page) => ( extends IdModel< + InferAttributes>, + Partial>> +> { + @IsIn([Object.values(IntegrationService)]) + @Column(DataType.STRING) + service: IntegrationService; + + @IsIn([Object.values(ImportState)]) + @Column(DataType.STRING) + state: ImportState; + + @Column(DataType.JSONB) + data: ImportData; + + @Default(0) + @Column(DataType.INTEGER) + pendingTaskCount: number; + + // associations + + @BelongsTo(() => User, "createdById") + createdBy: User; + + @ForeignKey(() => User) + @Column(DataType.UUID) + createdById: string; + + @BelongsTo(() => Integration, "integrationId") + integration: Integration; + + @ForeignKey(() => Integration) + @Column(DataType.UUID) + integrationId: string; +} + +export default Import; diff --git a/server/models/index.ts b/server/models/index.ts index 3edc7b7fdb..1ba5dc1df3 100644 --- a/server/models/index.ts +++ b/server/models/index.ts @@ -24,6 +24,8 @@ export { default as Group } from "./Group"; export { default as GroupUser } from "./GroupUser"; +export { default as Import } from "./Import"; + export { default as Integration } from "./Integration"; export { default as IntegrationAuthentication } from "./IntegrationAuthentication"; diff --git a/server/routes/api/imports/imports.ts b/server/routes/api/imports/imports.ts new file mode 100644 index 0000000000..9a9dd2de93 --- /dev/null +++ b/server/routes/api/imports/imports.ts @@ -0,0 +1,36 @@ +import Router from "koa-router"; +import { ImportState, UserRole } from "@shared/types"; +import auth from "@server/middlewares/authentication"; +import validate from "@server/middlewares/validate"; +import Import from "@server/models/Import"; +import { authorize } from "@server/policies"; +import { APIContext } from "@server/types"; +import * as T from "./schema"; + +const router = new Router(); + +router.post( + "imports.create", + auth({ role: UserRole.Admin }), + validate(T.ImportsCreateSchema), + async (ctx: APIContext) => { + const { integrationId, service, data } = ctx.input.body; + const { user } = ctx.state.auth; + + authorize(user, "createImport", user.team); + + await Import.create({ + service, + state: ImportState.Created, + data, + createdById: user.id, + integrationId, + }); + + ctx.body = { + success: true, + }; + } +); + +export default router; diff --git a/server/routes/api/imports/index.ts b/server/routes/api/imports/index.ts new file mode 100644 index 0000000000..01e8fb8633 --- /dev/null +++ b/server/routes/api/imports/index.ts @@ -0,0 +1 @@ +export { default } from "./imports"; diff --git a/server/routes/api/imports/schema.ts b/server/routes/api/imports/schema.ts new file mode 100644 index 0000000000..dbfb257113 --- /dev/null +++ b/server/routes/api/imports/schema.ts @@ -0,0 +1,20 @@ +import { z } from "zod"; +import { CollectionPermission, IntegrationService } from "@shared/types"; +import { BaseSchema } from "../schema"; + +export const ImportsCreateSchema = BaseSchema.extend({ + body: z.object({ + integrationId: z.string().uuid(), + service: z.nativeEnum(IntegrationService), + data: z.object({ + collection: z.array( + z.object({ + externalId: z.string(), + permission: z.nativeEnum(CollectionPermission).optional(), + }) + ), + }), + }), +}); + +export type ImportsCreateReq = z.infer; diff --git a/server/routes/api/index.ts b/server/routes/api/index.ts index 2ca9bd2085..65e36e35a3 100644 --- a/server/routes/api/index.ts +++ b/server/routes/api/index.ts @@ -20,6 +20,7 @@ import events from "./events"; import fileOperationsRoute from "./fileOperations"; import groupMemberships from "./groupMemberships"; import groups from "./groups"; +import imports from "./imports"; import installation from "./installation"; import integrations from "./integrations"; import apiErrorHandler from "./middlewares/apiErrorHandler"; @@ -97,6 +98,7 @@ router.use("/", fileOperationsRoute.routes()); router.use("/", urls.routes()); router.use("/", userMemberships.routes()); router.use("/", reactions.routes()); +router.use("/", imports.routes()); if (!env.isCloudHosted) { router.use("/", installation.routes()); diff --git a/shared/types.ts b/shared/types.ts index 431639ebad..52f486b481 100644 --- a/shared/types.ts +++ b/shared/types.ts @@ -54,6 +54,18 @@ export enum FileOperationState { Expired = "expired", } +export enum ImportState { + Created = "created", + InProgress = "in_progress", + Processed = "processed", + Completed = "completed", + Errored = "errored", +} + +export type ImportData = { + collection: { externalId: string; permission?: CollectionPermission }[]; +}; + export enum MentionType { User = "user", Document = "document",