Campaign stats in admin, user posts via new route, safer ad creation
- Admin: campaign stats panel (impressions, clicks, CTR per campaign and ad) with date range
- My posts: GET /api/v1/users/{id}/posts with paging, fallback to the legacy route
- Ad creation: retry after a failed video upload reuses the created ad; show backend 400 text
- Fullscreen ad form: only absolute http(s) target URLs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -16,6 +16,14 @@ export class AdminApiError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
/** Ошибка с текстом сервера в serverMessage (пусто, если сервер его не прислал). */
|
||||
async function adminError(res, fallback) {
|
||||
const message = await readErrorMessage(res, null);
|
||||
const err = new AdminApiError(res.status, message || fallback);
|
||||
err.serverMessage = message || "";
|
||||
return err;
|
||||
}
|
||||
|
||||
async function readErrorMessage(res, fallback) {
|
||||
try {
|
||||
const body = await res.json();
|
||||
@@ -34,7 +42,7 @@ async function readErrorMessage(res, fallback) {
|
||||
*/
|
||||
export async function getCampaigns() {
|
||||
const res = await apiFetch(`${ADMIN_BASE}/campaigns`);
|
||||
if (!res.ok) throw new AdminApiError(res.status, await readErrorMessage(res, "Failed to load campaigns"));
|
||||
if (!res.ok) throw await adminError(res, "Failed to load campaigns");
|
||||
return res.json();
|
||||
}
|
||||
|
||||
@@ -44,7 +52,7 @@ export async function createCampaign({ name, startDate, endDate, dailyImpression
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name, startDate, endDate, dailyImpressionCap }),
|
||||
});
|
||||
if (!res.ok) throw new AdminApiError(res.status, await readErrorMessage(res, "Failed to create campaign"));
|
||||
if (!res.ok) throw await adminError(res, "Failed to create campaign");
|
||||
return res.json();
|
||||
}
|
||||
|
||||
@@ -57,7 +65,7 @@ export async function updateCampaignStatus(campaignId, status) {
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ status }),
|
||||
});
|
||||
if (!res.ok) throw new AdminApiError(res.status, await readErrorMessage(res, "Failed to update status"));
|
||||
if (!res.ok) throw await adminError(res, "Failed to update status");
|
||||
return res.json();
|
||||
}
|
||||
|
||||
@@ -72,7 +80,24 @@ export async function updateCampaign(campaignId, { name, startDate, endDate, dai
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name, startDate, endDate, dailyImpressionCap }),
|
||||
});
|
||||
if (!res.ok) throw new AdminApiError(res.status, await readErrorMessage(res, "Failed to update campaign"));
|
||||
if (!res.ok) throw await adminError(res, "Failed to update campaign");
|
||||
return res.json();
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/v1/admin/campaigns/{id}/stats?from=YYYY-MM-DD&to=YYYY-MM-DD —
|
||||
* показы, клики и CTR (%) кампании и каждого её объявления за дни from..to
|
||||
* включительно (по умолчанию бэкенд берёт последние 30 дней, максимум 366).
|
||||
* Ответ: {campaignId, from, to, impressions, clicks, ctr, advertisings: [...]}.
|
||||
* Старый бэкенд маршрута не знает — 404.
|
||||
*/
|
||||
export async function getCampaignStats(campaignId, { from, to } = {}) {
|
||||
const params = new URLSearchParams();
|
||||
if (from) params.set("from", from);
|
||||
if (to) params.set("to", to);
|
||||
const query = params.toString();
|
||||
const res = await apiFetch(`${ADMIN_BASE}/campaigns/${encodeURIComponent(campaignId)}/stats${query ? `?${query}` : ""}`);
|
||||
if (!res.ok) throw await adminError(res, "Failed to load stats");
|
||||
return res.json();
|
||||
}
|
||||
|
||||
@@ -84,7 +109,7 @@ export async function updateCampaign(campaignId, { name, startDate, endDate, dai
|
||||
*/
|
||||
export async function getAdvertisingsByCampaign(campaignId) {
|
||||
const res = await apiFetch(`${ADMIN_BASE}/ads?campaignId=${encodeURIComponent(campaignId)}`);
|
||||
if (!res.ok) throw new AdminApiError(res.status, await readErrorMessage(res, "Failed to load advertisings"));
|
||||
if (!res.ok) throw await adminError(res, "Failed to load advertisings");
|
||||
return res.json();
|
||||
}
|
||||
|
||||
@@ -98,7 +123,7 @@ export async function createAdvertising({ campaignId, type, mediaKey, targetUrl,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ campaignId, type, mediaKey, targetUrl, weight }),
|
||||
});
|
||||
if (!res.ok) throw new AdminApiError(res.status, await readErrorMessage(res, "Failed to create advertising"));
|
||||
if (!res.ok) throw await adminError(res, "Failed to create advertising");
|
||||
return res.json();
|
||||
}
|
||||
|
||||
@@ -112,7 +137,7 @@ export async function updateAdvertising(advertisingId, { targetUrl, weight }) {
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ targetUrl, weight }),
|
||||
});
|
||||
if (!res.ok) throw new AdminApiError(res.status, await readErrorMessage(res, "Failed to update advertising"));
|
||||
if (!res.ok) throw await adminError(res, "Failed to update advertising");
|
||||
return res.json();
|
||||
}
|
||||
|
||||
@@ -128,7 +153,7 @@ export async function uploadAdImage(file) {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
});
|
||||
if (!res.ok) throw new AdminApiError(res.status, await readErrorMessage(res, "Failed to upload image"));
|
||||
if (!res.ok) throw await adminError(res, "Failed to upload image");
|
||||
return res.json(); // { mediaKey }
|
||||
}
|
||||
|
||||
@@ -138,7 +163,7 @@ export async function presignVideoUpload(advertisingId, contentType) {
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ contentType }),
|
||||
});
|
||||
if (!res.ok) throw new AdminApiError(res.status, await readErrorMessage(res, "Presign failed"));
|
||||
if (!res.ok) throw await adminError(res, "Presign failed");
|
||||
return res.json(); // { uploadUrl, sourceKey, expiryMinutes }
|
||||
}
|
||||
|
||||
@@ -151,13 +176,13 @@ export async function confirmVideoUpload(advertisingId, sourceKey) {
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ sourceKey }),
|
||||
});
|
||||
if (!res.ok) throw new AdminApiError(res.status, await readErrorMessage(res, "Confirm upload failed"));
|
||||
if (!res.ok) throw await adminError(res, "Confirm upload failed");
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function getAdvertisingStatus(advertisingId) {
|
||||
const res = await apiFetch(`${ADMIN_BASE}/ads/${advertisingId}/status`);
|
||||
if (!res.ok) throw new AdminApiError(res.status, await readErrorMessage(res, "Status check failed"));
|
||||
if (!res.ok) throw await adminError(res, "Status check failed");
|
||||
return res.json(); // { advertisingId, transcodingStatus }
|
||||
}
|
||||
|
||||
@@ -168,7 +193,7 @@ export async function getAdvertisingStatus(advertisingId) {
|
||||
*/
|
||||
export async function getFullscreenAds() {
|
||||
const res = await apiFetch(`${ADMIN_BASE}/fullscreen-ads`);
|
||||
if (!res.ok) throw new AdminApiError(res.status, await readErrorMessage(res, "Failed to load fullscreen ads"));
|
||||
if (!res.ok) throw await adminError(res, "Failed to load fullscreen ads");
|
||||
return res.json();
|
||||
}
|
||||
|
||||
@@ -181,7 +206,7 @@ export async function createFullscreenAd({ targetUrl }) {
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ targetUrl: targetUrl || null }),
|
||||
});
|
||||
if (!res.ok) throw new AdminApiError(res.status, await readErrorMessage(res, "Failed to create fullscreen ad"));
|
||||
if (!res.ok) throw await adminError(res, "Failed to create fullscreen ad");
|
||||
return res.json();
|
||||
}
|
||||
|
||||
@@ -191,7 +216,7 @@ export async function presignFullscreenVideoUpload(fullscreenAdId, contentType)
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ contentType }),
|
||||
});
|
||||
if (!res.ok) throw new AdminApiError(res.status, await readErrorMessage(res, "Presign failed"));
|
||||
if (!res.ok) throw await adminError(res, "Presign failed");
|
||||
return res.json(); // { uploadUrl, sourceKey, expiryMinutes }
|
||||
}
|
||||
|
||||
@@ -204,7 +229,7 @@ export async function confirmFullscreenVideoUpload(fullscreenAdId, sourceKey) {
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ sourceKey }),
|
||||
});
|
||||
if (!res.ok) throw new AdminApiError(res.status, await readErrorMessage(res, "Confirm upload failed"));
|
||||
if (!res.ok) throw await adminError(res, "Confirm upload failed");
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -216,7 +241,7 @@ export async function activateFullscreenAd(fullscreenAdId) {
|
||||
const res = await apiFetch(`${ADMIN_BASE}/fullscreen-ads/${fullscreenAdId}/activate`, {
|
||||
method: "PATCH",
|
||||
});
|
||||
if (!res.ok) throw new AdminApiError(res.status, await readErrorMessage(res, "Activate failed"));
|
||||
if (!res.ok) throw await adminError(res, "Activate failed");
|
||||
return res.json();
|
||||
}
|
||||
|
||||
@@ -224,6 +249,6 @@ export async function deactivateFullscreenAd(fullscreenAdId) {
|
||||
const res = await apiFetch(`${ADMIN_BASE}/fullscreen-ads/${fullscreenAdId}/deactivate`, {
|
||||
method: "PATCH",
|
||||
});
|
||||
if (!res.ok) throw new AdminApiError(res.status, await readErrorMessage(res, "Deactivate failed"));
|
||||
if (!res.ok) throw await adminError(res, "Deactivate failed");
|
||||
return res.json();
|
||||
}
|
||||
Reference in New Issue
Block a user