Skip to content

Commit

Permalink
fix: messed up on merge-conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
gastonfournier committed Aug 14, 2024
1 parent 8a57b2b commit 38bc206
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 31 deletions.
34 changes: 21 additions & 13 deletions src/lib/routes/admin-api/user-admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,22 +520,30 @@ export default class UserAdminController extends Controller {
? Number(rootRole)
: (rootRole as RoleName);

const { createdUser, inviteLink, emailSent } =
await this.userService.createUserWithEmail(
{
username,
email,
name,
password,
rootRole: normalizedRootRole,
},
sendEmail,
req.audit,
);
const createdUser = await this.userService.createUser(
{
username,
email,
name,
password,
rootRole: normalizedRootRole,
},
req.audit,
);

const inviteLink = await this.userService.newUserInviteLink(
createdUser,
req.audit,
);

// send email defaults to true
const emailSent = (sendEmail !== undefined ? sendEmail : true)
? await this.userService.sendWelcomeEmail(createdUser, inviteLink)
: false;

const responseData: CreateUserResponseSchema = {
...serializeDates(createdUser),
inviteLink: inviteLink,
inviteLink,
emailSent,
rootRole: normalizedRootRole,
};
Expand Down
32 changes: 14 additions & 18 deletions src/lib/services/user-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,20 +255,10 @@ class UserService {
return userCreated;
}

async createUserWithEmail(
{ username, email, name, password, rootRole }: ICreateUser,
sendEmail = true,
async newUserInviteLink(
user: IUserWithRootRole,
auditUser: IAuditUser = SYSTEM_USER_AUDIT,
): Promise<{
createdUser: IUserWithRootRole;
inviteLink: string;
emailSent: boolean;
}> {
const createdUser = await this.createUser(
{ username, email, name, password, rootRole },
auditUser,
);

): Promise<string> {
const passwordAuthSettings =
await this.settingService.getWithDefault<SimpleAuthSettings>(
simpleAuthSettingsKey,
Expand All @@ -278,20 +268,26 @@ class UserService {
let inviteLink = this.unleashUrl;
if (!passwordAuthSettings.disabled) {
const inviteUrl = await this.resetTokenService.createNewUserUrl(
createdUser.id,
user.id,
auditUser.username,
);
inviteLink = inviteUrl.toString();
}
return inviteLink;
}

async sendWelcomeEmail(
user: IUserWithRootRole,
inviteLink: string,
): Promise<boolean> {
let emailSent = false;
const emailConfigured = this.emailService.configured();

if (emailConfigured && sendEmail && createdUser.email) {
if (emailConfigured && user.email) {
try {
await this.emailService.sendGettingStartedMail(
createdUser.name || '',
createdUser.email,
user.name || '',
user.email,
this.unleashUrl,
inviteLink,
);
Expand All @@ -308,7 +304,7 @@ class UserService {
);
}

return { createdUser, inviteLink, emailSent };
return emailSent;
}

async updateUser(
Expand Down

0 comments on commit 38bc206

Please sign in to comment.