Skip to content

Commit

Permalink
feat: handledCreationOfPage without viewErrors
Browse files Browse the repository at this point in the history
description: handleCreationOfRecord is not implemented which will be done in addup PR but note that this doesnt contain viewErrors as well,see you soon
  • Loading branch information
Nabhag8848 committed Aug 20, 2023
1 parent 11991b5 commit 5899266
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions src/handlers/ExecuteViewSubmitHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -72,6 +77,14 @@ export class ExecuteViewSubmitHandler {
);
break;
}
case NotionPageOrRecord.VIEW_ID: {
return this.handleCreationOfPageOrRecord(
room,
oAuth2Storage,
modalInteraction
);
break;
}
default: {
}
}
Expand Down Expand Up @@ -190,4 +203,92 @@ export class ExecuteViewSubmitHandler {

return this.context.getInteractionResponder().successResponse();
}

private async handleCreationOfPageOrRecord(
room: IRoom,
oAuth2Storage: OAuth2Storage,
modalInteraction: ModalInteractionStorage
): Promise<IUIKitResponse> {
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<IUIKitResponse> {
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<IUIKitResponse> {
return this.context.getInteractionResponder().successResponse();
}
}

0 comments on commit 5899266

Please sign in to comment.