mirror of
https://github.com/Ravinou/borgwarehouse
synced 2026-03-14 14:25:46 +01:00
refactor: ⚡ updateAppriseAlert API
This commit is contained in:
parent
5ce6e2c19c
commit
8a0a69b7dc
7 changed files with 166 additions and 80 deletions
|
|
@ -4,13 +4,13 @@ import path from 'path';
|
|||
import { authOptions } from '../auth/[...nextauth]';
|
||||
import { getServerSession } from 'next-auth/next';
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { EmailAlert } from '~/types/api/notifications.types';
|
||||
import { EmailAlertDTO } from '~/types/api/notifications.types';
|
||||
import { ErrorResponse } from '~/types/api/error.types';
|
||||
import { BorgWarehouseUser } from '~/types/domain/config.types';
|
||||
|
||||
export default async function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse<EmailAlert | ErrorResponse>
|
||||
res: NextApiResponse<EmailAlertDTO | ErrorResponse>
|
||||
) {
|
||||
if (req.method !== 'GET') {
|
||||
res.status(405).json({ message: 'Bad request on API' });
|
||||
|
|
|
|||
|
|
@ -1,74 +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 == 'PUT') {
|
||||
//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;
|
||||
}
|
||||
|
||||
//The data we expect to receive
|
||||
let { appriseAlert } = req.body;
|
||||
|
||||
//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);
|
||||
|
||||
//1 : control the data
|
||||
if (typeof appriseAlert != 'boolean') {
|
||||
res.status(422).json({ message: 'Unexpected data' });
|
||||
return;
|
||||
}
|
||||
|
||||
//2 : 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;
|
||||
}
|
||||
|
||||
//3 : Change the appriseAlert settings
|
||||
try {
|
||||
//Modify the appriseAlert bool for the user
|
||||
let newUsersList = usersList.map((user) =>
|
||||
user.username == session.user.name ? { ...user, appriseAlert: appriseAlert } : user
|
||||
);
|
||||
//Stringify the new users list
|
||||
newUsersList = JSON.stringify(newUsersList);
|
||||
//Write the new JSON
|
||||
await fs.writeFile(jsonDirectory + '/users.json', newUsersList, (err) => {
|
||||
if (err) console.log(err);
|
||||
});
|
||||
res.status(200).json({ message: 'Successful API send' });
|
||||
} 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' });
|
||||
}
|
||||
}
|
||||
53
pages/api/account/updateAppriseAlert.ts
Normal file
53
pages/api/account/updateAppriseAlert.ts
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
// Imports
|
||||
import { getUsersList, updateUsersList } from '~/helpers/functions/fileHelpers';
|
||||
import { authOptions } from '../auth/[...nextauth]';
|
||||
import { getServerSession } from 'next-auth/next';
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { AppriseAlertDTO, AppriseAlertResponse } from '~/types/api/notifications.types';
|
||||
import { ErrorResponse } from '~/types/api/error.types';
|
||||
|
||||
export default async function handler(
|
||||
req: NextApiRequest & { body: AppriseAlertDTO },
|
||||
res: NextApiResponse<AppriseAlertResponse | ErrorResponse>
|
||||
) {
|
||||
if (req.method !== 'PUT') {
|
||||
return res.status(405);
|
||||
}
|
||||
|
||||
const session = await getServerSession(req, res, authOptions);
|
||||
if (!session) {
|
||||
return res.status(401);
|
||||
}
|
||||
|
||||
const { appriseAlert } = req.body;
|
||||
if (typeof appriseAlert !== 'boolean') {
|
||||
return res.status(422).json({ message: 'Unexpected data' });
|
||||
}
|
||||
|
||||
try {
|
||||
const usersList = await getUsersList();
|
||||
const userIndex = usersList.findIndex((u) => u.username === session.user?.name);
|
||||
|
||||
if (userIndex === -1) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ message: 'User is incorrect. Please, logout to update your session.' });
|
||||
}
|
||||
|
||||
const updatedUsersList = usersList.map((user, index) =>
|
||||
index === userIndex ? { ...user, appriseAlert } : user
|
||||
);
|
||||
|
||||
await updateUsersList(updatedUsersList);
|
||||
return res.status(200).json({ message: 'Successful API send' });
|
||||
} catch (error: any) {
|
||||
console.error(error);
|
||||
return res.status(500).json({
|
||||
status: 500,
|
||||
message:
|
||||
error.code === 'ENOENT'
|
||||
? 'No such file or directory'
|
||||
: 'API error, contact the administrator',
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue