-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from CoronaAdmin/abhiram_dev
added account module
- Loading branch information
Showing
13 changed files
with
431 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Controller, Logger, UseGuards, Get, Req,Post, Body } from '@nestjs/common'; | ||
import { ApiUseTags, ApiBearerAuth } from '@nestjs/swagger'; | ||
import { AccountService } from './account.service'; | ||
import { AuthGuard } from 'src/shared/auth-guard'; | ||
import { LoginDto } from './dto'; | ||
|
||
@ApiUseTags("Account Management") | ||
@Controller('api/v1/account') | ||
export class AccountController { | ||
private logger = new Logger("Account Controller") | ||
constructor( | ||
private readonly accountService:AccountService | ||
){} | ||
|
||
|
||
@ApiBearerAuth() | ||
@UseGuards(new AuthGuard()) | ||
@Get('user') | ||
getUser(@Req() req) { | ||
this.logger.verbose(`User Retrieved `); | ||
console.log(req); | ||
return this.accountService.getUser(req); | ||
} | ||
|
||
|
||
@ApiBearerAuth() | ||
@Post('login') | ||
login(@Body() loginDto:LoginDto){ | ||
this.logger.verbose('userLogged in ') | ||
return this.accountService.login(loginDto) | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AccountController } from './account.controller'; | ||
import { AccountService } from './account.service'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { Account } from './entity/account.entity'; | ||
import { AccountRepository } from './account.repository'; | ||
import { PassportModule } from '@nestjs/passport'; | ||
import { JwtModule } from '@nestjs/jwt'; | ||
import * as config from 'config'; | ||
const jwtConfig = config.get('jwt'); | ||
@Module({ | ||
imports:[ | ||
TypeOrmModule.forFeature([Account,AccountRepository]), | ||
PassportModule, | ||
JwtModule.register({ | ||
secret: process.env.JWT || jwtConfig.secret, | ||
signOptions: { expiresIn: '24h' }, | ||
}), | ||
], | ||
controllers: [AccountController], | ||
providers: [AccountService], | ||
exports: [AccountService] | ||
}) | ||
export class AccountModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { EntityRepository, Repository } from 'typeorm'; | ||
import { Account } from './entity/account.entity'; | ||
|
||
@EntityRepository(Account) | ||
export class AccountRepository extends Repository<Account> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { AccountRepository } from './account.repository'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { LoginDto } from './dto'; | ||
|
||
@Injectable() | ||
export class AccountService { | ||
private logger = new Logger('Account Service'); | ||
constructor( | ||
@InjectRepository(AccountRepository) | ||
private readonly accountRepository: AccountRepository, | ||
private readonly jwtService: JwtService, | ||
) {} | ||
|
||
async validateAccount(email: string): Promise<any> { | ||
const checkaccount = await this.accountRepository.findOne({ email: email }); | ||
if (checkaccount) { | ||
return checkaccount; | ||
} else return false; | ||
} | ||
async getUser(req: any): Promise<any> { | ||
if (req.user.type === 'ashaworker') { | ||
const { ashaWorkerId } = req.user; | ||
|
||
const ashaworker = await this.accountRepository.findOne({ | ||
ashaWorkerId: ashaWorkerId, | ||
}); | ||
if (ashaworker) { | ||
this.logger.verbose(`Faculty Logged In ${ashaworker.name}`); | ||
const { ...result } = ashaworker; | ||
delete result.id; | ||
let finalresult = { ...result, type: 'ashaworker' }; | ||
return { | ||
success: true, | ||
message: 'Success', | ||
data: finalresult, | ||
}; | ||
} | ||
} else { | ||
return { | ||
success: true, | ||
message: 'Success', | ||
data: { | ||
type: 'commonfolk', | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
async login(data: LoginDto): Promise<any> { | ||
const account = await this.validateAccount(data.email); | ||
if (account) { | ||
const { email, ashaWorkerId } = account; | ||
console.log(email,ashaWorkerId) | ||
const payload = { email, ashaWorkerId, type: 'ashaworker' }; | ||
return { | ||
success: true, | ||
// eslint-disable-next-line @typescript-eslint/camelcase | ||
access_token: this.jwtService.sign(payload), | ||
}; | ||
} else { | ||
const email = data.email; | ||
const payload = { email, type: 'commonfolk' }; | ||
return { | ||
success: true, | ||
// eslint-disable-next-line @typescript-eslint/camelcase | ||
access_token: this.jwtService.sign(payload), | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { ApiModelProperty } from "@nestjs/swagger"; | ||
|
||
|
||
export class LoginDto{ | ||
@ApiModelProperty({example:null}) | ||
email: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {LoginDto} from './LoginDto.dto'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { | ||
Column, | ||
CreateDateColumn, | ||
Entity, | ||
PrimaryGeneratedColumn, | ||
Unique, | ||
} from 'typeorm'; | ||
|
||
@Entity('account') | ||
@Unique(['email', 'ashaWorkerId']) | ||
export class Account { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column({ length: 128 }) | ||
ashaWorkerId: string; | ||
|
||
@Column({ length: 128 }) | ||
name: string; | ||
|
||
@Column({ length: 128 }) | ||
email: string; | ||
|
||
@Column({length:128}) | ||
status: string; | ||
|
||
@Column({ nullable: true }) | ||
lastLogin: Date; | ||
|
||
@CreateDateColumn() | ||
createdAt: Date; | ||
|
||
@CreateDateColumn() | ||
updatedAt: Date; | ||
} |
Oops, something went wrong.