Skip to content

Commit

Permalink
Merge pull request #1 from CoronaAdmin/abhiram_dev
Browse files Browse the repository at this point in the history
added account module
  • Loading branch information
abhirampai committed Jun 22, 2020
2 parents 982871c + a51198b commit eb22541
Show file tree
Hide file tree
Showing 13 changed files with 431 additions and 13 deletions.
125 changes: 123 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start": "nest start --watch",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
Expand All @@ -32,6 +32,8 @@
"class-transformer": "^0.2.3",
"class-validator": "^0.11.1",
"config": "^3.3.1",
"passport": "^0.4.1",
"pg": "^8.2.1",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^6.5.4",
Expand Down
33 changes: 33 additions & 0 deletions src/account/account.controller.ts
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)

}
}
24 changes: 24 additions & 0 deletions src/account/account.module.ts
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 {}
5 changes: 5 additions & 0 deletions src/account/account.repository.ts
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> {}
72 changes: 72 additions & 0 deletions src/account/account.service.ts
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),
};
}
}
}
7 changes: 7 additions & 0 deletions src/account/dto/LoginDto.dto.ts
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;
}
1 change: 1 addition & 0 deletions src/account/dto/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {LoginDto} from './LoginDto.dto';
35 changes: 35 additions & 0 deletions src/account/entity/account.entity.ts
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;
}
Loading

0 comments on commit eb22541

Please sign in to comment.