const API = "https://inboxapp.com/api/v2";
const headers = {
Authorization: `Bearer ${process.env.INBOX_API_TOKEN}`,
"Content-Type": "application/json",
};
async function get(path: string) {
const response = await fetch(`${API}${path}`, { headers });
if (!response.ok) {
const error = await response.json();
throw new Error(`${error.code}: ${error.message} (${error.requestId})`);
}
return response.json();
}
async function assignUnassigned() {
const { data: members } = await get("/members");
const { data: threads } = await get("/threads?unassigned=true&limit=100");
for (const [index, thread] of threads.entries()) {
const member = members[index % members.length];
await fetch(`${API}/threads/${thread.id}`, {
method: "PATCH",
headers,
body: JSON.stringify({ assigneeId: member.id }),
});
}
}