forked from admin/zeropost-tool
fix: restructure inbox API routes — channel/ and message/ namespaces
Next.js не позволяет [channelId] и [id] на одном уровне. Решение: /api/inbox/channel/[channelId]/* и /api/inbox/message/[id]/* InboxTab: все fetch пути обновлены
This commit is contained in:
@@ -1,27 +0,0 @@
|
|||||||
import { NextResponse } from 'next/server';
|
|
||||||
import { requireUser } from '@/lib/session';
|
|
||||||
|
|
||||||
const ENGINE_URL = process.env.ENGINE_URL || 'http://localhost:3030';
|
|
||||||
const ENGINE_SECRET = process.env.ENGINE_SECRET || '';
|
|
||||||
|
|
||||||
async function engineReq(path, opts = {}) {
|
|
||||||
const { method = 'GET', body, userId } = opts;
|
|
||||||
const headers = { 'x-internal-secret': ENGINE_SECRET };
|
|
||||||
if (userId) headers['x-user-id'] = String(userId);
|
|
||||||
if (body) headers['Content-Type'] = 'application/json';
|
|
||||||
const res = await fetch(`${ENGINE_URL}${path}`, {
|
|
||||||
method, headers, body: body ? JSON.stringify(body) : undefined,
|
|
||||||
});
|
|
||||||
return res.json();
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function GET(req, { params }) {
|
|
||||||
const user = await requireUser();
|
|
||||||
if (!user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
||||||
const { searchParams } = new URL(req.url);
|
|
||||||
const data = await engineReq(
|
|
||||||
`/api/inbox/${params.channelId}?${searchParams.toString()}`,
|
|
||||||
{ userId: user.id }
|
|
||||||
);
|
|
||||||
return NextResponse.json(data);
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
import { NextResponse } from 'next/server';
|
||||||
|
import { requireUser } from '@/lib/session';
|
||||||
|
|
||||||
|
const ENGINE_URL = process.env.ENGINE_URL || 'http://localhost:3030';
|
||||||
|
const ENGINE_SECRET = process.env.ENGINE_SECRET || '';
|
||||||
|
|
||||||
|
export async function GET(req, { params }) {
|
||||||
|
const user = await requireUser();
|
||||||
|
if (!user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||||
|
const { searchParams } = new URL(req.url);
|
||||||
|
const res = await fetch(
|
||||||
|
`${ENGINE_URL}/api/inbox/${params.channelId}?${searchParams.toString()}`,
|
||||||
|
{ headers: { 'x-internal-secret': ENGINE_SECRET, 'x-user-id': String(user.id) } }
|
||||||
|
);
|
||||||
|
return NextResponse.json(await res.json());
|
||||||
|
}
|
||||||
@@ -4,23 +4,14 @@ import { requireUser } from '@/lib/session';
|
|||||||
const ENGINE_URL = process.env.ENGINE_URL || 'http://localhost:3030';
|
const ENGINE_URL = process.env.ENGINE_URL || 'http://localhost:3030';
|
||||||
const ENGINE_SECRET = process.env.ENGINE_SECRET || '';
|
const ENGINE_SECRET = process.env.ENGINE_SECRET || '';
|
||||||
|
|
||||||
async function enginePost(path, userId, body) {
|
|
||||||
const res = await fetch(`${ENGINE_URL}${path}`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'x-internal-secret': ENGINE_SECRET,
|
|
||||||
'x-user-id': String(userId),
|
|
||||||
},
|
|
||||||
body: JSON.stringify(body),
|
|
||||||
});
|
|
||||||
return res.json();
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function POST(req, { params }) {
|
export async function POST(req, { params }) {
|
||||||
const user = await requireUser();
|
const user = await requireUser();
|
||||||
if (!user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
if (!user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||||
const body = await req.json().catch(() => ({}));
|
const body = await req.json().catch(() => ({}));
|
||||||
const data = await enginePost(`/api/inbox/${params.id}/reply`, user.id, body);
|
const res = await fetch(`${ENGINE_URL}/api/inbox/${params.id}/reply`, {
|
||||||
return NextResponse.json(data);
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json', 'x-internal-secret': ENGINE_SECRET, 'x-user-id': String(user.id) },
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
});
|
||||||
|
return NextResponse.json(await res.json());
|
||||||
}
|
}
|
||||||
@@ -10,11 +10,7 @@ export async function POST(req, { params }) {
|
|||||||
const body = await req.json().catch(() => ({}));
|
const body = await req.json().catch(() => ({}));
|
||||||
const res = await fetch(`${ENGINE_URL}/api/inbox/${params.id}/status`, {
|
const res = await fetch(`${ENGINE_URL}/api/inbox/${params.id}/status`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: { 'Content-Type': 'application/json', 'x-internal-secret': ENGINE_SECRET, 'x-user-id': String(user.id) },
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'x-internal-secret': ENGINE_SECRET,
|
|
||||||
'x-user-id': String(user.id),
|
|
||||||
},
|
|
||||||
body: JSON.stringify(body),
|
body: JSON.stringify(body),
|
||||||
});
|
});
|
||||||
return NextResponse.json(await res.json());
|
return NextResponse.json(await res.json());
|
||||||
@@ -49,7 +49,7 @@ export default function InboxTab({ channel }) {
|
|||||||
const load = useCallback(async (t = tab) => {
|
const load = useCallback(async (t = tab) => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`/api/inbox/${channel.id}?status=${t}&limit=40`).then(r => r.json());
|
const res = await fetch(`/api/inbox/channel/${channel.id}?status=${t}&limit=40`).then(r => r.json());
|
||||||
setMessages(res.messages || []);
|
setMessages(res.messages || []);
|
||||||
setTotal(res.total || 0);
|
setTotal(res.total || 0);
|
||||||
} catch {}
|
} catch {}
|
||||||
@@ -61,7 +61,7 @@ export default function InboxTab({ channel }) {
|
|||||||
async function setupWebhook() {
|
async function setupWebhook() {
|
||||||
setSetuppping(true);
|
setSetuppping(true);
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`/api/inbox/${channel.id}/setup-webhook`, { method: 'POST' }).then(r => r.json());
|
const res = await fetch(`/api/inbox/channel/${channel.id}/setup-webhook`, { method: 'POST' }).then(r => r.json());
|
||||||
if (res.ok) { setWebhookOk(true); load(tab); }
|
if (res.ok) { setWebhookOk(true); load(tab); }
|
||||||
else alert(res.error || 'Ошибка');
|
else alert(res.error || 'Ошибка');
|
||||||
} catch { alert('Ошибка соединения'); }
|
} catch { alert('Ошибка соединения'); }
|
||||||
@@ -72,7 +72,7 @@ export default function InboxTab({ channel }) {
|
|||||||
if (!replyText.trim()) return;
|
if (!replyText.trim()) return;
|
||||||
setSending(true);
|
setSending(true);
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`/api/inbox/${msg.id}/reply`, {
|
const res = await fetch(`/api/inbox/message/${msg.id}/reply`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ text: replyText }),
|
body: JSON.stringify({ text: replyText }),
|
||||||
@@ -87,7 +87,7 @@ export default function InboxTab({ channel }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function setStatus(msgId, status) {
|
async function setStatus(msgId, status) {
|
||||||
await fetch(`/api/inbox/${msgId}/status`, {
|
await fetch(`/api/inbox/message/${msgId}/status`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ status }),
|
body: JSON.stringify({ status }),
|
||||||
|
|||||||
Reference in New Issue
Block a user