Skip to content

Commit 2d34ffd

Browse files
authored
Merge pull request #347 from CodingCatDev/bug/notion-est
Bug/notion est
2 parents 9c74043 + 41d7fe7 commit 2d34ffd

4 files changed

Lines changed: 54 additions & 28 deletions

File tree

backend/firebase/functions/src/calendly/webhook.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import {
66
createPurrfectCompany,
77
createPurrfectPage,
88
queryPurrfectPageByCalendarId,
9-
patchPurrfectPage,
109
} from './../utilities/notion';
1110

1211
import { getEvent, listEventInvitees } from './../utilities/calendly';
12+
import { utcOffset } from '../utilities/date';
1313

1414
export const calendarPush = functions.https.onRequest(async (req, res) => {
1515
console.log('Headers', JSON.stringify(req.headers));
@@ -37,38 +37,40 @@ export const calendarPush = functions.https.onRequest(async (req, res) => {
3737

3838
try {
3939
const calendlyEvent = await getEvent(eventUuid);
40+
console.log('calendlyEvent', JSON.stringify(calendlyEvent));
4041
const calendlyInvitees = await listEventInvitees(eventUuid);
42+
console.log('calendlyInvitees', JSON.stringify(calendlyInvitees));
4143

4244
// Check if calendar event already created.
4345
console.log(
4446
'Searching for Pod with calendarid: ',
45-
webhookPayload.payload.event
47+
JSON.stringify(webhookPayload.payload.event)
4648
);
4749
const purrfectPageRes = await queryPurrfectPageByCalendarId(
4850
webhookPayload.payload.event
4951
);
5052
if (purrfectPageRes?.results.length > 0) {
51-
console.log('Pod found', JSON.stringify(purrfectPageRes));
52-
const purrfectPage = purrfectPageRes?.results?.[0];
53-
console.log(
54-
'Updating pod with time: ',
55-
calendlyEvent.resource.start_time
56-
);
57-
const purrfectPagePatchRes = await patchPurrfectPage({
58-
page_id: purrfectPage.id,
59-
properties: {
60-
'Recording Date': {
61-
date: {
62-
start: calendlyEvent.resource.start_time,
63-
end: null,
64-
time_zone: 'America/New_York',
65-
},
66-
},
67-
},
68-
});
53+
// console.log('Pod found', JSON.stringify(purrfectPageRes));
54+
// const purrfectPage = purrfectPageRes?.results?.[0];
55+
// console.log(
56+
// 'Updating pod with time: ',
57+
// JSON.stringify(calendlyEvent.resource.start_time)
58+
// );
59+
// const purrfectPagePatchRes = await patchPurrfectPage({
60+
// page_id: purrfectPage.id,
61+
// properties: {
62+
// 'Recording Date': {
63+
// date: {
64+
// start: utcOffset(calendlyEvent.resource.start_time),
65+
// end: null,
66+
// time_zone: 'America/New_York',
67+
// },
68+
// },
69+
// },
70+
// });
6971

70-
console.log('Patched', JSON.stringify(purrfectPagePatchRes));
71-
res.status(200).send('updated');
72+
// console.log('Patched', JSON.stringify(purrfectPagePatchRes));
73+
res.status(200).send('duplicate entry');
7274
}
7375

7476
// Check if Notion Company exists, if not create
@@ -120,7 +122,7 @@ export const calendarPush = functions.https.onRequest(async (req, res) => {
120122
const newPodcast = {
121123
guestIds: [guestId],
122124
companyIds,
123-
recordingDate: new Date(calendlyEvent.resource.start_time),
125+
recordingDate: utcOffset(calendlyEvent.resource.start_time),
124126
calendarid: webhookPayload.payload.event,
125127
};
126128
console.log('Creating Podcast with values: ', JSON.stringify(newPodcast));

backend/firebase/functions/src/utilities/calendly.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const call = async (path: string) => {
1717
throw response.status;
1818
}
1919
const json = await response.json();
20-
console.log('RESPONSE:', json);
20+
console.log('RESPONSE:', JSON.stringify(json));
2121
return json;
2222
};
2323

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export function utcOffset(isoString: string, timeZone = 'America/new_york') {
2+
console.log('conversion:input', isoString);
3+
const isoStringDate = new Date(isoString);
4+
const offset = getOffset(timeZone, isoStringDate);
5+
console.log('conversion:offsethours', offset / 60 / 60 / 1000);
6+
const output = new Date(Date.parse(isoStringDate.toISOString()) + offset)
7+
.toISOString()
8+
.replace('Z', '');
9+
console.log('conversion:output', output);
10+
return output;
11+
}
12+
13+
const getOffset = (timeZone = 'UTC', date = new Date()) => {
14+
const utcDate = new Date(date.toLocaleString('en-US', { timeZone: 'UTC' }));
15+
const tzDate = new Date(date.toLocaleString('en-US', { timeZone }));
16+
return tzDate.getTime() - utcDate.getTime();
17+
};
18+
19+
const calendlyDate = '2022-01-26T14:00:00.000000Z';
20+
console.log('hardcoded:calendlyDate', calendlyDate);
21+
console.log('hardcoded:fromFunc', utcOffset(calendlyDate));
22+
console.log('hardcoded:shouldMatch', '2022-01-26T09:00:00.000');

backend/firebase/functions/src/utilities/notion.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ export const createPurrfectPage = ({
7474
companyIds,
7575
}: {
7676
calendarid: string;
77-
recordingDate: Date;
77+
recordingDate: string;
7878
guestIds?: string[];
7979
companyIds?: string[];
8080
}) => {
81-
return notionClient.pages.create({
81+
const notionCreatePayload = {
8282
parent: {
8383
database_id: notionPurrfectDatabaseId,
8484
},
@@ -105,7 +105,7 @@ export const createPurrfectPage = ({
105105
id: 'br,*',
106106
type: 'date',
107107
date: {
108-
start: recordingDate.toISOString(),
108+
start: recordingDate,
109109
end: null,
110110
time_zone: 'America/New_York',
111111
},
@@ -138,7 +138,9 @@ export const createPurrfectPage = ({
138138
],
139139
},
140140
},
141-
} as any);
141+
} as any;
142+
console.log('notionCreatePayload', JSON.stringify(notionCreatePayload));
143+
return notionClient.pages.create(notionCreatePayload);
142144
};
143145

144146
export const patchPurrfectPage = (pageParams: UpdatePageParameters) => {

0 commit comments

Comments
 (0)