Skip to content

Commit 1acbd5e

Browse files
webhook fix
1 parent 0522187 commit 1acbd5e

File tree

4 files changed

+59
-15
lines changed

4 files changed

+59
-15
lines changed

src/app/api/strava/[slug]/route.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,18 @@ export async function POST(
5252
}
5353

5454
if (data.aspect_type === 'create' || data.aspect_type === 'update') {
55-
const { activity, photos } = await handleWebhookActivity({
55+
const {
56+
activities: [activity],
57+
photos,
58+
} = await handleWebhookActivity({
5659
activityId: data.object_id,
5760
athleteId: data.owner_id,
5861
});
5962

63+
if (!activity) {
64+
throw new Error('No activity returned from Strava');
65+
}
66+
6067
return new Response(
6168
`${data.aspect_type === 'create' ? 'Created' : 'Updated'} activity ${
6269
activity.id

src/components/sidebar/user.tsx

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import { ChevronsUpDown, LogOut, CircleArrowLeft, Loader2 } from 'lucide-react';
3+
import { ChevronsUpDown, LogOut, CircleArrowLeft, Loader2, CircleCheck } from 'lucide-react';
44

55
import Link from 'next/link';
66
import { signIn, signOut } from 'next-auth/react';
@@ -18,13 +18,12 @@ import {
1818
} from '~/components/ui/dropdown-menu';
1919

2020
import { Avatar, AvatarFallback, AvatarImage } from '~/components/ui/avatar';
21-
import { useToast } from '~/hooks/use-toast';
2221

2322
import * as React from 'react';
2423
import Image from 'next/image';
2524
import { cn } from '~/lib/utils';
26-
import { useState } from 'react';
2725
import { User2 } from 'lucide-react';
26+
import { manageWebhook } from '~/server/strava/actions';
2827

2928
export function UserSettings() {
3029
const { user, loading, account, loadFromStrava } = useShallowStore(
@@ -38,6 +37,9 @@ export function UserSettings() {
3837
},
3938
);
4039

40+
const checkWebhook = async () => {
41+
manageWebhook
42+
4143
const handleLoadActivities = async () => {
4244
try {
4345
console.log('Starting load activities, current loading state:', loading);
@@ -118,6 +120,12 @@ export function UserSettings() {
118120
<CircleArrowLeft />
119121
{loading ? 'Loading...' : 'Get Activities'}
120122
</DropdownMenuItem>
123+
<DropdownMenuItem onClick={()=>{
124+
checkWebhook();
125+
}}>
126+
<CircleCheck />
127+
Check Webhook
128+
</DropdownMenuItem>
121129
<DropdownMenuSeparator />
122130
<DropdownMenuItem onClick={() => signOut()}>
123131
<LogOut />

src/server/db/actions.ts

+32-10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ import type { AdapterAccount } from 'next-auth/adapters';
1010
// Use the AdapterAccount type from next-auth
1111
export type Account = AdapterAccount;
1212

13+
export const getUserByStravaId = async (stravaId: string) => {
14+
const account = await db.query.accounts.findFirst({
15+
where: (accounts, { eq }) => eq(accounts.providerAccountId, stravaId),
16+
});
17+
if (!account) throw new Error('Account not found');
18+
19+
const user = await db.query.users.findFirst({
20+
where: (users, { eq }) => eq(users.id, account.userId),
21+
});
22+
if (!user) throw new Error('User not found');
23+
24+
return user;
25+
};
26+
1327
export const getUser = async (id?: string) => {
1428
if (!id) {
1529
const session = await auth();
@@ -32,16 +46,24 @@ export const getAccount = async ({
3246
userId?: string;
3347
forceRefresh?: boolean;
3448
}) => {
35-
const resolvedUserId = userId ?? (await getUser()).id;
36-
37-
let account = providerAccountId
38-
? await db.query.accounts.findFirst({
39-
where: (accounts, { eq }) =>
40-
eq(accounts.providerAccountId, providerAccountId),
41-
})
42-
: await db.query.accounts.findFirst({
43-
where: (accounts, { eq }) => eq(accounts.userId, resolvedUserId),
44-
});
49+
let account;
50+
51+
if (providerAccountId) {
52+
account = await db.query.accounts.findFirst({
53+
where: (accounts, { eq }) =>
54+
eq(accounts.providerAccountId, providerAccountId),
55+
});
56+
} else if (userId) {
57+
account = await db.query.accounts.findFirst({
58+
where: (accounts, { eq }) => eq(accounts.userId, userId),
59+
});
60+
} else {
61+
// Only try to resolve through session if no IDs provided
62+
const resolvedUserId = await getUser().then((user) => user.id);
63+
account = await db.query.accounts.findFirst({
64+
where: (accounts, { eq }) => eq(accounts.userId, resolvedUserId),
65+
});
66+
}
4567

4668
if (!account) throw new Error('Account not found');
4769

src/server/strava/actions.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ export async function manageWebhook(url: string) {
365365
try {
366366
// Get existing subscriptions
367367
const subscriptions = await client.getSubscriptions();
368+
console.log('Subscriptions:', subscriptions);
368369

369370
// Delete existing subscriptions with different URLs
370371
await Promise.all(
@@ -390,7 +391,13 @@ export async function handleWebhookActivity({
390391
activityId: number;
391392
athleteId: number;
392393
}) {
393-
const account = await getAccount({ providerAccountId: athleteId.toString() });
394+
console.log('Handling webhook activity:', { activityId, athleteId });
395+
396+
// Get account directly using Strava athlete ID
397+
const account = await getAccount({
398+
providerAccountId: athleteId.toString(),
399+
});
400+
394401
if (!account.access_token) {
395402
throw new Error('No Strava access token found');
396403
}

0 commit comments

Comments
 (0)