ironGym/webapp/src/app/api/user/actions/settings/route.ts

119 lines
3.7 KiB
TypeScript

import dbConnect from "@/database/dbConnect";
import { NextResponse } from "next/server";
import userModel from "@/database/models/userModel";
// set the revalidate variable
export const revalidate = 5;
// GET METHOD
export async function GET(req:Request)
{
try{
// connect to the db
dbConnect();
// get the settings from the db
let doc = await userModel.findOne({} , 'settings')
// return the success response
return NextResponse.json({
success: true,
message: "requestTerminatedWithSuccess",
data: doc,
}, {
status: 200,
headers: {
"content-type": "application/json"
}
})
}catch(e)
{
// catch any error and return an error response
return NextResponse.json({
success: false,
message: "serverError",
}, {
status: 500,
headers: {
"content-type": "application/json"
}
})
}
}
// PUT METHOD
export async function PUT(req:Request)
{
try{
// connect to the db
dbConnect();
// get the request payload
const { payload } = await req.json();
// get data from formData
const appName : string | null = payload.appName as string | null,
appNameEN : string | null = payload.appNameEN as string | null,
gymName : string | null = payload.gymName as string | null,
email : string | null = payload.email as string | null,
phone : string | null = payload.phone as string | null,
address : string | null = payload.address as string | null,
showLogo : boolean | null = payload.showLogo as boolean | null,
currencySymbol : string | null = payload.currencySymbol as string | null;
let logo : string | null = payload.logo as string | null;
// Validate logo if provided
if (logo && typeof logo === 'string') {
// Check if it's a valid SVG
if (!logo.includes('<svg') || !logo.includes('</svg>')) {
return NextResponse.json({
success: false,
message: "invalidSVGFile",
}, {
status: 400,
headers: {
"content-type": "application/json"
}
})
}
// Remove any script tags for security
const cleanLogo = logo.replace(/<script[^>]*>.*?<\/script>/gi, '');
// Update the logo variable with cleaned content
logo = cleanLogo;
}
// update the doc
let updated_doc = await userModel.updateMany({} , {
$set: {
settings: {
appName,
appNameEN,
gymName,
email,
phone,
address,
showLogo,
logo,
currencySymbol
}
}
}, { new: true })
// return the success response
return NextResponse.json({
success: true,
message: "requestTerminatedWithSuccess",
data: updated_doc,
}, {
status: 200,
headers: {
"content-type": "application/json"
}
})
}catch(e)
{
// catch any error and return an error response
return NextResponse.json({
success: false,
message: "serverError",
}, {
status: 500,
headers: {
"content-type": "application/json"
}
})
}
}