Updating multiple statuses

We can send up to 20 user updates at a time with our Batch Update endpoints. In the following scenario, we'll use a scheduled lunchtime to trigger batch updates. To do so, we'll:

  • Identify and use time to trigger updates
  • Chunk users to send in group of 20 (using a helper function that chunks our array, as done in previous batch requests)
  • Make a PATCH request for each set of 20

Group and send users sample

const LUNCH_STATUS_ID = "1234";
const users = JSON.parse(localStorage.getItem("users"));
const setLunchStatus = async () => {
  const batches = chunkArray(users, 20);
  for (const batch of batches) {
    // Build array of users for this batch
    const usersPayload = batch.map(user => ({
      user_id: user.user_id
      status_id: LUNCH_STATUS_ID
    }));
    await fetch('https://api.zoom.us/v2/contact_center/users', {
      method: "PATCH",
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer YOUR_SECRET_TOKEN`,
      },
      body: JSON.stringify({ users: usersPayload }),
    });
  }
};

Since we're performing these updates based on a schedule, we'll need to track time throughout the day. We'll do a time check on each component mount, for this example.

Check if it's lunch time sample

useEffect(() => {
    const now = new Date();
    const nextNoon = new Date();
    nextNoon.setHours(12, 0, 0, 0);
    if (now > nextNoon) nextNoon.setDate(nextNoon.getDate() + 1);
    const delay = nextNoon - now;
    const timer = setTimeout(() => {
        setLunchStatus();
        setInterval(setLunchStatus, 24 * 60 * 60 * 1000); // repeat every 24h
    }, delay);
    return () => clearTimeout(timer);
}, []);