diff --git a/webapp/src/app/api/user/actions/workers/route.ts b/webapp/src/app/api/user/actions/workers/route.ts index 57e4c76..b3483ec 100644 --- a/webapp/src/app/api/user/actions/workers/route.ts +++ b/webapp/src/app/api/user/actions/workers/route.ts @@ -1,6 +1,7 @@ import dbConnect from "@/database/dbConnect"; import { NextResponse } from "next/server"; import workerModel from "@/database/models/workerModel"; +import mongoose from 'mongoose'; // POST METHOD export async function POST(req:Request) @@ -96,7 +97,7 @@ export async function GET(req:Request) { try{ // connect to the db - dbConnect(); + await dbConnect(); // get the parms const { searchParams } = new URL(req.url) const type = searchParams.get('type') @@ -124,7 +125,7 @@ export async function GET(req:Request) else if(type == 'search') { // get the searchKeyword param - let searchKeyword = searchParams.get('searchKeyword') + let searchKeyword = searchParams.get('searchKeyword') || ''; // get the search results docs let results = await workerModel.find({ $or: [ @@ -144,13 +145,28 @@ export async function GET(req:Request) return NextResponse.json({ success: true, message: "docsGetedSuccessfully", - data: responseData ? responseData : [], - } , { + data: responseData, + }, { status: 200, + headers: { + "content-type": "application/json" + } }) + } else { + // Handle case when type is not provided or is invalid + return NextResponse.json({ + success: false, + message: "invalidRequestType", + }, { + status: 400, + headers: { + "content-type": "application/json" + } + }); } }catch(e) { + console.error("Error in workers GET:", e); // catch any error and return an error response return NextResponse.json({ success: false, @@ -168,14 +184,40 @@ export async function DELETE(req:Request) { try{ // connect to the db - dbConnect(); + await dbConnect(); // get the parms const { searchParams } = new URL(req.url) const page = searchParams.get('page'), _id : string | null = searchParams.get('_id'), range : number[] = [(parseInt(page?page : '0') - 1) * 4 , 4]; + + if (!_id) { + return NextResponse.json({ + success: false, + message: "missingId", + }, { + status: 400, + headers: { + "content-type": "application/json" + } + }); + } + // delete the doc - await workerModel.findByIdAndRemove(_id) + const deleteResult = await workerModel.findByIdAndRemove(_id); + + if (!deleteResult) { + return NextResponse.json({ + success: false, + message: "workerNotFound", + }, { + status: 404, + headers: { + "content-type": "application/json" + } + }); + } + // get the docs by page const docs = await workerModel.find({}).skip(range[0]).limit(range[1]), // get the size of the docs @@ -183,14 +225,20 @@ export async function DELETE(req:Request) // prepare the return data return NextResponse.json({ success: true, - message: "docsGetedSuccessfully", + message: "workerDeletedSuccessfully", data: { docs, docs_count, } + }, { + status: 200, + headers: { + "content-type": "application/json" + } }) }catch(e) { + console.error("Error in workers DELETE:", e); // catch any error and return an error response return NextResponse.json({ success: false, @@ -208,9 +256,22 @@ export async function PUT(req:Request) { try{ // connect to the db - dbConnect(); + await dbConnect(); // get the request payload const { payload } = await req.json(); + + if (!payload || !payload._id) { + return NextResponse.json({ + success: false, + message: "missingRequiredFields", + }, { + status: 400, + headers: { + "content-type": "application/json" + } + }); + } + // get data from formData const firstName : string | null = payload.firstName as string | null, lastName : string | null = payload.lastName as string | null, @@ -221,8 +282,9 @@ export async function PUT(req:Request) email : string | null = payload.email as string | null, address : string | null = payload.address as string | null, _id : string | null = payload._id as string | null; + // update the doc - let updated_doc = await workerModel.findByIdAndUpdate(_id , { + let updated_doc = await workerModel.findByIdAndUpdate(_id, { firstName, lastName, jobType, @@ -231,7 +293,20 @@ export async function PUT(req:Request) phone, email, address - }, { new: true }) + }, { new: true }); + + if (!updated_doc) { + return NextResponse.json({ + success: false, + message: "workerNotFound", + }, { + status: 404, + headers: { + "content-type": "application/json" + } + }); + } + // return the success response return NextResponse.json({ success: true, @@ -245,6 +320,7 @@ export async function PUT(req:Request) }) }catch(e) { + console.error("Error in workers PUT:", e); // catch any error and return an error response return NextResponse.json({ success: false, diff --git a/webapp/src/redux/features/settings-slice.ts b/webapp/src/redux/features/settings-slice.ts index ccb5523..616328a 100644 --- a/webapp/src/redux/features/settings-slice.ts +++ b/webapp/src/redux/features/settings-slice.ts @@ -33,6 +33,16 @@ type SettingsState = { loadedFirstTime: boolean, } +// Add this to your settings slice +const initialState = { + value: { + authRedirectPage: '/dashboard', + // Add default values for all settings + currencySymbol: '$', // Default currency symbol + // Add other default settings here + } +}; + const initialState = { value: { authRedirectPage: '/dashboard', diff --git a/webapp/src/utils/i18nFallback.ts b/webapp/src/utils/i18nFallback.ts new file mode 100644 index 0000000..139b477 --- /dev/null +++ b/webapp/src/utils/i18nFallback.ts @@ -0,0 +1,16 @@ +/** + * Utility function to safely access translations with fallbacks + */ +export function safeTranslation(t: any, key: string, fallback: string = '') { + try { + const translation = t(key); + // If the translation returns the key itself or contains "MISSING", use fallback + if (translation === key || translation.includes('MISSING')) { + return fallback; + } + return translation; + } catch (error) { + console.warn(`Translation error for key "${key}":`, error); + return fallback; + } + } \ No newline at end of file