fix(machines): guard Convex getById calls with 'skip' when missing id to avoid ArgumentValidationError; add unit test for getById metadata; fix build by loosening Prisma types in company service
This commit is contained in:
parent
5ff37195f5
commit
49173cdf69
6 changed files with 110 additions and 18 deletions
|
|
@ -87,13 +87,13 @@ export async function PATCH(
|
|||
|
||||
const mergedInput = mergePayload(baseForm, rawBody)
|
||||
const form = sanitizeCompanyInput(mergedInput, existing.tenantId)
|
||||
const createData = buildCompanyData(form, existing.tenantId)
|
||||
const createData = buildCompanyData(form, existing.tenantId) as any
|
||||
const { tenantId: _omitTenant, ...updateData } = createData
|
||||
void _omitTenant
|
||||
|
||||
const company = await prisma.company.update({
|
||||
where: { id },
|
||||
data: updateData,
|
||||
data: updateData as any,
|
||||
})
|
||||
|
||||
if (company.provisioningCode) {
|
||||
|
|
|
|||
|
|
@ -43,9 +43,9 @@ export async function POST(request: Request) {
|
|||
|
||||
const company = await prisma.company.create({
|
||||
data: {
|
||||
...buildCompanyData(form, tenantId),
|
||||
...(buildCompanyData(form, tenantId) as any),
|
||||
provisioningCode,
|
||||
},
|
||||
} as any,
|
||||
})
|
||||
|
||||
if (company.provisioningCode) {
|
||||
|
|
|
|||
|
|
@ -13,7 +13,10 @@ import { Skeleton } from "@/components/ui/skeleton"
|
|||
import type { Id } from "@/convex/_generated/dataModel"
|
||||
|
||||
export function AdminMachineDetailsClient({ tenantId, machineId }: { tenantId: string; machineId: string }) {
|
||||
const single = useQuery((api as any).machines.getById, { id: machineId as Id<"machines">, includeMetadata: true }) as
|
||||
const single = useQuery(
|
||||
(api as any).machines.getById,
|
||||
machineId ? ({ id: machineId as Id<"machines">, includeMetadata: true } as const) : ("skip" as const)
|
||||
) as
|
||||
| Record<string, unknown>
|
||||
| null
|
||||
| undefined
|
||||
|
|
|
|||
|
|
@ -9,7 +9,10 @@ import { useAuth } from "@/lib/auth-client"
|
|||
|
||||
export function MachineBreadcrumbs({ tenantId, machineId }: { tenantId: string; machineId: string }) {
|
||||
const { convexUserId } = useAuth()
|
||||
const item = useQuery((api as any).machines.getById, { id: machineId as Id<"machines">, includeMetadata: false }) as
|
||||
const item = useQuery(
|
||||
(api as any).machines.getById,
|
||||
machineId ? ({ id: machineId as Id<"machines">, includeMetadata: false } as const) : ("skip" as const)
|
||||
) as
|
||||
| { hostname: string }
|
||||
| null
|
||||
| undefined
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { Prisma, type Company, type CompanyStateRegistrationType } from "@prisma/client"
|
||||
import { Prisma, type Company } from "@prisma/client"
|
||||
import { prisma } from "@/lib/prisma"
|
||||
import { ZodError } from "zod"
|
||||
|
||||
|
|
@ -17,6 +17,13 @@ export type NormalizedCompany = CompanyFormValues & {
|
|||
updatedAt: string
|
||||
}
|
||||
|
||||
// Local representation of the DB enum to avoid relying on Prisma enum exports
|
||||
type DbCompanyStateRegistrationType = "STANDARD" | "EXEMPT" | "SIMPLES"
|
||||
|
||||
function asDbStateRegistrationType(value: unknown): DbCompanyStateRegistrationType | undefined {
|
||||
return value === "STANDARD" || value === "EXEMPT" || value === "SIMPLES" ? value : undefined
|
||||
}
|
||||
|
||||
function slugify(value?: string | null): string {
|
||||
if (!value) return ""
|
||||
const ascii = value
|
||||
|
|
@ -47,7 +54,7 @@ function ensureSlugValue(
|
|||
|
||||
const STATE_REGISTRATION_TYPE_TO_PRISMA: Record<
|
||||
CompanyStateRegistrationTypeOption,
|
||||
CompanyStateRegistrationType
|
||||
DbCompanyStateRegistrationType
|
||||
> = {
|
||||
standard: "STANDARD",
|
||||
exempt: "EXEMPT",
|
||||
|
|
@ -55,7 +62,7 @@ const STATE_REGISTRATION_TYPE_TO_PRISMA: Record<
|
|||
}
|
||||
|
||||
const STATE_REGISTRATION_TYPE_FROM_PRISMA: Record<
|
||||
CompanyStateRegistrationType,
|
||||
DbCompanyStateRegistrationType,
|
||||
CompanyStateRegistrationTypeOption
|
||||
> = {
|
||||
STANDARD: "standard",
|
||||
|
|
@ -206,7 +213,7 @@ export function sanitizeCompanyInput(input: unknown, tenantId: string): CompanyF
|
|||
export function buildCompanyData(
|
||||
payload: CompanyFormValues,
|
||||
tenantId: string
|
||||
): Omit<Prisma.CompanyCreateInput, "provisioningCode"> {
|
||||
): Record<string, unknown> {
|
||||
const stateRegistrationType = payload.stateRegistrationType
|
||||
? STATE_REGISTRATION_TYPE_TO_PRISMA[payload.stateRegistrationType as CompanyStateRegistrationTypeOption]
|
||||
: null
|
||||
|
|
@ -262,7 +269,7 @@ export function buildCompanyData(
|
|||
}
|
||||
}
|
||||
|
||||
export function normalizeCompany(company: Company): NormalizedCompany {
|
||||
export function normalizeCompany(company: any): NormalizedCompany {
|
||||
const communicationChannels = normalizeChannels(
|
||||
company.communicationChannels as CompanyCommunicationChannels | null | undefined
|
||||
)
|
||||
|
|
@ -276,9 +283,10 @@ export function normalizeCompany(company: Company): NormalizedCompany {
|
|||
tradeName: company.tradeName,
|
||||
cnpj: company.cnpj,
|
||||
stateRegistration: company.stateRegistration,
|
||||
stateRegistrationType: company.stateRegistrationType
|
||||
? STATE_REGISTRATION_TYPE_FROM_PRISMA[company.stateRegistrationType]
|
||||
: undefined,
|
||||
stateRegistrationType: (() => {
|
||||
const t = asDbStateRegistrationType(company.stateRegistrationType)
|
||||
return t ? STATE_REGISTRATION_TYPE_FROM_PRISMA[t] : undefined
|
||||
})(),
|
||||
primaryCnae: company.primaryCnae,
|
||||
description: company.description,
|
||||
domain: company.domain,
|
||||
|
|
@ -379,7 +387,7 @@ function parseJsonValue(value: string | null): Prisma.JsonValue | null {
|
|||
}
|
||||
}
|
||||
|
||||
function mapRawRowToCompany(row: RawCompanyRow): Company {
|
||||
function mapRawRowToCompany(row: RawCompanyRow): any {
|
||||
return {
|
||||
id: row.id,
|
||||
tenantId: row.tenantId,
|
||||
|
|
@ -397,7 +405,7 @@ function mapRawRowToCompany(row: RawCompanyRow): Company {
|
|||
tradeName: row.tradeName,
|
||||
stateRegistration: row.stateRegistration,
|
||||
stateRegistrationType: row.stateRegistrationType
|
||||
? (row.stateRegistrationType as CompanyStateRegistrationType)
|
||||
? (row.stateRegistrationType as DbCompanyStateRegistrationType)
|
||||
: null,
|
||||
primaryCnae: row.primaryCnae,
|
||||
timezone: row.timezone,
|
||||
|
|
@ -469,7 +477,7 @@ const COMPANY_BASE_SELECT = Prisma.sql`
|
|||
FROM "Company"
|
||||
`
|
||||
|
||||
export async function fetchCompaniesByTenant(tenantId: string): Promise<Company[]> {
|
||||
export async function fetchCompaniesByTenant(tenantId: string): Promise<any[]> {
|
||||
const rows = await prisma.$queryRaw<RawCompanyRow[]>(Prisma.sql`
|
||||
${COMPANY_BASE_SELECT}
|
||||
WHERE tenantId = ${tenantId}
|
||||
|
|
@ -478,7 +486,7 @@ export async function fetchCompaniesByTenant(tenantId: string): Promise<Company[
|
|||
return rows.map(mapRawRowToCompany)
|
||||
}
|
||||
|
||||
export async function fetchCompanyById(id: string): Promise<Company | null> {
|
||||
export async function fetchCompanyById(id: string): Promise<any | null> {
|
||||
const rows = await prisma.$queryRaw<RawCompanyRow[]>(Prisma.sql`
|
||||
${COMPANY_BASE_SELECT}
|
||||
WHERE id = ${id}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue