Add debug logging to deletions

This commit is contained in:
Christopher Cookman 2025-02-17 10:06:44 -07:00
parent ef7d13cba1
commit 41e80eebf9
2 changed files with 49 additions and 44 deletions

View file

@ -17,7 +17,9 @@ module.exports.handleScheduled = async () => {
for (const deletion of deletions) { for (const deletion of deletions) {
const guild = client.guilds.cache.get(process.env.DISCORD_GUILD); const guild = client.guilds.cache.get(process.env.DISCORD_GUILD);
const member = guild ? await guild.members.fetch(deletion.discordId).catch(() => null) : null; const member = guild ? await guild.members.fetch(deletion.discordId).catch(() => null) : null;
log.debug(`Checking deletion for ${deletion.discordId}`);
const ext = await fpbx.getExtension(deletion.extension); const ext = await fpbx.getExtension(deletion.extension);
log.debug(`Got extension for deletion: ${ext}`);
if (!ext) { if (!ext) {
log.error(`Failed to get extension for deletion: ${deletion.discordId}. Something is seriously wrong!`); return; log.error(`Failed to get extension for deletion: ${deletion.discordId}. Something is seriously wrong!`); return;
} }

View file

@ -10,9 +10,9 @@ class FreepbxManager {
* @param {string} config.clientSecret - The client secret for authentication. * @param {string} config.clientSecret - The client secret for authentication.
* @param {Object} config.dbPool - The connection pool for managing database connections. * @param {Object} config.dbPool - The connection pool for managing database connections.
*/ */
constructor(config) { constructor(config) {
this.client = null; this.client = null;
this.renewClient = async () => { this.renewClient = async () => {
this.client = new FreepbxGqlClient(config.url, { this.client = new FreepbxGqlClient(config.url, {
client: { client: {
id: config.clientId, id: config.clientId,
@ -24,7 +24,10 @@ class FreepbxManager {
this.pbxCall = async (query, variables) => { this.pbxCall = async (query, variables) => {
try { try {
return await this.client.request(query, variables); return await this.client.request(query, variables).catch(err => {
throw err;
});
} catch (err) { } catch (err) {
if (err.response && err.response.error && err.response.error.message === "The resource owner or authorization server denied the request.") { if (err.response && err.response.error && err.response.error.message === "The resource owner or authorization server denied the request.") {
await this.renewClient(); await this.renewClient();
@ -36,21 +39,21 @@ class FreepbxManager {
} }
} }
this.pool = config.dbPool; this.pool = config.dbPool;
if (!this.pool) { if (!this.pool) {
throw new Error("Connection pool is required"); throw new Error("Connection pool is required");
} }
} }
async getExtension(ext) { async getExtension(ext) {
ext = String(ext); ext = String(ext);
const query = gql` const query = gql`
query fetchExtension($extensionId: ID!) { query fetchExtension($extensionId: ID!) {
fetchExtension(extensionId: $extensionId) { fetchExtension(extensionId: $extensionId) {
user { user {
@ -63,15 +66,15 @@ class FreepbxManager {
} }
`; `;
const variables = { const variables = {
extensionId: ext.match(/\d+/)[0], extensionId: ext.match(/\d+/)[0],
}; };
return await this.pbxCall(query, variables); return await this.pbxCall(query, variables);
} }
async listExtensions() { async listExtensions() {
const query = gql` const query = gql`
query { query {
fetchAllExtensions { fetchAllExtensions {
extension { extension {
@ -84,14 +87,14 @@ class FreepbxManager {
} }
`; `;
return await this.pbxCall(query); return await this.pbxCall(query);
} }
async addExtension(ext, name) { async addExtension(ext, name) {
ext = String(ext); ext = String(ext);
name = String(name); name = String(name);
name = name.replace(/[^a-zA-Z0-9\s]/g, ''); name = name.replace(/[^a-zA-Z0-9\s]/g, '');
const query = gql` const query = gql`
mutation addExtension($ext: ID!, $name: String!, $vmPassword: String!) { mutation addExtension($ext: ID!, $name: String!, $vmPassword: String!) {
addExtension(input: { addExtension(input: {
extensionId: $ext extensionId: $ext
@ -107,18 +110,18 @@ class FreepbxManager {
} }
`; `;
const variables = { const variables = {
ext, ext,
name, name,
vmPassword: ext, vmPassword: ext,
}; };
return await this.pbxCall(query, variables); return await this.pbxCall(query, variables);
} }
async deleteExtension(ext) { async deleteExtension(ext) {
ext = String(ext); ext = String(ext);
const query = gql` const query = gql`
mutation deleteExtension($ext: ID!) { mutation deleteExtension($ext: ID!) {
deleteExtension(input: { extensionId: $ext }) { deleteExtension(input: { extensionId: $ext }) {
status status
@ -126,17 +129,17 @@ class FreepbxManager {
} }
`; `;
const variables = { const variables = {
ext, ext,
}; };
const fpbxQuery = this.pbxCall(query, variables); const fpbxQuery = this.pbxCall(query, variables);
const dbQuery = this.pool.query('DELETE FROM paging_groups WHERE ext = ?', [ext]); const dbQuery = this.pool.query('DELETE FROM paging_groups WHERE ext = ?', [ext]);
return await Promise.all([fpbxQuery, dbQuery]); return await Promise.all([fpbxQuery, dbQuery]);
} }
async reload() { async reload() {
const query = gql` const query = gql`
mutation { mutation {
doreload(input: { clientMutationId: "${Math.random().toString(36).substring(2, 14)}" }) { doreload(input: { clientMutationId: "${Math.random().toString(36).substring(2, 14)}" }) {
status status
@ -144,11 +147,11 @@ class FreepbxManager {
} }
`; `;
return await this.pbxCall(query); return await this.pbxCall(query);
} }
// async updateName(ext, name) { // async updateName(ext, name) {
// const query = gql` // const query = gql`
// mutation updateName($ext: ID!, $name: String!) { // mutation updateName($ext: ID!, $name: String!) {
// updateExtension(input: {extensionId: $ext, name: $name}) { // updateExtension(input: {extensionId: $ext, name: $name}) {
// status, // status,
@ -156,13 +159,13 @@ class FreepbxManager {
// } // }
// }`; // }`;
// const variables = { // const variables = {
// ext, // ext,
// name, // name,
// }; // };
// return await this.pbxCall(query, variables); // return await this.pbxCall(query, variables);
// } // }
// TODO: Implement updateName method, Current implementation resets extension for some reason // TODO: Implement updateName method, Current implementation resets extension for some reason
async joinPageGroup(ext, pageGroup) { async joinPageGroup(ext, pageGroup) {