Skip to content

Commit

Permalink
The entire calendar app works end to end!
Browse files Browse the repository at this point in the history
  • Loading branch information
coco98 committed Oct 23, 2024
1 parent 10914af commit 637693d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/duckduckapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Connector,
Forbidden,
} from "@hasura/ndc-sdk-typescript";
import { JSONValue } from '@hasura/ndc-lambda-sdk';

import path from "node:path"
import { FunctionsSchema, getNdcSchema, printRelaxedTypesWarning } from "./lambda-sdk/schema";
Expand Down Expand Up @@ -276,4 +277,13 @@ export async function makeConnector(dda: duckduckapi): Promise<Connector<Configu
};

return Promise.resolve(connector);
}

export function getTokensFromHeader(headers: JSONValue, service: string): {access_token: string | null, refresh_token: string | null} {
const oauthServices = headers.value as any;
const decodedServices = Buffer.from(oauthServices['x-hasura-oauth-services'] as string, 'base64').toString('utf-8');
const serviceTokens = JSON.parse(decodedServices);
const access_token = serviceTokens[service]?.access_token;
const refresh_token = serviceTokens[service]?.refresh_token;
return {access_token, refresh_token};
}
14 changes: 11 additions & 3 deletions src/functions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { JSONValue } from '@hasura/ndc-lambda-sdk';
import { CalendarSyncManager } from './google-calendar-sync';
import { getTokensFromHeader } from './duckduckapi';

export let loaderStatus: string = 'stopped';

Expand All @@ -11,11 +12,18 @@ export let loaderStatus: string = 'stopped';
*/
export async function __dda_loader_init(headers: JSONValue): Promise<string> {

console.log(JSON.stringify(headers.value));
const oauthServices = headers.value as any;
const {access_token} = getTokensFromHeader(headers, 'google-calendar');

if (!access_token) {
console.log(headers.value);
loaderStatus = 'google-calendar key not found in oauth services. Login to google-calendar?';
return loaderStatus;
}

const syncManager = new CalendarSyncManager(
headers.value['google-calendar'].token,
1 // sync every 5 minutes
access_token,
1 // sync every minute
);
const result = await syncManager.test();

Expand Down

0 comments on commit 637693d

Please sign in to comment.