Spaces:
Sleeping

File size: 2,050 Bytes
4e83772
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { GithubIdsOutput } from "@/interfaces/repository/IUserRepository.js";
import { IUser } from "@/models/User.js";

interface UserResponseDTO {
	name: string;
	profileImage: string;
	_id: string;
	plan: string;
}
interface UserAuthProviderResponseDTO {
	user: string;
	providers: { id: string; provider: string }[];
}
interface UserResponseDetailedDTO {
	name: string;
	profileImage: string;
	email: string;
	projects: number;
	bandwidthMonthly: number;
	deploymentsToday: number;
	plan: string;
	createdAt: Date;
	_id: string;
	connectedAccounts: string[];
	githubInstallationId?: number;
	githubAccountId?: number;
}
export class UserMapper {
	static toUserInstallationResponse(data: GithubIdsOutput, userId: string): { ids: GithubIdsOutput & { _id: string } } {
		return { ids: { githubAccountId: data.githubAccountId, githubInstallationId: data.githubInstallationId, _id: userId } };
	}
	static toUserAuthProviderResponse(providers: IUser["authProviders"], userId: string): UserAuthProviderResponseDTO {
		return { user: userId, providers: providers.map((p) => ({ id: p.id, provider: p.provider })) };
	}
	static toUserResponse(user: IUser): { user: UserResponseDTO } {
		return {
			user: {
				_id: user._id.toString(),
				name: user.name,
				profileImage: user.profileImage,
				plan: user.plan,
			},
		};
	}
	static toUserDetailedResponse(data: { user: IUser; bandwidth: number }): { user: UserResponseDetailedDTO } {
		const { user } = data;
		const currentDate = new Date().toISOString().slice(0, 10);
		return {
			user: {
				_id: user._id.toString(),
				name: user.name,
				profileImage: user.profileImage,
				email: user.email,
				projects: Number(user.projects),
				deploymentsToday: Number(user.currentDate === currentDate ? user.deploymentsToday : 0),
				plan: user.plan,
				createdAt: user.createdAt,
				bandwidthMonthly: data.bandwidth,
				connectedAccounts: user.authProviders.map((p) => p.provider),
				githubInstallationId: user.githubInstallationId,
				githubAccountId: user.githubAccountId,
			},
		};
	}
}