From 3f5ecc08a0fee657971637383a9776d55a22771e Mon Sep 17 00:00:00 2001 From: Sethanant Pipatpakorn Date: Mon, 7 Jun 2021 22:43:23 +0700 Subject: [PATCH] Move regis to auth module --- package.json | 2 + src/app.module.ts | 2 + src/auth/auth.controller.ts | 45 +++++++++++++++++++ src/auth/auth.module.ts | 13 ++++++ src/auth/auth.service.ts | 7 +++ .../dto/regis-new-user-res.dto.ts | 0 src/{user => auth}/dto/regis-new-user.dto.ts | 0 src/user/user.controller.ts | 41 ++--------------- src/user/user.module.ts | 4 +- yarn.lock | 23 ++++++++++ 10 files changed, 97 insertions(+), 40 deletions(-) create mode 100644 src/auth/auth.controller.ts create mode 100644 src/auth/auth.module.ts create mode 100644 src/auth/auth.service.ts rename src/{user => auth}/dto/regis-new-user-res.dto.ts (100%) rename src/{user => auth}/dto/regis-new-user.dto.ts (100%) diff --git a/package.json b/package.json index 497e560..f35c102 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/app.module.ts b/src/app.module.ts index 4dd7593..6cfc42e 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -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: [ @@ -30,6 +31,7 @@ import { APP_GUARD } from '@nestjs/core' UserModule, ApiModule, LocationModule, + AuthModule, ], controllers: [AppController], providers: [ diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts new file mode 100644 index 0000000..43c6950 --- /dev/null +++ b/src/auth/auth.controller.ts @@ -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 } + } +} diff --git a/src/auth/auth.module.ts b/src/auth/auth.module.ts new file mode 100644 index 0000000..a3df83d --- /dev/null +++ b/src/auth/auth.module.ts @@ -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 {} diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts new file mode 100644 index 0000000..03a873d --- /dev/null +++ b/src/auth/auth.service.ts @@ -0,0 +1,7 @@ +import { Injectable } from '@nestjs/common' +import { UserService } from 'src/user/user.service' + +@Injectable() +export class AuthService { + constructor(private userService: UserService) {} +} diff --git a/src/user/dto/regis-new-user-res.dto.ts b/src/auth/dto/regis-new-user-res.dto.ts similarity index 100% rename from src/user/dto/regis-new-user-res.dto.ts rename to src/auth/dto/regis-new-user-res.dto.ts diff --git a/src/user/dto/regis-new-user.dto.ts b/src/auth/dto/regis-new-user.dto.ts similarity index 100% rename from src/user/dto/regis-new-user.dto.ts rename to src/auth/dto/regis-new-user.dto.ts diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts index 833653f..2054a06 100644 --- a/src/user/user.controller.ts +++ b/src/user/user.controller.ts @@ -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' }) @@ -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 } - } } diff --git a/src/user/user.module.ts b/src/user/user.module.ts index 3892445..df981de 100644 --- a/src/user/user.module.ts +++ b/src/user/user.module.ts @@ -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 {} diff --git a/yarn.lock b/yarn.lock index 12847dc..315b374 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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"