fixing: get product
This commit is contained in:
@@ -28,7 +28,7 @@ export class AuthService {
|
||||
username: user.username,
|
||||
sub: user.id,
|
||||
role: user.roles.name,
|
||||
partner: user.partner.id,
|
||||
partner: user.partner?.id,
|
||||
};
|
||||
|
||||
return {
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -263,7 +263,7 @@ export class UsersService {
|
||||
where: {
|
||||
username,
|
||||
},
|
||||
relations: ['roles'],
|
||||
relations: ['roles', 'partner'],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user