From 6b7ee6735952b3a0f928bce9631561df6c719b90 Mon Sep 17 00:00:00 2001 From: JeongSH1 Date: Fri, 8 Dec 2023 12:54:54 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20token=EC=9D=84=20=EC=97=85=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=ED=8A=B8=20=ED=95=98=EB=8A=94=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/save-token.js | 12 ++++++++++-- noti.js | 4 ++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/db/save-token.js b/db/save-token.js index 2a54f04..c1c35fa 100644 --- a/db/save-token.js +++ b/db/save-token.js @@ -1,6 +1,14 @@ import { connection } from "./connection.js"; +import { getTokenByUserId } from "./get-token.js"; export async function saveToken(userId, token) { - const [rows, fields] = await connection.execute('insert into tokens values(?, ?)', [userId, token]); - return rows; + const token = await getTokenByUserId(userId); + + if (!token){ + const [rows, fields] = await connection.execute('insert into tokens values(?, ?)', [userId, token]); + return rows; + } else { + const [rows, fields] = await connection.execute('update tokens set token = ? where userId = ?', [token, userId]); + return rows; + } } \ No newline at end of file diff --git a/noti.js b/noti.js index cb74cbd..a95e401 100644 --- a/noti.js +++ b/noti.js @@ -45,6 +45,7 @@ app.post('/send-notification', async (req, res) => { const { userId, title = '무제', body = 'blank' } = req.body; const token = await getTokenByUserId(userId); + console.log(userId, title, body, token); const message = { notification: { @@ -63,6 +64,9 @@ app.post('/send-notification', async (req, res) => { console.error('Error sending message:', error); res.status(500).send('Error sending notification'); }); + + + }); app.listen(port, () => {