How to export the product details as csv file using graphql and nodejs

This api used to pass the poduct details to a csv file .

 exportProducts: ({ EMAIL, TOKEN }) => {
        return token_check.token_verify(EMAIL, TOKEN)
            .then((data) => {

                if (data == "TOKEN_INVALID" || data == "TOKEN_EXPIRED" || data == "RECORD_NOT_FOUND")
                    return { summary: { status: "SUCCESS", reason: data } }


                else if (data == "TOKEN_VALID") {
                    return product.findAll({
                        where:
                        {
                            is_inactive: false, display_in_website: true,
                            quantity_available: { [Op.gt]: [0] },
                        },
                    })
                        .then((rows) => {
                            const product_json = rows.map(row => {
                                return {
                                    record_row_id: row.record_row_id,
                                    supplier_name: row.supplier_name,
                                    supplier_email: row.supplier_email,
                                    wine_producer_name: row.wine_producer_name,
                                                               name: row.name,
                                    title: row.title,
                                    sku: row.sku,
                                                                   

                                }
                            })
                            const csvData = csvjson.toCSV(product_json, {
                                headers: 'key',
                                delimiter: '\t'
                            });
                            //console.log("rows", csvData);
                            const file = Buffer.from(csvData).toString('base64');

                            return { summary: { status: "SUCCESS", reason: "FILE_EXPORTED" }, details: { file: file } }

                        })
                        .catch(function (error) {
                            console.log(error);
                            return { summary: { status: "SUCCESS", reason: "RECORD_NOT_FOUND" } }

                        });
                }
            })
            .catch(function (error) {
                console.log(error);
                return { summary: { status: "SUCCESS", reason: "RECORD_NOT_FOUND" } }

            });
    },

Leave a comment

Your email address will not be published. Required fields are marked *