Skip to content

Commit

Permalink
Merge pull request #70 from ridabensalem/fitchprofile
Browse files Browse the repository at this point in the history
Fitchprofile
  • Loading branch information
MustaphaJAM committed Jun 12, 2024
2 parents b88f2cf + 4234127 commit c5b68e9
Show file tree
Hide file tree
Showing 12 changed files with 21,672 additions and 7,327 deletions.
5 changes: 3 additions & 2 deletions app/actions/getUserProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ export default async function getUserProfile() {
id: currentUser.id,
},
select: {
name: true,
email: true,
username: true,
createdAt: true,
image: true
},
});

Expand Down
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' });
}
94 changes: 94 additions & 0 deletions app/api/profile/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { NextResponse } from "next/server";
import prisma from "@/app/libs/prismadb";
import getCurrentUser from "@/app/actions/getCurrentUser";

// Handler for the PUT request
export async function PUT(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();
}

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

// Destructure the image URL from the request body
const { image } = body;

// Update the user's profile image in the database
const user = await prisma.user.update({
where: { id: currentUser.id },
data: { image },
});

// Return the updated user profile as a JSON response
return NextResponse.json(user);
} catch (error) {
console.error('Error updating profile image:', error);
return NextResponse.error();
}
}


// Handler for the GET request
export async function GET(request: Request) {
try {
const currentUser = await getCurrentUser();

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

const userProfile = await prisma.user.findUnique({
where: { id: currentUser.id },
select: {
username: true,
name: true,
surname: true,
email: true,
telephone: true,
dateOfBirth: true,
gender: true,
createdAt: true,
image: true,
},
});

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

return NextResponse.json(userProfile);
} catch (e) {
console.error(e);
return NextResponse.error();
}
}
// Handler for the DELETE request
export async function DELETE(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();
}

// Update the user's profile image to null in the database
const user = await prisma.user.update({
where: { id: currentUser.id },
data: { image: null },
});

// Return the updated user profile as a JSON response
return NextResponse.json(user);
} catch (error) {
console.error('Error deleting profile image:', error);
return NextResponse.error();
}
}
69 changes: 69 additions & 0 deletions app/api/profile/security/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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();
}
// Prepare the update data object
const updateData: any = {};

// If password is provided, hash it and add to update data
if (password) {
updateData.hashedPassword = await bcrypt.hash(password, 12);
}
// If recoveryEmail is provided, add to update data
if (recoveryEmail) {
updateData.recoveryEmail = recoveryEmail;
}

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

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 });
}
}
11 changes: 6 additions & 5 deletions app/components/elementsUi/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ interface AvatarProps {

const Avatar: React.FC<AvatarProps> = ({ src }) => {
return (
<Image
className="rounded-full"
height="30"
width="30"
alt="Avatar"
<img
className="rounded-full object-cover"
style={{ height: '30px', width: '30px' }}
height="30"
width="30"
alt="Avatar"
src={src || '/images/placeholder.jpg'}
/>
);
Expand Down
Loading

0 comments on commit c5b68e9

Please sign in to comment.