From 58992669169ac79740b3fa620617c7203fe2047e Mon Sep 17 00:00:00 2001 From: Nabhag Motivaras Date: Thu, 13 Jul 2023 12:30:43 +0530 Subject: [PATCH] feat: handledCreationOfPage without viewErrors description: handleCreationOfRecord is not implemented which will be done in addup PR but note that this doesnt contain viewErrors as well,see you soon --- src/handlers/ExecuteViewSubmitHandler.ts | 101 +++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/src/handlers/ExecuteViewSubmitHandler.ts b/src/handlers/ExecuteViewSubmitHandler.ts index cc6dfcb..cea2486 100644 --- a/src/handlers/ExecuteViewSubmitHandler.ts +++ b/src/handlers/ExecuteViewSubmitHandler.ts @@ -26,6 +26,11 @@ import { Modals } from "../../enum/modals/common/Modals"; import { handleMissingProperties } from "../helper/handleMissingProperties"; import { getDuplicatePropertyNameViewErrors } from "../helper/getDuplicatePropNameViewError"; import { IMessageAttachmentField } from "@rocket.chat/apps-engine/definition/messages"; +import { NotionPageOrRecord } from "../../enum/modals/NotionPageOrRecord"; +import { NotionObjectTypes } from "../../enum/Notion"; +import { ITokenInfo } from "../../definition/authorization/IOAuth2Storage"; +import { IDatabase, IPage } from "../../definition/lib/INotion"; +import { SearchPageAndDatabase } from "../../enum/modals/common/SearchPageAndDatabaseComponent"; export class ExecuteViewSubmitHandler { private context: UIKitViewSubmitInteractionContext; @@ -72,6 +77,14 @@ export class ExecuteViewSubmitHandler { ); break; } + case NotionPageOrRecord.VIEW_ID: { + return this.handleCreationOfPageOrRecord( + room, + oAuth2Storage, + modalInteraction + ); + break; + } default: { } } @@ -190,4 +203,92 @@ export class ExecuteViewSubmitHandler { return this.context.getInteractionResponder().successResponse(); } + + private async handleCreationOfPageOrRecord( + room: IRoom, + oAuth2Storage: OAuth2Storage, + modalInteraction: ModalInteractionStorage + ): Promise { + const { user, view } = this.context.getInteractionData(); + const { state } = view; + + const tokenInfo = await oAuth2Storage.getCurrentWorkspace(user.id); + + if (!tokenInfo) { + await sendNotificationWithConnectBlock( + this.app, + user, + this.read, + this.modify, + room + ); + return this.context.getInteractionResponder().errorResponse(); + } + + // handle missing properties later + + const Object: IPage | IDatabase = JSON.parse( + state?.[SearchPageAndDatabase.BLOCK_ID]?.[ + SearchPageAndDatabase.ACTION_ID + ] + ); + + const { parent } = Object; + + const parentType: string = parent.type; + + if (parentType.includes(NotionObjectTypes.PAGE_ID)) { + return this.handleCreationOfPage( + tokenInfo, + room, + oAuth2Storage, + modalInteraction, + Object as IPage + ); + } + + return this.handleCreationOfRecord(); + } + + private async handleCreationOfPage( + tokenInfo: ITokenInfo, + room: IRoom, + oAuth2Storage: OAuth2Storage, + modalInteraction: ModalInteractionStorage, + page: IPage + ): Promise { + const { NotionSdk } = this.app.getUtils(); + const { view, user } = this.context.getInteractionData(); + const { state } = view; + const { access_token, workspace_name } = tokenInfo; + + const title: string = + state?.[NotionPageOrRecord.TITLE_BLOCK]?.[ + NotionPageOrRecord.TITLE_ACTION + ]; + + const createdPage = await NotionSdk.createPage(access_token, page, { + title, + }); + + let message: string; + + if (createdPage instanceof Error) { + this.app.getLogger().error(createdPage.message); + message = `🚫 Something went wrong while creating page in **${workspace_name}**.`; + } else { + const { name, link, title } = createdPage; + message = `✨ Your Page [**${title}**](${link}) is created successfully as a subpage in **${name}**.`; + } + + await sendNotification(this.read, this.modify, user, room, { + message, + }); + + return this.context.getInteractionResponder().successResponse(); + } + + private async handleCreationOfRecord(): Promise { + return this.context.getInteractionResponder().successResponse(); + } }