diff --git a/package.json b/package.json index eae83c4..e78fde3 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index b3eb1bb..cbc862f 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -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); } } diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 3e76f1c..56f4b63 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -35,4 +35,8 @@ export class AuthService { access_token: this.jwtService.sign(payload), }; } + + getProfile = async (userId: string) => { + return this.usersService.findOne(userId); + }; } diff --git a/src/auth/jwt.strategy.ts b/src/auth/jwt.strategy.ts index 08eccd1..43d220d 100644 --- a/src/auth/jwt.strategy.ts +++ b/src/auth/jwt.strategy.ts @@ -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, + }; } } diff --git a/src/product/entities/product-history-price.entity.ts b/src/product/entities/product-history-price.entity.ts index 6abf098..033b34d 100644 --- a/src/product/entities/product-history-price.entity.ts +++ b/src/product/entities/product-history-price.entity.ts @@ -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; diff --git a/src/product/entities/product.entity.ts b/src/product/entities/product.entity.ts index cc2241f..16c8b37 100644 --- a/src/product/entities/product.entity.ts +++ b/src/product/entities/product.entity.ts @@ -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; } diff --git a/src/product/product.controller.ts b/src/product/product.controller.ts index 691f3e1..cdf0348 100644 --- a/src/product/product.controller.ts +++ b/src/product/product.controller.ts @@ -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', }; diff --git a/src/product/product.module.ts b/src/product/product.module.ts index e514519..b0fc683 100644 --- a/src/product/product.module.ts +++ b/src/product/product.module.ts @@ -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: [ diff --git a/src/product/product.service.ts b/src/product/product.service.ts index 0e14188..51f1790 100644 --- a/src/product/product.service.ts +++ b/src/product/product.service.ts @@ -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, 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) { diff --git a/src/transaction/transaction.service.ts b/src/transaction/transaction.service.ts index 3af4c7a..86e609b 100644 --- a/src/transaction/transaction.service.ts +++ b/src/transaction/transaction.service.ts @@ -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'; diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index f8e4e9c..4c15b31 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -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, diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 743e04c..74edf69 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -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'], }); }