fix: make user avatar field required

This commit is contained in:
yassinedorbozgithub 2025-01-09 10:44:32 +01:00
parent 3c1f115a7c
commit 8ab2f16c56
7 changed files with 11 additions and 7 deletions

View File

@ -137,6 +137,7 @@ describe('AuthController', () => {
first_name: 'test',
last_name: 'test',
roles: [role!.id],
avatar: null,
};
await invitationService.create(baseUser);
});
@ -157,6 +158,7 @@ describe('AuthController', () => {
email: 'test@test.test',
password: 'test',
roles: ['invalid role value'],
avatar: null,
};
await expect(authController.signup(userCreateDto)).rejects.toThrow(
@ -174,6 +176,7 @@ describe('AuthController', () => {
email: 'test@test.test',
password: 'test',
roles: ['659564cb4aa383c0d0dbc688'],
avatar: null,
};
const result = await authController.signup(userCreateDto);
expect(userService.create).toHaveBeenCalledWith(userCreateDto);

View File

@ -210,6 +210,7 @@ describe('UserController', () => {
email: 'test@test.test',
password: 'test',
roles: [role!.id],
avatar: null,
};
const result = await userController.create(userDto);
expect(userService.create).toHaveBeenCalledWith(userDto);

View File

@ -250,7 +250,7 @@ export class ReadWriteUserController extends ReadOnlyUserController {
.map((role) => role.id),
avatar: user.avatar
? (await this.attachmentService.findOne(user.avatar))?.id
: undefined,
: null,
},
});
return await this.userService.create(user);

View File

@ -60,7 +60,7 @@ export class UserCreateDto {
@IsOptional()
@IsString()
@IsObjectId({ message: 'Avatar must be a valid ObjectId' })
avatar?: string;
avatar: string | null = null;
}
export class UserEditProfileDto extends OmitType(PartialType(UserCreateDto), [

View File

@ -35,6 +35,6 @@ export class AuthSerializer extends PassportSerializer {
done: (err: Error | null, user: SessionUser | null) => void,
) {
const user = payload.id ? await this.userService.findOne(payload.id) : null;
user ? done(null, user) : done(null, null);
done(null, user);
}
}

View File

@ -91,7 +91,7 @@ export class UserStub extends BaseSchema {
ref: 'Attachment',
default: null,
})
avatar?: unknown;
avatar: unknown;
@Prop({
type: String,
@ -112,7 +112,7 @@ export class User extends UserStub {
roles: string[];
@Transform(({ obj }) => obj.avatar?.toString() || null)
avatar?: string | null;
avatar: string | null;
}
@Schema({ timestamps: true })

View File

@ -9,7 +9,7 @@
import mongoose from 'mongoose';
import { UserCreateDto } from '@/user/dto/user.dto';
import { UserModel, User } from '@/user/schemas/user.schema';
import { User, UserModel } from '@/user/schemas/user.schema';
import { hash } from '@/user/utilities/bcryptjs';
import { getFixturesWithDefaultValues } from '../defaultValues';
@ -25,6 +25,7 @@ export const users: UserCreateDto[] = [
email: 'admin@admin.admin',
password: 'adminadmin',
roles: ['0', '1'],
avatar: null,
},
];
@ -34,7 +35,6 @@ export const userDefaultValues: TFixturesDefaultValues<User> = {
timezone: 'Europe/Berlin',
sendEmail: false,
resetCount: 0,
avatar: null,
};
export const getUserFixtures = (users: UserCreateDto[]) =>