Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 19 additions & 24 deletions app/api/push/send/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ import { NextRequest, NextResponse } from 'next/server';
import { createClient } from '@/lib/supabase';
import { sendPushNotificationBatch, type PushNotificationPayload } from '@/lib/web-push';

interface SubscriptionRow {
endpoint: string;
p256dh_key: string;
auth_key: string;
user_id: string;
users: {
role?: string;
} | null;
}

/**
* POST /api/push/send
* Send push notifications to users
Expand All @@ -28,17 +38,11 @@ export async function POST(request: NextRequest) {

// Validate payload
if (!payload || typeof payload !== 'object') {
return NextResponse.json(
{ error: 'Payload is required' },
{ status: 400 }
);
return NextResponse.json({ error: 'Payload is required' }, { status: 400 });
}

if (!payload.title || !payload.body) {
return NextResponse.json(
{ error: 'Payload must include title and body' },
{ status: 400 }
);
return NextResponse.json({ error: 'Payload must include title and body' }, { status: 400 });
}

// Create Supabase client
Expand All @@ -51,10 +55,7 @@ export async function POST(request: NextRequest) {

if (userIds !== 'all') {
if (!Array.isArray(userIds) || userIds.length === 0) {
return NextResponse.json(
{ error: 'userIds must be an array or "all"' },
{ status: 400 }
);
return NextResponse.json({ error: 'userIds must be an array or "all"' }, { status: 400 });
}
query = query.in('user_id', userIds);
}
Expand All @@ -63,10 +64,7 @@ export async function POST(request: NextRequest) {

if (fetchError) {
console.error('Error fetching subscriptions:', fetchError);
return NextResponse.json(
{ error: 'Failed to fetch subscriptions' },
{ status: 500 }
);
return NextResponse.json({ error: 'Failed to fetch subscriptions' }, { status: 500 });
}

if (!subscriptions || subscriptions.length === 0) {
Expand All @@ -78,10 +76,10 @@ export async function POST(request: NextRequest) {
}

// Filter by roles if specified
let filteredSubscriptions = subscriptions;
let filteredSubscriptions: SubscriptionRow[] = subscriptions as SubscriptionRow[];
if (roles && Array.isArray(roles) && roles.length > 0) {
filteredSubscriptions = subscriptions.filter((sub) => {
const userRole = (sub.users as { role?: string })?.role;
filteredSubscriptions = filteredSubscriptions.filter((sub: SubscriptionRow) => {
const userRole = sub.users?.role;
return userRole && roles.includes(userRole);
});
}
Expand All @@ -95,7 +93,7 @@ export async function POST(request: NextRequest) {
}

// Convert to push subscription format
const pushSubscriptions = filteredSubscriptions.map((sub) => ({
const pushSubscriptions = filteredSubscriptions.map((sub: SubscriptionRow) => ({
endpoint: sub.endpoint,
keys: {
p256dh: sub.p256dh_key,
Expand Down Expand Up @@ -139,9 +137,6 @@ export async function POST(request: NextRequest) {
});
} catch (error) {
console.error('Error in send route:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
}
}
59 changes: 24 additions & 35 deletions app/api/push/subscribe/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@ export async function POST(request: NextRequest) {

// Validate input
if (!userId || typeof userId !== 'string') {
return NextResponse.json(
{ error: 'User ID is required' },
{ status: 400 }
);
return NextResponse.json({ error: 'User ID is required' }, { status: 400 });
}

if (!validatePushSubscription(subscription)) {
return NextResponse.json(
{ error: 'Invalid subscription object' },
{ status: 400 }
);
return NextResponse.json({ error: 'Invalid subscription object' }, { status: 400 });
}

// Create Supabase client
Expand All @@ -37,24 +31,23 @@ export async function POST(request: NextRequest) {
.eq('endpoint', subscription.endpoint)
.single();

if (existingSubscription) {
if (existingSubscription && 'id' in existingSubscription) {
// Update existing subscription
const updatePayload: Record<string, unknown> = {
p256dh_key: subscription.keys.p256dh,
auth_key: subscription.keys.auth,
user_agent: request.headers.get('user-agent'),
updated_at: new Date().toISOString(),
};

const { error: updateError } = await supabase
.from('push_subscriptions')
.update({
p256dh_key: subscription.keys.p256dh,
auth_key: subscription.keys.auth,
user_agent: request.headers.get('user-agent'),
updated_at: new Date().toISOString(),
})
.eq('id', existingSubscription.id);
.update(updatePayload as never)
.eq('id', (existingSubscription as Record<string, unknown>).id as string);

if (updateError) {
console.error('Error updating subscription:', updateError);
return NextResponse.json(
{ error: 'Failed to update subscription' },
{ status: 500 }
);
return NextResponse.json({ error: 'Failed to update subscription' }, { status: 500 });
}

return NextResponse.json({
Expand All @@ -64,22 +57,21 @@ export async function POST(request: NextRequest) {
}

// Insert new subscription
const insertPayload: Record<string, unknown> = {
user_id: userId,
endpoint: subscription.endpoint,
p256dh_key: subscription.keys.p256dh,
auth_key: subscription.keys.auth,
user_agent: request.headers.get('user-agent'),
};

const { error: insertError } = await supabase
.from('push_subscriptions')
.insert({
user_id: userId,
endpoint: subscription.endpoint,
p256dh_key: subscription.keys.p256dh,
auth_key: subscription.keys.auth,
user_agent: request.headers.get('user-agent'),
});
.insert(insertPayload as never);

if (insertError) {
console.error('Error inserting subscription:', insertError);
return NextResponse.json(
{ error: 'Failed to save subscription' },
{ status: 500 }
);
return NextResponse.json({ error: 'Failed to save subscription' }, { status: 500 });
}

return NextResponse.json({
Expand All @@ -88,9 +80,6 @@ export async function POST(request: NextRequest) {
});
} catch (error) {
console.error('Error in subscribe route:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
}
}
20 changes: 4 additions & 16 deletions app/api/push/unsubscribe/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,11 @@ export async function POST(request: NextRequest) {

// Validate input
if (!userId || typeof userId !== 'string') {
return NextResponse.json(
{ error: 'User ID is required' },
{ status: 400 }
);
return NextResponse.json({ error: 'User ID is required' }, { status: 400 });
}

if (!endpoint || typeof endpoint !== 'string') {
return NextResponse.json(
{ error: 'Endpoint is required' },
{ status: 400 }
);
return NextResponse.json({ error: 'Endpoint is required' }, { status: 400 });
}

// Create Supabase client
Expand All @@ -37,10 +31,7 @@ export async function POST(request: NextRequest) {

if (error) {
console.error('Error deleting subscription:', error);
return NextResponse.json(
{ error: 'Failed to delete subscription' },
{ status: 500 }
);
return NextResponse.json({ error: 'Failed to delete subscription' }, { status: 500 });
}

return NextResponse.json({
Expand All @@ -49,9 +40,6 @@ export async function POST(request: NextRequest) {
});
} catch (error) {
console.error('Error in unsubscribe route:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
}
}
5 changes: 1 addition & 4 deletions app/api/push/vapid/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ export async function GET() {
const vapidPublicKey = process.env.NEXT_PUBLIC_VAPID_PUBLIC_KEY;

if (!vapidPublicKey) {
return NextResponse.json(
{ error: 'VAPID public key not configured' },
{ status: 500 }
);
return NextResponse.json({ error: 'VAPID public key not configured' }, { status: 500 });
}

return NextResponse.json({ publicKey: vapidPublicKey });
Expand Down
2 changes: 1 addition & 1 deletion app/chaser/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export default function ChaserPage() {
{nearbyRunners.length === 0 ? (
<p className="text-sm text-gray-500">レーダー範囲内に逃走者なし</p>
) : (
nearbyRunners.map((runner) => (
nearbyRunners.map((runner: User) => (
<div
key={runner.id}
className="flex items-center justify-between rounded bg-gray-50 p-2"
Expand Down
15 changes: 8 additions & 7 deletions app/gamemaster/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ export default function GamemasterPage() {
);
}

const runners = allPlayers.filter((p) => p.role === 'runner');
const chasers = allPlayers.filter((p) => p.role === 'chaser');
const activePlayers = allPlayers.filter((p) => p.status === 'active');
const capturedPlayers = allPlayers.filter((p) => p.status === 'captured');
const runners = allPlayers.filter((p: User) => p.role === 'runner');
const chasers = allPlayers.filter((p: User) => p.role === 'chaser');
const activePlayers = allPlayers.filter((p: User) => p.status === 'active');
const capturedPlayers = allPlayers.filter((p: User) => p.status === 'captured');

const playerWithLocation = allPlayers.find((p) => p.location);
const playerWithLocation = allPlayers.find((p: User) => p.location);
const mapCenter: [number, number] = playerWithLocation?.location
? [playerWithLocation.location.lat, playerWithLocation.location.lng]
: [35.5522, 139.7797];
Expand All @@ -150,14 +150,15 @@ export default function GamemasterPage() {
<p className="font-semibold">🏃 逃走者</p>
<p>合計 {runners.length}人</p>
<p className="text-green-600">
逃走中 {runners.filter((r) => r.status === 'active').length}人
逃走中 {runners.filter((r: User) => r.status === 'active').length}人
</p>
</div>
<div className="rounded bg-red-50 p-2">
<p className="font-semibold">👹 鬼</p>
<p>合計 {chasers.length}人</p>
<p className="text-blue-600">
捕獲数 {chasers.reduce((sum, c) => sum + (c.captureCount || 0), 0)}人
捕獲数 {chasers.reduce((sum: number, c: User) => sum + (c.captureCount || 0), 0)}
</p>
</div>
</div>
Expand Down
Loading