Skip to content

De #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 27 commits into
base: feauture/supply-savvyy
Choose a base branch
from
Open

De #22

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e294372
Merge pull request #20 from zuttoo/feauture/supply-savvyy
Prajna1999 Jun 10, 2024
f7b2421
Merge pull request #21 from zuttoo/feauture/supply-savvyy
Prajna1999 Jun 10, 2024
ce2abe8
Merge pull request #23 from zuttoo/feauture/supply-savvyy
Prajna1999 Jun 10, 2024
bad0a10
Merge pull request #24 from zuttoo/feauture/supply-savvyy
Prajna1999 Jun 10, 2024
f81495f
Merge pull request #25 from zuttoo/feauture/supply-savvyy
Prajna1999 Jun 10, 2024
e6b6a61
Merge pull request #26 from zuttoo/feauture/supply-savvyy
Prajna1999 Jun 11, 2024
448136b
FIX: runner
Prajna1999 Jun 11, 2024
d547fbd
CHORE: refactor s3-utils to s3 service
Prajna1999 Jun 18, 2024
60f87ae
CHORE: bump braces package
Prajna1999 Jun 18, 2024
8f87993
Bump braces from 3.0.2 to 3.0.3
dependabot[bot] Jun 18, 2024
302d3a5
Merge pull request #27 from zuttoo/dependabot/npm_and_yarn/braces-3.0.3
Prajna1999 Jun 18, 2024
59c6d19
ADD: verify jwt
Prajna1999 Jun 18, 2024
ae65b24
ADD: authorization accesstoken
Prajna1999 Jun 21, 2024
3aab141
Merge pull request #28 from zuttoo/feature/auth
Prajna1999 Jun 21, 2024
ca1710a
Merge pull request #29 from zuttoo/feature/qassurify
Prajna1999 Jun 21, 2024
de86687
ADD: textract
Prajna1999 Jun 23, 2024
d5a4adc
ADD: S3 Service
Prajna1999 Jun 29, 2024
4ba1c25
Merge pull request #30 from zuttoo/feature/qassurify
Prajna1999 Jun 29, 2024
73ce728
ADD:optimaintain
Prajna1999 Jun 30, 2024
5693663
ADD:CRUD for optimaintain
Prajna1999 Jul 1, 2024
f40bb34
ADD: asset hieracrhy schema
Prajna1999 Jul 1, 2024
7964ef4
ADD: processlines schema
Prajna1999 Jul 2, 2024
d8f773b
ADD: create process line
Prajna1999 Jul 4, 2024
0c006b2
ADD: update processLine
Prajna1999 Jul 6, 2024
ce99073
Merge pull request #31 from zuttoo/optimaintain
Prajna1999 Jul 11, 2024
8d80bb7
ADD: SAP
Prajna1999 Jul 12, 2024
92d2d42
ADD:client CRUD routes
Prajna1999 Jul 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/deploy-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ jobs:
- name: Delete old container
run: docker rm -f zuttoo-backend-container
- name: Run docker container
run: docker run -d -p 8000:8000 -name zuttoo-backend-container prajna1999/zuttoo-backend
run: docker run -d -p 8000:8000 --name zuttoo-backend-container prajna1999/zuttoo-backend
11 changes: 11 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
coverageDirectory: 'coverage',
collectCoverageFrom: ['src/**/*.ts'],
globals: {
'ts-jest': {
tsconfig: 'tsconfig.json',
},
},
};
5,303 changes: 4,156 additions & 1,147 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
},
"dependencies": {
"@aws-sdk/client-cognito-identity-provider": "^3.569.0",
"@aws-sdk/client-s3": "^3.588.0",
"@aws-sdk/client-sesv2": "^3.569.0",
"@aws-sdk/client-textract": "^3.600.0",
"@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.2.2",
"@nestjs/core": "^10.0.0",
Expand All @@ -44,6 +44,7 @@
"@nestjs/typeorm": "^10.0.2",
"aws-jwt-verify": "^4.0.1",
"aws-sdk": "^2.1613.0",
"braces": "^3.0.3",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"config": "^3.3.11",
Expand All @@ -63,22 +64,27 @@
"uuid": "^9.0.1"
},
"devDependencies": {
"@aws-sdk/client-s3": "^3.598.0",
"@aws-sdk/s3-request-presigner": "^3.598.0",
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@sap-cloud-sdk/generator": "^3.16.0",
"@sap-cloud-sdk/openapi-generator": "^3.16.0",
"@swc/cli": "^0.3.12",
"@swc/core": "^1.4.13",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/jest": "^29.5.12",
"@types/multer": "^1.4.11",
"@types/node": "^20.3.1",
"@types/passport-jwt": "^4.0.1",
"@types/supertest": "^6.0.0",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"jest": "^29.5.0",
"jest": "^29.7.0",
"prettier": "^3.2.5",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
Expand Down
1 change: 1 addition & 0 deletions service-specifications/API_BUSINESS_PARTNER.edmx

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions service-specifications/options-per-service.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"service-specifications/API_BUSINESS_PARTNER.edmx": {
"packageName": "business-partner-service",
"directoryName": "business-partner-service",
"basePath": "/sap/opu/odata/sap/API_BUSINESS_PARTNER"
}
}
18 changes: 12 additions & 6 deletions src/addresses/entities/address.entity.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Client } from 'src/clients/entities/client.entity';
import { DefaultEntity } from '../../common/default.entity';
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity, OneToOne, JoinColumn, OneToMany } from 'typeorm';

Expand All @@ -6,20 +7,25 @@ export class Address extends DefaultEntity {


@Column()
address1: string;

@Column()
address2: string;
address: string;

@Column()
city: string;

@Column()
state: string;

@Column({
nullable:true
})
postalCode: string;

@Column()
country: string;

@Column()
postalCode: number;
@OneToOne(() => Client, (client)=>client.address)
@JoinColumn()
client: typeof Client;


}
10 changes: 10 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@ import { SuppliersModule } from './suppliers/suppliers.module';
import { OemsModule } from './oems/oems.module';
import { AddressesModule } from './addresses/addresses.module';
import { AuthModule } from './auth/auth.module';
import { AuthGuard } from './auth/auth.guard';
import { APP_GUARD } from '@nestjs/core';
import { MailModule } from './Mail/Mail.module';
import { OrdersModule } from './orders/orders.module';
import { ProductModule } from './product/product.module';
import { SupplyChainIssueModule } from './supplychain-issue/supplychain-issue.module';
import { QualityAssuranceModule } from './quality-assurance/quality-assurance.module';
import { S3Service } from './s3-service/s3-service';
import { OptimaintainModule } from './optimaintain/optimaintain.module';

import { SapService } from './sap/sap.service';



@Module({
Expand All @@ -30,6 +38,8 @@ import { SupplyChainIssueModule } from './supplychain-issue/supplychain-issue.mo
OemsModule,
ProductModule,
SupplyChainIssueModule,
QualityAssuranceModule,
OptimaintainModule,

],
controllers: [AppController],
Expand Down
1 change: 0 additions & 1 deletion src/app.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
getHello(): string {
Expand Down
2 changes: 2 additions & 0 deletions src/auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
4. User should be able to forget password.
5. User details with extra metadata to be stores in a RDS.
6. User details should be stored in neondb


22 changes: 21 additions & 1 deletion src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Controller, Post, Body, UsePipes, ValidationPipe } from '@nestjs/common';
import { Controller, Post, Body, UsePipes, ValidationPipe,Headers, Request, Req } from '@nestjs/common';
import { AuthService } from './auth.service';
import { CreateUserDto } from '../users/dto/create-user.dto';


@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}
Expand Down Expand Up @@ -37,4 +38,23 @@ export class AuthController {
){
return this.authService.confirmPasswordResetCode(confirmCodeDto.email, confirmCodeDto.code, confirmCodeDto.newPassword);
}

@Post('verify-jwt')
async verifyJwt(@Body('token') token:string , @Body('intent') intent:string){
return await this.authService.verifyJwt(token,intent);
}

@Post('refresh-access-token')
async refreshAccessToken(
@Body('refreshToken') refreshToken:string,
@Body('accessToken') accessToken:string
){
return await this.authService.refreshAccessToken(refreshToken,accessToken);
}
@Post('logout')
async logout(@Headers('refresh-token') refreshToken: string,
@Req() req:Request) {
console.log(req.headers);
return this.authService.logout(refreshToken);
}
}
29 changes: 29 additions & 0 deletions src/auth/auth.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { CanActivate, ExecutionContext, Injectable, UnauthorizedException } from "@nestjs/common";
import { AuthService } from "./auth.service";

@Injectable()
export class AuthGuard implements CanActivate{
constructor(private authService:AuthService){}

async canActivate(context:ExecutionContext):Promise<boolean>{
const request=context.switchToHttp().getRequest();
const authHeader=request.headers.authorization;

if(!authHeader){
throw new UnauthorizedException('No authorization header provided');
}

const [bearer, token]=authHeader.split(' ');
if(bearer !=='Bearer' || !token){
throw new UnauthorizedException('Invalid authorization header format');

}
try{
const payload=await this.authService.verifyJwt(token, 'ACCESS');
request['user']=payload;
return true;
}catch(error){
throw new UnauthorizedException('Invalid Exception');
}
}
}
8 changes: 6 additions & 2 deletions src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthController } from './auth.controller';
import { UsersModule } from '../users/users.module';
import { PassportModule } from '@nestjs/passport';
import { JwtStrategy } from './strategies/jwt.strategy';

@Module({
imports:[UsersModule],
imports:[UsersModule, PassportModule.register({defaultStrategy:'jwt'})],
controllers: [AuthController],
providers: [AuthService],
providers: [AuthService,JwtStrategy],
exports:[AuthService]
})
export class AuthModule {}
80 changes: 59 additions & 21 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ import {
ConfirmForgotPasswordCommandInput,
ConfirmForgotPasswordCommand,
ConfirmForgotPasswordCommandOutput,
AdminInitiateAuthCommandOutput
AdminInitiateAuthCommandOutput,
RevokeTokenCommandInput,
RevokeTokenCommand
} from '@aws-sdk/client-cognito-identity-provider';
import { AuthGuard } from '@nestjs/passport'
import { CognitoJwtVerifier } from "aws-jwt-verify";
import { createHmac, privateDecrypt } from 'crypto';
import { UsersService } from '../users/users.service';
import { InjectRepository } from '@nestjs/typeorm';
Expand All @@ -35,8 +39,6 @@ const { accessKey, secretKey, region } = config.get('awsConfig');
@Injectable()
export class AuthService {
private cognitoClient: CognitoIdentityProviderClient;


constructor(
private usersService: UsersService,
@InjectRepository(User) private userRepository:Repository<User>
Expand All @@ -47,6 +49,8 @@ export class AuthService {
accessKeyId: accessKey,
secretAccessKey: secretKey,
},


});

}
Expand Down Expand Up @@ -121,7 +125,6 @@ export class AuthService {
try {
const command = new AdminInitiateAuthCommand(params);
const response = await this.cognitoClient.send(command);
console.log(response);
return {
message:"Login Sucessful",
data:response
Expand Down Expand Up @@ -240,26 +243,26 @@ export class AuthService {
}
}

async refreshAccessToken(refreshToken:string, idToken:string):Promise<{message:'Successfully generate access token'; accessToken:string; idToken:string; refreshToken:string}>{
const email=this.getEmailFromIdToken(idToken);
const secretHash=this.calculateSecretHash(email);
async refreshAccessToken(refreshToken:string, accessToken:string):Promise<{message:'Successfully generate access token'; accessToken:string; idToken:string; refreshToken:string}>{
const decryptedPayload=await this.verifyJwt(accessToken,"ACCESS");
const secretHash=this.calculateSecretHash(decryptedPayload.sub);
const params:AdminInitiateAuthCommandInput={
UserPoolId:cognitoUserPoolId,
ClientId:cognitoClientId,
AuthFlow:'REFRESH_TOKEN_AUTH',
AuthParameters:{
REFRESH_TOKEN: refreshToken,
SECRET_HASH: secretHash,
SECRET_HASH: secretHash,
},
};
const command=new AdminInitiateAuthCommand(params);


try{
const response:AdminInitiateAuthCommandOutput=await this.cognitoClient.send(command);

const response=await this.cognitoClient.send(command);
const {AuthenticationResult}=response;
if(!AuthenticationResult){
throw new Error('Failed to refresh access token')
throw new Error('New Access Tokens and Id tokens could not be generated.')
};

return{
Expand All @@ -273,17 +276,7 @@ export class AuthService {
}
}

getEmailFromIdToken(idToken:string): string{
if(!idToken){
throw new Error('ID Token missing');
}
const decoded=jwt.decode(idToken) as {email?:string};

if(!decoded || !decoded.email){
throw new Error('Email not found in ID Token');
}
return decoded.email;
}
private async updateUserAttributes(userId:string, attributes: {[key:string]: string}):Promise <any>{
const params:AdminUpdateUserAttributesCommandInput={
UserPoolId:cognitoUserPoolId,
Expand Down Expand Up @@ -323,4 +316,49 @@ export class AuthService {
}

}

async logout(refreshToken:string):Promise<{message:string}>{
try{
// await this.verifyJwt(accessToken, 'ACCESS');

const params:RevokeTokenCommandInput={
Token:refreshToken,
ClientId:cognitoClientId,
ClientSecret:cognitoClientSecret,
}
const command=new RevokeTokenCommand(params);
await this.cognitoClient.send(command);
return {
message:'Logout Successful'
}

}catch(error){
throw new BadRequestException('Failed to Logout');
}
}

async verifyJwt(jwtToken:string, intent:string):Promise<any>{

let tokenUseIntent:any;
if(intent==="ID"){
tokenUseIntent='id'
}else if(intent==="ACCESS"){
tokenUseIntent='access'
}
const verifier=CognitoJwtVerifier.create({
userPoolId:cognitoUserPoolId,
tokenUse:tokenUseIntent,
clientId:cognitoClientId
});

try{
const payload=await verifier.verify(jwtToken);

return payload

}catch(error){
console.log("Token not valid");
throw new BadRequestException("Token Not Valid");
}
}
}
5 changes: 5 additions & 0 deletions src/auth/guards/jwt-auth.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

@Injectable()
export class JwtGuard extends AuthGuard('jwt') {}
Loading
Loading