From 313a2f30f9351437d52779bc61dd5540b4614425 Mon Sep 17 00:00:00 2001 From: Ravinou Date: Sun, 16 Feb 2025 20:27:23 +0100 Subject: [PATCH] =?UTF-8?q?refactor:=20=E2=9A=A1=20getAppriseServices=20AP?= =?UTF-8?q?I?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/api/account/getAppriseServices.js | 57 ------------------------- pages/api/account/getAppriseServices.ts | 54 +++++++++++++++++++++++ 2 files changed, 54 insertions(+), 57 deletions(-) delete mode 100644 pages/api/account/getAppriseServices.js create mode 100644 pages/api/account/getAppriseServices.ts diff --git a/pages/api/account/getAppriseServices.js b/pages/api/account/getAppriseServices.js deleted file mode 100644 index c02994a..0000000 --- a/pages/api/account/getAppriseServices.js +++ /dev/null @@ -1,57 +0,0 @@ -//Lib -import { promises as fs } from 'fs'; -import path from 'path'; -import { authOptions } from '../auth/[...nextauth]'; -import { getServerSession } from 'next-auth/next'; - -export default async function handler(req, res) { - if (req.method == 'GET') { - //Verify that the user is logged in. - const session = await getServerSession(req, res, authOptions); - if (!session) { - res.status(401).json({ message: 'You must be logged in.' }); - return; - } - try { - //Read the users file - //Find the absolute path of the json directory - const jsonDirectory = path.join(process.cwd(), '/config'); - let usersList = await fs.readFile(jsonDirectory + '/users.json', 'utf8'); - //Parse the usersList - usersList = JSON.parse(usersList); - - //Verify that the user of the session exists - const userIndex = usersList.map((user) => user.username).indexOf(session.user.name); - if (userIndex === -1) { - res.status(400).json({ - message: 'User is incorrect. Please, logout to update your session.', - }); - return; - } else { - //Send the appriseServices array - res.status(200).json({ - appriseServices: usersList[userIndex].appriseServices, - }); - return; - } - } catch (error) { - //Log for backend - console.log(error); - //Log for frontend - if (error.code == 'ENOENT') { - res.status(500).json({ - status: 500, - message: 'No such file or directory', - }); - } else { - res.status(500).json({ - status: 500, - message: 'API error, contact the administrator', - }); - } - return; - } - } else { - res.status(405).json({ message: 'Bad request on API' }); - } -} diff --git a/pages/api/account/getAppriseServices.ts b/pages/api/account/getAppriseServices.ts new file mode 100644 index 0000000..253e1b7 --- /dev/null +++ b/pages/api/account/getAppriseServices.ts @@ -0,0 +1,54 @@ +//Lib +import { promises as fs } from 'fs'; +import path from 'path'; +import { authOptions } from '../auth/[...nextauth]'; +import { getServerSession } from 'next-auth/next'; +import { NextApiRequest, NextApiResponse } from 'next'; +import { AppriseServicesResponse } from '~/types/api/apprise.types'; +import { ErrorResponse } from '~/types/api/error.types'; +import { BorgWarehouseUser } from '~/types/domain/config.types'; + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse +) { + if (req.method !== 'GET') { + res.status(405).json({ message: 'Bad request on API' }); + return; + } + //Verify that the user is logged in. + const session = await getServerSession(req, res, authOptions); + if (!session) { + res.status(401).json({ message: 'You must be logged in.' }); + return; + } + try { + //Read the users file + const jsonDirectory = path.join(process.cwd(), '/config'); + const fileContent = await fs.readFile(jsonDirectory + '/users.json', 'utf8'); + + //Parse the usersList + const usersList: Array = JSON.parse(fileContent); + + //Verify that the user of the session exists + const user = usersList.find((u) => u.username === session.user?.name); + if (!user) { + res.status(400).json({ + message: 'User is incorrect. Please, logout to update your session.', + }); + return; + } + + res.status(200).json({ + appriseServices: user.appriseServices, + }); + } catch (error: any) { + console.log(error); + const errorMessage = + error.code === 'ENOENT' + ? 'No such file or directory' + : 'API error, contact the administrator'; + + res.status(500).json({ status: 500, message: errorMessage }); + } +}