-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
tgxn
committed
Mar 12, 2024
1 parent
2e1f044
commit 5435c6b
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const { getTopicBySecretKey } = require("../services/topic"); | ||
|
||
const { createPushToTopic, pushToTopicDevices } = require("../services/push"); | ||
|
||
const { appLogger } = require("../middleware/logging.js"); | ||
|
||
const createGrafanaPushRequest = async (request, response, next) => { | ||
try { | ||
console.log("inputPayload", request.body); | ||
let { title, state, message } = request.body; | ||
console.log("inputPayload", { title, state, message }); | ||
|
||
const pushPayload = { | ||
categoryId: "simple.push", | ||
title: `Grafana: ${title}`, | ||
body: message, | ||
}; | ||
|
||
const foundTopic = await getTopicBySecretKey(request.params.topicSecret); | ||
if (!foundTopic) { | ||
return next(new Error("Topic secret key does not exist")); | ||
} | ||
appLogger.debug("foundTopic", foundTopic.dataValues.id); | ||
|
||
const createdPush = await createPushToTopic(foundTopic); | ||
appLogger.info("createPush", createdPush.dataValues); | ||
|
||
// respond early | ||
response | ||
.status(200) | ||
.json({ | ||
success: true, | ||
pushIdent: createdPush.dataValues.pushIdent, | ||
}) | ||
.send(); | ||
|
||
await pushToTopicDevices(foundTopic, createdPush, pushPayload); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
createGrafanaPushRequest, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters