extract shared mongo mapper utils
This commit is contained in:
1 parent
bd13e9d9c8
commit
825a81d326
16 files changed
+89
-268
No files matched your search
@@ -1,27 +1,14 @@
|
||||
import mongoose from "mongoose";
|
||||
import { IUsersRepository } from "@/domain/users/user.repository.interface.js";
|
||||
import { UserModel, type UserDocument } from "@/domain/users/user.model.js";
|
||||
import type { User, UserProfileImage } from "@/domain/users/user.type.js";
|
||||
import { GenerateAvatarImage } from "@/utils/imageProcessing.js";
|
||||
import { ParseBoolean } from "@/utils/utils.js";
|
||||
import { AppError } from "@/utils/AppError.js";
|
||||
import { toStringId, toDateString } from "@/utils/mongoMappers.js";
|
||||
const SERVICE_NAME = "MongoUsersRepository";
|
||||
|
||||
class MongoUsersRepository implements IUsersRepository {
|
||||
static SERVICE_NAME = SERVICE_NAME;
|
||||
private toStringId = (value?: mongoose.Types.ObjectId | string | null): string => {
|
||||
if (!value) {
|
||||
return "";
|
||||
}
|
||||
return value instanceof mongoose.Types.ObjectId ? value.toString() : String(value);
|
||||
};
|
||||
|
||||
private toDateString = (value?: Date | string | null): string => {
|
||||
if (!value) {
|
||||
return new Date(0).toISOString();
|
||||
}
|
||||
return value instanceof Date ? value.toISOString() : new Date(value).toISOString();
|
||||
};
|
||||
|
||||
private mapProfileImage = (image?: (UserProfileImage & { data?: Buffer }) | null) => {
|
||||
if (!image) {
|
||||
@@ -35,7 +22,7 @@ class MongoUsersRepository implements IUsersRepository {
|
||||
|
||||
protected toEntity = (doc: UserDocument): User => {
|
||||
return {
|
||||
id: this.toStringId(doc._id),
|
||||
id: toStringId(doc._id),
|
||||
firstName: doc.firstName,
|
||||
lastName: doc.lastName,
|
||||
email: doc.email,
|
||||
@@ -45,10 +32,10 @@ class MongoUsersRepository implements IUsersRepository {
|
||||
isActive: doc.isActive ?? false,
|
||||
isVerified: doc.isVerified ?? false,
|
||||
role: doc.role ?? [],
|
||||
teamId: this.toStringId(doc.teamId),
|
||||
teamId: toStringId(doc.teamId),
|
||||
checkTTL: doc.checkTTL ?? undefined,
|
||||
createdAt: this.toDateString(doc.createdAt),
|
||||
updatedAt: this.toDateString(doc.updatedAt),
|
||||
createdAt: toDateString(doc.createdAt),
|
||||
updatedAt: toDateString(doc.updatedAt),
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user