Skip to content

Commit

Permalink
add user onboard status
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdip-b committed Jun 18, 2024
1 parent e6f3a0e commit 55db857
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"cSpell.words": ["onboarded"]
}
13 changes: 13 additions & 0 deletions src/auth/guard/auth/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import {
ExecutionContext,
ForbiddenException,
Injectable,
UnauthorizedException,
} from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { Request } from 'express';
import { Reflector } from '@nestjs/core';
import { IS_PUBLIC_KEY } from '../../../decorators/public.decorator';
import { PrismaService } from '../../../prisma/prisma.service';
import { User } from '@prisma/client';
import { ONBOARDING_BYPASSED } from '../../../decorators/bypass-onboarding.decorator';

const X_E2E_USER_EMAIL = 'x-e2e-user-email';

Expand Down Expand Up @@ -74,6 +76,17 @@ export class AuthGuard implements CanActivate {
}
}

const onboardingBypassed =
this.reflector.getAllAndOverride<boolean>(ONBOARDING_BYPASSED, [
context.getHandler(),
context.getClass(),
]) ?? false;

// If the onboarding is not finished, we throw an UnauthorizedException.
if (!onboardingBypassed && !user.onboarded) {
throw new UnauthorizedException('Onboarding not finished');
}

// We attach the user to the request object.
request['user'] = user;
return true;
Expand Down
11 changes: 11 additions & 0 deletions src/decorators/bypass-onboarding.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { SetMetadata } from '@nestjs/common';

/**
* There are some routes that we want the users to be able to access
* even before they are done with the onboarding process. This decorator
* is used to mark those routes.
*/

export const ONBOARDING_BYPASSED = 'onboarding_bypassed';
export const BypassOnboardingCheck = () =>
SetMetadata(ONBOARDING_BYPASSED, true);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "User" ADD COLUMN "onboarded" BOOLEAN NOT NULL DEFAULT false;
1 change: 1 addition & 0 deletions src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ model User {
email String? @unique
name String?
location String?
onboarded Boolean @default(false)
profilePictureUrl String?
socialAccounts SocialAccount[]
authType AuthType
Expand Down
9 changes: 9 additions & 0 deletions src/user/controller/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Controller,
Delete,
Get,
Patch,
Put,
Req,
Res,
Expand All @@ -29,6 +30,7 @@ import { LinkedInOAuthStrategyFactory } from '../../oauth/factory/linkedin/linke
import { AuthGuard } from '@nestjs/passport';
import { UpdateUserSettingsDto } from '../dto/update-user-settings.dto';
import { UserSettingsDto } from '../dto/user-settings.dto';
import { BypassOnboardingCheck } from '../../decorators/bypass-onboarding.decorator';

@Controller('user')
@ApiBearerAuth()
Expand All @@ -40,6 +42,7 @@ export class UserController {
) {}

@Get()
@BypassOnboardingCheck()
@ApiOperation({
summary: 'Get current user',
description: 'Get the currently logged in user',
Expand Down Expand Up @@ -143,6 +146,12 @@ export class UserController {
return this.userService.updateSettings(user.id, data);
}

@Patch('onboard')
@BypassOnboardingCheck()
async onboardUser(@CurrentUser() user: User) {
return this.userService.onboardUser(user);
}

/**
* Delete the entire user account
*/
Expand Down
12 changes: 12 additions & 0 deletions src/user/service/user.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
BadRequestException,
ConflictException,
Inject,
Injectable,
InternalServerErrorException,
Expand Down Expand Up @@ -138,6 +139,17 @@ export class UserService {
}
}

async onboardUser(user: User) {
if (user.onboarded) {
throw new ConflictException('User has already been onboarded');
}

await this.prisma.user.update({
where: { id: user.id },
data: { onboarded: true },
});
}

updateSettings(id: string, data: UpdateUserSettingsDto) {
return this.prisma.userSettings.update({
where: {
Expand Down
1 change: 1 addition & 0 deletions src/user/user.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('User Controller Tests', () => {
name: 'John Doe',
isEmailVerified: true,
authType: AuthType.EMAIL,
onboarded: true,
},
});
});
Expand Down

0 comments on commit 55db857

Please sign in to comment.