Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: AutoCreate Admin On Startup #101

Merged
merged 6 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
DATABASE_URL=postgresql://postgres:<your-project-password>@db.<your-project-name>.supabase.co:5432/postgres
SUPABASE_API_URL=https://<your-project-name>.supabase.co
SUPABASE_ANON_KEY=
[email protected]

GITHUB_CLIENT_ID=
GITHUB_CLIENT_SECRET=
Expand Down
6 changes: 3 additions & 3 deletions apps/api/src/mail/services/interface.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface IMailService {
role: WorkspaceRole
): Promise<void>

accountLoginEmail(
email: string
): Promise<void>
accountLoginEmail(email: string): Promise<void>

adminUserCreateEmail(email: string): Promise<void>
}
24 changes: 21 additions & 3 deletions apps/api/src/mail/services/mail.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ export class MailService implements IMailService {
await this.sendEmail(email, subject, body)
}

async accountLoginEmail(
email: string
): Promise<void> {
async accountLoginEmail(email: string): Promise<void> {
const subject = 'LogIn Invitation Accepted'
const body = `<!DOCTYPE html>
<html>
Expand All @@ -120,6 +118,26 @@ export class MailService implements IMailService {
await this.sendEmail(email, subject, body)
}

async adminUserCreateEmail(email: string): Promise<void> {
const subject = 'Admin User Created!!'
const body = `<!DOCTYPE html>
<html>
<head>
<title>Admin User Was Created!</title>
</head>
<body>
<h1>Welcome to keyshade!</h1>
<p>Hello there!</p>
<p>Your admin account has been setup. Please login to your account for further process.</p>
<p>Your email is: <strong>${email}</strong></p>
<p>Thank you for choosing us.</p>
<p>Best Regards,</p>
<p>keyshade Team</p>
</body>
`
await this.sendEmail('[email protected]', subject, body)
}

private async sendEmail(
email: string,
subject: string,
Expand Down
4 changes: 4 additions & 0 deletions apps/api/src/mail/services/mock.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export class MockMailService implements IMailService {
)
}

async adminUserCreateEmail(email: string): Promise<void> {
this.log.log(`Create pAdmin User Email: ${email}`)
}

async sendOtp(email: string, otp: string): Promise<void> {
this.log.log(`OTP for ${email} is ${otp}`)
}
Expand Down
39 changes: 38 additions & 1 deletion apps/api/src/user/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ export class UserService {
private readonly log = new Logger(UserService.name)

constructor(
// @Inject(USER_REPOSITORY) private readonly repository: IUserRepository
private readonly prisma: PrismaService,
@Inject(MAIL_SERVICE) private readonly mailService: IMailService
) {}

async onApplicationBootstrap() {
await this.checkIfAdminExistsOrCreate()
}

async getSelf(user: User) {
return excludeFields(user, 'isActive')
}
Expand Down Expand Up @@ -172,4 +175,38 @@ export class UserService {

return newUser
}

private async checkIfAdminExistsOrCreate() {
const adminExists =
(await this.prisma.user.count({
where: {
isAdmin: true
}
})) > 0

if (!adminExists) {
this.log.warn('No admin user found', 'UserService')
await this.createAdminUser()
return
}
this.log.log('Admin user found', 'UserService')
}

private async createAdminUser() {
HarshPatel5940 marked this conversation as resolved.
Show resolved Hide resolved
this.log.log('Creating admin user', 'UserService')

// Create the admin user
const adminUser = await this.prisma.user.create({
data: {
name: 'Admin',
email: process.env.ADMIN_EMAIL || '[email protected]',
isAdmin: true,
isActive: true,
isOnboardingFinished: true
}
})

await this.mailService.adminUserCreateEmail(adminUser.email)
this.log.log('Created admin user', 'UserService')
}
}
3 changes: 2 additions & 1 deletion apps/web/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"next-env.d.ts",
".next/types/**/*.ts",
"../../apps/web/dist/.next/types/**/*.ts",
"dist/.next/types/**/*.ts"
"dist/.next/types/**/*.ts",
"../../apps/web/dist/dist/.next/types/**/*.ts"
],
"exclude": [
"node_modules",
Expand Down
Loading