Efficiently Remove and Replace Outdated Products in MongoDB with Envato API in Node.js

I have around 200,000 products in my MongoDB database. I need to write a script to remove products that are no longer available on Envato and replace them with new ones. I’ve written the following script to accomplish this, but I’m looking for suggestions to improve its efficiency and robustness.

The main script iterates over the products, checks their availability on Envato using the envatoApi, and replaces unavailable products with similar or new items. The code uses batching, Redis for tracking progress, and handles API rate limiting and retries.

Here’s my main script:

import { envatoApi } from '#apis';
import { productService, redisService, websiteService } from '#services';
import AppError from '#shared/AppError';
import { convert } from 'html-to-text';
import slugify from 'slugify';
import Model from '#models/Product';
import { Mutex } from 'async-mutex';

const config = {
    BATCH_SIZE: 500,
    API_BATCH_SIZE: 5,
    API_DELAY: 3000,
    MAX_ATTEMPTS: 10,
    SKIP_COUNT: 0,
    REDIS_KEY: 'processed_count',
    REDIS_PAGE_KEY: 'current_page',
};

const mutex = new Mutex();

const removeOldProductsAndAddNew = async category => {
    let processedCount = await redisService.get(config.REDIS_KEY);
    processedCount = processedCount ? parseInt(processedCount, 10) : config.SKIP_COUNT;
    let noOfBatches = 1;
    let batch = [];
    let skippedCount = 0;

    try {
        console.log('skipped items', processedCount);
        let productCursor = Model.find({ category: { $in: [category] } }).cursor();
        let run = true;

        while (run) {
            let hasNext = true;
            try {
                for await (const doc of productCursor) {
                    if (skippedCount < processedCount) {
                        skippedCount++;
                        continue;
                    }
                    batch.push(doc);
                    if (batch.length >= config.BATCH_SIZE) {
                        await processBatch(batch);
                        noOfBatches++;
                        console.log(`Mongo batch ${noOfBatches} processed.`);
                        batch = [];
                        processedCount += config.BATCH_SIZE;
                        await redisService.set(config.REDIS_KEY, processedCount);
                    }
                }
                hasNext = false; // No more documents to process
            } catch (error) {
                if (error.code === 43) {
                    console.warn('Cursor not found, reopening cursor...');
                    productCursor = Model.find({ category: { $in: [category] } })
                        .skip(processedCount)
                        .cursor();
                } else {
                    throw error;
                }
            } finally {
                try {
                    await productCursor.close();
                } catch (closeError) {
                    console.error('Error closing cursor:', closeError);
                }
            }

            if (!hasNext) break;
        }

        if (batch.length > 0) {
            await processBatch(batch);
            processedCount += batch.length;
            await redisService.set(config.REDIS_KEY, processedCount);
        }
        console.log(`Processing completed. Number of Mongo batches processed: ${noOfBatches}`);
    } catch (error) {
        console.error('Error processing batches:', error);
        throw new AppError(error || 'An unexpected error occurred', error.statusCode || 500);
    }
};

const processBatch = async batch => {
    for (let i = 0; i < batch.length; i += config.API_BATCH_SIZE) {
        const batchSlice = batch.slice(i, i + config.API_BATCH_SIZE);
        console.log(`Processing API requests: ${i + config.API_BATCH_SIZE}`);
        for (const product of batchSlice) {
            await removeOldProducts(product);
        }
        await delay(config.API_DELAY);
    }
};

const delay = ms => new Promise(resolve => setTimeout(resolve, ms));

const removeOldProducts = async product => {
    const [site, externalId] = parseExternalId(product?.externalId);
    try {
        await envatoApi.getItemDetail(externalId);
    } catch (error) {
        if (error?.error === 404) {
            console.log('Product not found, initiating replacement...');
            await handleProductReplacement({ id: externalId, site }, product);
        } else {
            throw new AppError(error.description || 'An unexpected error occurred', error.error || 500);
        }
    }
};

const parseExternalId = externalId => {
    if (!externalId) return [null, null];
    const [sitePart, idPart] = externalId.split('_');
    return [sitePart, idPart];
};

const handleProductReplacement = async ({ id, site }, product) => {
    let attempts = 0;

    while (attempts < config.MAX_ATTEMPTS) {
        let newProduct = null;

        await productService.remove(product._id);

        try {
            newProduct = await fetchSimilarOrNewItem(id, site, product);
            if (newProduct) {
                await updateProductReferences(product, newProduct);
                return { message: 'Product updated successfully' };
            }
        } catch (error) {
            const errorCode = error?.error || error?.code || error?.statusCode || 500;
            const errorMessage = error?.message || 'An unexpected error occurred';

            if (errorCode === 404) {
                console.log(`404 Error, item not found, retrying... Attempts: ${attempts}`);
            } else {
                throw new AppError(errorMessage, errorCode);
            }
        }

        attempts++;
    }

    console.log(`Maximum attempts reached for product ${product._id}, removing from all websites.`);
    await websiteService.collection.updateMany({ products: { $in: [product._id] } }, { $pull: { products: product._id } });

    return { message: 'Product removed after maximum attempts' };
};

The envatoApi module used in the script:

import axios from 'axios';
import config from '#config';

const instance = axios.create({
    baseURL: 'https://api.envato.com',
    headers: {
        Authorization: `Bearer ${config.apis.envato.apiKey}`,
    },
});

const onFulfilled = response => {
    const { data } = response;
    return data;
};

const onRejected = err => {
    const { error, description } = err.response.data;
    return Promise.reject({ error, description });
};

instance.interceptors.response.use(onFulfilled, onRejected);

/**
 *
 * @param {string} purchaseCode
 * @returns {Promise<string>}
 */
const getDownloadUrl = async purchaseCode => {
    const { download_url: url } = await instance.get('/v3/market/buyer/download', { params: { purchase_code: purchaseCode, shorten_url: true } });
    return url;
};

const getItemDetail = async id => {
    const data = await instance.get(`/v3/market/catalog/item?id=${id}`);
    return data;
};

const getSimilarItem = async id => {
    const data = await instance.get(`/v1/discovery/search/search/more_like_this?item_id=${id}`);
    return data;
};

const getSimilarItems = async (site, page) => {
    const data = await instance.get(`/v1/discovery/search/search/item?site=${site}&date=this-year&page=${page}`);
    return data;
};

export default {
    getDownloadUrl,
    getItemDetail,
    getSimilarItem,
    getSimilarItems,
};

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật