Add a handler for posting a deal product

Im making a plugin based on the floating window and need to add a handler that allows me to post products to a deal. The problem is There is no function that allows me to post products to a deal so I have to make one by myself.

I have written the code but I have trouble accessing the user TOKEN in order to post.

Any advice?

import logger from '../../shared/logger';
import fetch from 'node-fetch';
import { getAPIClient } from '../../shared/oauth';
import { DealsApi } from 'pipedrive';

const log = logger('Post Deal Products API 🛍️');


const handler = async (req, res) => {
  try {
    const client = getAPIClient(req, res);
    const { dealId, products } = req.body;
    const API_TOKEN = client.API_TOKEN;

    const BASE_URL = 'https://pipedrive.com/api/v1';
    //log.info(`${BASE_URL}/deals/${dealId}/products?api_token=${API_TOKEN}`)
    for (const product of products) {
      const response = await fetch(`${BASE_URL}/deals/${dealId}/products?api_token=${API_TOKEN}`, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          product_id: product.productId,
          item_price: product.itemPrice || 0,
          quantity: product.quantity || 0,
        }),
      });

      if (!response.ok) {
        throw new Error(`Failed to add product ${product.productId} to deal ${dealId}`);
      }

      log.info(`Product ${product.productId} added to deal ${dealId}`);
    }

    res.status(200).json({ success: true, message: 'All products added to the deal successfully.' });
  } catch (error) {
    log.error('Failed posting deal products', error);
    res.status(500).json({ success: false, error: error.message });
  }
};

export default handler;

The client.API_TOKEN is here undefined.

This topic was automatically closed after 60 days. New replies are no longer allowed.