refactor: fixed types, updated comments, removed most of the getters from UserEntity

This commit is contained in:
user
2021-09-14 21:55:37 +02:00
parent fd74adc47d
commit 982215cda4
5 changed files with 21 additions and 26 deletions

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -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' })

View File

@@ -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;
}

View File

@@ -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({