All files / src/lib/api/admin blogs.ts

98.33% Statements 59/60
75.51% Branches 37/49
100% Functions 36/36
100% Lines 55/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335                                        95x   3x 3x 2x 5x 5x       3x 3x       2x 2x 2x           1x       1x             1x             1x           1x           1x             2x                 1x             1x                   1x               1x             1x             2x 2x       1x             1x             1x             1x       1x             1x             1x             4x 4x           2x       2x                   1x                                 1x                 1x               2x 2x 2x 2x               1x                                   1x                     1x         1x                           1x 1x       1x 1x 1x     1x                           1x                     1x            
import { request, uploadFile } from "../base";
import type {
	Blog,
	BlogCategory,
	BlogTag,
	CreateBlogInput,
	UpdateBlogInput,
	AddBlogProInput,
	AddBlogProjectInput,
	BlogListFilters,
	ProBlogSuggestion,
} from "../blogs";
import type {
	BlogImage,
	AddPexelsImageInput,
	UpdateBlogImageInput,
	BlogMediaItem,
	BlogMediaPage,
} from "../blog-images";
 
export const adminBlogsApi = {
	async listBlogs(filters?: BlogListFilters) {
		const params = new URLSearchParams();
		if (filters) {
			Object.entries(filters).forEach(([key, value]) => {
				Eif (value !== undefined) {
					params.append(key, String(value));
				}
			});
		}
		const query = params.toString() ? `?${params.toString()}` : "";
		return request<Blog[]>(`/api/admin/blogs${query}`);
	},
 
	async checkBlogSlug(slug: string, excludeId?: string) {
		const params = new URLSearchParams({ slug });
		if (excludeId) params.set("excludeId", excludeId);
		return request<{ available: boolean }>(
			`/api/admin/blogs/check-slug?${params.toString()}`,
		);
	},
 
	async getBlog(id: string) {
		return request<Blog>(`/api/admin/blogs/${id}`);
	},
 
	async createBlog(data: CreateBlogInput) {
		return request<Blog>(`/api/admin/blogs`, {
			method: "POST",
			body: data,
		});
	},
 
	async updateBlog(id: string, data: UpdateBlogInput) {
		return request<Blog>(`/api/admin/blogs/${id}`, {
			method: "PUT",
			body: data,
		});
	},
 
	async deleteBlog(id: string) {
		return request(`/api/admin/blogs/${id}`, {
			method: "DELETE",
		});
	},
 
	async publishBlog(id: string) {
		return request<Blog>(`/api/admin/blogs/${id}/publish`, {
			method: "POST",
		});
	},
 
	async unpublishBlog(id: string) {
		return request<Blog>(`/api/admin/blogs/${id}/unpublish`, {
			method: "POST",
		});
	},
 
	// Cover Image Upload
	async uploadBlogCover(blogId: string, file: File, altText?: string) {
		return uploadFile<{ blog: Blog; imageUrl: string }>(
			`/api/admin/blogs/${blogId}/upload-cover`,
			file,
			altText ? { altText } : undefined,
		);
	},
 
	// Blog Pros
	async addBlogPro(blogId: string, data: AddBlogProInput) {
		return request(`/api/admin/blogs/${blogId}/pros`, {
			method: "POST",
			body: data,
		});
	},
 
	async removeBlogPro(blogId: string, proId: string) {
		return request(`/api/admin/blogs/${blogId}/pros/${proId}`, {
			method: "DELETE",
		});
	},
 
	async updateBlogPro(
		blogId: string,
		proId: string,
		data: Partial<AddBlogProInput>,
	) {
		return request(`/api/admin/blogs/${blogId}/pros/${proId}`, {
			method: "PUT",
			body: data,
		});
	},
 
	// Blog Projects
	async addBlogProject(blogId: string, data: AddBlogProjectInput) {
		return request(`/api/admin/blogs/${blogId}/projects`, {
			method: "POST",
			body: data,
		});
	},
 
	async removeBlogProject(blogId: string, projectId: string) {
		return request(`/api/admin/blogs/${blogId}/projects/${projectId}`, {
			method: "DELETE",
		});
	},
 
	// Blog Categories
	async listBlogCategories(includeInactive = false) {
		const query = includeInactive ? "?includeInactive=true" : "";
		return request<BlogCategory[]>(`/api/admin/blogs/categories/all${query}`);
	},
 
	async createBlogCategory(data: Partial<BlogCategory>) {
		return request<BlogCategory>(`/api/admin/blogs/categories`, {
			method: "POST",
			body: data,
		});
	},
 
	async updateBlogCategory(id: string, data: Partial<BlogCategory>) {
		return request<BlogCategory>(`/api/admin/blogs/categories/${id}`, {
			method: "PUT",
			body: data,
		});
	},
 
	async deleteBlogCategory(id: string) {
		return request(`/api/admin/blogs/categories/${id}`, {
			method: "DELETE",
		});
	},
 
	// Blog Tags
	async listBlogTags() {
		return request<BlogTag[]>(`/api/admin/blogs/tags/all`);
	},
 
	async createBlogTag(data: Partial<BlogTag>) {
		return request<BlogTag>(`/api/admin/blogs/tags`, {
			method: "POST",
			body: data,
		});
	},
 
	async updateBlogTag(id: string, data: Partial<BlogTag>) {
		return request<BlogTag>(`/api/admin/blogs/tags/${id}`, {
			method: "PUT",
			body: data,
		});
	},
 
	async deleteBlogTag(id: string) {
		return request(`/api/admin/blogs/tags/${id}`, {
			method: "DELETE",
		});
	},
 
	// Blog Suggestions
	async listBlogSuggestions(status?: string) {
		const query = status ? `?status=${status}` : "";
		return request<ProBlogSuggestion[]>(
			`/api/admin/blogs/suggestions/all${query}`,
		);
	},
 
	async listProBlogSuggestions(filters?: { status?: string }) {
		return adminBlogsApi.listBlogSuggestions(filters?.status);
	},
 
	async acceptBlogSuggestion(id: string, adminNotes?: string) {
		return request<ProBlogSuggestion>(
			`/api/admin/blogs/suggestions/${id}/accept`,
			{
				method: "PUT",
				body: { adminNotes },
			},
		);
	},
 
	async rejectBlogSuggestion(id: string, adminNotes: string) {
		return request<ProBlogSuggestion>(
			`/api/admin/blogs/suggestions/${id}/reject`,
			{
				method: "PUT",
				body: { adminNotes },
			},
		);
	},
 
	async updateProBlogSuggestion(
		id: string,
		data: {
			status?: "pending" | "accepted" | "rejected" | "published";
			acceptedBlogId?: string;
			adminNotes?: string;
		},
	) {
		return request<ProBlogSuggestion>(`/api/admin/blogs/suggestions/${id}`, {
			method: "PUT",
			body: data,
		});
	},
 
	// ── Blog Image Methods (admin) ────────────────────────────────────────────
 
	async getAdminBlogImages(blogId: string) {
		return request<BlogImage[]>(`/api/admin/blogs/${blogId}/images`);
	},
 
	async uploadAdminBlogImage(
		blogId: string,
		file: File,
		extra?: { altText?: string; caption?: string },
	) {
		const fields: Record<string, string> = {};
		if (extra?.altText) fields.altText = extra.altText;
		Iif (extra?.caption) fields.caption = extra.caption;
		return uploadFile<BlogImage>(
			`/api/admin/blogs/${blogId}/images/upload`,
			file,
			Object.keys(fields).length ? fields : undefined,
		);
	},
 
	async addAdminPexelsImage(blogId: string, input: AddPexelsImageInput) {
		return request<BlogImage>(`/api/admin/blogs/${blogId}/images/pexels`, {
			method: "POST",
			body: {
				pexelsId: input.pexelsId,
				url: input.url,
				width: input.width,
				height: input.height,
				altText: input.altText,
				photographer: input.photographer,
				photographerUrl: input.photographerUrl,
			},
		});
	},
 
	async addAdminMediaImage(
		blogId: string,
		input: { mediaId: number; altText?: string; caption?: string },
	) {
		return request<BlogImage>(`/api/admin/blogs/${blogId}/images/media`, {
			method: "POST",
			body: {
				mediaId: input.mediaId,
				...(input.altText !== undefined && { altText: input.altText }),
				...(input.caption !== undefined && { caption: input.caption }),
			},
		});
	},
 
	async searchAdminPexels(query: string, page = 1, perPage = 20) {
		const params = new URLSearchParams({
			q: query,
			page: page.toString(),
			per_page: perPage.toString(),
		});
		return request(
			`/api/admin/blogs/images/search/pexels?${params.toString()}`,
		);
	},
 
	async searchAllProsMedia(
		_blogId: string,
		params: {
			search?: string;
			proId?: string;
			page?: number;
			perPage?: number;
		} = {},
	): Promise<BlogMediaPage> {
		const { search = "", proId, page = 1, perPage = 20 } = params;
		const qs = new URLSearchParams({
			page: page.toString(),
			per_page: perPage.toString(),
		});
		Eif (search.trim()) qs.set("q", search.trim());
		Eif (proId) qs.set("proId", proId);
		const response = await request<BlogMediaItem[]>(
			`/api/admin/blogs/images/gallery?${qs.toString()}`,
		);
		return {
			data: response.data ?? [],
			meta: {
				total: response.meta?.total ?? 0,
				page: response.meta?.page ?? page,
				limit: response.meta?.limit ?? perPage,
				totalPages: response.meta?.totalPages ?? 1,
				hasNext: response.meta?.hasNext ?? false,
				hasPrev: response.meta?.hasPrev ?? false,
			},
		};
	},
 
	async deleteAdminBlogImage(blogId: string, imageId: number) {
		return request<{ message: string }>(
			`/api/admin/blogs/${blogId}/images/${imageId}`,
			{ method: "DELETE" },
		);
	},
 
	async updateAdminBlogImage(
		blogId: string,
		imageId: number,
		input: UpdateBlogImageInput,
	) {
		return request<BlogImage>(`/api/admin/blogs/${blogId}/images/${imageId}`, {
			method: "PUT",
			body: input,
		});
	},
};