Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
0thigs committed Nov 30, 2024
2 parents a754b24 + 5049801 commit 2e36acb
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 31 deletions.
1 change: 1 addition & 0 deletions apps/server/src/api/controllers/locations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { RegisterLocationController } from './register-location-controller'
export {GetLocationController} from "./get-location-controller"
export { DeleteLocationsController } from './delete-locations-controller'
export { UpdateLocationController } from './update-location-controller'
export { ListLocationsController } from './list-locations-controller'
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { locationsRepository } from '@/database'
import type { IHttp } from '@stocker/core/interfaces'
import { ListLocationsUseCase } from '@stocker/core/use-cases'

import { locationsRepository } from '@/database'
import { HTTP_STATUS_CODE } from '@stocker/core/constants'

type RouteParams = {
page: string
type QueryParams = {
page: number
name?: string
}

export class ListLocationsController {
async handle(http: IHttp) {
const { companyId } = await http.getUser()
const { page } = http.getQueryParams<RouteParams>()
const pageNumber = parseInt(page || '1', 10)
const user = await http.getUser()
const companyId = user.companyId
const { page, name } = http.getQueryParams<QueryParams>()

const useCase = new ListLocationsUseCase(locationsRepository)
const response = await useCase.execute({ page: pageNumber, companyId: companyId })
return http.send(response, HTTP_STATUS_CODE.ok)
const locationsDto = await useCase.execute({ companyId, page, name })

return http.send(locationsDto)
}
}
4 changes: 2 additions & 2 deletions apps/server/src/app/fastify/routes/locations-routes.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { DeleteLocationsController, GetLocationController, RegisterLocationController } from "@/api/controllers/locations";
import type { FastifyInstance } from "fastify";
import { FastifyHttp } from "../fastify-http";
import { UpdateLocationController } from "@/api/controllers/locations/update-location-controller";
import { ListLocationsController } from "@/api/controllers/locations/list-locations-controller";
import { UpdateLocationController } from '@/api/controllers/locations/update-location-controller'
import { ListLocationsController } from '@/api/controllers/locations/list-locations-controller'

export const LocationsRoutes = async (app: FastifyInstance) => {
const registerLocationController = new RegisterLocationController()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Location } from '@stocker/core/entities'
import type { PrismaLocations } from '../types'
import type { PrismaLocation } from '../types'

export class PrismaLocationsMapper {
toDomain(prismaLocation: PrismaLocations): Location {
toDomain(prismaLocation: PrismaLocation): Location {
return Location.create({
id: prismaLocation.id,
name: prismaLocation.name,
Expand All @@ -18,7 +18,7 @@ export class PrismaLocationsMapper {
});
}

toPrisma(location: Location): PrismaLocations {
toPrisma(location: Location): PrismaLocation {
const locationDto = location.dto;

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Location } from '@stocker/core/entities'
import type { ILocationsRepository } from '@stocker/core/interfaces'
import type { PaginationResponse } from '@stocker/core/responses'
import type { CategoriesListParams } from '@stocker/core/types'
import { PaginationResponse } from '@stocker/core/responses'
import type { CategoriesListParams, LocationsListParams } from '@stocker/core/types'
import { prisma } from '../prisma-client'
import { PrismaError } from '../prisma-error'
import { PAGINATION } from '@stocker/core/constants'
Expand Down Expand Up @@ -102,7 +102,7 @@ export class PrismaLocationsRepository implements ILocationsRepository {
}
}

async findMany(params: CategoriesListParams): Promise<Location[]> {
async findMany(params: LocationsListParams): Promise<{locations: Location[], count: number}> {
try {
const prismaLocations = await prisma.location.findMany({
take: PAGINATION.itemsPerPage,
Expand All @@ -116,13 +116,20 @@ export class PrismaLocationsRepository implements ILocationsRepository {
},
orderBy: { registered_at: 'desc' },
})
const count = await prisma.location.count({
where: {
company_id: params.companyId,
parent_location_id: null,
},
})

const locations = prismaLocations.map(this.mapper.toDomain)
return locations
return {locations, count}
} catch (error) {
throw new PrismaError(error)
}
}

async count(): Promise<number> {
try {
const count = await prisma.location.count()
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/database/prisma/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ export type { PrismaUser } from './prisma-user'
export type { PrismaStockLevelNotification } from './prisma-stock-level-notification'
export type { PrismaExpirationDateNotification } from './prisma-expiration-date-notification'
export type { PrismaSupplier } from './prisma-suppliers'
export type { PrismaLocation } from './prisma-locations'
export type { PrismaLocation } from './prisma-location'
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Location } from '@prisma/client'

export type PrismaLocation = Location & {
subLocation?: Location[]
subLocation: Location[]
}
2 changes: 1 addition & 1 deletion apps/server/src/database/prisma/types/prisma-product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Product } from '@prisma/client'
import type { PrismaBatch } from './prisma-batch'
import type { PrismaCategory } from './prisma-category'
import type { PrismaSupplier } from './prisma-suppliers'
import type { PrismaLocation } from './prisma-locations'
import type { PrismaLocation } from './prisma-location'

export type PrismaProduct = Product & {
batches: PrismaBatch[]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Location } from '../../domain/entities/location'
import type { PaginationResponse } from '../../responses'
import type { CategoriesListParams } from '../../types'
import type { LocationsListParams } from '../../types'

export interface ILocationsRepository {
findById(locationId: string): Promise<Location | null>
findByName(locationName: string): Promise<Location | null>
findMany(params: CategoriesListParams): Promise<Location[]>
findMany(params: LocationsListParams): Promise<{locations: Location[], count: number}>
count(): Promise<number>
add(location: Location): Promise<void>
update(location: Location, locationId: string): Promise<void>
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type { ProductsListParams } from './products-list-params'
export type { LocationsListParams } from './locations-list-params'
export type { InventoryMovementsListParams } from './inventory-movements-list-params'
export type { CategoriesListParams } from './categories-list-params'
export type { StockLevel } from './stock-level'
Expand Down
17 changes: 11 additions & 6 deletions packages/core/src/use-cases/locations/list-locations-use-case.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { LocationDto } from '../../dtos'
import type { ILocationsRepository } from '../../interfaces'
import { PaginationResponse } from '../../responses'

type Request = {
page: number
companyId: string
page: number
name?: string
}

export class ListLocationsUseCase {
Expand All @@ -13,12 +15,15 @@ export class ListLocationsUseCase {
this.locationsRepository = locationsRepository
}

async execute({ page, companyId }: Request) {
const locations = await this.locationsRepository.findMany({ page, companyId })
const locationsCount = await this.locationsRepository.count()
async execute({
companyId,
page,
name,
}: Request): Promise<PaginationResponse<LocationDto>> {
const locations = await this.locationsRepository.findMany({ name, companyId, page })
return new PaginationResponse({
items: locations.map((location) => location.dto),
itemsCount: locationsCount
items: locations.items.map((location) => location.dto),
itemsCount: locations.itemsCount,
})
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import type { INotificationsRepository } from '../../interfaces'

type Request = {
companyId: string
}

export class ListNotificationsUseCase {
constructor(private readonly notificationsRepository: INotificationsRepository) {}

async execute(companyId: string) {
async execute({ companyId }: Request) {
const stockNotifications =
await this.notificationsRepository.findManyStockLevelNotificationsByCompany(
companyId,
Expand Down

0 comments on commit 2e36acb

Please sign in to comment.