Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
kaufon committed Nov 29, 2024
2 parents 7ad1550 + f6a4a5f commit 7e0ff0e
Show file tree
Hide file tree
Showing 29 changed files with 2,942 additions and 3,559 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class ListProductsController {
async handle(http: IHttp) {
const { companyId } = await http.getUser()
const { page, name, locationId, categoryId, supplierId } = http.getQueryParams<RouteParams>()
const pageNumber = parseInt(page || '1', 10)
const pageNumber = parseInt(page || '1')

const useCase = new ListProductsUseCase(productsRepository)
const response = await useCase.execute({ page: pageNumber, name: name, locationId: locationId, categoryId: categoryId, supplierId: supplierId, companyId: companyId })
Expand Down
33 changes: 28 additions & 5 deletions apps/server/src/database/prisma/mappers/prisma-product-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { PrismaProduct } from '../types'

export class PrismaProductMapper {
toDomain(prismaProduct: PrismaProduct): Product {
return Product.create({
const product = Product.create({
id: prismaProduct.id,
name: prismaProduct.name,
image: prismaProduct.image,
Expand All @@ -13,15 +13,35 @@ export class PrismaProductMapper {
sellingPrice: prismaProduct.selling_price,
description: prismaProduct.description,
height: prismaProduct.height,
categoryId: prismaProduct.category_id,
category: prismaProduct.category
? {
id: prismaProduct.category.id,
dto: {
id: prismaProduct.category.id,
name: prismaProduct.category.name,
companyId: prismaProduct.category.company_id,
subCategories: [],
},
}
: undefined,
companyId: prismaProduct.company_id,
uom: prismaProduct.uom,
weight: prismaProduct.weight,
width: prismaProduct.width,
isActive: prismaProduct.is_active,
model: prismaProduct.model,
length: prismaProduct.length,
supplierId: prismaProduct.supplier_id ?? undefined,
supplier: prismaProduct.supplier
? {
id: prismaProduct.supplier.id,
dto: {
id: prismaProduct.supplier.id,
name: prismaProduct.supplier.name,
email: prismaProduct.supplier.email,
companyId: prismaProduct.supplier.company_id,
},
}
: undefined,
minimumStock: prismaProduct.minimum_stock,
batches: prismaProduct.batches
.filter((batch) => Boolean(batch.id))
Expand All @@ -35,6 +55,8 @@ export class PrismaProductMapper {
resgisteredAt: prismaBatch.registered_at,
})),
})

return product
}

toPrisma(product: Product): PrismaProduct {
Expand All @@ -45,7 +67,7 @@ export class PrismaProductMapper {
name: productDto.name,
brand: productDto.brand,
height: productDto.height,
category_id: productDto.categoryId ?? null,
category_id: productDto.category?.id ?? null,
company_id: productDto.companyId,
code: productDto.code,
description: productDto.description,
Expand All @@ -58,7 +80,8 @@ export class PrismaProductMapper {
weight: productDto.weight,
width: productDto.width,
is_active: productDto.isActive,
supplier_id: productDto.supplierId ?? null,
supplier_id: productDto.supplier?.id ?? null,
location_id: productDto.location?.id ?? null,
model: productDto.model ?? null,
batches: productDto.batches?.length
? productDto.batches.map((batchDto) => ({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AddForeignKey
ALTER TABLE "products" ADD CONSTRAINT "products_supplier_id_fkey" FOREIGN KEY ("supplier_id") REFERENCES "suppliers"("id") ON DELETE SET NULL ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- AlterTable
ALTER TABLE "products" ADD COLUMN "location_id" TEXT;

-- AddForeignKey
ALTER TABLE "products" ADD CONSTRAINT "products_location_id_fkey" FOREIGN KEY ("location_id") REFERENCES "locations"("id") ON DELETE SET NULL ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class PrismaBatchesRepository implements IBatchesRepository {
try {
return prisma.batch.count({
where: {
Product: {
product: {
company_id: companyId,
},
},
Expand All @@ -144,7 +144,7 @@ export class PrismaBatchesRepository implements IBatchesRepository {
items_count: true,
},
where: {
Product: {
product: {
company_id: companyId,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ export class PrismaNotificationsRepository implements INotificationsRepository {
id: notificationId,
},
include: {
Product: true,
product: {
include: {
batches: true,
},
},
},
})

Expand All @@ -37,7 +41,7 @@ export class PrismaNotificationsRepository implements INotificationsRepository {
id: notificationId,
},
include: {
Batch: true,
batch: true,
},
})

Expand All @@ -60,7 +64,11 @@ export class PrismaNotificationsRepository implements INotificationsRepository {
product_id: productId,
},
include: {
Product: true,
product: {
include: {
batches: true,
},
},
},
})

Expand All @@ -82,7 +90,11 @@ export class PrismaNotificationsRepository implements INotificationsRepository {
},
orderBy: { registered_at: 'desc' },
include: {
Product: true,
product: {
include: {
batches: true,
},
},
},
})
const notifications = prismaNotifications.map(
Expand All @@ -105,7 +117,7 @@ export class PrismaNotificationsRepository implements INotificationsRepository {
},
orderBy: { registered_at: 'desc' },
include: {
Batch: true,
batch: true,
},
})
const mappedNotifications = notifications.map((notification) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class PrismaProductsRepository implements IProductsRepository {
weight: prismaProduct.weight,
company_id: prismaProduct.company_id,
category_id: prismaProduct.category_id,
supplier_id: prismaProduct.supplier_id,
selling_price: prismaProduct.selling_price,
uom: prismaProduct.uom,
code: prismaProduct.code,
Expand Down Expand Up @@ -67,6 +68,7 @@ export class PrismaProductsRepository implements IProductsRepository {
weight: prismaProduct.weight,
company_id: prismaProduct.company_id,
category_id: prismaProduct.category_id,
supplier_id: prismaProduct.supplier_id,
selling_price: prismaProduct.selling_price,
uom: prismaProduct.uom,
code: prismaProduct.code,
Expand All @@ -88,6 +90,8 @@ export class PrismaProductsRepository implements IProductsRepository {
id: productId,
},
include: {
category: true,
supplier: true,
batches: {
orderBy: [
{
Expand Down Expand Up @@ -119,6 +123,8 @@ export class PrismaProductsRepository implements IProductsRepository {
}: ProductsListParams) {
try {
const prismaProducts = await prisma.product.findMany({
take: PAGINATION.itemsPerPage,
skip: page > 0 ? (page - 1) * PAGINATION.itemsPerPage : 1,
where: {
company_id: companyId,
...(name && { name: { contains: name, mode: 'insensitive' } }),
Expand All @@ -128,6 +134,8 @@ export class PrismaProductsRepository implements IProductsRepository {
},
orderBy: { registered_at: 'desc' },
include: {
category: true,
supplier: true,
batches: {
orderBy: [
{
Expand Down Expand Up @@ -169,6 +177,8 @@ export class PrismaProductsRepository implements IProductsRepository {
company_id: companyId,
},
include: {
category: true,
supplier: true,
batches: {
orderBy: [
{
Expand Down Expand Up @@ -211,6 +221,8 @@ export class PrismaProductsRepository implements IProductsRepository {
},
orderBy: { registered_at: 'desc' },
include: {
category: true,
supplier: true,
batches: {
orderBy: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class PrismaSuppliersRepository implements ISuppliersRepository {
async add(supplier: Supplier): Promise<void> {
try {
const prismaSupplier = this.mapper.toPrisma(supplier)
await prisma.suppliers.create({
await prisma.supplier.create({
data: {
id: prismaSupplier.id,
email: prismaSupplier.email,
Expand All @@ -32,7 +32,7 @@ export class PrismaSuppliersRepository implements ISuppliersRepository {
async addMany(suppliers: Supplier[]): Promise<void> {
try {
const prismaSuppliers = suppliers.map(this.mapper.toPrisma)
await prisma.suppliers.createMany({
await prisma.supplier.createMany({
data: prismaSuppliers.map((prismaSupplier) => ({
id: prismaSupplier.id,
email: prismaSupplier.email,
Expand All @@ -50,7 +50,7 @@ export class PrismaSuppliersRepository implements ISuppliersRepository {

async deleteMany(suppliersIds: string[]): Promise<void> {
try {
await prisma.suppliers.deleteMany({
await prisma.supplier.deleteMany({
where: {
id: { in: suppliersIds },
},
Expand All @@ -62,7 +62,7 @@ export class PrismaSuppliersRepository implements ISuppliersRepository {

async findById(supplierId: string): Promise<Supplier | null> {
try {
const prismaSupplier = await prisma.suppliers.findUnique({
const prismaSupplier = await prisma.supplier.findUnique({
where: { id: supplierId },
})
if (!prismaSupplier) return null
Expand All @@ -74,7 +74,7 @@ export class PrismaSuppliersRepository implements ISuppliersRepository {

async findByCnpj(cnpj: string): Promise<Supplier | null> {
try {
const prismaSupplier = await prisma.suppliers.findFirst({
const prismaSupplier = await prisma.supplier.findFirst({
where: {
cnpj,
},
Expand All @@ -90,7 +90,7 @@ export class PrismaSuppliersRepository implements ISuppliersRepository {

async findByEmail(email: string): Promise<Supplier | null> {
try {
const prismaSupplier = await prisma.suppliers.findFirst({
const prismaSupplier = await prisma.supplier.findFirst({
where: {
email,
},
Expand All @@ -106,7 +106,7 @@ export class PrismaSuppliersRepository implements ISuppliersRepository {

async findByPhone(phone: string): Promise<Supplier | null> {
try {
const prismaSupplier = await prisma.suppliers.findFirst({
const prismaSupplier = await prisma.supplier.findFirst({
where: {
phone,
},
Expand All @@ -122,7 +122,7 @@ export class PrismaSuppliersRepository implements ISuppliersRepository {

async findMany({ page, name, companyId }: SuppliersListParams) {
try {
const prismaSuppliers = await prisma.suppliers.findMany({
const prismaSuppliers = await prisma.supplier.findMany({
take: PAGINATION.itemsPerPage,
skip: page > 0 ? (page - 1) * PAGINATION.itemsPerPage : 1,
where: {
Expand All @@ -132,7 +132,7 @@ export class PrismaSuppliersRepository implements ISuppliersRepository {
orderBy: { registered_at: 'desc' },
})

const count = await prisma.suppliers.count({
const count = await prisma.supplier.count({
where: {
company_id: companyId,
...(name && { name: { contains: name, mode: 'insensitive' } }),
Expand All @@ -153,7 +153,7 @@ export class PrismaSuppliersRepository implements ISuppliersRepository {
async update(supplier: Supplier, supplierId: string): Promise<void> {
try {
const prismaSupplier = this.mapper.toPrisma(supplier)
await prisma.suppliers.update({
await prisma.supplier.update({
data: {
email: prismaSupplier.email,
name: prismaSupplier.name,
Expand Down
Loading

0 comments on commit 7e0ff0e

Please sign in to comment.