diff --git a/deletions.js b/deletions.js index 844f928..a25acbd 100644 --- a/deletions.js +++ b/deletions.js @@ -17,7 +17,9 @@ module.exports.handleScheduled = async () => { for (const deletion of deletions) { const guild = client.guilds.cache.get(process.env.DISCORD_GUILD); 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); + log.debug(`Got extension for deletion: ${ext}`); if (!ext) { log.error(`Failed to get extension for deletion: ${deletion.discordId}. Something is seriously wrong!`); return; } diff --git a/freepbx.js b/freepbx.js index 2672e13..fd6b393 100644 --- a/freepbx.js +++ b/freepbx.js @@ -10,9 +10,9 @@ class FreepbxManager { * @param {string} config.clientSecret - The client secret for authentication. * @param {Object} config.dbPool - The connection pool for managing database connections. */ - constructor(config) { + constructor(config) { this.client = null; - this.renewClient = async () => { + this.renewClient = async () => { this.client = new FreepbxGqlClient(config.url, { client: { id: config.clientId, @@ -24,7 +24,10 @@ class FreepbxManager { this.pbxCall = async (query, variables) => { try { - return await this.client.request(query, variables); + return await this.client.request(query, variables).catch(err => { + throw err; + + }); } catch (err) { if (err.response && err.response.error && err.response.error.message === "The resource owner or authorization server denied the request.") { await this.renewClient(); @@ -36,21 +39,21 @@ class FreepbxManager { } } - this.pool = config.dbPool; + this.pool = config.dbPool; if (!this.pool) { throw new Error("Connection pool is required"); } - } + } - async getExtension(ext) { + async getExtension(ext) { ext = String(ext); - const query = gql` + const query = gql` query fetchExtension($extensionId: ID!) { fetchExtension(extensionId: $extensionId) { user { @@ -63,15 +66,15 @@ class FreepbxManager { } `; - const variables = { - extensionId: ext.match(/\d+/)[0], - }; + const variables = { + extensionId: ext.match(/\d+/)[0], + }; - return await this.pbxCall(query, variables); - } + return await this.pbxCall(query, variables); + } - async listExtensions() { - const query = gql` + async listExtensions() { + const query = gql` query { fetchAllExtensions { 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); name = String(name); name = name.replace(/[^a-zA-Z0-9\s]/g, ''); - const query = gql` + const query = gql` mutation addExtension($ext: ID!, $name: String!, $vmPassword: String!) { addExtension(input: { extensionId: $ext @@ -107,18 +110,18 @@ class FreepbxManager { } `; - const variables = { - ext, - name, - vmPassword: ext, - }; + const variables = { + ext, + name, + vmPassword: ext, + }; - return await this.pbxCall(query, variables); - } + return await this.pbxCall(query, variables); + } - async deleteExtension(ext) { + async deleteExtension(ext) { ext = String(ext); - const query = gql` + const query = gql` mutation deleteExtension($ext: ID!) { deleteExtension(input: { extensionId: $ext }) { status @@ -126,17 +129,17 @@ class FreepbxManager { } `; - const variables = { - ext, - }; + const variables = { + ext, + }; const fpbxQuery = this.pbxCall(query, variables); 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() { - const query = gql` + async reload() { + const query = gql` mutation { doreload(input: { clientMutationId: "${Math.random().toString(36).substring(2, 14)}" }) { status @@ -144,11 +147,11 @@ class FreepbxManager { } `; - return await this.pbxCall(query); - } + return await this.pbxCall(query); + } - // async updateName(ext, name) { - // const query = gql` + // async updateName(ext, name) { + // const query = gql` // mutation updateName($ext: ID!, $name: String!) { // updateExtension(input: {extensionId: $ext, name: $name}) { // status, @@ -156,13 +159,13 @@ class FreepbxManager { // } // }`; - // const variables = { - // ext, - // name, - // }; + // const variables = { + // ext, + // name, + // }; - // return await this.pbxCall(query, variables); - // } + // return await this.pbxCall(query, variables); + // } // TODO: Implement updateName method, Current implementation resets extension for some reason async joinPageGroup(ext, pageGroup) {