From 66f86ca2a470ff0cfa832c4850b2533f29a84e26 Mon Sep 17 00:00:00 2001 From: Malte <42342921+ccev@users.noreply.github.com> Date: Wed, 11 Sep 2024 23:19:19 +0200 Subject: [PATCH 1/2] suppressMessage parameter for tracking endpoints --- src/routes/apiTrackingEgg.js | 12 ++++++++---- src/routes/apiTrackingGym.js | 12 ++++++++---- src/routes/apiTrackingInvasion.js | 12 ++++++++---- src/routes/apiTrackingLure.js | 12 ++++++++---- src/routes/apiTrackingMonster.js | 12 ++++++++---- src/routes/apiTrackingNest.js | 12 ++++++++---- src/routes/apiTrackingQuest.js | 12 ++++++++---- src/routes/apiTrackingRaid.js | 12 ++++++++---- 8 files changed, 64 insertions(+), 32 deletions(-) diff --git a/src/routes/apiTrackingEgg.js b/src/routes/apiTrackingEgg.js index 1e88c196d..238bdb413 100644 --- a/src/routes/apiTrackingEgg.js +++ b/src/routes/apiTrackingEgg.js @@ -152,6 +152,8 @@ module.exports = async (fastify, options) => { } } + if (req.query.suppressMessage) message = '' + await fastify.query.deleteWhereInQuery( 'egg', { @@ -180,10 +182,12 @@ module.exports = async (fastify, options) => { language, }] - data.forEach((job) => { - if (['discord:user', 'discord:channel', 'webhook'].includes(job.type)) fastify.discordQueue.push(job) - if (['telegram:user', 'telegram:channel'].includes(job.type)) fastify.telegramQueue.push(job) - }) + if (message) { + data.forEach((job) => { + if (['discord:user', 'discord:channel', 'webhook'].includes(job.type)) fastify.discordQueue.push(job) + if (['telegram:user', 'telegram:channel'].includes(job.type)) fastify.telegramQueue.push(job) + }) + } return { status: 'ok', diff --git a/src/routes/apiTrackingGym.js b/src/routes/apiTrackingGym.js index 97694c8b1..e5de2302b 100644 --- a/src/routes/apiTrackingGym.js +++ b/src/routes/apiTrackingGym.js @@ -150,6 +150,8 @@ module.exports = async (fastify, options) => { } } + if (req.query.suppressMessage) message = '' + await fastify.query.deleteWhereInQuery( 'gym', { @@ -178,10 +180,12 @@ module.exports = async (fastify, options) => { language, }] - data.forEach((job) => { - if (['discord:user', 'discord:channel', 'webhook'].includes(job.type)) fastify.discordQueue.push(job) - if (['telegram:user', 'telegram:channel'].includes(job.type)) fastify.telegramQueue.push(job) - }) + if (message) { + data.forEach((job) => { + if (['discord:user', 'discord:channel', 'webhook'].includes(job.type)) fastify.discordQueue.push(job) + if (['telegram:user', 'telegram:channel'].includes(job.type)) fastify.telegramQueue.push(job) + }) + } return { status: 'ok', diff --git a/src/routes/apiTrackingInvasion.js b/src/routes/apiTrackingInvasion.js index 51e1214f1..b3891e32f 100644 --- a/src/routes/apiTrackingInvasion.js +++ b/src/routes/apiTrackingInvasion.js @@ -143,6 +143,8 @@ module.exports = async (fastify, options) => { } } + if (req.query.suppressMessage) message = '' + await fastify.query.deleteWhereInQuery( 'invasion', { @@ -171,10 +173,12 @@ module.exports = async (fastify, options) => { language, }] - data.forEach((job) => { - if (['discord:user', 'discord:channel', 'webhook'].includes(job.type)) fastify.discordQueue.push(job) - if (['telegram:user', 'telegram:channel'].includes(job.type)) fastify.telegramQueue.push(job) - }) + if (message) { + data.forEach((job) => { + if (['discord:user', 'discord:channel', 'webhook'].includes(job.type)) fastify.discordQueue.push(job) + if (['telegram:user', 'telegram:channel'].includes(job.type)) fastify.telegramQueue.push(job) + }) + } return { status: 'ok', diff --git a/src/routes/apiTrackingLure.js b/src/routes/apiTrackingLure.js index d185b8292..3b6b0efc0 100644 --- a/src/routes/apiTrackingLure.js +++ b/src/routes/apiTrackingLure.js @@ -143,6 +143,8 @@ module.exports = async (fastify, options) => { } } + if (req.query.suppressMessage) message = '' + await fastify.query.deleteWhereInQuery( 'lures', { @@ -171,10 +173,12 @@ module.exports = async (fastify, options) => { language, }] - data.forEach((job) => { - if (['discord:user', 'discord:channel', 'webhook'].includes(job.type)) fastify.discordQueue.push(job) - if (['telegram:user', 'telegram:channel'].includes(job.type)) fastify.telegramQueue.push(job) - }) + if (message) { + data.forEach((job) => { + if (['discord:user', 'discord:channel', 'webhook'].includes(job.type)) fastify.discordQueue.push(job) + if (['telegram:user', 'telegram:channel'].includes(job.type)) fastify.telegramQueue.push(job) + }) + } return { status: 'ok', diff --git a/src/routes/apiTrackingMonster.js b/src/routes/apiTrackingMonster.js index aa694fd73..535fd1c2c 100644 --- a/src/routes/apiTrackingMonster.js +++ b/src/routes/apiTrackingMonster.js @@ -208,6 +208,8 @@ module.exports = async (fastify, options) => { } } + if (req.query.suppressMessage) message = '' + // await fastify.query.deleteWhereInQuery('monsters', { // id, // profile_no: currentProfileNo, @@ -239,10 +241,12 @@ module.exports = async (fastify, options) => { language, }] - data.forEach((job) => { - if (['discord:user', 'discord:channel', 'webhook'].includes(job.type)) fastify.discordQueue.push(job) - if (['telegram:user', 'telegram:channel'].includes(job.type)) fastify.telegramQueue.push(job) - }) + if (message) { + data.forEach((job) => { + if (['discord:user', 'discord:channel', 'webhook'].includes(job.type)) fastify.discordQueue.push(job) + if (['telegram:user', 'telegram:channel'].includes(job.type)) fastify.telegramQueue.push(job) + }) + } if (fastify.triggerReloadAlerts) fastify.triggerReloadAlerts() diff --git a/src/routes/apiTrackingNest.js b/src/routes/apiTrackingNest.js index f88c0ec20..953702701 100644 --- a/src/routes/apiTrackingNest.js +++ b/src/routes/apiTrackingNest.js @@ -139,6 +139,8 @@ module.exports = async (fastify, options) => { } } + if (req.query.suppressMessage) message = '' + await fastify.query.deleteWhereInQuery( 'nests', { @@ -167,10 +169,12 @@ module.exports = async (fastify, options) => { language, }] - data.forEach((job) => { - if (['discord:user', 'discord:channel', 'webhook'].includes(job.type)) fastify.discordQueue.push(job) - if (['telegram:user', 'telegram:channel'].includes(job.type)) fastify.telegramQueue.push(job) - }) + if (message) { + data.forEach((job) => { + if (['discord:user', 'discord:channel', 'webhook'].includes(job.type)) fastify.discordQueue.push(job) + if (['telegram:user', 'telegram:channel'].includes(job.type)) fastify.telegramQueue.push(job) + }) + } return { status: 'ok', diff --git a/src/routes/apiTrackingQuest.js b/src/routes/apiTrackingQuest.js index 95be98a9f..4d7b7492f 100644 --- a/src/routes/apiTrackingQuest.js +++ b/src/routes/apiTrackingQuest.js @@ -146,6 +146,8 @@ module.exports = async (fastify, options) => { } } + if (req.query.suppressMessage) message = '' + await fastify.query.deleteWhereInQuery( 'quest', { @@ -174,10 +176,12 @@ module.exports = async (fastify, options) => { language, }] - data.forEach((job) => { - if (['discord:user', 'discord:channel', 'webhook'].includes(job.type)) fastify.discordQueue.push(job) - if (['telegram:user', 'telegram:channel'].includes(job.type)) fastify.telegramQueue.push(job) - }) + if (message) { + data.forEach((job) => { + if (['discord:user', 'discord:channel', 'webhook'].includes(job.type)) fastify.discordQueue.push(job) + if (['telegram:user', 'telegram:channel'].includes(job.type)) fastify.telegramQueue.push(job) + }) + } return { status: 'ok', diff --git a/src/routes/apiTrackingRaid.js b/src/routes/apiTrackingRaid.js index 0fd6a57c6..50bcec35b 100644 --- a/src/routes/apiTrackingRaid.js +++ b/src/routes/apiTrackingRaid.js @@ -158,6 +158,8 @@ module.exports = async (fastify, options) => { } } + if (req.query.suppressMessage) message = '' + await fastify.query.deleteWhereInQuery( 'raid', { @@ -186,10 +188,12 @@ module.exports = async (fastify, options) => { language, }] - data.forEach((job) => { - if (['discord:user', 'discord:channel', 'webhook'].includes(job.type)) fastify.discordQueue.push(job) - if (['telegram:user', 'telegram:channel'].includes(job.type)) fastify.telegramQueue.push(job) - }) + if (message) { + data.forEach((job) => { + if (['discord:user', 'discord:channel', 'webhook'].includes(job.type)) fastify.discordQueue.push(job) + if (['telegram:user', 'telegram:channel'].includes(job.type)) fastify.telegramQueue.push(job) + }) + } return { status: 'ok', From 1b8b6e448db55d4a3b258d7733347b679b1373f5 Mon Sep 17 00:00:00 2001 From: Malte <42342921+ccev@users.noreply.github.com> Date: Tue, 17 Sep 2024 22:39:19 +0200 Subject: [PATCH 2/2] more parameters in POST tracking responses --- .gitignore | 1 + src/controllers/controller.js | 8 ++++++-- src/controllers/query.js | 8 ++++++-- src/routes/apiTrackingEgg.js | 6 +++++- src/routes/apiTrackingGym.js | 6 +++++- src/routes/apiTrackingInvasion.js | 6 +++++- src/routes/apiTrackingLure.js | 6 +++++- src/routes/apiTrackingMonster.js | 10 +++++++++- src/routes/apiTrackingNest.js | 6 +++++- src/routes/apiTrackingQuest.js | 6 +++++- src/routes/apiTrackingRaid.js | 6 +++++- 11 files changed, 57 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index f28397541..e0a0572b7 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ docker-compose.yml .idea/* .vscode *.heapsnapshot +/src/util/translations.json nominatimData/* !nominatimData/.gitkeep diff --git a/src/controllers/controller.js b/src/controllers/controller.js index 6df472859..3f3e327f8 100644 --- a/src/controllers/controller.js +++ b/src/controllers/controller.js @@ -482,10 +482,14 @@ class Controller extends EventEmitter { } } - async insertQuery(table, values) { + async insertQuery(table, values, returning = undefined) { if (Array.isArray(values) && !values.length) return try { - return await this.db.insert(values).into(table) + const statement = this.db.insert(values).into(table) + if (returning) { + statement.returning(returning) + } + return await statement } catch (err) { throw { source: 'insertQuery', error: err } } diff --git a/src/controllers/query.js b/src/controllers/query.js index 3edcf9cdd..9d0f9d953 100644 --- a/src/controllers/query.js +++ b/src/controllers/query.js @@ -150,10 +150,14 @@ class Query { } } - async insertQuery(table, values) { + async insertQuery(table, values, returning = undefined) { if (Array.isArray(values) && !values.length) return try { - return await this.db.insert(values).into(table) + const statement = this.db.insert(values).into(table) + if (returning) { + statement.returning(returning) + } + return await statement } catch (err) { throw { source: 'insertQuery', error: err } } diff --git a/src/routes/apiTrackingEgg.js b/src/routes/apiTrackingEgg.js index 238bdb413..d7e6c6fab 100644 --- a/src/routes/apiTrackingEgg.js +++ b/src/routes/apiTrackingEgg.js @@ -164,7 +164,7 @@ module.exports = async (fastify, options) => { 'uid', ) - await fastify.query.insertQuery('egg', [...insert, ...updates]) + const newUids = await fastify.query.insertQuery('egg', [...insert, ...updates], 'uid') // Send message to user @@ -192,6 +192,10 @@ module.exports = async (fastify, options) => { return { status: 'ok', message, + newUids, + alreadyPresent: alreadyPresent.length, + updates: updates.length, + insert: insert.length, } } catch (err) { fastify.logger.error(`API: ${req.ip} ${req.routeOptions.method} ${req.routeOptions.url}`, err) diff --git a/src/routes/apiTrackingGym.js b/src/routes/apiTrackingGym.js index e5de2302b..bc33b9f6d 100644 --- a/src/routes/apiTrackingGym.js +++ b/src/routes/apiTrackingGym.js @@ -162,7 +162,7 @@ module.exports = async (fastify, options) => { 'uid', ) - await fastify.query.insertQuery('gym', [...insert, ...updates]) + const newUids = await fastify.query.insertQuery('gym', [...insert, ...updates], 'uid') // Send message to user @@ -190,6 +190,10 @@ module.exports = async (fastify, options) => { return { status: 'ok', message, + newUids, + alreadyPresent: alreadyPresent.length, + updates: updates.length, + insert: insert.length, } } catch (err) { fastify.logger.error(`API: ${req.ip} ${req.routeOptions.method} ${req.routeOptions.url}`, err) diff --git a/src/routes/apiTrackingInvasion.js b/src/routes/apiTrackingInvasion.js index b3891e32f..fa78d6f32 100644 --- a/src/routes/apiTrackingInvasion.js +++ b/src/routes/apiTrackingInvasion.js @@ -155,7 +155,7 @@ module.exports = async (fastify, options) => { 'uid', ) - await fastify.query.insertQuery('invasion', [...insert, ...updates]) + const newUids = await fastify.query.insertQuery('invasion', [...insert, ...updates], 'uid') // Send message to user @@ -183,6 +183,10 @@ module.exports = async (fastify, options) => { return { status: 'ok', message, + newUids, + alreadyPresent: alreadyPresent.length, + updates: updates.length, + insert: insert.length, } } catch (err) { fastify.logger.error(`API: ${req.ip} ${req.routeOptions.method} ${req.routeOptions.url}`, err) diff --git a/src/routes/apiTrackingLure.js b/src/routes/apiTrackingLure.js index 3b6b0efc0..138216681 100644 --- a/src/routes/apiTrackingLure.js +++ b/src/routes/apiTrackingLure.js @@ -155,7 +155,7 @@ module.exports = async (fastify, options) => { 'uid', ) - await fastify.query.insertQuery('lures', [...insert, ...updates]) + const newUids = await fastify.query.insertQuery('lures', [...insert, ...updates], 'uid') // Send message to user @@ -183,6 +183,10 @@ module.exports = async (fastify, options) => { return { status: 'ok', message, + newUids, + alreadyPresent: alreadyPresent.length, + updates: updates.length, + insert: insert.length, } } catch (err) { fastify.logger.error(`API: ${req.ip} ${req.routeOptions.method} ${req.routeOptions.url}`, err) diff --git a/src/routes/apiTrackingMonster.js b/src/routes/apiTrackingMonster.js index 535fd1c2c..3dbdd8b9b 100644 --- a/src/routes/apiTrackingMonster.js +++ b/src/routes/apiTrackingMonster.js @@ -218,11 +218,15 @@ module.exports = async (fastify, options) => { // 'uid') // await fastify.query.insertQuery('monsters', [...insert, ...updates]) + let newUids = [] + if (insert.length) { - await fastify.query.insertQuery('monsters', insert) + const result = await fastify.query.insertQuery('monsters', insert, 'uid') + if (result) newUids = [...newUids, ...result] } for (const row of updates) { await fastify.query.updateQuery('monsters', row, { uid: row.uid }) + newUids.push(row.uid) } // Send message to user @@ -253,6 +257,10 @@ module.exports = async (fastify, options) => { return { status: 'ok', message, + newUids, + alreadyPresent: alreadyPresent.length, + updates: updates.length, + insert: insert.length, } } catch (err) { fastify.logger.error(`API: ${req.ip} ${req.routeOptions.method} ${req.routeOptions.url}`, err) diff --git a/src/routes/apiTrackingNest.js b/src/routes/apiTrackingNest.js index 953702701..bf9657b12 100644 --- a/src/routes/apiTrackingNest.js +++ b/src/routes/apiTrackingNest.js @@ -151,7 +151,7 @@ module.exports = async (fastify, options) => { 'uid', ) - await fastify.query.insertQuery('nests', [...insert, ...updates]) + const newUids = await fastify.query.insertQuery('nests', [...insert, ...updates], 'uid') // Send message to user @@ -179,6 +179,10 @@ module.exports = async (fastify, options) => { return { status: 'ok', message, + newUids, + alreadyPresent: alreadyPresent.length, + updates: updates.length, + insert: insert.length, } } catch (err) { fastify.logger.error(`API: ${req.ip} ${req.routeOptions.method} ${req.routeOptions.url}`, err) diff --git a/src/routes/apiTrackingQuest.js b/src/routes/apiTrackingQuest.js index 4d7b7492f..81542c648 100644 --- a/src/routes/apiTrackingQuest.js +++ b/src/routes/apiTrackingQuest.js @@ -158,7 +158,7 @@ module.exports = async (fastify, options) => { 'uid', ) - await fastify.query.insertQuery('quest', [...insert, ...updates]) + const newUids = await fastify.query.insertQuery('quest', [...insert, ...updates], 'uid') // Send message to user @@ -186,6 +186,10 @@ module.exports = async (fastify, options) => { return { status: 'ok', message, + newUids, + alreadyPresent: alreadyPresent.length, + updates: updates.length, + insert: insert.length, } } catch (err) { fastify.logger.error(`API: ${req.ip} ${req.routeOptions.method} ${req.routeOptions.url}`, err) diff --git a/src/routes/apiTrackingRaid.js b/src/routes/apiTrackingRaid.js index 50bcec35b..999b533f3 100644 --- a/src/routes/apiTrackingRaid.js +++ b/src/routes/apiTrackingRaid.js @@ -170,7 +170,7 @@ module.exports = async (fastify, options) => { 'uid', ) - await fastify.query.insertQuery('raid', [...insert, ...updates]) + const newUids = await fastify.query.insertQuery('raid', [...insert, ...updates], 'uid') // Send message to user @@ -198,6 +198,10 @@ module.exports = async (fastify, options) => { return { status: 'ok', message, + newUids, + alreadyPresent: alreadyPresent.length, + updates: updates.length, + insert: insert.length, } } catch (err) { fastify.logger.error(`API: ${req.ip} ${req.routeOptions.method} ${req.routeOptions.url}`, err)