-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathuser-profile.profile.ts
39 lines (36 loc) · 1.14 KB
/
user-profile.profile.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
import { convertUsing, mapWith } from '@automapper/core';
import { AutomapperProfile, InjectMapper } from '@automapper/nestjs';
import type { Converter, Mapper, MappingProfile } from '@automapper/types';
import { Injectable } from '@nestjs/common';
import { Avatar, AvatarVm } from '../models/avatar';
import { UserProfile, UserProfileVm } from '../models/user-profile';
export const dateToStringConverter: Converter<Date, string> = {
convert(source: Date | string): string {
if (typeof source === 'string') return new Date(source).toDateString();
return source.toDateString();
},
};
@Injectable()
export class UserProfileProfile extends AutomapperProfile {
constructor(@InjectMapper() mapper: Mapper) {
super(mapper);
}
mapProfile(): MappingProfile {
return (mapper) => {
mapper
.createMap(UserProfile, UserProfileVm)
.forMember(
(d) => d.avatar,
mapWith(
() => AvatarVm,
(s) => s.avatar,
() => Avatar
)
)
.forMember(
(d) => d.birthday,
convertUsing(dateToStringConverter, (s) => s.birthday)
);
};
}
}