From b0dbb4913712897068b9139ac831e368ca606b2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Carvalho?= Date: Sat, 30 Nov 2024 15:44:02 +0000 Subject: [PATCH] =?UTF-8?q?[back]=20=F0=9F=90=9B=20fix:=20list=20inventory?= =?UTF-8?q?=20movements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../list-inventory-movements-controller.ts | 10 ++++--- .../suppliers/list-suppliers-controller.ts | 11 +++++--- .../prisma-inventory-movements-mapper.ts | 1 + .../prisma/mappers/prisma-users-mapper.ts | 25 ++++++++++-------- .../prisma-inventory-movements-repository.ts | 26 ++++++++++++------- .../prisma-suppliers-repository.ts | 2 ++ .../repositories/prisma-users-repository.ts | 4 +-- .../src/api/services/categories-service.ts | 4 +-- .../web/src/api/services/suppliers-service.ts | 4 +-- .../supplier-select/use-supplier-select.ts | 5 ++-- .../src/domain/entities/inventory-movement.ts | 12 ++++++--- packages/core/src/domain/entities/user.ts | 17 ++++++++---- .../types/inventory-movements-list-params.ts | 2 +- .../core/src/types/suppliers-list-params.ts | 2 +- .../use-cases/auth/confirm-auth-use-case.ts | 2 +- .../core/src/use-cases/auth/login-use-case.ts | 3 +-- .../list-inventory-movements-use-case.ts | 12 +++++---- .../suppliers/list-suppliers-use-case.ts | 12 ++++++--- .../use-cases/users/list-users-use-case.ts | 12 +++++++-- 19 files changed, 107 insertions(+), 59 deletions(-) diff --git a/apps/server/src/api/controllers/inventory-movements/list-inventory-movements-controller.ts b/apps/server/src/api/controllers/inventory-movements/list-inventory-movements-controller.ts index f1e6fd48..0aa4c826 100644 --- a/apps/server/src/api/controllers/inventory-movements/list-inventory-movements-controller.ts +++ b/apps/server/src/api/controllers/inventory-movements/list-inventory-movements-controller.ts @@ -6,7 +6,7 @@ import { inventoryMovementsRepository } from '@/database' type QueryParams = { page?: string productId?: string - employeeId?: string + responsibleId?: string startDate?: string endDate?: string movementType?: 'inbound' | 'outbound' @@ -14,20 +14,22 @@ type QueryParams = { export class ListInventoryMovementsController { async handle(http: IHttp) { - const { page, productId, employeeId, startDate, endDate, movementType } = + const { page, productId, responsibleId, startDate, endDate, movementType } = http.getQueryParams() const { companyId } = await http.getUser() const useCase = new ListInventoryMovementsUseCase(inventoryMovementsRepository) + const params = { - page: page ? parseInt(page, 10) : undefined, + page: page ? Number(page) : 1, companyId, productId, - employeeId, + responsibleId, movementType, startDate: startDate ? new Date(startDate) : undefined, endDate: endDate ? new Date(endDate) : undefined, } const result = await useCase.execute(params) + return http.send(result, HTTP_STATUS_CODE.ok) } } diff --git a/apps/server/src/api/controllers/suppliers/list-suppliers-controller.ts b/apps/server/src/api/controllers/suppliers/list-suppliers-controller.ts index 328429b8..150c5e9f 100644 --- a/apps/server/src/api/controllers/suppliers/list-suppliers-controller.ts +++ b/apps/server/src/api/controllers/suppliers/list-suppliers-controller.ts @@ -14,13 +14,16 @@ export class ListSuppliersController { async handle(http: IHttp) { const { companyId } = await http.getUser() const { page, name } = http.getQueryParams() - if (!companyId) { - return http.send(null, HTTP_STATUS_CODE.badRequest) - } const pageNumber = parseInt(page || '1', 10) + console.log(page, name) + const useCase = new ListSuplliersUseCase(suppliersRepository) - const response = await useCase.execute({ page: pageNumber, name: name, companyId: companyId }) + const response = await useCase.execute({ + page: pageNumber, + name: name, + companyId: companyId, + }) return http.send(response, HTTP_STATUS_CODE.ok) } diff --git a/apps/server/src/database/prisma/mappers/prisma-inventory-movements-mapper.ts b/apps/server/src/database/prisma/mappers/prisma-inventory-movements-mapper.ts index dae0a2cd..fdac4796 100644 --- a/apps/server/src/database/prisma/mappers/prisma-inventory-movements-mapper.ts +++ b/apps/server/src/database/prisma/mappers/prisma-inventory-movements-mapper.ts @@ -27,6 +27,7 @@ export class PrismaInventoryMovementsMapper { if (prismaInventoryMovement.User) inventoryMovement.responsible = new PrismaUsersMapper().toDomain( prismaInventoryMovement.User, + false, ) return inventoryMovement diff --git a/apps/server/src/database/prisma/mappers/prisma-users-mapper.ts b/apps/server/src/database/prisma/mappers/prisma-users-mapper.ts index f0a0c57b..80a95c79 100644 --- a/apps/server/src/database/prisma/mappers/prisma-users-mapper.ts +++ b/apps/server/src/database/prisma/mappers/prisma-users-mapper.ts @@ -6,16 +6,19 @@ import type { UserRole } from '@stocker/core/types' import type { PrismaUser } from '../types' export class PrismaUsersMapper { - toDomain(prismaUser: PrismaUser): User { - return User.create({ - id: prismaUser.id, - name: prismaUser.name, - email: prismaUser.email, - password: prismaUser.password, - role: prismaUser.role.toLowerCase() as UserRole, - hasFirstPasswordReset: prismaUser.has_first_password_reset, - companyId: prismaUser.company_id, - }) + toDomain(prismaUser: PrismaUser, includePassword = true): User { + return User.create( + { + id: prismaUser.id, + name: prismaUser.name, + email: prismaUser.email, + password: prismaUser.password, + role: prismaUser.role.toLowerCase() as UserRole, + hasFirstPasswordReset: prismaUser.has_first_password_reset, + companyId: prismaUser.company_id, + }, + includePassword, + ) } toPrisma(user: User): PrismaUser { @@ -23,7 +26,7 @@ export class PrismaUsersMapper { id: user.id, name: user.name, email: user.email, - password: user.password, + password: String(user.password), role: user.role.toUpperCase() as PrismaUserRole, has_first_password_reset: user.hasFirstPasswordReset, company_id: user.companyId, diff --git a/apps/server/src/database/prisma/repositories/prisma-inventory-movements-repository.ts b/apps/server/src/database/prisma/repositories/prisma-inventory-movements-repository.ts index 5754b121..93094eaf 100644 --- a/apps/server/src/database/prisma/repositories/prisma-inventory-movements-repository.ts +++ b/apps/server/src/database/prisma/repositories/prisma-inventory-movements-repository.ts @@ -86,12 +86,9 @@ export class PrismaInventoryMovementsRepository implements IInventoryMovementsRe movementType, productId, companyId, - employeeId, + responsibleId, }: InventoryMovementsListParams) { try { - const whereCondition = productId ? { product_id: productId } : undefined - const count = await prisma.inventoryMovement.count({ where: whereCondition }) - let paginationParams = {} if (page) { @@ -124,9 +121,9 @@ export class PrismaInventoryMovementsRepository implements IInventoryMovementsRe productIdFilter = { product_id: productId } } - let employeeIdFilter = {} - if (employeeId) { - employeeIdFilter = { user_id: employeeId } + let responsibleIdFilter = {} + if (responsibleId) { + responsibleIdFilter = { user_id: responsibleId } } const prismaInventoryMovements = await prisma.inventoryMovement.findMany({ @@ -135,7 +132,7 @@ export class PrismaInventoryMovementsRepository implements IInventoryMovementsRe ...productIdFilter, ...movementTypeFilter, ...dateRangeParams, - ...employeeIdFilter, + ...responsibleIdFilter, Product: { company_id: companyId, }, @@ -154,10 +151,21 @@ export class PrismaInventoryMovementsRepository implements IInventoryMovementsRe }) const inventoryMovements = prismaInventoryMovements.map(this.mapper.toDomain) + const count = await prisma.inventoryMovement.count({ + where: { + ...productIdFilter, + ...movementTypeFilter, + ...dateRangeParams, + ...responsibleIdFilter, + Product: { + company_id: companyId, + }, + }, + }) return { inventoryMovements, - count: page ? count : inventoryMovements.length, + count, } } catch (error) { throw new PrismaError(error) diff --git a/apps/server/src/database/prisma/repositories/prisma-suppliers-repository.ts b/apps/server/src/database/prisma/repositories/prisma-suppliers-repository.ts index 45128f3d..247632de 100644 --- a/apps/server/src/database/prisma/repositories/prisma-suppliers-repository.ts +++ b/apps/server/src/database/prisma/repositories/prisma-suppliers-repository.ts @@ -139,6 +139,8 @@ export class PrismaSuppliersRepository implements ISuppliersRepository { }, }) + console.log({ page, name, companyId }) + const suppliers = prismaSuppliers.map(this.mapper.toDomain) return { diff --git a/apps/server/src/database/prisma/repositories/prisma-users-repository.ts b/apps/server/src/database/prisma/repositories/prisma-users-repository.ts index 20961879..8ce674d3 100644 --- a/apps/server/src/database/prisma/repositories/prisma-users-repository.ts +++ b/apps/server/src/database/prisma/repositories/prisma-users-repository.ts @@ -71,7 +71,7 @@ export class PrismaUsersRepository implements IUsersRepository { orderBy: { registered_at: 'desc' }, }) - return prismaUsers.map(this.mapper.toDomain) + return prismaUsers.map((prismaUser) => this.mapper.toDomain(prismaUser)) } catch (error) { throw new PrismaError(error) } @@ -106,7 +106,7 @@ export class PrismaUsersRepository implements IUsersRepository { }, }) - const users = prismaUsers.map(this.mapper.toDomain) + const users = prismaUsers.map((prismaUser) => this.mapper.toDomain(prismaUser)) return { users, diff --git a/apps/web/src/api/services/categories-service.ts b/apps/web/src/api/services/categories-service.ts index c38f685e..f057a64c 100644 --- a/apps/web/src/api/services/categories-service.ts +++ b/apps/web/src/api/services/categories-service.ts @@ -13,9 +13,9 @@ export const CategoriesService = (apiClient: IApiClient): ICategoriesService => return await apiClient.get(`/categories/${categoryId}`) }, - async listCategories({ page,name }) { + async listCategories({ page, name }) { apiClient.setParam('page', String(page)) - apiClient.setParam('name',String(name)) + if (name) apiClient.setParam('name', String(name)) return await apiClient.get>('/categories') }, diff --git a/apps/web/src/api/services/suppliers-service.ts b/apps/web/src/api/services/suppliers-service.ts index ad3fd9f4..71cf750e 100644 --- a/apps/web/src/api/services/suppliers-service.ts +++ b/apps/web/src/api/services/suppliers-service.ts @@ -13,9 +13,9 @@ export const SuppliersService = (apiClient: IApiClient): ISuppliersService => { return await apiClient.get(`/suppliers/${supplierId}`) }, - async listSuppliers({ page,name }) { + async listSuppliers({ page = 1, name }) { apiClient.setParam('page', String(page)) - apiClient.setParam('name',String(name)) + if (name) apiClient.setParam('name', String(name)) return await apiClient.get>('/suppliers') }, diff --git a/apps/web/src/ui/components/commons/supplier-select/use-supplier-select.ts b/apps/web/src/ui/components/commons/supplier-select/use-supplier-select.ts index 2304786f..a9c25161 100644 --- a/apps/web/src/ui/components/commons/supplier-select/use-supplier-select.ts +++ b/apps/web/src/ui/components/commons/supplier-select/use-supplier-select.ts @@ -1,7 +1,7 @@ import { CACHE } from '@/constants' import { useApi, useCache, useToast, useUrlParamNumber } from '@/ui/hooks' import { Supplier } from '@stocker/core/entities' -import { useEffect, useState } from 'react' +import { useState } from 'react' import { useAuthContext } from '../../contexts/auth-context' export function useSupplierSelect( @@ -36,7 +36,6 @@ export function useSupplierSelect( const response = await suppliersService.listSuppliers({ page, - companyId: company.id, }) if (response.isFailure) { showError(response.errorMessage) @@ -68,6 +67,8 @@ export function useSupplierSelect( })) } + console.log(suppliersData) + console.log(company?.id) const suppliers = suppliersData ? suppliersData.items.map(Supplier.create) : [] const itemsCount = suppliersData ? suppliersData.itemsCount : 0 diff --git a/packages/core/src/domain/entities/inventory-movement.ts b/packages/core/src/domain/entities/inventory-movement.ts index 407a0b8c..a0e43fd8 100644 --- a/packages/core/src/domain/entities/inventory-movement.ts +++ b/packages/core/src/domain/entities/inventory-movement.ts @@ -47,7 +47,7 @@ export class InventoryMovement extends Entity { if (dto.product.dto) inventoryMovement.product = Product.create(dto.product.dto) if (dto.responsible.dto) - inventoryMovement.responsible = User.create(dto.responsible.dto) + inventoryMovement.responsible = User.create(dto.responsible.dto, false) return inventoryMovement } @@ -61,8 +61,14 @@ export class InventoryMovement extends Entity { id: this.id, movementType: this.props.movementType, itemsCount: this.props.itemsCount, - responsible: this.props.responsible, - product: this.props.product, + responsible: { + id: this.props.responsible.id, + dto: this.props.responsible.entity?.dto, + }, + product: { + id: this.props.product.id, + dto: this.props.product.entity?.dto, + }, registeredAt: this.props.registeredAt, remark: this.props.remark ?? undefined, } diff --git a/packages/core/src/domain/entities/user.ts b/packages/core/src/domain/entities/user.ts index 59ace2fc..ad5cd460 100644 --- a/packages/core/src/domain/entities/user.ts +++ b/packages/core/src/domain/entities/user.ts @@ -7,7 +7,7 @@ type UserProps = { role: UserRole email: string name: string - password: string + password?: string companyId: string hasFirstPasswordReset: boolean } @@ -15,24 +15,31 @@ type UserProps = { const DEAFAULT_PASSWORD = 'stocker@123' export class User extends Entity { - static create(dto: UserDto): User { + static create(dto: UserDto, hasPassword = true): User { const role = dto.role if (!User.isUserRole(role)) { throw new ValidationError(`${role} não é um tipo de usuário válido`) } - return new User( + const user = new User( { name: dto.name, email: dto.email, - password: dto.password ?? DEAFAULT_PASSWORD, companyId: dto.companyId, role: role, hasFirstPasswordReset: dto.hasFirstPasswordReset ?? true, }, dto.id, ) + + if (hasPassword) { + user.password = dto.password ?? DEAFAULT_PASSWORD + } + + console.log('password', hasPassword) + + return user } static isUserRole(userRole: string): userRole is UserRole { @@ -68,7 +75,7 @@ export class User extends Entity { return this.props.email } - get password(): string { + get password(): string | undefined { return this.props.password } diff --git a/packages/core/src/types/inventory-movements-list-params.ts b/packages/core/src/types/inventory-movements-list-params.ts index ebd1d7ec..7c31033b 100644 --- a/packages/core/src/types/inventory-movements-list-params.ts +++ b/packages/core/src/types/inventory-movements-list-params.ts @@ -5,7 +5,7 @@ export type InventoryMovementsListParams = { companyId?: string startDate?: Date endDate?: Date - employeeId?: string + responsibleId?: string movementType?: InventoryMovementType productId?: string } diff --git a/packages/core/src/types/suppliers-list-params.ts b/packages/core/src/types/suppliers-list-params.ts index d8c1e8d1..bad3d8bd 100644 --- a/packages/core/src/types/suppliers-list-params.ts +++ b/packages/core/src/types/suppliers-list-params.ts @@ -1,5 +1,5 @@ export type SuppliersListParams = { page: number name?: string - companyId: string + companyId?: string } diff --git a/packages/core/src/use-cases/auth/confirm-auth-use-case.ts b/packages/core/src/use-cases/auth/confirm-auth-use-case.ts index ea37d59d..7dc4c98d 100644 --- a/packages/core/src/use-cases/auth/confirm-auth-use-case.ts +++ b/packages/core/src/use-cases/auth/confirm-auth-use-case.ts @@ -23,7 +23,7 @@ export class ConfirmAuthUseCase { const isPasswordValid = await this.cryptoProvider.validateHash( password, - user.password, + String(user.password), ) return isPasswordValid diff --git a/packages/core/src/use-cases/auth/login-use-case.ts b/packages/core/src/use-cases/auth/login-use-case.ts index 3cd4025d..ec79fc52 100644 --- a/packages/core/src/use-cases/auth/login-use-case.ts +++ b/packages/core/src/use-cases/auth/login-use-case.ts @@ -23,14 +23,13 @@ export class LoginUseCase { const isPasswordValid = await this.cryptoProvider.validateHash( password, - user.password, + String(user.password), ) if (!isPasswordValid) { throw new NotAllowedError('Credenciais inválidas') } - return user.dto } } diff --git a/packages/core/src/use-cases/inventory-movements/list-inventory-movements-use-case.ts b/packages/core/src/use-cases/inventory-movements/list-inventory-movements-use-case.ts index 08c4fb61..7731eed3 100644 --- a/packages/core/src/use-cases/inventory-movements/list-inventory-movements-use-case.ts +++ b/packages/core/src/use-cases/inventory-movements/list-inventory-movements-use-case.ts @@ -1,6 +1,6 @@ import type { InventoryMovementsListParams } from '../../types' import type { IInventoryMovementsRepository } from '../../interfaces' -import type { InventoryMovement } from '../../domain/entities' +import { PaginationResponse } from '../../responses' export class ListInventoryMovementsUseCase { private readonly inventoryMovementsRepository: IInventoryMovementsRepository @@ -9,11 +9,13 @@ export class ListInventoryMovementsUseCase { this.inventoryMovementsRepository = inventoryMovementsRepository } - async execute( - params: InventoryMovementsListParams, - ): Promise<{ inventoryMovements: InventoryMovement[]; count: number }> { + async execute(params: InventoryMovementsListParams) { const { inventoryMovements, count } = await this.inventoryMovementsRepository.findMany(params) - return { inventoryMovements, count } + + return new PaginationResponse({ + items: inventoryMovements.map((movement) => movement.dto), + itemsCount: count, + }) } } diff --git a/packages/core/src/use-cases/suppliers/list-suppliers-use-case.ts b/packages/core/src/use-cases/suppliers/list-suppliers-use-case.ts index 6dc9c488..55ccbd5d 100644 --- a/packages/core/src/use-cases/suppliers/list-suppliers-use-case.ts +++ b/packages/core/src/use-cases/suppliers/list-suppliers-use-case.ts @@ -2,8 +2,8 @@ import type { ISuppliersRepository } from '../../interfaces/repositories/supplie import { PaginationResponse } from '../../responses' type Request = { - page: number, - name?: string, + page: number + name?: string companyId: string } @@ -14,7 +14,13 @@ export class ListSuplliersUseCase { } async execute({ page, name, companyId }: Request) { - const { suppliers, count } = await this.suppliersRepository.findMany({ page, name, companyId }) + const { suppliers, count } = await this.suppliersRepository.findMany({ + page, + name, + companyId, + }) + + console.log(suppliers) return new PaginationResponse({ items: suppliers.map((supplier) => supplier.dto), diff --git a/packages/core/src/use-cases/users/list-users-use-case.ts b/packages/core/src/use-cases/users/list-users-use-case.ts index dda9ca13..f90b85ec 100644 --- a/packages/core/src/use-cases/users/list-users-use-case.ts +++ b/packages/core/src/use-cases/users/list-users-use-case.ts @@ -16,10 +16,18 @@ export class ListUsersUseCase { } async execute({ page, name, role, companyId }: Request) { - const { users, count } = await this.usersRepository.findMany({ page, name, role, companyId }) + const { users, count } = await this.usersRepository.findMany({ + page, + name, + role, + companyId, + }) return new PaginationResponse({ - items: users.map((user) => user.dto), + items: users.map((user) => { + user.dto.password = undefined + return user.dto + }), itemsCount: count, }) }