Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/management/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { User } from './types';
* Transforms user objects by converting roles to roleNames
*/
export function transformUsersForBatch(users: User[]): any[] {
return users.map(({ roles, ...user }) => ({
return users.map(({ loginIdOrUserId, roles, ...user }) => ({
...user,
loginId: loginIdOrUserId,
roleNames: roles,
}));
}
4 changes: 3 additions & 1 deletion lib/management/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,9 @@ export type AttributesTypes = string | boolean | number | string[] | null;
export type TemplateOptions = Record<string, string>; // for providing messaging template options (templates that are being sent via email / text message)

export type User = {
loginId: string;
/** When a userId is provided, the user must already exist — no new user is created,
* and the invite is sent to the existing user (useful for re-inviting). */
loginIdOrUserId: string;
email?: string;
phone?: string;
displayName?: string;
Expand Down
43 changes: 39 additions & 4 deletions lib/management/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,29 @@ describe('Management User', () => {
response: httpResponse,
});
});

it('should send the correct request when passing a userId', async () => {
const httpResponse = {
ok: true,
json: () => mockMgmtUserResponse,
clone: () => ({
json: () => Promise.resolve(mockMgmtUserResponse),
}),
status: 200,
};
mockHttpClient.post.mockResolvedValue(httpResponse);

const userId = 'U2abc1234567890123456789';
await management.user.invite(userId, { email: 'a@b.c', sendMail: true });

expect(mockHttpClient.post).toHaveBeenCalledWith(apiPaths.user.create, {
loginId: userId,
email: 'a@b.c',
roleNames: undefined,
invite: true,
sendMail: true,
});
});
});

describe('invite batch', () => {
Expand Down Expand Up @@ -373,8 +396,14 @@ describe('Management User', () => {

const resp: SdkResponse<CreateOrInviteBatchResponse> = await management.user.inviteBatch(
[
{ loginId: 'one', roles: ['r1'], email: 'one@one', password: 'clear', seed: 'aaa' },
{ loginId: 'two', roles: ['r1'], email: 'two@two', hashedPassword: hashed },
{
loginIdOrUserId: 'one',
roles: ['r1'],
email: 'one@one',
password: 'clear',
seed: 'aaa',
},
{ loginIdOrUserId: 'two', roles: ['r1'], email: 'two@two', hashedPassword: hashed },
],
'https://invite.me',
true,
Expand Down Expand Up @@ -443,8 +472,14 @@ describe('Management User', () => {
};

const resp: SdkResponse<CreateOrInviteBatchResponse> = await management.user.createBatch([
{ loginId: 'one', roles: ['r1'], email: 'one@one', password: 'clear', seed: 'aaa' },
{ loginId: 'two', roles: ['r1'], email: 'two@two', hashedPassword: hashed },
{
loginIdOrUserId: 'one',
roles: ['r1'],
email: 'one@one',
password: 'clear',
seed: 'aaa',
},
{ loginIdOrUserId: 'two', roles: ['r1'], email: 'two@two', hashedPassword: hashed },
]);

expect(mockHttpClient.post).toHaveBeenCalledWith(apiPaths.user.createBatch, {
Expand Down
20 changes: 13 additions & 7 deletions lib/management/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,14 @@ const withUser = (httpClient: HttpClient) => {
/* Create Test User End */

/* Invite User */
/**
* Create a new user and invite them to set up their credentials.
* When loginIdOrUserId is a loginId, a new user is created if one doesn't already exist.
* When loginIdOrUserId is a userId, the user must already exist — no new user is created,
* and the invite is sent to the existing user (useful for re-inviting).
*/
function invite(
loginId: string,
loginIdOrUserId: string,
options?: UserOptions & {
inviteUrl?: string;
sendMail?: boolean; // send invite via mail, default is according to project settings
Expand All @@ -225,7 +231,7 @@ const withUser = (httpClient: HttpClient) => {
},
): Promise<SdkResponse<UserResponse>>;
function invite(
loginId: string,
loginIdOrUserId: string,
email?: string,
phone?: string,
displayName?: string,
Expand All @@ -246,7 +252,7 @@ const withUser = (httpClient: HttpClient) => {
): Promise<SdkResponse<UserResponse>>;

function invite(
loginId: string,
loginIdOrUserId: string,
emailOrOptions?: string | UserOptions,
phone?: string,
displayName?: string,
Expand All @@ -266,12 +272,12 @@ const withUser = (httpClient: HttpClient) => {
templateId?: string,
): Promise<SdkResponse<UserResponse>> {
// We support both the old and new parameters forms of invite user
// 1. The new form - invite(loginId, { email, phone, ... }})
// 2. The old form - invite(loginId, email, phone, ...)
// 1. The new form - invite(loginIdOrUserId, { email, phone, ... }})
// 2. The old form - invite(loginIdOrUserId, email, phone, ...)
const body =
typeof emailOrOptions === 'string'
? {
loginId,
loginId: loginIdOrUserId,
email: emailOrOptions,
phone,
displayName,
Expand All @@ -292,7 +298,7 @@ const withUser = (httpClient: HttpClient) => {
templateId,
}
: {
loginId,
loginId: loginIdOrUserId,
...emailOrOptions,
roleNames: emailOrOptions?.roles,
roles: undefined,
Expand Down
Loading