Skip to content

Commit 1271b41

Browse files
committed
Add profile api service
1 parent e62e328 commit 1271b41

38 files changed

Lines changed: 932 additions & 423 deletions

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Else, refer to the following sections.
4444
| ------------------------------ | ------------------------------ |
4545
| `dfx deploy frontend` | Deploy to a local DFX replica |
4646
| `pnpm turbo start -F frontend` | Run a local development server |
47+
| `pnpm turbo build -F frontend` | Create a prod build |
4748
| `pnpm turbo test -F frontend` | Run unit tests |
4849

4950
### Docs
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './commit-review';
2+
export * from './profile';
23
export * from './proposal';
34
export * from './review';
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './profile-api.model';
2+
export * from './profile-api.service';
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
import { Ok, toCandidOpt } from '../../utils';
2+
import {
3+
GetMyUserProfileResponse as GetMyUserProfileApiResponse,
4+
UpdateMyUserProfileRequest as UpdateMyUserProfileApiRequest,
5+
SocialLink as ApiSocialLink,
6+
SocialLinkPlatform as ApiSocialLinkPlatform,
7+
} from '@cg/backend';
8+
import {
9+
GetMyUserProfileResponse,
10+
SocialMediaLink,
11+
SocialMediaLinkType,
12+
UpdateMyUserProfileRequest,
13+
UserRole,
14+
} from './profile-api.model';
15+
16+
export function mapGetMyUserProfileResponse(
17+
res: Ok<GetMyUserProfileApiResponse>,
18+
): GetMyUserProfileResponse {
19+
if ('reviewer' in res.config) {
20+
const config = res.config.reviewer;
21+
22+
return {
23+
role: UserRole.Reviewer,
24+
id: res.id,
25+
username: res.username,
26+
bio: config.bio,
27+
neuronId: config.neuron_id,
28+
walletAddress: config.wallet_address,
29+
socialMedia: mapSocialLinksResponse(config.social_links),
30+
};
31+
} else if ('admin' in res.config) {
32+
return {
33+
role: UserRole.Admin,
34+
id: res.id,
35+
username: res.username,
36+
bio: res.config.admin.bio,
37+
};
38+
} else {
39+
return {
40+
role: UserRole.Anonymous,
41+
id: res.id,
42+
username: res.username,
43+
};
44+
}
45+
}
46+
47+
function mapSocialLinksResponse(
48+
socialLinks: ApiSocialLink[],
49+
): SocialMediaLink[] {
50+
return socialLinks.map(link => ({
51+
type: mapSocialLinkPlatformResponse(link.platform),
52+
username: link.username,
53+
}));
54+
}
55+
56+
function mapSocialLinkPlatformResponse(
57+
platform: ApiSocialLinkPlatform,
58+
): SocialMediaLinkType {
59+
if ('dscvr' in platform) {
60+
return SocialMediaLinkType.DSCVR;
61+
}
62+
63+
if ('openchat' in platform) {
64+
return SocialMediaLinkType.OpenChat;
65+
}
66+
67+
if ('taggr' in platform) {
68+
return SocialMediaLinkType.Taggr;
69+
}
70+
71+
if ('x' in platform) {
72+
return SocialMediaLinkType.X;
73+
}
74+
75+
if ('github' in platform) {
76+
return SocialMediaLinkType.Github;
77+
}
78+
79+
if ('dfinityforum' in platform) {
80+
return SocialMediaLinkType.DfinityForum;
81+
}
82+
83+
if ('discord' in platform) {
84+
return SocialMediaLinkType.Discord;
85+
}
86+
87+
if ('website' in platform) {
88+
return SocialMediaLinkType.Website;
89+
}
90+
91+
throw new Error(`Unknown social link platform: ${JSON.stringify(platform)}`);
92+
}
93+
94+
export function mapUpdateMyUserProfileRequest(
95+
req: UpdateMyUserProfileRequest,
96+
): UpdateMyUserProfileApiRequest {
97+
switch (req.role) {
98+
case UserRole.Anonymous: {
99+
return {
100+
username: toCandidOpt(req.username),
101+
config: toCandidOpt(),
102+
};
103+
}
104+
105+
case UserRole.Reviewer: {
106+
return {
107+
username: toCandidOpt(req.username),
108+
config: toCandidOpt(
109+
req.bio || req.socialMedia || req.walletAddress
110+
? {
111+
reviewer: {
112+
bio: toCandidOpt(req.bio),
113+
social_links: toCandidOpt(
114+
mapSocialLinksRequest(req.socialMedia),
115+
),
116+
wallet_address: toCandidOpt(req.walletAddress),
117+
},
118+
}
119+
: undefined,
120+
),
121+
};
122+
}
123+
124+
case UserRole.Admin: {
125+
return {
126+
username: toCandidOpt(req.username),
127+
config: toCandidOpt(
128+
req.bio
129+
? {
130+
admin: {
131+
bio: toCandidOpt(req.bio),
132+
},
133+
}
134+
: undefined,
135+
),
136+
};
137+
}
138+
}
139+
}
140+
141+
function mapSocialLinksRequest(
142+
links?: SocialMediaLink[],
143+
): ApiSocialLink[] | undefined {
144+
return links?.map(link => ({
145+
platform: mapSocialLinkPlatformRequest(link.type),
146+
username: link.username,
147+
}));
148+
}
149+
150+
function mapSocialLinkPlatformRequest(
151+
platform: SocialMediaLinkType,
152+
): ApiSocialLinkPlatform {
153+
switch (platform) {
154+
case SocialMediaLinkType.DSCVR:
155+
return { dscvr: null };
156+
157+
case SocialMediaLinkType.OpenChat:
158+
return { openchat: null };
159+
160+
case SocialMediaLinkType.Taggr:
161+
return { taggr: null };
162+
163+
case SocialMediaLinkType.X:
164+
return { x: null };
165+
166+
case SocialMediaLinkType.Github:
167+
return { github: null };
168+
169+
case SocialMediaLinkType.DfinityForum:
170+
return { dfinityforum: null };
171+
172+
case SocialMediaLinkType.Discord:
173+
return { discord: null };
174+
175+
case SocialMediaLinkType.Website:
176+
return { website: null };
177+
}
178+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Generic types
3+
*/
4+
5+
export enum UserRole {
6+
Anonymous = 'Anonymous',
7+
Reviewer = 'Reviewer',
8+
Admin = 'Admin',
9+
}
10+
11+
export enum SocialMediaLinkType {
12+
DSCVR = 'DSCVR',
13+
OpenChat = 'OpenChat',
14+
Taggr = 'Taggr',
15+
X = 'X',
16+
Github = 'Github',
17+
DfinityForum = 'DfinityForum',
18+
Discord = 'Discord',
19+
Website = 'Website',
20+
}
21+
22+
export interface SocialMediaLink {
23+
type: SocialMediaLinkType;
24+
username: string;
25+
}
26+
27+
/**
28+
* GetMyUserProfile types
29+
*/
30+
31+
export interface BaseGetMyUserProfileResponse<T extends UserRole> {
32+
role: T;
33+
id: string;
34+
username: string;
35+
}
36+
37+
export type AnonymousGetMyUserProfileResponse =
38+
BaseGetMyUserProfileResponse<UserRole.Anonymous>;
39+
40+
export interface ReviewerGetMyUserProfileResponse
41+
extends BaseGetMyUserProfileResponse<UserRole.Reviewer> {
42+
neuronId: bigint;
43+
walletAddress: string;
44+
bio: string;
45+
socialMedia: SocialMediaLink[];
46+
}
47+
48+
export interface AdminGetMyUserProfileResponse
49+
extends BaseGetMyUserProfileResponse<UserRole.Admin> {
50+
bio: string;
51+
}
52+
53+
export type GetMyUserProfileResponse =
54+
| AnonymousGetMyUserProfileResponse
55+
| ReviewerGetMyUserProfileResponse
56+
| AdminGetMyUserProfileResponse;
57+
58+
/**
59+
* UpdateMyUserProfile types
60+
*/
61+
62+
export interface BaseUpdateMyUserProfileRequest<T extends UserRole> {
63+
role: T;
64+
username?: string;
65+
}
66+
67+
export type AnonymousUpdateMyUserProfileRequest =
68+
BaseUpdateMyUserProfileRequest<UserRole.Anonymous>;
69+
70+
export interface ReviewerUpdateMyUserProfileRequest
71+
extends BaseUpdateMyUserProfileRequest<UserRole.Reviewer> {
72+
bio?: string;
73+
socialMedia?: SocialMediaLink[];
74+
walletAddress?: string;
75+
}
76+
77+
export interface AdminUpdateMyUserProfileRequest
78+
extends BaseUpdateMyUserProfileRequest<UserRole.Admin> {
79+
bio?: string;
80+
}
81+
82+
export type UpdateMyUserProfileRequest =
83+
| AnonymousUpdateMyUserProfileRequest
84+
| ReviewerUpdateMyUserProfileRequest
85+
| AdminUpdateMyUserProfileRequest;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { ProfileApiService } from './profile-api.service';
2+
3+
export type ProfileApiServiceMock = jasmine.SpyObj<ProfileApiService>;
4+
5+
export function profileApiServiceMockFactory(): ProfileApiServiceMock {
6+
return jasmine.createSpyObj<ProfileApiServiceMock>('ProfileApiService', [
7+
'getMyUserProfile',
8+
'updateMyUserProfile',
9+
'createMyUserProfile',
10+
]);
11+
}

0 commit comments

Comments
 (0)