diff --git a/packages/api/src/resolvers/integrations/index.ts b/packages/api/src/resolvers/integrations/index.ts index 502b06880..d976fa6a5 100644 --- a/packages/api/src/resolvers/integrations/index.ts +++ b/packages/api/src/resolvers/integrations/index.ts @@ -53,7 +53,7 @@ export const setIntegrationResolver = authorized< SetIntegrationSuccess, SetIntegrationError, MutationSetIntegrationArgs ->(async (_, { input }, { uid }) => { +>(async (_, { input }, { uid, log }) => { const integrationToSave: DeepPartial = { ...input, user: { id: uid }, @@ -104,11 +104,28 @@ export const setIntegrationResolver = authorized< if (settings.parentDatabaseId) { // update notion database properties const notion = new NotionClient(integration.token, integration) + try { - await notion.updateDatabase(settings.parentDatabaseId) + const database = await notion.findDatabase(settings.parentDatabaseId) + + try { + await notion.updateDatabase(database) + } catch (error) { + log.error('failed to update notion database', { + databaseId: settings.parentDatabaseId, + }) + + return { + errorCodes: [SetIntegrationErrorCode.BadRequest], + } + } } catch (error) { + log.error('notion database not found', { + databaseId: settings.parentDatabaseId, + }) + return { - errorCodes: [SetIntegrationErrorCode.BadRequest], + errorCodes: [SetIntegrationErrorCode.NotFound], } } } diff --git a/packages/api/src/services/integrations/notion.ts b/packages/api/src/services/integrations/notion.ts index 57165c437..3732986dd 100644 --- a/packages/api/src/services/integrations/notion.ts +++ b/packages/api/src/services/integrations/notion.ts @@ -1,4 +1,5 @@ import { Client } from '@notionhq/client' +import { GetDatabaseResponse } from '@notionhq/client/build/src/api-endpoints' import axios from 'axios' import { HighlightType } from '../../entity/highlight' import { Integration } from '../../entity/integration' @@ -377,14 +378,13 @@ export class NotionClient implements IntegrationClient { return true } - private findDatabase = async (databaseId: string) => { + findDatabase = async (databaseId: string) => { return this.client.databases.retrieve({ database_id: databaseId, }) } - updateDatabase = async (databaseId: string) => { - const database = await this.findDatabase(databaseId) + updateDatabase = async (database: GetDatabaseResponse) => { // find the title property and update it const titleProperty = Object.entries(database.properties).find( ([, property]) => property.type === 'title'