refactor: fixed types, updated comments, removed most of the getters from UserEntity
This commit is contained in:
@@ -105,16 +105,18 @@ export abstract class Entity<EntityProps> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an Entity to a plain object.
|
||||
* Convert an Entity and all sub-entities/Value Objects it
|
||||
* contains to a plain object with primitive types. Can be
|
||||
* useful when logging an entity during testing/debugging
|
||||
*/
|
||||
public toObject(): EntityProps & BaseEntityProps {
|
||||
const propsCopy = convertPropsToObject(this.props);
|
||||
public toObject(): unknown {
|
||||
const plainProps = convertPropsToObject(this.props);
|
||||
|
||||
const result = {
|
||||
id: this._id.value,
|
||||
createdAt: this._createdAt.value,
|
||||
updatedAt: this._updatedAt.value,
|
||||
...propsCopy,
|
||||
...plainProps,
|
||||
};
|
||||
return Object.freeze(result);
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ function isEntity(obj: unknown): obj is Entity<unknown> {
|
||||
);
|
||||
}
|
||||
|
||||
function convertToRaw(item: any): any {
|
||||
function convertToPlainObject(item: any): any {
|
||||
if (ValueObject.isValueObject(item)) {
|
||||
return item.getRawProps();
|
||||
}
|
||||
@@ -37,10 +37,10 @@ export function convertPropsToObject(props: any): any {
|
||||
for (const prop in propsCopy) {
|
||||
if (Array.isArray(propsCopy[prop])) {
|
||||
propsCopy[prop] = (propsCopy[prop] as Array<unknown>).map(item => {
|
||||
return convertToRaw(item);
|
||||
return convertToPlainObject(item);
|
||||
});
|
||||
}
|
||||
propsCopy[prop] = convertToRaw(propsCopy[prop]);
|
||||
propsCopy[prop] = convertToPlainObject(propsCopy[prop]);
|
||||
}
|
||||
|
||||
return propsCopy;
|
||||
|
||||
@@ -4,9 +4,9 @@ import { IdResponse } from '../dtos/id.response.dto';
|
||||
|
||||
export class ResponseBase extends IdResponse {
|
||||
constructor(entity: BaseEntityProps) {
|
||||
super(entity.id?.value as string);
|
||||
this.createdAt = (entity.createdAt?.value as Date).toISOString();
|
||||
this.updatedAt = (entity.updatedAt?.value as Date).toISOString();
|
||||
super(entity.id.value);
|
||||
this.createdAt = entity.createdAt.value.toISOString();
|
||||
this.updatedAt = entity.updatedAt.value.toISOString();
|
||||
}
|
||||
|
||||
@ApiProperty({ example: '2020-11-24T17:43:15.970Z' })
|
||||
|
||||
@@ -38,18 +38,10 @@ export class UserEntity extends AggregateRoot<UserProps> {
|
||||
return user;
|
||||
}
|
||||
|
||||
/* Private properties and getters without a setter protects entity
|
||||
from outside modifications by using assignment, for example:
|
||||
"user.email = someOtherEmail". This technique only allows
|
||||
updating value by using a dedicated 'update' method (see updateAddress below) */
|
||||
get address(): Address {
|
||||
return this.props.address;
|
||||
}
|
||||
|
||||
get email(): Email {
|
||||
return this.props.email;
|
||||
}
|
||||
|
||||
/* You can create getters only for the properties that you need to access and leave the rest of the properties private to keep entity
|
||||
encapsulated. To get all entity properties (for saving it to a
|
||||
database or mapping a response) use .getPropsCopy() method
|
||||
defined in a EntityBase parent class */
|
||||
get role(): UserRoles {
|
||||
return this.props.role;
|
||||
}
|
||||
|
||||
@@ -13,10 +13,11 @@ export class UserResponse extends ResponseBase implements User {
|
||||
(avoid blacklisting, which will return everything
|
||||
but blacklisted items, which can lead to a data leak).
|
||||
*/
|
||||
this.email = user.email.value;
|
||||
this.country = user.address.country;
|
||||
this.postalCode = user.address.postalCode;
|
||||
this.street = user.address.street;
|
||||
const props = user.getPropsCopy();
|
||||
this.email = props.email.value;
|
||||
this.country = props.address.country;
|
||||
this.postalCode = props.address.postalCode;
|
||||
this.street = props.address.street;
|
||||
}
|
||||
|
||||
@ApiProperty({
|
||||
|
||||
Reference in New Issue
Block a user