commit 94025e9e154ad5603e2e2a3942dc03d7589f42fa
parent b96f687633fb314a9fcc2e9f56426808689e9832
Author: Sunny <122193933+Sunny-JP@users.noreply.github.com>
Date: Sat, 24 Jan 2026 22:03:20 +0900
Merge pull request #19 from Sunny-JP/v4-sunny-dev
fix api
Diffstat:
1 file changed, 6 insertions(+), 38 deletions(-)
diff --git a/src/app/api/tap/route.ts b/src/app/api/tap/route.ts
@@ -12,7 +12,6 @@ export async function POST(request: Request) {
try {
const { tapTime, onesignalId, ticket1Time, ticket2Time } = await request.json();
const authHeader = request.headers.get('Authorization');
-
if (!authHeader) return NextResponse.json({ error: 'No token' }, { status: 401 });
const supabase = createClient(
@@ -24,16 +23,10 @@ export async function POST(request: Request) {
const { data: { user }, error: authError } = await supabase.auth.getUser();
if (authError || !user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
- console.log(`[Debug] Start process for user: ${user.id}`);
-
- // 通知ボタンまたはタップ時に ID があればクリーンアップを実行
- // 現在のデバイスID (onesignalId) を渡して保護する
if (onesignalId) {
- console.log(`[Cleanup] Triggered by ID: ${onesignalId}`);
await cleanupDevices(user.id, onesignalId);
}
- // DB更新処理
const { data: profile } = await supabase.from('profiles').select('tap_history').eq('id', user.id).single();
const newHistory = tapTime ? [...(profile?.tap_history || []), tapTime] : (profile?.tap_history || []);
@@ -48,13 +41,11 @@ export async function POST(request: Request) {
if (upsertError) throw new Error(upsertError.message);
- // 通知スケジュール
if (tapTime && shouldScheduleNotification(new Date(tapTime))) {
await scheduleNotification(user.id, tapTime);
}
return NextResponse.json({ success: true, history: newHistory });
-
} catch (error: any) {
console.error("[Error] Critical API Error:", error);
return NextResponse.json({ error: error.message }, { status: 500 });
@@ -62,55 +53,32 @@ export async function POST(request: Request) {
}
async function cleanupDevices(userId: string, currentId: string) {
- console.log(`[Cleanup] Fetching subscriptions for ${userId}...`);
try {
const res = await fetch(`https://onesignal.com/api/v1/apps/${APP_ID}/users/by/external_id/${userId}`, {
- headers: {
- "Authorization": `Basic ${API_KEY}`,
- "Content-Type": "application/json"
- }
+ headers: { "Authorization": `Basic ${API_KEY}` }
});
-
- if (!res.ok) {
- console.error(`[Cleanup] Fetch failed: ${res.status}`);
- return;
- }
+ if (!res.ok) return;
const data = await res.json();
- const subscriptions = data.subscriptions || (data.identity && data.identity.subscriptions) || [];
+ const subs = data.subscriptions || (data.identity && data.identity.subscriptions) || [];
- // Push通知端末を全て抽出
- const pushSubs = subscriptions.filter((s: any) => {
- const typeStr = String(s.type);
- return typeStr.includes("Push") || typeStr === "1";
- });
-
- // 今操作しているデバイスをリストから除外して保護
+ const pushSubs = subs.filter((s: any) => String(s.type).includes("Push") || String(s.type) === "1");
const otherDevices = pushSubs.filter((s: any) => s.id !== currentId);
-
- console.log(`[Cleanup] Keeping Current: ${currentId}. Others count: ${otherDevices.length}`);
- // 他のデバイスをアクティブ順(または作成順)にソート
otherDevices.sort((a: any, b: any) => {
const timeA = Number(a.last_active || a.created_at || 0);
const timeB = Number(b.last_active || b.created_at || 0);
return timeB - timeA;
});
- // 全体で3台以上(自分 + 他2台以上)ある場合、他の中で最新の1台以外を削除
if (pushSubs.length > 2) {
- const targets = otherDevices.slice(1);
- console.log(`[Cleanup] Target for deletion: ${targets.length} old devices.`);
-
- for (const sub of targets) {
- console.log(`[Cleanup] Deleting sub_id: ${sub.id}`);
+ const toDelete = otherDevices.slice(1);
+ for (const sub of toDelete) {
await fetch(`https://onesignal.com/api/v1/apps/${APP_ID}/subscriptions/${sub.id}`, {
method: "DELETE",
headers: { "Authorization": `Basic ${API_KEY}` }
});
}
- } else {
- console.log(`[Cleanup] Within limit (Total: ${pushSubs.length})`);
}
} catch (e) {
console.error("[Cleanup] Error:", e);