From 24caac2ed5feb11db244f1a52f7ad73bb1800e72 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Wed, 3 Aug 2022 18:50:47 +0800 Subject: [PATCH] Update integration updatedAt after syncing --- packages/api/src/services/highlights.ts | 4 +++ packages/api/src/services/integrations.ts | 33 +++++++++++++++-------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/packages/api/src/services/highlights.ts b/packages/api/src/services/highlights.ts index 62a514fc4..7addcbb7a 100644 --- a/packages/api/src/services/highlights.ts +++ b/packages/api/src/services/highlights.ts @@ -1,7 +1,11 @@ import { diff_match_patch } from 'diff-match-patch' +import { homePageURL } from '../env' export const getHighlightLocation = (patch: string): number | undefined => { const dmp = new diff_match_patch() const patches = dmp.patch_fromText(patch) return patches[0].start1 || undefined } + +export const getHighlightUrl = (slug: string, highlightId: string): string => + `${homePageURL()}/me/${slug}#${highlightId}` diff --git a/packages/api/src/services/integrations.ts b/packages/api/src/services/integrations.ts index 34d45a25f..58641bf4a 100644 --- a/packages/api/src/services/integrations.ts +++ b/packages/api/src/services/integrations.ts @@ -1,11 +1,13 @@ import { IntegrationType } from '../generated/graphql' -import { env, homePageURL } from '../env' +import { env } from '../env' import axios from 'axios' import { wait } from '../utils/helpers' import { Page } from '../elastic/types' -import { getHighlightLocation } from './highlights' +import { getHighlightLocation, getHighlightUrl } from './highlights' +import { Integration } from '../entity/integration' +import { getRepository } from '../entity/utils' -export interface ReadwiseHighlight { +interface ReadwiseHighlight { // The highlight text, (technically the only field required in a highlight object) text: string // The title of the page the highlight is on @@ -61,9 +63,6 @@ const validateReadwiseToken = async (token: string): Promise => { } } -const highlightUrl = (slug: string, highlightId: string): string => - `${homePageURL()}/me/${slug}#${highlightId}` - const pageToReadwiseHighlight = (page: Page): ReadwiseHighlight[] => { if (!page.highlights) return [] return page.highlights.map((highlight) => { @@ -72,7 +71,7 @@ const pageToReadwiseHighlight = (page: Page): ReadwiseHighlight[] => { text: highlight.quote, title: page.title, author: page.author, - highlight_url: highlightUrl(page.slug, highlight.id), + highlight_url: getHighlightUrl(page.slug, highlight.id), highlighted_at: highlight.createdAt.toISOString(), category: 'articles', image_url: page.image, @@ -86,16 +85,28 @@ const pageToReadwiseHighlight = (page: Page): ReadwiseHighlight[] => { } export const syncWithIntegration = async ( - token: string, - type: IntegrationType, + integration: Integration, pages: Page[] ): Promise => { - switch (type) { + let result = false + switch (integration.type) { case IntegrationType.Readwise: - return syncWithReadwise(token, pages.flatMap(pageToReadwiseHighlight)) + result = await syncWithReadwise( + integration.token, + pages.flatMap(pageToReadwiseHighlight) + ) + break default: return false } + // update integration updatedAt if successful + if (result) { + console.log('updating integration updatedAt') + await getRepository(Integration).update(integration.id, { + updatedAt: new Date(), + }) + } + return result } export const syncWithReadwise = async (