|
| 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 | +} |
0 commit comments