forked from Sairyss/domain-driven-hexagon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.mapper.ts
69 lines (64 loc) · 2.18 KB
/
user.mapper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { Mapper } from '@libs/ddd';
import { UserModel, userSchema } from './database/user.repository';
import { Address } from './domain/value-objects/address.value-object';
import { UserEntity } from './domain/user.entity';
import { UserResponseDto } from './dtos/user.response.dto';
import { Injectable } from '@nestjs/common';
/**
* Mapper constructs objects that are used in different layers:
* Record is an object that is stored in a database,
* Entity is an object that is used in application domain layer,
* and a ResponseDTO is an object returned to a user (usually as json).
*/
@Injectable()
export class UserMapper
implements Mapper<UserEntity, UserModel, UserResponseDto>
{
toPersistence(entity: UserEntity): UserModel {
const copy = entity.getProps();
const record: UserModel = {
id: copy.id,
createdAt: copy.createdAt,
updatedAt: copy.updatedAt,
email: copy.email,
country: copy.address.country,
postalCode: copy.address.postalCode,
street: copy.address.street,
role: copy.role,
};
return userSchema.parse(record);
}
toDomain(record: UserModel): UserEntity {
const entity = new UserEntity({
id: record.id,
createdAt: new Date(record.createdAt),
updatedAt: new Date(record.updatedAt),
props: {
email: record.email,
role: record.role,
address: new Address({
street: record.street,
postalCode: record.postalCode,
country: record.country,
}),
},
});
return entity;
}
toResponse(entity: UserEntity): UserResponseDto {
const props = entity.getProps();
const response = new UserResponseDto(entity);
response.email = props.email;
response.country = props.address.country;
response.postalCode = props.address.postalCode;
response.street = props.address.street;
return response;
}
/* ^ Data returned to the user is whitelisted to avoid leaks.
If a new property is added, like password or a
credit card number, it won't be returned
unless you specifically allow this.
(avoid blacklisting, which will return everything
but blacklisted items, which can lead to a data leak).
*/
}