Skip to content

Commit

Permalink
Move regis to auth module
Browse files Browse the repository at this point in the history
  • Loading branch information
thetkpark committed Jun 7, 2021
1 parent da9520b commit 3f5ecc0
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 40 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@nestjs/config": "^0.6.3",
"@nestjs/core": "^7.5.1",
"@nestjs/mongoose": "^7.2.4",
"@nestjs/passport": "^7.1.5",
"@nestjs/platform-express": "^7.5.1",
"@nestjs/swagger": "^4.8.0",
"@nestjs/throttler": "^1.1.4",
Expand All @@ -35,6 +36,7 @@
"mongodb": "^3.6.9",
"mongoose": "^5.12.13",
"nanoid": "^3.1.23",
"passport": "^0.4.1",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^6.6.3",
Expand Down
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { LocationModule } from './location/location.module'
import { MongooseModule } from '@nestjs/mongoose'
import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler'
import { APP_GUARD } from '@nestjs/core'
import { AuthModule } from './auth/auth.module';

@Module({
imports: [
Expand All @@ -30,6 +31,7 @@ import { APP_GUARD } from '@nestjs/core'
UserModule,
ApiModule,
LocationModule,
AuthModule,
],
controllers: [AppController],
providers: [
Expand Down
45 changes: 45 additions & 0 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { UserService } from 'src/user/user.service'
import { BadRequestException, Body, Controller, NotFoundException, Post } from '@nestjs/common'
import { ApiBadRequestResponse, ApiNotFoundResponse, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'
import { RegisNewUserDto } from './dto/regis-new-user.dto'
import { RegisNewUserResponseDo } from './dto/regis-new-user-res.dto'
import { ApiService } from 'src/api/api.service'
import { UserDocument } from 'src/schema/User.schema'
import { OTPService } from 'src/api/otp.service'
import { LocationService } from 'src/location/location.service'

@Controller('auth')
@ApiTags('Authentication')
export class AuthController {
constructor(
private userService: UserService,
private apiService: ApiService,
private otpService: OTPService,
private locationService: LocationService
) {}

@Post('regis')
@ApiOperation({ summary: 'Register new user' })
@ApiResponse({ status: 201, type: RegisNewUserResponseDo })
@ApiBadRequestResponse({ description: 'LaserID and/or natioalID is/are in wrong format' })
@ApiNotFoundResponse({ description: 'Not found in national external API' })
async registerNewUser(@Body() { laserID, nationalID, phoneNumber, preferedLocation }: RegisNewUserDto) {
const personData = await this.apiService.searchByNationalID(nationalID, laserID)
const existingUser = await this.userService.findByNationalID(nationalID)
if (existingUser && existingUser.isPhoneVerify) throw new BadRequestException('User already register')

const preferedLocationDoc = await this.locationService.findById(preferedLocation)
if (!preferedLocation) throw new NotFoundException('Prefered location is not found')

let user: UserDocument
if (!existingUser) {
user = await this.userService.createUser(personData, phoneNumber, preferedLocationDoc)
} else {
existingUser.phoneNumber = phoneNumber
existingUser.preferedLocation = preferedLocationDoc
user = await existingUser.save()
}
// const refCode = await this.otpService.generatedAndSentOTP(user.id, phoneNumber)
// return { refCode }
}
}
13 changes: 13 additions & 0 deletions src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Module } from '@nestjs/common'
import { UserModule } from 'src/user/user.module'
import { AuthService } from './auth.service'
import { AuthController } from './auth.controller'
import { LocationModule } from 'src/location/location.module'
import { ApiModule } from 'src/api/api.module'

@Module({
imports: [UserModule, LocationModule, ApiModule],
providers: [AuthService],
controllers: [AuthController],
})
export class AuthModule {}
7 changes: 7 additions & 0 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Injectable } from '@nestjs/common'
import { UserService } from 'src/user/user.service'

@Injectable()
export class AuthService {
constructor(private userService: UserService) {}
}
File renamed without changes.
File renamed without changes.
41 changes: 3 additions & 38 deletions src/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
import { BadRequestException, Body, Controller, Get, NotFoundException, Post, Query } from '@nestjs/common'
import { ApiBadRequestResponse, ApiNotFoundResponse, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'
import { Controller, Get, Query } from '@nestjs/common'
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'
import { ApiService } from 'src/api/api.service'
import { OTPService } from 'src/api/otp.service'
import { UserDocument } from 'src/schema/User.schema'
import { LocationService } from 'src/location/location.service'
import { RegisNewUserDto } from './dto/regis-new-user.dto'
import { UserService } from './user.service'
import { RegisNewUserResponseDo } from './dto/regis-new-user-res.dto'

@Controller('user')
@ApiTags('User')
export class UserController {
constructor(
private userService: UserService,
private apiService: ApiService,
private locationService: LocationService,
private otpService: OTPService
) {}
constructor(private userService: UserService, private apiService: ApiService) {}

@Get('/nationalInfo')
@ApiOperation({ summary: 'Get user info from external national API' })
Expand All @@ -25,29 +15,4 @@ export class UserController {
async getPersonalInfo(@Query('nationalID') nationalID: string, @Query('laserID') laserID: string) {
return this.apiService.searchByNationalID(nationalID, laserID)
}

@Post('regis')
@ApiOperation({ summary: 'Register new user' })
@ApiResponse({ status: 201, type: RegisNewUserResponseDo })
@ApiBadRequestResponse({ description: 'LaserID and/or natioalID is/are in wrong format' })
@ApiNotFoundResponse({ description: 'Not found in national external API' })
async registerNewUser(@Body() { laserID, nationalID, phoneNumber, preferedLocation }: RegisNewUserDto) {
const personData = await this.apiService.searchByNationalID(nationalID, laserID)
const existingUser = await this.userService.findByNationalID(nationalID)
if (existingUser && existingUser.isPhoneVerify) throw new BadRequestException('User already register')

const preferedLocationDoc = await this.locationService.findById(preferedLocation)
if (!preferedLocation) throw new NotFoundException('Prefered location is not found')

let user: UserDocument
if (!existingUser) {
user = await this.userService.createUser(personData, phoneNumber, preferedLocationDoc)
} else {
existingUser.phoneNumber = phoneNumber
existingUser.preferedLocation = preferedLocationDoc
user = await existingUser.save()
}
const refCode = await this.otpService.generatedAndSentOTP(user.id, phoneNumber)
return { refCode }
}
}
4 changes: 2 additions & 2 deletions src/user/user.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { UserService } from './user.service'
import { UserController } from './user.controller'
import { User, UserSchema } from 'src/schema/User.schema'
import { ApiModule } from 'src/api/api.module'
import { LocationModule } from 'src/location/location.module'
import { MongooseModule } from '@nestjs/mongoose'

@Module({
imports: [MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]), LocationModule, ApiModule],
imports: [MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]), ApiModule],
providers: [UserService],
controllers: [UserController],
exports: [UserService],
})
export class UserModule {}
23 changes: 23 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,11 @@
resolved "https://registry.yarnpkg.com/@nestjs/mongoose/-/mongoose-7.2.4.tgz#60a50532a244279703344368d74ecd802aeda4a4"
integrity sha512-NTE/IwijFUEJytPHLoHRYe0X5p16W1Awf8tm6I3mWsIUuBnSDfMyN0fy30uVaM/exYvCkMwEsAVpeSeKVPMHxg==

"@nestjs/passport@^7.1.5":
version "7.1.5"
resolved "https://registry.yarnpkg.com/@nestjs/passport/-/passport-7.1.5.tgz#b32fc0492008d73ebae4327fbc0012a738a83664"
integrity sha512-Hu9hPxTdBZA0C4GrWTsSflzwsJ99oAk9jqAwpcszdFNqfjMjkPGuCM9QsVZbBP2bE8fxrVrPsNOILS6puY8e/A==

"@nestjs/platform-express@^7.5.1":
version "7.6.17"
resolved "https://registry.yarnpkg.com/@nestjs/platform-express/-/platform-express-7.6.17.tgz#80b6dc2ac3636af19b5d70573926b5b09da35810"
Expand Down Expand Up @@ -4676,6 +4681,19 @@ pascalcase@^0.1.1:
resolved "https://registry.yarnpkg.com/pascalcase/-/pascalcase-0.1.1.tgz#b363e55e8006ca6fe21784d2db22bd15d7917f14"
integrity sha1-s2PlXoAGym/iF4TS2yK9FdeRfxQ=

passport-strategy@1.x.x:
version "1.0.0"
resolved "https://registry.yarnpkg.com/passport-strategy/-/passport-strategy-1.0.0.tgz#b5539aa8fc225a3d1ad179476ddf236b440f52e4"
integrity sha1-tVOaqPwiWj0a0XlHbd8ja0QPUuQ=

passport@^0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/passport/-/passport-0.4.1.tgz#941446a21cb92fc688d97a0861c38ce9f738f270"
integrity sha512-IxXgZZs8d7uFSt3eqNjM9NQ3g3uQCW5avD8mRNoXV99Yig50vjuaez6dQK2qC0kVWPRTujxY0dWgGfT09adjYg==
dependencies:
passport-strategy "1.x.x"
pause "0.0.1"

path-exists@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-4.0.0.tgz#513bdbe2d3b95d7762e8c1137efa195c6c61b5b3"
Expand Down Expand Up @@ -4716,6 +4734,11 @@ path-type@^4.0.0:
resolved "https://registry.yarnpkg.com/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b"
integrity sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==

pause@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/pause/-/pause-0.0.1.tgz#1d408b3fdb76923b9543d96fb4c9dfd535d9cb5d"
integrity sha1-HUCLP9t2kjuVQ9lvtMnf1TXZy10=

picomatch@^2.0.4, picomatch@^2.2.1, picomatch@^2.2.3:
version "2.3.0"
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.0.tgz#f1f061de8f6a4bf022892e2d128234fb98302972"
Expand Down

0 comments on commit 3f5ecc0

Please sign in to comment.