72 lines
2.0 KiB
TypeScript
72 lines
2.0 KiB
TypeScript
import axios from "axios";
|
|
|
|
const connectWiseApi = axios.create({
|
|
baseURL: `https://ttscw.totaltech.net/v4_6_release/apis/3.0/`,
|
|
headers: {
|
|
Authorization: `Basic ${process.env.CW_BASIC_TOKEN}`,
|
|
clientId: `${process.env.CW_CLIENT_ID}`,
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
|
|
async function main() {
|
|
// Fetch inactive catalog items
|
|
const pageSize = 1000;
|
|
let page = 1;
|
|
const inactiveItems: any[] = [];
|
|
|
|
while (true) {
|
|
const response = await connectWiseApi.get(
|
|
`/procurement/catalog?page=${page}&pageSize=${pageSize}&conditions=inactiveFlag=true&fields=id,identifier,description,_info`,
|
|
);
|
|
if (response.data.length === 0) break;
|
|
inactiveItems.push(...response.data);
|
|
page++;
|
|
}
|
|
|
|
console.log(`Found ${inactiveItems.length} inactive catalog items`);
|
|
console.log(`Checking inventory for each (batches of 50)...\n`);
|
|
|
|
const withStock: any[] = [];
|
|
const batchSize = 50;
|
|
|
|
for (let i = 0; i < inactiveItems.length; i += batchSize) {
|
|
const batch = inactiveItems.slice(i, i + batchSize);
|
|
await Promise.all(
|
|
batch.map(async (item) => {
|
|
try {
|
|
const res = await connectWiseApi.get(
|
|
`/procurement/catalog/${item.id}/inventory?fields=onHand`,
|
|
);
|
|
const totalOnHand = (res.data as { onHand: number }[]).reduce(
|
|
(sum, e) => sum + (e.onHand || 0),
|
|
0,
|
|
);
|
|
if (totalOnHand > 0) {
|
|
withStock.push({
|
|
id: item.id,
|
|
identifier: item.identifier,
|
|
description: item.description,
|
|
totalOnHand,
|
|
});
|
|
}
|
|
} catch {}
|
|
}),
|
|
);
|
|
const done = Math.min(i + batchSize, inactiveItems.length);
|
|
if (done % 500 === 0 || done === inactiveItems.length) {
|
|
console.log(` ${done}/${inactiveItems.length} checked`);
|
|
}
|
|
}
|
|
|
|
console.log(
|
|
`\nInactive items with inventory: ${withStock.length}/${inactiveItems.length}\n`,
|
|
);
|
|
|
|
if (withStock.length > 0) {
|
|
console.log(JSON.stringify(withStock, null, 2));
|
|
}
|
|
}
|
|
|
|
main().catch(console.error);
|