• Delete a manga entry in AniList.

    Parameters

    • entryId: number

      The AniList entry ID to delete.

    • token: string

      The user's authentication token.

    Returns Promise<{ success: boolean; error?: string }>

    A promise resolving to an object indicating success or error.

    export async function deleteMangaEntry(
    entryId: number,
    token: string,
    ): Promise<{ success: boolean; error?: string }> {
    // Generate an operation ID for tracking in logs
    const operationId = `del-${entryId}-${Date.now().toString(36).substring(4, 10)}`;

    console.log(
    `🗑️ [${operationId}] Starting delete operation for entry ID ${entryId}`,
    );

    if (!token) {
    console.error(`❌ [${operationId}] No authentication token provided`);
    return {
    success: false,
    error: "No authentication token provided",
    };
    }

    try {
    const variables = {
    id: entryId,
    };

    console.log(
    `📦 [${operationId}] Variables:`,
    JSON.stringify(variables, null, 2),
    );

    // Define the expected response structure
    interface DeleteMediaListEntryData {
    DeleteMediaListEntry?: {
    deleted: boolean;
    };
    data?: {
    DeleteMediaListEntry?: {
    deleted: boolean;
    };
    };
    }

    const response = await request<DeleteMediaListEntryData>(
    DELETE_MANGA_ENTRY,
    variables,
    token,
    );

    // Check for GraphQL errors
    if (response.errors && response.errors.length > 0) {
    const errorMessages = response.errors
    .map((err) => err.message)
    .join(", ");
    console.error(
    `❌ [${operationId}] GraphQL errors for delete operation:`,
    response.errors,
    );
    return {
    success: false,
    error: `GraphQL error: ${errorMessages}`,
    };
    }

    // Handle nested response structure
    const responseData = response.data?.data
    ? response.data.data
    : response.data;

    if (responseData?.DeleteMediaListEntry?.deleted) {
    console.log(
    `✅ [${operationId}] Successfully deleted entry with ID ${entryId}`,
    );
    return {
    success: true,
    };
    } else {
    console.error(
    `❌ [${operationId}] Missing DeleteMediaListEntry in response:`,
    JSON.stringify(response, null, 2),
    );
    return {
    success: false,
    error: "Delete failed: Entry was not deleted",
    };
    }
    } catch (error) {
    const errorMessage = error instanceof Error ? error.message : String(error);
    console.error(
    `❌ [${operationId}] Error deleting manga entry ${entryId}:`,
    error,
    );

    // Try to get more detailed information from the error object
    if (error instanceof Error) {
    console.error(` [${operationId}] Error type: ${error.name}`);
    console.error(` [${operationId}] Error message: ${error.message}`);
    console.error(
    ` [${operationId}] Stack trace:`,
    error.stack || "No stack trace available",
    );
    }

    return {
    success: false,
    error: errorMessage,
    };
    }
    }