Skip to content

Commit

Permalink
feat: ✨ Update UserDocument
Browse files Browse the repository at this point in the history
  • Loading branch information
zhumeisongsong committed Nov 25, 2024
1 parent 5d6023d commit ef3f8ca
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,31 @@ export class MongooseUsersRepository implements UsersRepository {
) {}

async findById(id: string): Promise<User | null> {

const _id = new Types.ObjectId(id);
const userDocument = await this.userModel.findById(_id).exec();

if (!userDocument) {
return null;
}
return new User(userDocument.id, userDocument.name);
return new User(
userDocument.id,
userDocument.lastName,
userDocument.firstName,
userDocument.email,
userDocument.password,
);
}

async findByEmail(email: string): Promise<User | null> {
const userDocument = await this.userModel.findOne({ email }).exec();
return userDocument
? new User(
userDocument.id,
userDocument.lastName,
userDocument.firstName,
userDocument.email,
userDocument.password,
)
: null;
}
}
13 changes: 11 additions & 2 deletions libs/users/infrastructure/mongoose/src/lib/user.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ import { Document } from 'mongoose';
@Schema()
export class UserDocument extends Document {
@Prop({ required: true })
name!: string;
email!: string;

@Prop({ required: false })
firstName!: string;

@Prop({ required: false })
lastName!: string;

@Prop({ required: true, nullable: true })
password!: string;
}

export const UserSchema = SchemaFactory.createForClass(UserDocument);
export const UserSchema = SchemaFactory.createForClass(UserDocument);

0 comments on commit ef3f8ca

Please sign in to comment.