refactor: getAppriseAlert API

This commit is contained in:
Ravinou 2025-02-15 22:11:14 +01:00
commit 7aa47195f1
No known key found for this signature in database
GPG key ID: EEEE670C40F6A4D7
5 changed files with 68 additions and 57 deletions

View file

@ -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 appriseAlert bool
res.status(200).json({
appriseAlert: usersList[userIndex].appriseAlert,
});
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' });
}
}

View file

@ -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 { BorgWarehouseUser } from '~/types/domain/config.types';
import { AppriseAlertResponse } from '~/types/api/apprise.types';
import { ErrorResponse } from '~/types/api/error.types';
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<AppriseAlertResponse | ErrorResponse>
) {
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<BorgWarehouseUser> = 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({ appriseAlert: user.appriseAlert });
} 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 });
}
}

View file

@ -8,3 +8,7 @@ export type AppriseModeResponse = {
export type AppriseServicesResponse = {
appriseServices?: string[];
};
export type AppriseAlertResponse = {
appriseAlert?: boolean;
};

4
types/api/error.types.ts Normal file
View file

@ -0,0 +1,4 @@
export type ErrorResponse = {
status?: number;
message: string;
};

View file

@ -1,3 +1,5 @@
import { IntegrationTokenType } from '../api/integrations.types';
export type Repository = {
id: number;
alias: string;
@ -23,6 +25,10 @@ export type BorgWarehouseUser = {
email: string;
emailAlert?: boolean;
appriseAlert?: boolean;
appriseMode?: AppriseModeEnum;
appriseStatelessURL?: string;
appriseServices?: string[];
tokens?: Array<IntegrationTokenType>;
};
export enum WizardEnvEnum {