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

View File

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

View File

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

View File

@ -60,7 +60,7 @@ export class UserCreateDto {
@IsOptional() @IsOptional()
@IsString() @IsString()
@IsObjectId({ message: 'Avatar must be a valid ObjectId' }) @IsObjectId({ message: 'Avatar must be a valid ObjectId' })
avatar?: string; avatar: string | null = null;
} }
export class UserEditProfileDto extends OmitType(PartialType(UserCreateDto), [ 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, done: (err: Error | null, user: SessionUser | null) => void,
) { ) {
const user = payload.id ? await this.userService.findOne(payload.id) : null; 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', ref: 'Attachment',
default: null, default: null,
}) })
avatar?: unknown; avatar: unknown;
@Prop({ @Prop({
type: String, type: String,
@ -112,7 +112,7 @@ export class User extends UserStub {
roles: string[]; roles: string[];
@Transform(({ obj }) => obj.avatar?.toString() || null) @Transform(({ obj }) => obj.avatar?.toString() || null)
avatar?: string | null; avatar: string | null;
} }
@Schema({ timestamps: true }) @Schema({ timestamps: true })

View File

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