Skip to content

Aaa #1

@atul-mane01

Description

@atul-mane01

const fs = require('fs');
const express = require('express');
const csvParser = require('csv-parser');
const axios = require('axios');
const multer = require('multer');

const app = express();
const upload = multer({ dest: 'uploads/' });

const applicationClientId = 'your_application_client_id'; // Replace this with your client ID
const applicationClientSecret = 'your_application_client_secret'; // Replace this with your client secret
const bearerToken = 'your_bearer_token'; // Replace this with your Bearer token

app.post('/upload', upload.single('csvFile'), async (req, res) => {
const timestampGroups = new Map(); // Map to group payload data by timestamp

const readStream = fs.createReadStream(req.file.path);

readStream
.pipe(csvParser())
.on('data', (row) => {
// Assuming the CSV has a 'timestamp' column containing timestamps
const timestamp = new Date(row.timestamp).getTime(); // Convert timestamp to milliseconds
const payload = {
...row,
clientId: applicationClientId,
clientSecret: applicationClientSecret,
};

  if (!timestampGroups.has(timestamp)) {
    timestampGroups.set(timestamp, []);
  }
  timestampGroups.get(timestamp).push(payload); // Group payloads by timestamp
})
.on('end', async () => {
  let prevTimestamp = null;

  for (const [timestamp, payloads] of timestampGroups.entries()) {
    const now = Date.now();
    const delay = prevTimestamp !== null ? Math.max(0, timestamp - prevTimestamp) : 0;

    if (delay > 0) {
      await new Promise((resolve) => setTimeout(resolve, delay)); // Wait for the specified delay
    }

    for (const payload of payloads) {
      try {
        const response = await axios.post('https://example.com/api/endpoint', payload, {
          headers: {
            'Content-Type': 'application/json', // Specify request content type as JSON
            Authorization: `Bearer ${bearerToken}`, // Include the Bearer token in the request headers
          },
        });

        // Handle response data as needed
        console.log('API Call Response:', response.data);
      } catch (error) {
        // Handle error if the API call fails
        console.error('API Call Error:', error.message);
      }
    }

    prevTimestamp = timestamp;
  }

  // Cleanup the temporary CSV file
  fs.unlinkSync(req.file.path);

  // Send a response back to the client (optional)
  res.status(200).json({ message: 'API calls completed' });
});

});

const port = 3000;
app.listen(port, () => {
console.log(Server listening on port ${port});
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions