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