Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { ValidationExceptionFilter } from './helpers/validation-filter.exception
import { LocalGovernmentModule } from './modules/local-government/local-government.module';
import { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter';
import { PostgresConnectionOptions } from 'typeorm/driver/postgres/PostgresConnectionOptions';
import { NotificationModule } from './modules/notification/notification.module';

@Module({
imports: [
Expand Down Expand Up @@ -168,6 +169,7 @@ import { PostgresConnectionOptions } from 'typeorm/driver/postgres/PostgresConne
GuardModule,
PhaseModule,
DistrictModule,
NotificationModule,
],
controllers: [AppController],
providers: [
Expand Down
8 changes: 8 additions & 0 deletions backend/src/helpers/env.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ class EnvVariables {

@IsString()
FRONTEND_URL!: string;

@IsNumber()
@Transform(({ value }) => parseInt(value as string, 10))
NOTIFICATION_INDIVIDUAL_THRESHOLD!: number;

@IsNumber()
@Transform(({ value }) => parseInt(value as string, 10))
NOTIFICATION_BULK_THRESHOLD!: number;
}

export function validateEnv(config: Record<string, unknown>): EnvVariables {
Expand Down
19 changes: 18 additions & 1 deletion backend/src/modules/admin/admin.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ import {
import { AdminService } from './admin.service';
import { SkipThrottle } from '@nestjs/throttler';
import { StoreQueryValidator } from '../store/dto/store.dto';
import { AssignLocationDto, UserQueryValidator } from '../user/dto/user.dto';
import {
AssignLocationDto,
UserQueryValidator,
BulkApproveUsersDto,
} from '../user/dto/user.dto';

@Controller('admin')
@UseGuards(RoleGuard)
Expand Down Expand Up @@ -72,6 +76,19 @@ export class AdminController {
return this.userService.verifyUser(userId, req.user.sub);
}

@Mutation()
@HttpCode(HttpStatus.OK)
@Post('users/bulk-approve')
async bulkApproveUsers(
@Body() bulkApproveUsersDto: BulkApproveUsersDto,
@Req() req: Request & { user: { sub: string } },
) {
return this.userService.verifyUsersBulk(
bulkApproveUsersDto.userIds,
req.user.sub,
);
}

@Get('users')
async getUsers(@Query() query: UserQueryValidator) {
return this.userService.listUsers(query);
Expand Down
3 changes: 2 additions & 1 deletion backend/src/modules/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { UserModule } from '../user/user.module';
import { MailModule } from '../mail/mail.module';
import { AuthController } from './auth.controller';
import { TokenModule } from '../token/token.module';
import { NotificationModule } from '../notification/notification.module';

@Module({
imports: [UserModule, TokenModule, MailModule],
imports: [UserModule, TokenModule, MailModule, NotificationModule],
controllers: [AuthController],
providers: [AuthService],
})
Expand Down
6 changes: 6 additions & 0 deletions backend/src/modules/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { UserInterface } from '../user/types/user.interface';
import { CustomHttpException } from '~/helpers/custom.exception';
import CreateUserRecordOptions from '../user/types/create-user.type';
import { UserRole, UserStatus } from '../user/constants/user.constant';
import { NotificationService } from '../notification/notification.service';

const mockUserService = {
getUserByEmail: jest.fn(),
Expand Down Expand Up @@ -39,6 +40,10 @@ const mockConfigService = {
}),
};

const mockNotificationService = {
notifyNewUserSignup: jest.fn(),
};

const testEmail = 'test@example.com';
const testPassword = 'password123';
const testHashedPassword = 'hashedPassword123';
Expand Down Expand Up @@ -173,6 +178,7 @@ describe('AuthService', () => {
{ provide: TokenService, useValue: mockTokenService },
{ provide: MailService, useValue: mockMailService },
{ provide: ConfigService, useValue: mockConfigService },
{ provide: NotificationService, useValue: mockNotificationService },
],
}).compile();

Expand Down
22 changes: 22 additions & 0 deletions backend/src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { HttpStatus, Injectable, Logger } from '@nestjs/common';
import { CustomHttpException } from '~/helpers/custom.exception';
import CreateUserRecordOptions from '../user/types/create-user.type';
import { UserRole, UserStatus } from '../user/constants/user.constant';
import { NotificationService } from '../notification/notification.service';

@Injectable()
export class AuthService {
Expand All @@ -28,6 +29,7 @@ export class AuthService {
private readonly mailService: MailService,
private readonly tokenService: TokenService,
private readonly configService: ConfigService,
private readonly notificationService: NotificationService,
) {}
private readonly logger = new Logger('AuthService');

Expand Down Expand Up @@ -117,6 +119,16 @@ export class AuthService {
);
});

if (createdUser.status === UserStatus.UNVERIFIED) {
void this.notificationService
.notifyNewUserSignup(createdUser.id)
.catch(() => {
this.logger.error(
`Failed to queue notification for user ${createdUser.id}`,
);
});
}

return {
data: createdUser,
message: SYS_MSG.RESOURCE_CREATED_SUCCESSFULLY('User'),
Expand Down Expand Up @@ -191,6 +203,16 @@ export class AuthService {
);
});

if (createdUser.status === UserStatus.UNVERIFIED) {
void this.notificationService
.notifyNewUserSignup(createdUser.id)
.catch(() => {
this.logger.error(
`Failed to queue notification for user ${createdUser.id}`,
);
});
}

return {
message: SYS_MSG.RESOURCE_CREATED_SUCCESSFULLY('User'),
data: createdUser,
Expand Down
233 changes: 233 additions & 0 deletions backend/src/modules/mail/templates/user-approval-bulk.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
<html lang='en'>

<head>
<meta charset='UTF-8' />
<meta name='viewport' content='width=device-width, initial-scale=1.0' />
<meta http-equiv='X-UA-Compatible' content='ie=edge' />
<title>Bulk User Approval Required</title>
<style type='text/css'>
body,
table,
td,
a {
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}
table,
td {
mso-table-lspace: 0pt;
mso-table-rspace: 0pt;
}
img {
-ms-interpolation-mode: bicubic;
border: 0;
height: auto;
line-height: 100%;
outline: none;
text-decoration: none;
}
table {
border-collapse: collapse !important;
}
body {
height: 100% !important;
margin: 0 !important;
padding: 0 !important;
width: 100% !important;
background-color: #ffffff;
}
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap');
a[x-apple-data-detectors] {
color: inherit !important;
text-decoration: none !important;
font-size: inherit !important;
font-family: inherit !important;
font-weight: inherit !important;
line-height: inherit !important;
}
@media screen and (max-width: 600px) {
.email-container {
width: 100% !important;
max-width: 100% !important;
}
.content-padding {
padding-left: 20px !important;
padding-right: 20px !important;
}
.header-text {
font-size: 24px !important;
}
.button-td,
.button-a {
padding: 12px 25px !important;
font-size: 14px !important;
}
}
</style>
</head>

<body style='margin:0; padding:0; background-color:#ffffff;'>
<center>
<table
role='presentation'
cellpadding='0'
cellspacing='0'
border='0'
align='center'
width='100%'
style='max-width:600px;'
class='email-container'
>

<tr>
<td
style='background-color:#800000; padding:20px 30px;'
class='content-padding'
>
<table
role='presentation'
cellpadding='0'
cellspacing='0'
border='0'
width='100%'
>
<tr>
<td
style="font-family:'Inter',Arial,Helvetica,sans-serif; font-size:28px; font-weight:600; color:#ffffff; text-align:center;"
class='header-text'
>
Retail Intelligence
</td>
</tr>
</table>
</td>
</tr>

<tr>
<td
style="background-color:#ffffff; padding:30px 30px 20px; font-family:'Inter',Arial,Helvetica,sans-serif; font-size:14px; line-height:1.5; color:#333333;"
class='content-padding'
>
<p
style='margin:0 0 15px; font-size:16px; font-weight:600; color:#111111;'
>
Bulk User Approval Required
</p>
<p
style='margin:0 0 25px; font-size:14px; font-weight:400; color:#333333; line-height:1.6;'
>
{{userCount}}
new user(s) have signed up and require your approval:
</p>
<table
role='presentation'
cellpadding='0'
cellspacing='0'
border='0'
width='100%'
style='margin:0 0 25px 0; border:1px solid #e0e0e0; border-radius:4px;'
>
{{#each users}}
<tr>
<td
style='padding:12px 15px; border-bottom:1px solid #e0e0e0; font-size:14px; color:#333333;'
>
<strong>{{name}}</strong>
<br />
<span
style='color:#666666; font-size:13px;'
>{{email}}</span>
</td>
</tr>
{{/each}}
</table>
<table
role='presentation'
cellpadding='0'
cellspacing='0'
border='0'
align='left'
width='100%'
style='margin:0 auto 25px;'
>
<tr>
<td align='left'>
<table
role='presentation'
cellpadding='0'
cellspacing='0'
border='0'
align='left'
>
<tr>
<td
style='border-radius:4px; background-color:#800000;'
class='button-td'
>
<a
href='{{approvalUrl}}'
target='_blank'
style="font-size:14px; font-family:'Inter',Arial,Helvetica,sans-serif; font-weight:500; color:#ffffff; text-decoration:none; display:inline-block; padding:12px 30px; border-radius:4px;"
class='button-a'
>
Approve All Users
</a>
</td>
</tr>
</table>
</td>
</tr>
</table>

<p
style='margin:0 0 5px; font-size:14px; font-weight:500; color:#111111;'
>
Warm Regards,
</p>
<p
style='margin:0; font-size:14px; font-weight:500; color:#111111;'
>
Retail Intelligence
</p>
</td>
</tr>

<tr>
<td
style="background-color:#800000; padding:20px 30px; text-align:center; font-family:'Inter',Arial,Helvetica,sans-serif; font-size:12px; line-height:1.4; color:#ffffff;"
class='content-padding'
>
<p
style='margin:0 0 15px; font-size:14px; font-weight:400; color:#ffffff;'
>
Thank you for choosing Retail Intelligence!
</p>
<table
role='presentation'
cellpadding='0'
cellspacing='0'
border='0'
width='100%'
style='margin:0 auto 15px;'
>
<tr>
<td
style='font-size:0; line-height:0; border-top:1px solid #ffffff; height:1px; width:100%;'
></td>
</tr>
</table>
<p
style='margin:0; font-size:12px; font-weight:400; color:#ffffff;'
>
©
{{currentYear}}
Retail Intelligence. All rights reserved.
</p>
</td>
</tr>

</table>
</center>
</body>

</html>
Loading
Loading