Skip to content

Commit f7c8f93

Browse files
committed
added command to update record
added help created update record modal added info fectch of selected db
1 parent 9f29a7a commit f7c8f93

8 files changed

Lines changed: 302 additions & 5 deletions

File tree

enum/CommandParam.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ export enum CommandParam {
77
WORKSPACE = "workspace",
88
WS = "ws",
99
SHARE = "share",
10-
VIEW = "view"
10+
VIEW = "view",
11+
UPDATE = "update"
1112
}
1213

1314
export enum SubCommandParam {
1415
DATABASE = "db",
16+
RECORD = "record"
1517
}

enum/messages.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export enum Messages {
55
• use \`/notion create\` to create page or record
66
• use \`/notion create db\` to create database
77
• use \`/notion workspace\` to change workspace
8-
• use \`/notion share\` to share pages
8+
• use \`/notion share\` to share pages
9+
• use \`/notion update record\` to update database record
910
`,
1011
HELPER_TEXT = `Need some help with \`/notion\`?`,
1112
}

enum/modals/NotionUpdateRecord.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export enum NotionUpdateRecord {
2+
VIEW_ID = "notion-update-record-view-id",
3+
TITLE = "Update Database Record",
4+
SEARCH_DB_ACTION_ID = "notion-update-record-search-database-component-action-id",
5+
TITLE_BLOCK = "title-database-block-id",
6+
TITLE_ACTION = "title-database-action-id",
7+
PROPERTY_SELECTED_BLOCK_ELEMENT = "property-selected-element-update-record-db-block-id",
8+
ADD_PROPERTY_ACTION = "add-property-notion-update-record-action-id",
9+
ADD_PROPERTY_BLOCK = "add-property-notion-update-record-block-id",
10+
ADD_PROPERTY_BUTTON_TEXT = "Add Property",
11+
UPDATE_RECORD = "Update",
12+
UPDATE_RECORD_ACTION = "update-record-action-id",
13+
UPDATE_RECORD_BLOCK = "update-record-block-id",
14+
CLOSE = "Close",
15+
CLOSE_ACTION = "close-update-record-action-id",
16+
CLOSE_BLOCK = "close-update-record-block-id",
17+
}

src/commands/CommandUtility.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export class CommandUtility implements ICommandUtility {
123123
await handler.shareNotionPage();
124124
break;
125125
}
126-
case CommandParam.VIEW : {
126+
case CommandParam.VIEW: {
127127
await handler.viewNotionTable();
128128
break;
129129
}
@@ -156,6 +156,22 @@ export class CommandUtility implements ICommandUtility {
156156
);
157157
break;
158158
}
159+
160+
case CommandParam.UPDATE: {
161+
if (subparam.toLowerCase() === SubCommandParam.RECORD) {
162+
await handler.updateDatabaseRecord();
163+
return;
164+
}
165+
166+
await sendHelperNotification(
167+
this.read,
168+
this.modify,
169+
this.sender,
170+
this.room
171+
);
172+
173+
break;
174+
}
159175
default: {
160176
await sendHelperNotification(
161177
this.read,

src/handlers/ExecuteBlockActionHandler.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ import { getPropertiesIdsObject } from "../helper/getPropertiesIdsObject";
5151
import { SharePage } from "../../enum/modals/SharePage";
5252
import { NotionTable } from "../../enum/modals/NotionTable";
5353
import { SendMessagePage } from "../../enum/modals/SendMessagePage";
54+
import { NotionUpdateRecord } from "../../enum/modals/NotionUpdateRecord";
55+
import { updateRecordModal } from "../modals/updateRecordModal";
5456

5557
export class ExecuteBlockActionHandler {
5658
private context: UIKitBlockInteractionContext;
@@ -167,6 +169,14 @@ export class ExecuteBlockActionHandler {
167169
);
168170
break;
169171
}
172+
173+
case NotionUpdateRecord.SEARCH_DB_ACTION_ID:{
174+
return this.handleSearchDbUpdateRecord(
175+
modalInteraction,
176+
oAuth2Storage,
177+
roomInteractionStorage
178+
)
179+
}
170180
case SendMessagePage.ACTION_ID:
171181
case SharePage.ACTION_ID: {
172182
return this.handleSelectPageAction();
@@ -1043,4 +1053,78 @@ export class ExecuteBlockActionHandler {
10431053
errors: {},
10441054
});
10451055
}
1056+
1057+
private async handleSearchDbUpdateRecord(
1058+
modalInteraction: ModalInteractionStorage,
1059+
oAuth2Storage: OAuth2Storage,
1060+
roomInteractionStorage: RoomInteractionStorage
1061+
): Promise<IUIKitResponse> {
1062+
const { value, user } = this.context.getInteractionData();
1063+
1064+
const tokenInfo = await oAuth2Storage.getCurrentWorkspace(user.id);
1065+
const roomId = await roomInteractionStorage.getInteractionRoomId();
1066+
const room = (await this.read.getRoomReader().getById(roomId)) as IRoom;
1067+
1068+
if (!tokenInfo) {
1069+
await sendNotificationWithConnectBlock(
1070+
this.app,
1071+
user,
1072+
this.read,
1073+
this.modify,
1074+
room
1075+
);
1076+
1077+
return this.context.getInteractionResponder().errorResponse();
1078+
}
1079+
1080+
if (!value) {
1081+
return this.context.getInteractionResponder().errorResponse();
1082+
}
1083+
1084+
let Object: IDatabase = JSON.parse(value);
1085+
1086+
const database = Object as IDatabase;
1087+
1088+
const databaseId = database.parent.database_id;
1089+
const { NotionSdk } = this.app.getUtils();
1090+
const { access_token } = tokenInfo;
1091+
1092+
const properties = await NotionSdk.retrieveDatabase(
1093+
access_token,
1094+
databaseId
1095+
);
1096+
1097+
if (properties instanceof Error) {
1098+
this.app.getLogger().error(properties.message);
1099+
return this.context.getInteractionResponder().errorResponse();
1100+
}
1101+
1102+
await modalInteraction.storeInputElementState(
1103+
NotionUpdateRecord.SEARCH_DB_ACTION_ID,
1104+
properties
1105+
);
1106+
1107+
await this.storeAllElements(properties, tokenInfo, modalInteraction);
1108+
1109+
const modal = await updateRecordModal(
1110+
this.app,
1111+
user,
1112+
this.read,
1113+
this.persistence,
1114+
this.modify,
1115+
room,
1116+
modalInteraction,
1117+
tokenInfo,
1118+
database
1119+
);
1120+
1121+
if (modal instanceof Error) {
1122+
this.app.getLogger().error(modal.message);
1123+
return this.context.getInteractionResponder().errorResponse();
1124+
}
1125+
1126+
return this.context
1127+
.getInteractionResponder()
1128+
.updateModalViewResponse(modal);
1129+
}
10461130
}

src/handlers/ExecuteViewSubmitHandler.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import { SendMessagePage } from "../../enum/modals/SendMessagePage";
5252
import { NotionTable } from "../../enum/modals/NotionTable";
5353
import { SearchDatabaseComponent } from "../../enum/modals/common/SearchDatabaseComponent";
5454
import { table } from "table";
55+
import { NotionUpdateRecord } from "../../enum/modals/NotionUpdateRecord";
5556

5657
export class ExecuteViewSubmitHandler {
5758
private context: UIKitViewSubmitInteractionContext;
@@ -139,6 +140,14 @@ export class ExecuteViewSubmitHandler {
139140
);
140141
break;
141142
}
143+
case NotionUpdateRecord.VIEW_ID: {
144+
return this.handleUpdateRecord(
145+
room,
146+
oAuth2Storage,
147+
modalInteraction
148+
);
149+
break;
150+
}
142151
default: {
143152
}
144153
}
@@ -341,7 +350,6 @@ export class ExecuteViewSubmitHandler {
341350
const parentType: string = parent.type;
342351

343352
if (parentType.includes(NotionObjectTypes.PAGE_ID)) {
344-
345353
return this.handleCreationOfPage(
346354
tokenInfo,
347355
room,
@@ -350,7 +358,7 @@ export class ExecuteViewSubmitHandler {
350358
Objects as IPage
351359
);
352360
}
353-
361+
354362
return this.handleCreationOfRecord(
355363
tokenInfo,
356364
room,
@@ -998,4 +1006,27 @@ export class ExecuteViewSubmitHandler {
9981006

9991007
return this.context.getInteractionResponder().successResponse();
10001008
}
1009+
1010+
private async handleUpdateRecord(
1011+
room: IRoom,
1012+
oAuth2Storage: OAuth2Storage,
1013+
modalInteraction: ModalInteractionStorage
1014+
): Promise<IUIKitResponse> {
1015+
const { view, user } = this.context.getInteractionData();
1016+
1017+
const tokenInfo = await oAuth2Storage.getCurrentWorkspace(user.id);
1018+
1019+
if (!tokenInfo) {
1020+
await sendNotificationWithConnectBlock(
1021+
this.app,
1022+
user,
1023+
this.read,
1024+
this.modify,
1025+
room
1026+
);
1027+
return this.context.getInteractionResponder().errorResponse();
1028+
}
1029+
1030+
return this.context.getInteractionResponder().successResponse();
1031+
}
10011032
}

src/handlers/Handler.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { createCommentContextualBar } from "../modals/createCommentContextualBar
2020
import { NotionPageOrRecord } from "../../enum/modals/NotionPageOrRecord";
2121
import { createPageOrRecordModal } from "../modals/createPageOrRecordModal";
2222
import { changeWorkspaceModal } from "../modals/changeWorkspaceModal";
23+
import { updateRecordModal } from "../modals/updateRecordModal";
2324
import { NotionWorkspace } from "../../enum/modals/NotionWorkspace";
2425
import { SearchPageAndDatabase } from "../../enum/modals/common/SearchPageAndDatabaseComponent";
2526
import { NotionOwnerType } from "../../enum/Notion";
@@ -33,6 +34,7 @@ import { SendMessagePage } from "../../enum/modals/SendMessagePage";
3334
import { sendMessagePageModal } from "../modals/sendMessagePageModal";
3435
import { NotionTable } from "../../enum/modals/NotionTable";
3536
import { viewNotionTableModal } from "../modals/viewNotionTableModal";
37+
import { NotionUpdateRecord } from "../../enum/modals/NotionUpdateRecord";
3638

3739
export class Handler implements IHandler {
3840
public app: NotionApp;
@@ -515,4 +517,64 @@ export class Handler implements IHandler {
515517
.openSurfaceView(modal, { triggerId }, this.sender);
516518
}
517519
}
520+
521+
522+
public async updateDatabaseRecord(
523+
): Promise<void> {
524+
const userId = this.sender.id;
525+
const roomId = this.room.id;
526+
const tokenInfo = await this.oAuth2Storage.getCurrentWorkspace(userId);
527+
528+
if (!tokenInfo) {
529+
await sendNotificationWithConnectBlock(
530+
this.app,
531+
this.sender,
532+
this.read,
533+
this.modify,
534+
this.room
535+
);
536+
return;
537+
}
538+
539+
const persistenceRead = this.read.getPersistenceReader();
540+
const modalInteraction = new ModalInteractionStorage(
541+
this.persis,
542+
persistenceRead,
543+
userId,
544+
NotionUpdateRecord.VIEW_ID
545+
);
546+
547+
const { workspace_id, access_token } = tokenInfo;
548+
549+
await Promise.all([
550+
this.roomInteractionStorage.storeInteractionRoomId(roomId),
551+
modalInteraction.clearAllInteractionActionId(),
552+
]);
553+
554+
const modal = await updateRecordModal(
555+
this.app,
556+
this.sender,
557+
this.read,
558+
this.persis,
559+
this.modify,
560+
this.room,
561+
modalInteraction,
562+
tokenInfo
563+
);
564+
565+
if (modal instanceof Error) {
566+
this.app.getLogger().error(modal.message);
567+
return;
568+
}
569+
570+
const triggerId = this.triggerId;
571+
572+
if (triggerId) {
573+
await this.modify
574+
.getUiController()
575+
.openSurfaceView(modal, { triggerId }, this.sender);
576+
}
577+
578+
return;
579+
}
518580
}

0 commit comments

Comments
 (0)