refactor(structure): move legacy import and remove lib leftovers
This commit is contained in:
@@ -4,8 +4,15 @@ import { revalidatePath } from "next/cache"
|
||||
import Papa from "papaparse"
|
||||
import { flattenError } from "zod"
|
||||
|
||||
import { type ImportFormType, importSchema } from "@/lib/schemas/import.schemas"
|
||||
import type { CreateMovementFormType } from "@/lib/schemas/movement.schemas"
|
||||
import { type ImportFormType, importSchema } from "@/schemas/import.schema"
|
||||
import type { CreateMovementFormType } from "@/schemas/movement.schema"
|
||||
import { AssetService } from "@/services/asset.service"
|
||||
import { AssignmentService } from "@/services/assignment.service"
|
||||
import { getAuthenticatedUserId } from "@/services/auth.service"
|
||||
import { CategoryService } from "@/services/category.service"
|
||||
import { ItemService } from "@/services/item.service"
|
||||
import { MovementService } from "@/services/movement.service"
|
||||
import { RecipientService } from "@/services/recipient.service"
|
||||
import type {
|
||||
Asset,
|
||||
Assignment,
|
||||
@@ -13,13 +20,7 @@ import type {
|
||||
ImportItem,
|
||||
Item,
|
||||
Recipient,
|
||||
} from "@/lib/types"
|
||||
import { AssetService } from "@/services/asset.service"
|
||||
import { AssignmentService } from "@/services/assignment.service"
|
||||
import { CategoryService } from "@/services/category.service"
|
||||
import { ItemService } from "@/services/item.service"
|
||||
import { MovementService } from "@/services/movement.service"
|
||||
import { RecipientService } from "@/services/recipient.service"
|
||||
} from "@/types"
|
||||
|
||||
export async function importItems(formData: ImportFormType) {
|
||||
const validatedFields = importSchema.safeParse(formData)
|
||||
@@ -31,6 +32,7 @@ export async function importItems(formData: ImportFormType) {
|
||||
}
|
||||
|
||||
const { file, categoryId } = validatedFields.data
|
||||
const userId = await getAuthenticatedUserId()
|
||||
|
||||
if (!file) {
|
||||
return {
|
||||
@@ -313,6 +315,7 @@ export async function importItems(formData: ImportFormType) {
|
||||
assetId: newAsset?.id || "",
|
||||
recipientId: newRecipient?.id || "",
|
||||
assignmentDate: new Date(),
|
||||
createdBy: userId,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -332,7 +335,10 @@ export async function importItems(formData: ImportFormType) {
|
||||
movementData.recipientId = newRecipient.id
|
||||
}
|
||||
|
||||
await MovementService.create(movementData)
|
||||
await MovementService.create({
|
||||
...movementData,
|
||||
userId,
|
||||
})
|
||||
}
|
||||
|
||||
revalidatePath("/inventory/items")
|
||||
@@ -5,11 +5,10 @@ import { useRouter } from "next/navigation"
|
||||
import type { ChangeEvent } from "react"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { toast } from "sonner"
|
||||
|
||||
import { importItems } from "@/actions/import.actions"
|
||||
import { SubmitButton } from "@/components/forms/submitButton"
|
||||
import { importItems } from "@/lib/actions/import.actions"
|
||||
import { type ImportFormType, importSchema } from "@/lib/schemas/import.schemas"
|
||||
import type { CategorySummary } from "@/lib/types"
|
||||
import { type ImportFormType, importSchema } from "@/schemas/import.schema"
|
||||
import type { CategorySummary } from "@/types"
|
||||
|
||||
export default function ImportForm({
|
||||
categories,
|
||||
|
||||
@@ -15,7 +15,7 @@ export default async function ImportPage() {
|
||||
<h1 className="text-2xl font-bold">Mass Import</h1>
|
||||
</div>
|
||||
<div className="flex items-center justify-end gap-4">
|
||||
{ENVIRONMENT === "demo" && (
|
||||
{(ENVIRONMENT === "development" || ENVIRONMENT === "demo") && (
|
||||
<Link href="/sample_data.csv" download>
|
||||
<Button variant="outline">
|
||||
<Download />
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
import {
|
||||
type CreateMovementFormType,
|
||||
createMovementSchema,
|
||||
} from "../schemas/movement.schemas"
|
||||
|
||||
export async function createMovementAction(data: CreateMovementFormType) {
|
||||
const validatedFields = createMovementSchema.safeParse(data)
|
||||
|
||||
if (!validatedFields.success) {
|
||||
return {
|
||||
success: false,
|
||||
errors: validatedFields.error.flatten().fieldErrors,
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
-5
@@ -1,10 +1,9 @@
|
||||
import type { Prisma } from "@/generated/prisma/client"
|
||||
import type { PaginatedResult } from "@/lib/types"
|
||||
import type { PaginatedResult } from "@/types"
|
||||
|
||||
//eslint-disable-next-line
|
||||
type PaginationArgs<T> = {
|
||||
model: any // prisma.<model>
|
||||
where?: Prisma.Enumerable<any> // filtros
|
||||
type PaginationArgs<_T> = {
|
||||
model: any
|
||||
where?: Prisma.Enumerable<any>
|
||||
page?: number
|
||||
pageSize?: number
|
||||
orderBy?: any
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
import { z } from "zod"
|
||||
|
||||
export const movementSchema = z.object({
|
||||
type: z.enum(["IN", "OUT", "ASSIGNMENT", "RETURN", "ADJUSTMENT"]),
|
||||
quantity: z.coerce
|
||||
.number()
|
||||
.int()
|
||||
.nonnegative()
|
||||
.min(1, {
|
||||
error: "Quantity is required"
|
||||
}),
|
||||
details: z.string().optional(),
|
||||
notes: z.string().optional(),
|
||||
itemId: z.string().optional(),
|
||||
assetId: z.string().optional(),
|
||||
userId: z.string(),
|
||||
assignmentId: z.string().optional(),
|
||||
recipientId: z.string().optional(),
|
||||
})
|
||||
|
||||
export const createMovementSchema = movementSchema.omit({
|
||||
userId: true,
|
||||
})
|
||||
|
||||
export type CreateMovementFormType = z.infer<typeof createMovementSchema>
|
||||
@@ -1,34 +0,0 @@
|
||||
import type {
|
||||
Assignment,
|
||||
Asset as PrismaAsset,
|
||||
ItemStatus as PrismaItemStatus,
|
||||
} from "@/generated/prisma/client"
|
||||
|
||||
export type Asset = PrismaAsset
|
||||
|
||||
export type ItemStatus = PrismaItemStatus
|
||||
|
||||
export type NewAssetStatus = "AVAILABLE" | "ASSIGNED"
|
||||
|
||||
export type UpdateAssetStatus = NewAssetStatus | "RESERVED" | "IN_REPAIR"
|
||||
|
||||
export type AssetSummary = Pick<Asset, "id" | "serialNumber" | "status">
|
||||
|
||||
export type AssetWithAssignment = Asset & {
|
||||
assignment: Assignment | null
|
||||
}
|
||||
|
||||
export type AssetWithItemAndCategory = {
|
||||
id: string
|
||||
serialNumber: string
|
||||
deliveryNote?: string | null
|
||||
status: ItemStatus
|
||||
item: {
|
||||
id: string
|
||||
name: string
|
||||
category: {
|
||||
id: string
|
||||
name: string
|
||||
}
|
||||
} | null
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
import type { Assignment as PrismaAssignment } from "@/generated/prisma/client"
|
||||
|
||||
import type { Asset } from "./asset"
|
||||
import type { Item } from "./item"
|
||||
import type { Recipient } from "./recipient"
|
||||
|
||||
export type Assignment = PrismaAssignment
|
||||
|
||||
export type AssignmentSummary = Pick<Assignment, "id" | "quantity">
|
||||
|
||||
export type AssignmentWithRecipientItemAsset = Assignment & {
|
||||
returnDate: Date | null
|
||||
recipient: Recipient | null
|
||||
item: Item | null
|
||||
asset: Asset | null
|
||||
}
|
||||
|
||||
export type AssignmentWithItemAndAsset = AssignmentSummary & {
|
||||
item: Item
|
||||
asset: Asset
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
import type { Category as PrismaCategory } from "@/generated/prisma/client"
|
||||
|
||||
export type Category = PrismaCategory
|
||||
|
||||
export type CategorySummary = Pick<Category, "id" | "name">
|
||||
|
||||
export type CategoryWithItemsCount = CategorySummary & {
|
||||
_count: {
|
||||
items: number
|
||||
}
|
||||
}
|
||||
|
||||
export interface CreateCategoryInput {
|
||||
name: string
|
||||
description?: string | null
|
||||
}
|
||||
|
||||
export interface UpdateCategoryInput {
|
||||
name?: string
|
||||
description?: string | null
|
||||
isActive?: boolean
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
export interface ImportItem {
|
||||
name: string
|
||||
stock?: number
|
||||
serialNumber?: string
|
||||
categoryId?: string
|
||||
category?: string
|
||||
deliveryNote?: string
|
||||
assigned?: boolean
|
||||
username?: string
|
||||
firstName?: string
|
||||
lastName?: string
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
export * from "./asset"
|
||||
export * from "./assignment"
|
||||
export * from "./category"
|
||||
export * from "./import"
|
||||
export * from "./item"
|
||||
export * from "./movement"
|
||||
export * from "./paginate"
|
||||
export * from "./recipient"
|
||||
export * from "./user"
|
||||
@@ -1,21 +0,0 @@
|
||||
import type { Category, Item as PrismaItem } from "@/generated/prisma/client"
|
||||
|
||||
export type Item = PrismaItem
|
||||
|
||||
export type ItemSummary = Pick<Item, "id" | "name" | "stock"> & {
|
||||
category: Pick<Category, "id" | "name">
|
||||
}
|
||||
|
||||
export type ItemWithoutStock = Pick<Item, "id" | "name">
|
||||
|
||||
export type ItemWithAssetCount = ItemSummary & {
|
||||
_count: {
|
||||
assets: number
|
||||
}
|
||||
}
|
||||
|
||||
export type ItemWithAssetAndMovementCount = ItemWithAssetCount & {
|
||||
_count: {
|
||||
movements: number
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
import type { Movement as PrismaMovement } from "@/generated/prisma/client"
|
||||
|
||||
import type { Asset } from "./asset"
|
||||
import type { Item } from "./item"
|
||||
import type { Recipient } from "./recipient"
|
||||
|
||||
export type Movement = PrismaMovement
|
||||
|
||||
export type MovementSummary = Pick<Movement, "id" | "type" | "quantity">
|
||||
|
||||
export type MovementWithItem = Movement & {
|
||||
item: Item | null
|
||||
}
|
||||
|
||||
export type MovementWithItemAndAsset = Movement & {
|
||||
item: Item | null
|
||||
asset: Asset | null
|
||||
}
|
||||
|
||||
export type MovementWithRecipientItemAsset = Movement & {
|
||||
recipient: Recipient | null
|
||||
item: Item | null
|
||||
asset: Asset | null
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
export type PaginatedResult<T> = {
|
||||
data: T[]
|
||||
totalItems: number
|
||||
totalPages: number
|
||||
currentPage: number
|
||||
pageSize: number
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
import type { Recipient as PrismaRecipient } from "@/generated/prisma/client"
|
||||
|
||||
export type Recipient = PrismaRecipient
|
||||
@@ -1,3 +0,0 @@
|
||||
import type { User as PrismaUser } from "@/generated/prisma/client"
|
||||
|
||||
export type User = PrismaUser
|
||||
@@ -3,10 +3,9 @@ import { z } from "zod"
|
||||
const ACCEPTED_MIME_TYPES = ["text/csv", "text/comma-separated-values"]
|
||||
|
||||
export const importSchema = z.object({
|
||||
file: z.file()
|
||||
.refine((file) => ACCEPTED_MIME_TYPES.includes(file.type), {
|
||||
error: "File must be a CSV"
|
||||
}),
|
||||
file: z.file().refine((file) => ACCEPTED_MIME_TYPES.includes(file.type), {
|
||||
error: "File must be a CSV",
|
||||
}),
|
||||
categoryId: z.string().optional(),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user