Remove dupes

This commit is contained in:
Christopher Cookman 2025-05-24 14:14:27 -06:00
parent 33d71ae1a6
commit a7d4a91936

View file

@ -194,33 +194,21 @@ class FreepbxManager {
};
async getNextAvailableExtension() {
console.log("Fetching list of extensions...");
const extList = await this.listExtensions();
console.log("Raw extension list:", JSON.stringify(extList, null, 2));
const exts = extList.fetchAllExtensions.extension;
console.log("Parsed extensions array:", exts);
const startExt = process.env.START_EXT ? parseInt(process.env.START_EXT, 10) : 1000;
console.log("Start extension:", startExt);
const existingExts = exts.map(ext => parseInt(ext.user.extension, 10));
console.log("Existing extensions (parsed as numbers):", existingExts);
existingExts.sort((a, b) => a - b);
console.log("Sorted existing extensions:", existingExts);
// Remove duplicates by using a Set
const existingExtsSet = new Set(exts.map(ext => parseInt(ext.user.extension, 10)));
const existingExts = Array.from(existingExtsSet).sort((a, b) => a - b);
let nextExt = startExt;
for (let i = 0; i < existingExts.length; i++) {
console.log(`Checking if existingExts[${i}] (${existingExts[i]}) === nextExt (${nextExt})`);
if (existingExts[i] !== nextExt) {
console.log(`Found gap at ${nextExt}, breaking loop.`);
break;
}
nextExt++;
}
console.log("Next available extension determined:", nextExt);
return nextExt;
}
}