Skip to content

Commit

Permalink
Merge branch 'profile' of https://github.com/ridabensalem/tour-trek i…
Browse files Browse the repository at this point in the history
…nto profile
  • Loading branch information
fatimzzahra committed Jun 7, 2024
2 parents 973601e + 08d3483 commit f3ac0ec
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 1 deletion.
43 changes: 43 additions & 0 deletions app/api/profile/perfernces/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { NextResponse } from 'next/server';
import prisma from "@/app/libs/prismadb";
import getCurrentUser from "@/app/actions/getCurrentUser";

// Handler for GET request to retrieve preferences
export async function GET() {
const currentUser = await getCurrentUser();

if (!currentUser) {
return NextResponse.error();
}

// Retrieve user preferences from the database
const user = await prisma.user.findUnique({
where: { id: currentUser.id },
select: {
currency: true,
language: true,
},
});

return NextResponse.json(user);
}

// Handler for POST request to save preferences
export async function POST(request: Request) {
const currentUser = await getCurrentUser();

if (!currentUser) {
return NextResponse.error();
}

const body = await request.json();
const { currency, language } = body;

// Save preferences to the database
await prisma.user.update({
where: { id: currentUser.id },
data: { currency, language },
});

return NextResponse.json({ message: 'Preferences saved successfully' });
}
65 changes: 65 additions & 0 deletions app/api/profile/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { NextResponse } from "next/server";
import prisma from "@/app/libs/prismadb";
import getCurrentUser from "@/app/actions/getCurrentUser";

// Handler for the PUT
export async function PUT(request: Request) {
// Get the current user
const currentUser = await getCurrentUser();

// If there is no current user, return an error response
if (!currentUser) {
return NextResponse.error();
}

// Parse the request body as JSON
const body = await request.json();

// Destructure the required properties from the request body
const {
name,
surname,
username,
dateOfBirth,
email,
telephone,
gender,
image
} = body;

// Update the user's favoriteIds in the database
const user = await prisma.user.update({
where:{id: currentUser.id},
data: {
name,
surname,
username,
dateOfBirth,
email,
telephone,
gender,
image
}
});

// Return the created listing as a JSON response
return NextResponse.json(user);
}
// Handler for the GET request
export async function GET(request: Request) {
try {
// Get the current user
const currentUser = await getCurrentUser();

// If there is no current user, return an error response
if (!currentUser) {
return NextResponse.error();
}

// Return the user data as a JSON response
return NextResponse.json(currentUser);
} catch (error) {
console.error(error);
return NextResponse.error();
}
}
59 changes: 59 additions & 0 deletions app/api/profile/security/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { NextResponse } from "next/server";
import prisma from "@/app/libs/prismadb";
import bcrypt from "bcrypt";
import getCurrentUser from "@/app/actions/getCurrentUser";


export async function PUT(request: Request) {
try {
const body = await request.json();

const { password , recoveryEmail } = body;

// Get the current user
const currentUser = await getCurrentUser();

// If no user is found, return an error response
if (!currentUser) {
return NextResponse.error();
}
const hashedPassword = await bcrypt.hash(password, 12);

const user = await prisma.user.update({
where: {
id: currentUser.id
},
data: { hashedPassword, recoveryEmail },
});

return NextResponse.json(user);
} catch (error) {
console.error(error);
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
}
}
//Delete user account endpoint
export async function DELETE(request: Request) {
try {
// Get the current user
const currentUser = await getCurrentUser();

// If no user is found, return an error response
if (!currentUser) {
return NextResponse.error();
}

// Delete the user from the database
await prisma.user.delete({
where: {
id: currentUser.id
}
});

// Return a success response
return NextResponse.json({ message: "User account deleted successfully" });
} catch (error) {
console.error(error);
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
}
}
4 changes: 3 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ model User {
email String? @unique
emailVerified DateTime?
telephone Int?
recoveryEmail Int?
recoveryEmail String?
gender String?
image String?
hashedPassword String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
favoriteIds String[] @db.ObjectId
currency String?
language String?
accounts Account[]
listings Listing[]
Expand Down

0 comments on commit f3ac0ec

Please sign in to comment.