Menu dinâmico

Alguém sabe o que estou fazendo de errado?

 

 

const channelJs = "0mn.io";

const orderDetails = {

  orderId: "1432110501453-01",

  creationDate: "2024-05-13T19:00:26.0000000+00:00",

  shippingEstimatedDate: null,

  status: "invoiced",

  totalValue: 47625,

  trackingCode: "067388-5"

};

 

function run(canal, orderDetails) {

    try {

      const textHeader = "";

      const textBody = `Aqui estão seus pedidos. Selecione um para mais informações. :package:`;

      const textFooter = ``;

 

      const arrayOptions = [orderDetails];

 

      if (canal === "whatsapp") {

        return {

          type: "application/json",

          content: {

            "recipient_type": "individual",

            "type": "interactive",

            interactive: {

              "type": "list",

              header: {

                "type": "text",

                text: textHeader,

              },

              body: {

                text: textBody,

              },

              footer: {

                text: textFooter,

              },

              action: {

                "button": "Clique aqui",

                "sections": [

                  {

                    "title": "",

                    "rows": mountMenuWhatsApp(arrayOptions)

                  }

                ]

              },

            },

          }

        };

      }

      if (canal === "0mni.io") {

        return {

          type: "application/vnd.lime.select+json",

          content: {

            text: textBody,

            options: mountMenuBlipChat(arrayOptions),

          },

        };

      }

 

      return {

        type: "text/plain",

        content: mountMenuText(textBody, arrayOptions),

      };

    } catch (error) {

      return error.message;

    }

  }

 

  function mountMenuWhatsApp(arrayOptions) {

   

    function parseDate(str) {

      const parts = str.split('/');

      return new Date(parts[2], parts[1] - 1, parts[0]);

    }

 

    const sortedOptions = arrayOptions.sort((a, b) => parseDate(b.data_criacao) - parseDate(a.data_criacao));

 

    const recentOptions = sortedOptions.slice(0, 10);

    console.log(typeof arrayOptions);

    return recentOptions.reduce((acc, eachOption, index) => {

      if (typeof eachOption === 'string') {

        acc.push({

          id: index + 1,

          title: eachOption,

          description: ''

        });

      } else {

        acc.push({

          id: index + 1,

          title: `${eachOption.id}`,

          description: `Data: ${eachOption.data_criacao} - Valor: R$ ${eachOption.valor_final}` || ''

        });

      }

      return acc;

    }, []);

  }

 

  function mountMenuBlipChat(arrayOptions) {

    return arrayOptions.reduce((acc, eachOption, index) => {

      acc.push({

        order: eachOption,

        text: eachOption,

      });

      return acc;

    }, []);

  }

 

  function mountMenuText(textBody, arrayOptions) {

    const textOptions = arrayOptions.reduce((acc, eachOption, index) => {

      acc.push(`${index + 1} - ${eachOption}`);

 

      return acc;

    }, []);

 

    return `${textBody}\n\n${textOptions.join("\n")}`;

  }

 

  console.log(run(channelJs, orderDetails))

 

 

 

 

 

RETORNO DO CONSOLE: 

{

  type: 'text/plain',

  content:

    'Aqui estão seus pedidos. Selecione um para mais informações. :package:\n' +

    '\n' +

    '1 - [object Object]'

}

  ​​​​​at ​run(channelJs, orderDetails)​​​ ​index.js:118:3​