Skip to content

Commit

Permalink
[Implement][Provider] Login API for Provider
Browse files Browse the repository at this point in the history
  • Loading branch information
nghiavohuynhdai authored and hideonbush106 committed Jan 26, 2024
1 parent 1aa6fff commit 21070c2
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { CustomerModule } from '@customer/customer.module'
import { AuthModule } from '@auth/auth.module'
import { ProductModule } from '@product/product.module'
import { CartModule } from '@cart/cart.module'
import { StaffModule } from '@staff/staff.module'

@Module({
imports: [
Expand Down Expand Up @@ -61,7 +62,8 @@ import { CartModule } from '@cart/cart.module'
CustomerModule,
AuthModule,
ProductModule,
CartModule
CartModule,
StaffModule
],
controllers: [AppController],
providers: [AppService]
Expand Down
7 changes: 5 additions & 2 deletions src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ import { JwtAccessStrategy } from '@auth/strategies/jwt-access.strategy'
import { PassportModule } from '@nestjs/passport'
import { CustomerModule } from '@customer/customer.module'
import { AuthService } from '@auth/services/auth.service'
import { AuthController } from '@auth/controllers/auth.controller'
import { AuthCustomerController } from '@auth/controllers/customer.controller'
import { JwtModule } from '@nestjs/jwt'
import { ConfigModule } from '@nestjs/config'
import { JwtRefreshStrategy } from '@auth/strategies/jwt-refresh.strategy'
import { StaffModule } from '@staff/staff.module'
import { AuthProviderController } from '@auth/controllers/provider.controller'

@Module({
imports: [
ConfigModule,
CustomerModule,
StaffModule,
PassportModule,
JwtModule
],
controllers: [AuthController],
controllers: [AuthCustomerController, AuthProviderController],
providers: [AuthService, JwtAccessStrategy, JwtRefreshStrategy],
exports: [AuthService]
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { UserSide } from '@common/contracts/constant'
import { JwtAuthGuard } from '@auth/guards/jwt-auth.guard'
import { RegisterReqDto } from '@auth/dto/register.dto'

@ApiTags('Auth')
@Controller()
export class AuthController {
@ApiTags('Auth - Customer')
@Controller('customer')
export class AuthCustomerController {
constructor(private readonly authService: AuthService) {}

@Post('customer/login')
@Post('login')
@ApiBody({ type: LoginReqDto })
@ApiOkResponse({ type: ResponseTokenDto })
@ApiBadRequestResponse({ type: ErrorResponse })
Expand All @@ -23,7 +23,7 @@ export class AuthController {
return res
}

@Post('customer/register')
@Post('register')
@ApiBody({ type: RegisterReqDto })
@ApiOkResponse({ type: ResponseSuccessDto })
@ApiBadRequestResponse({ type: ErrorResponse })
Expand All @@ -32,7 +32,7 @@ export class AuthController {
}

@UseGuards(JwtAuthGuard.REFRESH_TOKEN)
@Post('customer/refresh')
@Post('refresh')
@ApiBearerAuth()
@ApiOkResponse({ type: ResponseTokenDto })
@ApiBadRequestResponse({ type: ErrorResponse })
Expand Down
21 changes: 21 additions & 0 deletions src/auth/controllers/provider.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Body, Controller, Post } from "@nestjs/common"
import { ApiBadRequestResponse, ApiBody, ApiOkResponse, ApiTags } from "@nestjs/swagger"
import { UserSide } from "@common/contracts/constant"
import { ErrorResponse } from "@common/contracts/dto"
import { LoginReqDto } from "@auth/dto/login.dto"
import { ResponseTokenDto, TokenResDto } from "@auth/dto/token.dto"
import { AuthService } from "@auth/services/auth.service"

@ApiTags('Auth - Provider')
@Controller('provider')
export class AuthProviderController {
constructor(private readonly authService: AuthService) {}

@Post('login')
@ApiBody({ type: LoginReqDto })
@ApiOkResponse({ type: ResponseTokenDto })
@ApiBadRequestResponse({ type: ErrorResponse })
async login(@Body() loginReqDto: LoginReqDto): Promise<TokenResDto> {
return await this.authService.login(loginReqDto, UserSide.PROVIDER)
}
}
15 changes: 14 additions & 1 deletion src/auth/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ import { RefreshTokenPayload } from '@auth/strategies/jwt-refresh.strategy'
import { TokenResDto } from '@auth/dto/token.dto'
import { ConfigService } from '@nestjs/config'
import { RegisterReqDto } from '@auth/dto/register.dto'
import { StaffRepository } from '@staff/repositories/staff.repository'
import { Staff } from '@staff/schemas/staff.schema'

@Injectable()
export class AuthService {
constructor(
private readonly customerRepository: CustomerRepository,
private readonly staffRepository: StaffRepository,
private readonly jwtService: JwtService,
private readonly configService: ConfigService
) {}

public async login(loginReqDto: LoginReqDto, side: UserSide): Promise<TokenResDto> {
let user: Customer | null
let user: Customer | Staff
let userRole: UserRole

if (side === UserSide.CUSTOMER) {
Expand All @@ -34,6 +37,16 @@ export class AuthService {
userRole = UserRole.CUSTOMER
}

if (side === UserSide.PROVIDER) {
user = (await this.staffRepository.findOne({
conditions: {
email: loginReqDto.email
}
}))

userRole = user?.role
}

if (!user) throw new BadRequestException(Errors.WRONG_EMAIL_OR_PASSWORD.message)

const isPasswordMatch = await this.comparePassword(loginReqDto.password, user.password)
Expand Down
12 changes: 12 additions & 0 deletions src/staff/repositories/staff.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { PaginateModel } from 'mongoose'
import { Injectable } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'
import { AbstractRepository } from '@common/repositories'
import { Staff, StaffDocument } from '@staff/schemas/staff.schema'

@Injectable()
export class StaffRepository extends AbstractRepository<StaffDocument> {
constructor(@InjectModel(Staff.name) model: PaginateModel<StaffDocument>) {
super(model)
}
}
83 changes: 83 additions & 0 deletions src/staff/schemas/staff.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { ApiProperty } from '@nestjs/swagger'
import { Status, UserRole } from '@src/common/contracts/constant'
import { Transform } from 'class-transformer'
import { HydratedDocument } from 'mongoose'
import * as paginate from 'mongoose-paginate-v2'

export type StaffDocument = HydratedDocument<Staff>

@Schema({
collection: 'staffs',
timestamps: {
createdAt: true,
updatedAt: true
},
toJSON: {
transform(doc, ret) {
delete ret.__v
}
}
})
export class Staff {
constructor(id?: string) {
this._id = id
}

@ApiProperty()
@Transform(({ value }) => value?.toString())
_id: string

@ApiProperty()
@Prop({ type: String, maxlength: 30, required: true })
firstName: string

@ApiProperty()
@Prop({ type: String, maxlength: 30, required: true })
lastName: string

@ApiProperty()
@Prop({
type: String,
required: true
})
email: string

@ApiProperty()
@Prop({ type: String, required: true })
password: string

@ApiProperty()
@Prop({ type: String, required: true })
staffCode: string

@ApiProperty()
@Prop({
type: String,
required: true
})
phone: string

@ApiProperty()
@Prop({
type: String
})
avatar: string

@ApiProperty()
@Prop({
enum: UserRole,
default: UserRole.STAFF
})
role: UserRole

@Prop({
enum: Status,
default: Status.ACTIVE
})
status: Status
}

export const StaffSchema = SchemaFactory.createForClass(Staff)

StaffSchema.plugin(paginate)
12 changes: 12 additions & 0 deletions src/staff/staff.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common'
import { MongooseModule } from '@nestjs/mongoose'
import { StaffRepository } from '@staff/repositories/staff.repository'
import { Staff, StaffSchema } from '@staff/schemas/staff.schema'

@Module({
imports: [MongooseModule.forFeature([{ name: Staff.name, schema: StaffSchema }])],
controllers: [],
providers: [StaffRepository],
exports: [StaffRepository]
})
export class StaffModule {}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@booking/*": ["src/booking/*"],
"@product/*": ["src/product/*"],
"@cart/*": ["src/cart/*"],
"@staff/*": ["src/staff/*"],
}
}
}

0 comments on commit 21070c2

Please sign in to comment.