Merge branch 'development' into 'devops-staging'
Development See merge request empatnusabangsa/ppob/ppob-backend!20
This commit is contained in:
commit
ab50354f06
|
@ -10,8 +10,9 @@
|
|||
"build": "nest build",
|
||||
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
|
||||
"start": "nest start",
|
||||
"start:dev": "nest start --watch | pino-pretty",
|
||||
"start:debug": "nest start --debug --watch | pino-pretty",
|
||||
"start:formatted": "nest start | pino-pretty",
|
||||
"start:dev": "nest start --watch",
|
||||
"start:debug": "nest start --debug --watch",
|
||||
"start:prod": "node dist/main",
|
||||
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
|
||||
"test": "jest",
|
||||
|
@ -42,7 +43,6 @@
|
|||
"passport-jwt": "^4.0.0",
|
||||
"passport-local": "^1.0.0",
|
||||
"pg": "^8.7.1",
|
||||
"pino": "^7.5.1",
|
||||
"pino-http": "^6.3.0",
|
||||
"pino-pretty": "^7.3.0",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
import { Controller, Post, UseGuards, Request, Get } from '@nestjs/common';
|
||||
import { Controller, Get, Post, Request, UseGuards } from '@nestjs/common';
|
||||
import { LocalAuthGuard } from './local-auth.guard';
|
||||
import { AuthService } from './auth.service';
|
||||
import { JwtAuthGuard } from './jwt-auth.guard';
|
||||
import {Public} from "./public.decorator";
|
||||
import { Public } from './public.decorator';
|
||||
|
||||
@Controller({
|
||||
path: 'auth',
|
||||
|
@ -18,9 +17,8 @@ export class AuthController {
|
|||
return this.authService.login(req.user);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('profile')
|
||||
getProfile(@Request() req) {
|
||||
return req.user;
|
||||
return this.authService.getProfile(req.user.userId);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,4 +35,8 @@ export class AuthService {
|
|||
access_token: this.jwtService.sign(payload),
|
||||
};
|
||||
}
|
||||
|
||||
getProfile = async (userId: string) => {
|
||||
return this.usersService.findOne(userId);
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { ExtractJwt, Strategy } from 'passport-jwt';
|
||||
import { PassportStrategy } from '@nestjs/passport';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import {ConfigService} from "@nestjs/config";
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
|
||||
@Injectable()
|
||||
export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
|
@ -14,6 +14,9 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
|
|||
}
|
||||
|
||||
async validate(payload: any) {
|
||||
return { userId: payload.sub, username: payload.username };
|
||||
return {
|
||||
userId: payload.sub,
|
||||
username: payload.username,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ import { Product } from './product.entity';
|
|||
import { BaseModel } from '../../config/basemodel.entity';
|
||||
import { productType } from '../../helper/enum-list';
|
||||
import { User } from '../../users/entities/user.entity';
|
||||
import { Partner } from '../../users/entities/partner.entity';
|
||||
|
||||
@Entity()
|
||||
export class ProductHistoryPrice extends BaseModel {
|
||||
|
@ -12,8 +13,8 @@ export class ProductHistoryPrice extends BaseModel {
|
|||
@ManyToOne(() => Product, (product) => product.id)
|
||||
product: Product;
|
||||
|
||||
@ManyToOne(() => User, (user) => user.id)
|
||||
user: User;
|
||||
@ManyToOne(() => Partner, (partner) => partner.id)
|
||||
partner: Partner;
|
||||
|
||||
@Column()
|
||||
price: number;
|
||||
|
|
|
@ -12,6 +12,7 @@ import {
|
|||
import { ProductSubCategories } from './product-sub-category.entity';
|
||||
import { BaseModel } from '../../config/basemodel.entity';
|
||||
import { Supplier } from '../../users/entities/supplier.entity';
|
||||
import { ProductHistoryPrice } from './product-history-price.entity';
|
||||
|
||||
@Entity()
|
||||
export class Product extends BaseModel {
|
||||
|
@ -54,4 +55,16 @@ export class Product extends BaseModel {
|
|||
},
|
||||
)
|
||||
supplier: Supplier;
|
||||
|
||||
@OneToMany(
|
||||
() => {
|
||||
return ProductHistoryPrice;
|
||||
},
|
||||
(php) => {
|
||||
return php.product;
|
||||
},
|
||||
)
|
||||
priceHistory: ProductHistoryPrice;
|
||||
|
||||
currentPrice: ProductHistoryPrice;
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import {
|
|||
ParseUUIDPipe,
|
||||
HttpStatus,
|
||||
Query,
|
||||
Request,
|
||||
} from '@nestjs/common';
|
||||
import { ProductService } from './product.service';
|
||||
import { ProductCategoriesService } from './product-categories.service';
|
||||
|
@ -78,16 +79,37 @@ export class ProductController {
|
|||
};
|
||||
}
|
||||
|
||||
@Get('by-categories-all')
|
||||
async findByCategoriesAll(
|
||||
@Query('page') page: number,
|
||||
@Query('categories') categories: string,
|
||||
) {
|
||||
const data = await this.productService.findAllByCategories(
|
||||
page,
|
||||
categories,
|
||||
);
|
||||
|
||||
return {
|
||||
...data,
|
||||
statusCode: HttpStatus.OK,
|
||||
message: 'success',
|
||||
};
|
||||
}
|
||||
|
||||
@Get('by-categories')
|
||||
async findByCategories(
|
||||
@Query('page') page: number,
|
||||
@Query('categories') categories: string,
|
||||
@Request() req,
|
||||
) {
|
||||
const [data, count] = await this.productService.findAll(page);
|
||||
const data = await this.productService.findAllByCategoriesAndPartner(
|
||||
page,
|
||||
categories,
|
||||
req.user.username,
|
||||
);
|
||||
|
||||
return {
|
||||
data,
|
||||
count,
|
||||
...data,
|
||||
statusCode: HttpStatus.OK,
|
||||
message: 'success',
|
||||
};
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { Module } from '@nestjs/common';
|
||||
import { forwardRef, Module } from '@nestjs/common';
|
||||
import { ProductService } from './product.service';
|
||||
import { ProductController } from './product.controller';
|
||||
import { ProductCategoriesService } from './product-categories.service';
|
||||
|
@ -8,6 +8,7 @@ import { ProductCategories } from './entities/product-category.entity';
|
|||
import { ProductHistoryPrice } from './entities/product-history-price.entity';
|
||||
import { ProductSubCategories } from './entities/product-sub-category.entity';
|
||||
import { ProductSubCategoriesService } from './product-sub-categories.service';
|
||||
import { UsersModule } from '../users/users.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
|
@ -17,6 +18,7 @@ import { ProductSubCategoriesService } from './product-sub-categories.service';
|
|||
ProductHistoryPrice,
|
||||
ProductSubCategories,
|
||||
]),
|
||||
forwardRef(() => UsersModule),
|
||||
],
|
||||
controllers: [ProductController],
|
||||
providers: [
|
||||
|
|
|
@ -1,4 +1,9 @@
|
|||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import {
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Injectable,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import { EntityNotFoundError, Repository } from 'typeorm';
|
||||
import { Product } from './entities/product.entity';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
@ -8,6 +13,8 @@ import { UpdateProductDto } from './dto/product/update-product.dto';
|
|||
import { ProductHistoryPrice } from './entities/product-history-price.entity';
|
||||
import { productType } from '../helper/enum-list';
|
||||
import { UpdatePriceProductDto } from './dto/product/update-price-product.dto';
|
||||
import { Raw } from 'typeorm/browser';
|
||||
import { UsersService } from '../users/users.service';
|
||||
|
||||
export class ProductService {
|
||||
constructor(
|
||||
|
@ -16,6 +23,7 @@ export class ProductService {
|
|||
@InjectRepository(ProductHistoryPrice)
|
||||
private productHistoryPrice: Repository<ProductHistoryPrice>,
|
||||
private productSubCategoriesService: ProductSubCategoriesService,
|
||||
private usersService: UsersService,
|
||||
) {}
|
||||
|
||||
async create(createProductDto: CreateProductDto) {
|
||||
|
@ -54,18 +62,65 @@ export class ProductService {
|
|||
});
|
||||
}
|
||||
|
||||
findAllByCategories(page, categories) {
|
||||
return this.productRepository.findAndCount({
|
||||
join: {
|
||||
alias: 'sub_categories',
|
||||
innerJoin: { sub_categories: 'roles.users' },
|
||||
},
|
||||
skip: page * 10,
|
||||
take: 10,
|
||||
order: {
|
||||
version: 'DESC',
|
||||
},
|
||||
});
|
||||
async findAllByCategories(page, categories) {
|
||||
const baseQuery = this.productRepository
|
||||
.createQueryBuilder('product')
|
||||
.leftJoin('product.sub_categories', 'sub_categories')
|
||||
.where('sub_categories.category_id = :id', {
|
||||
id: categories,
|
||||
})
|
||||
.leftJoinAndMapOne(
|
||||
'product.currentPrice',
|
||||
'product.priceHistory',
|
||||
'current_price',
|
||||
'current_price.partner_id is null',
|
||||
);
|
||||
|
||||
const data = await baseQuery
|
||||
.skip(page * 10)
|
||||
.take(10)
|
||||
.getMany();
|
||||
|
||||
const totalData = await baseQuery.getCount();
|
||||
|
||||
return {
|
||||
data,
|
||||
count: totalData,
|
||||
};
|
||||
}
|
||||
|
||||
async findAllByCategoriesAndPartner(
|
||||
page: number,
|
||||
categories: string,
|
||||
username: string,
|
||||
) {
|
||||
const user = await this.usersService.findOneByUsername(username);
|
||||
|
||||
const baseQuery = this.productRepository
|
||||
.createQueryBuilder('product')
|
||||
.leftJoin('product.sub_categories', 'sub_categories')
|
||||
.where('sub_categories.category_id = :id', {
|
||||
id: categories,
|
||||
})
|
||||
.leftJoinAndMapOne(
|
||||
'product.currentPrice',
|
||||
'product.priceHistory',
|
||||
'current_price',
|
||||
'current_price.partner_id = :id_partner and current_price.end_date is NULL',
|
||||
)
|
||||
.setParameter('id_partner', user.partner.id);
|
||||
|
||||
const data = await baseQuery
|
||||
.skip(page * 10)
|
||||
.take(10)
|
||||
.getMany();
|
||||
|
||||
const totalData = await baseQuery.getCount();
|
||||
|
||||
return {
|
||||
data,
|
||||
count: totalData,
|
||||
};
|
||||
}
|
||||
|
||||
async findOne(code: string) {
|
||||
|
|
|
@ -17,7 +17,6 @@ import {
|
|||
typeTransaction,
|
||||
} from '../helper/enum-list';
|
||||
import { ProductService } from '../product/product.service';
|
||||
import * as irsService from '../helper/irs-service';
|
||||
import { CreateJournalDto } from './dto/create-journal.dto';
|
||||
import { UsersService } from 'src/users/users.service';
|
||||
import { AddSaldoSupplier } from './dto/add-saldo-supplier.dto';
|
||||
|
|
|
@ -191,6 +191,18 @@ export class UsersController {
|
|||
};
|
||||
}
|
||||
|
||||
@Get(':id/:type')
|
||||
async setStatusMembership(
|
||||
@Param('id', ParseUUIDPipe) id: string,
|
||||
@Param('type') type: string,
|
||||
) {
|
||||
return {
|
||||
data: await this.usersService.setStatus(id, type),
|
||||
statusCode: HttpStatus.CREATED,
|
||||
message: 'success',
|
||||
};
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
async update(
|
||||
@Param('id', ParseUUIDPipe) id: string,
|
||||
|
|
|
@ -261,6 +261,22 @@ export class UsersService {
|
|||
return userData;
|
||||
}
|
||||
|
||||
setStatus = async (id: string, type: string) => {
|
||||
const userData = new User();
|
||||
|
||||
if (type === 'active') {
|
||||
userData.isActive = true;
|
||||
} else {
|
||||
userData.isActive = false;
|
||||
}
|
||||
|
||||
await this.connection.transaction(async (manager) => {
|
||||
await manager.update(User, { id: id }, userData);
|
||||
});
|
||||
|
||||
return userData;
|
||||
};
|
||||
|
||||
async remove(id: string) {
|
||||
try {
|
||||
await this.usersRepository.findOneOrFail(id);
|
||||
|
@ -286,7 +302,7 @@ export class UsersService {
|
|||
where: {
|
||||
username,
|
||||
},
|
||||
relations: ['roles'],
|
||||
relations: ['roles', 'partner'],
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user