Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat]: Update DB Records #70

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NotionApp.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//Started working on Update DB Record Feature
import {
IAppAccessors,
IAppInstallationContext,
Expand Down
4 changes: 3 additions & 1 deletion enum/CommandParam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ export enum CommandParam {
WORKSPACE = "workspace",
WS = "ws",
SHARE = "share",
VIEW = "view"
VIEW = "view",
UPDATE = "update"
}

export enum SubCommandParam {
DATABASE = "db",
RECORD = "record"
}
3 changes: 2 additions & 1 deletion enum/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export enum Messages {
• use \`/notion create\` to create page or record
• use \`/notion create db\` to create database
• use \`/notion workspace\` to change workspace
• use \`/notion share\` to share pages
• use \`/notion share\` to share pages
• use \`/notion update record\` to update database record
`,
HELPER_TEXT = `Need some help with \`/notion\`?`,
}
Expand Down
17 changes: 17 additions & 0 deletions enum/modals/NotionUpdateRecord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export enum NotionUpdateRecord {
VIEW_ID = "notion-update-record-view-id",
TITLE = "Update Database Record",
SEARCH_DB_ACTION_ID = "notion-update-record-search-database-component-action-id",
TITLE_BLOCK = "title-database-block-id",
TITLE_ACTION = "title-database-action-id",
PROPERTY_SELECTED_BLOCK_ELEMENT = "property-selected-element-update-record-db-block-id",
ADD_PROPERTY_ACTION = "add-property-notion-update-record-action-id",
ADD_PROPERTY_BLOCK = "add-property-notion-update-record-block-id",
ADD_PROPERTY_BUTTON_TEXT = "Add Property",
UPDATE_RECORD = "Update",
UPDATE_RECORD_ACTION = "update-record-action-id",
UPDATE_RECORD_BLOCK = "update-record-block-id",
CLOSE = "Close",
CLOSE_ACTION = "close-update-record-action-id",
CLOSE_BLOCK = "close-update-record-block-id",
}
6 changes: 6 additions & 0 deletions enum/modals/common/SearchRecordComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum SearchRecordComponent {
PLACEHOLDER = "Select Record",
BLOCK_ID = "search-record-component-block-id",
ACTION_ID = "search-record-component-action-id",
LABEL = "Record Title *",
}
18 changes: 17 additions & 1 deletion src/commands/CommandUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export class CommandUtility implements ICommandUtility {
await handler.shareNotionPage();
break;
}
case CommandParam.VIEW : {
case CommandParam.VIEW: {
await handler.viewNotionTable();
break;
}
Expand Down Expand Up @@ -156,6 +156,22 @@ export class CommandUtility implements ICommandUtility {
);
break;
}

case CommandParam.UPDATE: {
if (subparam.toLowerCase() === SubCommandParam.RECORD) {
await handler.updateDatabaseRecord();
return;
}

await sendHelperNotification(
this.read,
this.modify,
this.sender,
this.room
);

break;
}
default: {
await sendHelperNotification(
this.read,
Expand Down
84 changes: 84 additions & 0 deletions src/handlers/ExecuteBlockActionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ import { getPropertiesIdsObject } from "../helper/getPropertiesIdsObject";
import { SharePage } from "../../enum/modals/SharePage";
import { NotionTable } from "../../enum/modals/NotionTable";
import { SendMessagePage } from "../../enum/modals/SendMessagePage";
import { NotionUpdateRecord } from "../../enum/modals/NotionUpdateRecord";
import { updateRecordModal } from "../modals/updateRecordModal";

export class ExecuteBlockActionHandler {
private context: UIKitBlockInteractionContext;
Expand Down Expand Up @@ -167,6 +169,14 @@ export class ExecuteBlockActionHandler {
);
break;
}

case NotionUpdateRecord.SEARCH_DB_ACTION_ID:{
return this.handleSearchDbUpdateRecord(
modalInteraction,
oAuth2Storage,
roomInteractionStorage
)
}
case SendMessagePage.ACTION_ID:
case SharePage.ACTION_ID: {
return this.handleSelectPageAction();
Expand Down Expand Up @@ -1043,4 +1053,78 @@ export class ExecuteBlockActionHandler {
errors: {},
});
}

private async handleSearchDbUpdateRecord(
modalInteraction: ModalInteractionStorage,
oAuth2Storage: OAuth2Storage,
roomInteractionStorage: RoomInteractionStorage
): Promise<IUIKitResponse> {
const { value, user } = this.context.getInteractionData();

const tokenInfo = await oAuth2Storage.getCurrentWorkspace(user.id);
const roomId = await roomInteractionStorage.getInteractionRoomId();
const room = (await this.read.getRoomReader().getById(roomId)) as IRoom;

if (!tokenInfo) {
await sendNotificationWithConnectBlock(
this.app,
user,
this.read,
this.modify,
room
);

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

if (!value) {
return this.context.getInteractionResponder().errorResponse();
}

let Object: IDatabase = JSON.parse(value);

const database = Object as IDatabase;

const databaseId = database.parent.database_id;
const { NotionSdk } = this.app.getUtils();
const { access_token } = tokenInfo;

const properties = await NotionSdk.retrieveDatabase(
access_token,
databaseId
);

if (properties instanceof Error) {
this.app.getLogger().error(properties.message);
return this.context.getInteractionResponder().errorResponse();
}

await modalInteraction.storeInputElementState(
NotionUpdateRecord.SEARCH_DB_ACTION_ID,
properties
);

await this.storeAllElements(properties, tokenInfo, modalInteraction);

const modal = await updateRecordModal(
this.app,
user,
this.read,
this.persistence,
this.modify,
room,
modalInteraction,
tokenInfo,
database
);

if (modal instanceof Error) {
this.app.getLogger().error(modal.message);
return this.context.getInteractionResponder().errorResponse();
}

return this.context
.getInteractionResponder()
.updateModalViewResponse(modal);
}
}
35 changes: 33 additions & 2 deletions src/handlers/ExecuteViewSubmitHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import { SendMessagePage } from "../../enum/modals/SendMessagePage";
import { NotionTable } from "../../enum/modals/NotionTable";
import { SearchDatabaseComponent } from "../../enum/modals/common/SearchDatabaseComponent";
import { table } from "table";
import { NotionUpdateRecord } from "../../enum/modals/NotionUpdateRecord";

export class ExecuteViewSubmitHandler {
private context: UIKitViewSubmitInteractionContext;
Expand Down Expand Up @@ -139,6 +140,14 @@ export class ExecuteViewSubmitHandler {
);
break;
}
case NotionUpdateRecord.VIEW_ID: {
return this.handleUpdateRecord(
room,
oAuth2Storage,
modalInteraction
);
break;
}
default: {
}
}
Expand Down Expand Up @@ -341,7 +350,6 @@ export class ExecuteViewSubmitHandler {
const parentType: string = parent.type;

if (parentType.includes(NotionObjectTypes.PAGE_ID)) {

return this.handleCreationOfPage(
tokenInfo,
room,
Expand All @@ -350,7 +358,7 @@ export class ExecuteViewSubmitHandler {
Objects as IPage
);
}

return this.handleCreationOfRecord(
tokenInfo,
room,
Expand Down Expand Up @@ -998,4 +1006,27 @@ export class ExecuteViewSubmitHandler {

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

private async handleUpdateRecord(
room: IRoom,
oAuth2Storage: OAuth2Storage,
modalInteraction: ModalInteractionStorage
): Promise<IUIKitResponse> {
const { view, user } = this.context.getInteractionData();

const tokenInfo = await oAuth2Storage.getCurrentWorkspace(user.id);

if (!tokenInfo) {
await sendNotificationWithConnectBlock(
this.app,
user,
this.read,
this.modify,
room
);
return this.context.getInteractionResponder().errorResponse();
}

return this.context.getInteractionResponder().successResponse();
}
}
62 changes: 62 additions & 0 deletions src/handlers/Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { createCommentContextualBar } from "../modals/createCommentContextualBar
import { NotionPageOrRecord } from "../../enum/modals/NotionPageOrRecord";
import { createPageOrRecordModal } from "../modals/createPageOrRecordModal";
import { changeWorkspaceModal } from "../modals/changeWorkspaceModal";
import { updateRecordModal } from "../modals/updateRecordModal";
import { NotionWorkspace } from "../../enum/modals/NotionWorkspace";
import { SearchPageAndDatabase } from "../../enum/modals/common/SearchPageAndDatabaseComponent";
import { NotionOwnerType } from "../../enum/Notion";
Expand All @@ -33,6 +34,7 @@ import { SendMessagePage } from "../../enum/modals/SendMessagePage";
import { sendMessagePageModal } from "../modals/sendMessagePageModal";
import { NotionTable } from "../../enum/modals/NotionTable";
import { viewNotionTableModal } from "../modals/viewNotionTableModal";
import { NotionUpdateRecord } from "../../enum/modals/NotionUpdateRecord";

export class Handler implements IHandler {
public app: NotionApp;
Expand Down Expand Up @@ -515,4 +517,64 @@ export class Handler implements IHandler {
.openSurfaceView(modal, { triggerId }, this.sender);
}
}


public async updateDatabaseRecord(
): Promise<void> {
const userId = this.sender.id;
const roomId = this.room.id;
const tokenInfo = await this.oAuth2Storage.getCurrentWorkspace(userId);

if (!tokenInfo) {
await sendNotificationWithConnectBlock(
this.app,
this.sender,
this.read,
this.modify,
this.room
);
return;
}

const persistenceRead = this.read.getPersistenceReader();
const modalInteraction = new ModalInteractionStorage(
this.persis,
persistenceRead,
userId,
NotionUpdateRecord.VIEW_ID
);

const { workspace_id, access_token } = tokenInfo;

await Promise.all([
this.roomInteractionStorage.storeInteractionRoomId(roomId),
modalInteraction.clearAllInteractionActionId(),
]);

const modal = await updateRecordModal(
this.app,
this.sender,
this.read,
this.persis,
this.modify,
this.room,
modalInteraction,
tokenInfo
);

if (modal instanceof Error) {
this.app.getLogger().error(modal.message);
return;
}

const triggerId = this.triggerId;

if (triggerId) {
await this.modify
.getUiController()
.openSurfaceView(modal, { triggerId }, this.sender);
}

return;
}
}
Loading