Skip to content

Commit eba38e4

Browse files
committed
refactor: rename UserViewDto to UserPreviewDto and create new DTO for user profile
1 parent 5d7252b commit eba38e4

File tree

6 files changed

+55
-52
lines changed

6 files changed

+55
-52
lines changed

server/src/search/search.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Inject, Injectable, Logger } from '@nestjs/common';
22
import { SearchQueryDTO } from '@shared/validation/common/dto/SearchQuery.dto';
33
import { SongViewDto } from '@shared/validation/song/dto/SongView.dto';
4-
import { UserViewDto } from '@shared/validation/user/dto/UserView.dto';
4+
import { UserPreviewDto } from '@shared/validation/user/dto/UserPreview.dto';
55

66
import { SongService } from '@server/song/song.service';
77
import { UserService } from '@server/user/user.service';
@@ -61,7 +61,7 @@ export class SearchService {
6161
return {
6262
users: users.map(
6363
({ username, profileImage }) =>
64-
new UserViewDto({
64+
new UserPreviewDto({
6565
username,
6666
profileImage,
6767
}),

server/src/user/user.service.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { CreateUser } from '@shared/validation/user/dto/CreateUser.dto';
66
import { GetUser } from '@shared/validation/user/dto/GetUser.dto';
77
import { UpdateUsernameDto } from '@shared/validation/user/dto/UpdateUsername.dto';
88
import { UpdateUserProfileDto } from '@shared/validation/user/dto/UpdateUserProfile.dto';
9-
import { UserViewDto } from '@shared/validation/user/dto/UserView.dto';
9+
import { UserProfileViewDto } from '@shared/validation/user/dto/UserProfileView.dto';
1010
import { validate } from 'class-validator';
1111
import { Model } from 'mongoose';
1212

@@ -156,12 +156,14 @@ export class UserService {
156156
user = await this.findByUsername(username);
157157
}
158158

159-
if (user) return new UserViewDto(user);
159+
if (!user) {
160+
throw new HttpException(
161+
'You must provide an email, ID or username',
162+
HttpStatus.BAD_REQUEST,
163+
);
164+
}
160165

161-
throw new HttpException(
162-
'You must provide an email, ID or username',
163-
HttpStatus.BAD_REQUEST,
164-
);
166+
return UserProfileViewDto.fromUserDocument(user);
165167
}
166168

167169
public async getHydratedUser(user: UserDocument) {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
export class UserViewDto {
1+
export class UserPreviewDto {
22
username: string;
33
profileImage: string;
44

5-
constructor(partial: UserViewDto) {
5+
constructor(partial: UserPreviewDto) {
66
Object.assign(this, partial);
77
}
88
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { UserDocument } from '@server/user/entity/user.entity';
2+
3+
export class UserProfileViewDto {
4+
username: string;
5+
publicName: string;
6+
profileImage: string;
7+
description: string;
8+
lastSeen: Date;
9+
loginCount: number;
10+
loginStreak: number;
11+
playCount: number;
12+
// socialLinks: Record<keyof typeof UserLinks, string | undefined>;
13+
14+
public static fromUserDocument(user: UserDocument): UserProfileViewDto {
15+
return new UserProfileViewDto({
16+
username: user.username,
17+
publicName: user.publicName,
18+
profileImage: user.profileImage,
19+
description: user.description,
20+
lastSeen: user.lastSeen,
21+
loginCount: user.loginCount,
22+
loginStreak: user.loginStreak,
23+
playCount: user.playCount,
24+
// socialLinks: user.socialLinks,
25+
});
26+
}
27+
28+
constructor(partial: UserProfileViewDto) {
29+
Object.assign(this, partial);
30+
}
31+
}
32+
33+
// TODO: refactor all DTOs as ...Request.dto and ...Response.dto

web/src/modules/auth/types/User.ts

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,10 @@ export type LoggedUserData = {
1717
prefersDarkTheme: boolean;
1818
creationDate: string;
1919
lastEdited: string;
20-
lastLogin: string;
20+
lastSeen: string;
2121
createdAt: string;
2222
updatedAt: string;
2323
id: string;
2424
};
2525

26-
export enum SocialLinksTypes {
27-
BANDCAMP = 'bandcamp',
28-
DISCORD = 'discord',
29-
FACEBOOK = 'facebook',
30-
GITHUB = 'github',
31-
INSTAGRAM = 'instagram',
32-
REDDIT = 'reddit',
33-
SNAPCHAT = 'snapchat',
34-
SOUNDCLOUD = 'soundcloud',
35-
SPOTIFY = 'spotify',
36-
STEAM = 'steam',
37-
TELEGRAM = 'telegram',
38-
TIKTOK = 'tiktok',
39-
THREADS = 'threads',
40-
TWITCH = 'twitch',
41-
X = 'x',
42-
YOUTUBE = 'youtube',
43-
}
44-
45-
export type SocialLinks = {
46-
[K in SocialLinksTypes]?: string;
47-
};
48-
49-
export type UserProfileData = {
50-
lastLogin: Date;
51-
loginStreak: number;
52-
playCount: number;
53-
publicName: string;
54-
description: string;
55-
profileImage: string;
56-
socialLinks: SocialLinks;
57-
};
26+
// TODO: make this a DTO (part of the validation module)

web/src/modules/user/components/UserProfile.tsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1+
import { UserProfileViewDto } from '@shared/validation/user/dto/UserProfileView.dto';
12
import Image from 'next/image';
23

3-
import { SocialLinksTypes, UserProfileData } from '../../auth/types/User';
4-
54
type UserProfileProps = {
6-
userData: UserProfileData;
5+
userData: UserProfileViewDto;
76
};
87

98
const UserProfile = ({ userData }: UserProfileProps) => {
109
const {
11-
lastLogin,
10+
lastSeen,
1211
loginStreak,
1312
playCount,
1413
publicName,
1514
description,
1615
profileImage,
17-
socialLinks,
16+
// socialLinks,
1817
} = userData;
1918

2019
return (
@@ -28,12 +27,12 @@ const UserProfile = ({ userData }: UserProfileProps) => {
2827
/>
2928
<h1 className='text-2xl font-bold'>{publicName}</h1>
3029
<p className='text-gray-500'>{description}</p>
31-
<p className='text-gray-500'>Last Login: {lastLogin.toLocaleString()}</p>
30+
<p className='text-gray-500'>Last Login: {lastSeen.toLocaleString()}</p>
3231
<p className='text-gray-500'>Login Streak: {loginStreak}</p>
3332
<p className='text-gray-500'>Play Count: {playCount}</p>
34-
<ul className='mt-4'>
33+
{/* <ul className='mt-4'>
3534
{Object.keys(socialLinks).map((key, index) => {
36-
const link = socialLinks[key as SocialLinksTypes];
35+
const link = socialLinks[key as keyof UserLinks];
3736
if (!link) return null;
3837
3938
return (
@@ -44,7 +43,7 @@ const UserProfile = ({ userData }: UserProfileProps) => {
4443
</li>
4544
);
4645
})}
47-
</ul>
46+
</ul> */}
4847
</section>
4948
);
5049
};

0 commit comments

Comments
 (0)