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, () => {