Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud-Lyard committed Aug 13, 2024
2 parents e9ca705 + 3d18719 commit fbf119a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions client/src/app/[lang]/(user)/user/profile/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export default function Profile({
.push<IResponse, any>(`users/update`, formData, undefined, true);
if (response.status === 'success') {
setMessage(response.message);
location.reload();
}
} catch (e: any) {
checkErrors(e.response.data);
Expand Down
25 changes: 21 additions & 4 deletions server/src/user/service/user.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Prisma, PrismaClient, User } from '@prisma/client';
import fs from 'fs-extra';
import { IUser } from '../../types/user';
import AppError from '../../utils/appError';
import { signJwt } from '../../utils/jwt';
import { IUserUpdateDto, UserDto } from '../dto/user.dto';
import { UserRepository } from '../repository/user.repository';
import { File } from '../../types/file';
import { removeExistingImages } from '../../utils/removeExistingImages';
export const createUser = async (user: UserDto) => {
return await UserRepository.createUser(user);
};
Expand Down Expand Up @@ -117,11 +117,28 @@ export async function updateUser({
avatar: null,
};
try {
if (!avatar) {
const userHasImage = await UserRepository.findByUserId(user.id);

if (!avatar && !userHasImage!.avatar) {
userUpdate.avatar = user.avatar;
await UserRepository.updateUser(userUpdate);
}
// TODO: Logic if user change avatar

if (!avatar && userHasImage!.avatar) {
userUpdate.avatar = userHasImage!.avatar;
}

if (avatar && !userHasImage!.avatar) {
userUpdate.avatar = avatar.filename;
}

if (avatar && userHasImage!.avatar) {
userUpdate.avatar = avatar.filename;
await removeExistingImages({
filename: userHasImage!.avatar,
environment: process.env.NODE_ENV,
});
}
await UserRepository.updateUser(userUpdate);
} catch (err: any) {
console.error(err);
throw new AppError(400, 'Erreur lors de la mise à jour du profil.');
Expand Down
18 changes: 18 additions & 0 deletions server/src/utils/removeExistingImages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import fs from 'fs-extra';

export const removeExistingImages = async ({
filename,
environment,
}: {
filename: string;
environment: string;
}) => {
if (environment === 'production') {
const path = `dist/public/uploads/${filename}`;
await fs.unlink(path);
}
if (environment === 'development') {
const path = `public/uploads/${filename}`;
await fs.unlink(path);
}
};

0 comments on commit fbf119a

Please sign in to comment.