Skip to content

Commit 6cc550b

Browse files
committed
fix getPrimaryEmail
1 parent 7007259 commit 6cc550b

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
3+
* Licensed under the GNU Affero General Public License (AGPL).
4+
* See License.AGPL.txt in the project root for license information.
5+
*/
6+
7+
import { Timestamp } from "@bufbuild/protobuf";
8+
import { Identity, User, User_ProfileDetails } from "@gitpod/public-api/lib/gitpod/v1/user_pb";
9+
import * as chai from "chai";
10+
import { getPrimaryEmail } from "./user-utils";
11+
12+
const expect = chai.expect;
13+
14+
describe("getPrimaryEmail", function () {
15+
const user = new User({
16+
organizationId: undefined,
17+
profile: new User_ProfileDetails({
18+
emailAddress: "[email protected]",
19+
}),
20+
identities: [
21+
new Identity({
22+
primaryEmail: "[email protected]",
23+
}),
24+
],
25+
});
26+
it(`should return email from profile exists`, () => {
27+
const email = getPrimaryEmail(user);
28+
expect(email).to.equal(user.profile!.emailAddress);
29+
});
30+
it(`should return email from SSO provider for org-owned accounts`, () => {
31+
const ssoEmail = "[email protected]";
32+
user.identities.unshift(
33+
new Identity({
34+
primaryEmail: ssoEmail,
35+
// SSO identities have `lastSigninTime` set
36+
lastSigninTime: Timestamp.fromDate(new Date()),
37+
}),
38+
);
39+
user.organizationId = "any";
40+
const email = getPrimaryEmail(user);
41+
expect(email).to.equal(ssoEmail);
42+
});
43+
});

components/public-api/typescript-common/src/user-utils.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ export function getPrimaryEmail(user: User | UserProtocol): string | undefined {
3636

3737
// In case of a personal account, check for the email stored by the user.
3838
if (!isOrganizationOwned(user)) {
39-
const emailAddress = UserProtocol.is(user)
40-
? user.additionalData?.profile?.emailAddress
41-
: user.profile?.emailAddress;
39+
const emailAddress =
40+
user instanceof User //
41+
? user.profile?.emailAddress
42+
: user.additionalData?.profile?.emailAddress;
4243
if (emailAddress) {
4344
return emailAddress;
4445
}
@@ -56,7 +57,7 @@ export function getPrimaryEmail(user: User | UserProtocol): string | undefined {
5657
}
5758

5859
export function getName(user: User | UserProtocol): string | undefined {
59-
const name = /* user.fullName ||*/ user.name;
60+
const name = user.name;
6061
if (name) {
6162
return name;
6263
}

0 commit comments

Comments
 (0)