diff --git a/src/intervals/departmentPager.ts b/src/intervals/departmentPager.ts index bbf3f1f..38900fb 100644 --- a/src/intervals/departmentPager.ts +++ b/src/intervals/departmentPager.ts @@ -90,6 +90,7 @@ export default function departmentPager(client: Client): NodeJS.Timeout { while (status) { randomPagerNumber = `00${String(Math.floor(Math.random() * 9) + 1)}`; const check = await client.db.PagerNumber.findOne({ num: randomPagerNumber }); + if (check) status = false; if (check?.num !== randomPagerNumber) status = false; } const acknowledgement = resolveStaffInformation(member.id); @@ -100,6 +101,7 @@ export default function departmentPager(client: Client): NodeJS.Timeout { emailAddresses: [acknowledgement.emailAddress], discordIDs: [member.id], }); + if (await client.db.PagerNumber.findOne({ num: randomPagerNumber })) return; pager = await newNumber.save(); client.getDMChannel(member.id).then((chan) => { chan.createMessage(`__**Pager Number Creation**__\nYour individual pager number has been automatically created. Your number (PN) is ${randomPagerNumber}.`); @@ -113,7 +115,7 @@ export default function departmentPager(client: Client): NodeJS.Timeout { while (status) { randomPagerNumber = `01${String(Math.floor(Math.random() * 9) + 1)}`; const check = await client.db.PagerNumber.findOne({ num: randomPagerNumber }); - if (check?.num !== randomPagerNumber) status = false; + if (!check || (check?.num !== randomPagerNumber)) status = true; } const acknowledgement = resolveStaffInformation(member.id); if (!acknowledgement || !acknowledgement.emailAddress) return; @@ -123,6 +125,7 @@ export default function departmentPager(client: Client): NodeJS.Timeout { emailAddresses: [acknowledgement.emailAddress], discordIDs: [member.id], }); + if (await client.db.PagerNumber.findOne({ num: randomPagerNumber })) return; pager = await newNumber.save(); client.getDMChannel(member.id).then((chan) => { chan.createMessage(`__**Pager Number Creation**__\nYour individual pager number has been automatically created. Your number (PN) is ${randomPagerNumber}.`); @@ -137,7 +140,7 @@ export default function departmentPager(client: Client): NodeJS.Timeout { randomPagerNumber = `10${String(Math.floor(Math.random() * 99) + 1)}`; if (randomPagerNumber.length === 3) randomPagerNumber = `${randomPagerNumber}0`; const check = await client.db.PagerNumber.findOne({ num: randomPagerNumber }); - if (check?.num !== randomPagerNumber) status = false; + if (!check || (check?.num !== randomPagerNumber)) status = true; } const acknowledgement = resolveStaffInformation(member.id); if (!acknowledgement || !acknowledgement.emailAddress) return; @@ -147,6 +150,7 @@ export default function departmentPager(client: Client): NodeJS.Timeout { emailAddresses: [acknowledgement.emailAddress], discordIDs: [member.id], }); + if (await client.db.PagerNumber.findOne({ num: randomPagerNumber })) return; pager = await newNumber.save(); client.getDMChannel(member.id).then((chan) => { chan.createMessage(`__**Pager Number Creation**__\nYour individual pager number has been automatically created. Your number (PN) is ${randomPagerNumber}.`); @@ -161,7 +165,7 @@ export default function departmentPager(client: Client): NodeJS.Timeout { randomPagerNumber = `20${String(Math.floor(Math.random() * 99) + 1)}`; if (randomPagerNumber.length === 3) randomPagerNumber = `${randomPagerNumber}0`; const check = await client.db.PagerNumber.findOne({ num: randomPagerNumber }); - if (check?.num !== randomPagerNumber) status = false; + if (!check || (check?.num !== randomPagerNumber)) status = true; } const acknowledgement = resolveStaffInformation(member.id); if (!acknowledgement || !acknowledgement.emailAddress) return; @@ -171,6 +175,7 @@ export default function departmentPager(client: Client): NodeJS.Timeout { emailAddresses: [acknowledgement.emailAddress], discordIDs: [member.id], }); + if (await client.db.PagerNumber.findOne({ num: randomPagerNumber })) return; pager = await newNumber.save(); client.getDMChannel(member.id).then((chan) => { chan.createMessage(`__**Pager Number Creation**__\nYour individual pager number has been automatically created. Your number (PN) is ${randomPagerNumber}.`); @@ -185,7 +190,7 @@ export default function departmentPager(client: Client): NodeJS.Timeout { randomPagerNumber = `21${String(Math.floor(Math.random() * 999) + 1)}`; if (randomPagerNumber.length === 4) randomPagerNumber = `${randomPagerNumber}0`; const check = await client.db.PagerNumber.findOne({ num: randomPagerNumber }); - if (check?.num !== randomPagerNumber) status = false; + if (!check || (check?.num !== randomPagerNumber)) status = true; } const acknowledgement = resolveStaffInformation(member.id); if (!acknowledgement || !acknowledgement.emailAddress) return; @@ -195,6 +200,7 @@ export default function departmentPager(client: Client): NodeJS.Timeout { emailAddresses: [acknowledgement.emailAddress], discordIDs: [member.id], }); + if (await client.db.PagerNumber.findOne({ num: randomPagerNumber })) return; pager = await newNumber.save(); client.getDMChannel(member.id).then((chan) => { chan.createMessage(`__**Pager Number Creation**__\nYour individual pager number has been automatically created. Your number (PN) is ${randomPagerNumber}.`); @@ -209,7 +215,7 @@ export default function departmentPager(client: Client): NodeJS.Timeout { randomPagerNumber = `22${String(Math.floor(Math.random() * 999) + 1)}`; if (randomPagerNumber.length === 4) randomPagerNumber = `${randomPagerNumber}0`; const check = await client.db.PagerNumber.findOne({ num: randomPagerNumber }); - if (check?.num !== randomPagerNumber) status = false; + if (!check || (check?.num !== randomPagerNumber)) status = true; } const acknowledgement = resolveStaffInformation(member.id); if (!acknowledgement || !acknowledgement.emailAddress) return; @@ -219,6 +225,7 @@ export default function departmentPager(client: Client): NodeJS.Timeout { emailAddresses: [acknowledgement.emailAddress], discordIDs: [member.id], }); + if (await client.db.PagerNumber.findOne({ num: randomPagerNumber })) return; pager = await newNumber.save(); client.getDMChannel(member.id).then((chan) => { chan.createMessage(`__**Pager Number Creation**__\nYour individual pager number has been automatically created. Your number (PN) is ${randomPagerNumber}.`);