fix(types fixed of status, method)
This commit is contained in:
@@ -132,6 +132,7 @@ enum ClaimStatus {
|
||||
APPROVED
|
||||
CANCELLED
|
||||
REVIEW
|
||||
VOID
|
||||
}
|
||||
|
||||
model ServiceLine {
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
import { z } from "zod";
|
||||
import { Decimal } from "decimal.js";
|
||||
import { Staff } from "@repo/db/types";
|
||||
import { makeEnumOptions } from "../utils";
|
||||
|
||||
export const insertClaimSchema = (
|
||||
ClaimUncheckedCreateInputObjectSchema as unknown as z.ZodObject<any>
|
||||
@@ -36,6 +37,14 @@ export const ExtendedClaimSchema = (
|
||||
|
||||
export type Claim = z.infer<typeof ClaimUncheckedCreateInputObjectSchema>;
|
||||
export type ClaimStatus = z.infer<typeof ClaimStatusSchema>;
|
||||
export const claimStatusOptions =
|
||||
makeEnumOptions<
|
||||
typeof ClaimStatusSchema extends z.ZodTypeAny
|
||||
? z.infer<typeof ClaimStatusSchema>
|
||||
: string
|
||||
>(ClaimStatusSchema);
|
||||
export type ClaimStatusOptions =
|
||||
(typeof claimStatusOptions)[keyof typeof claimStatusOptions];
|
||||
|
||||
export type ClaimFileMeta = {
|
||||
id?: number;
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
} from "@repo/db/usedSchemas";
|
||||
import { Prisma } from "@repo/db/generated/prisma";
|
||||
import { z } from "zod";
|
||||
import { makeEnumOptions } from "../utils";
|
||||
|
||||
// ========== BASIC TYPES ==========
|
||||
|
||||
@@ -31,8 +32,29 @@ export type PaymentStatus = z.infer<typeof PaymentStatusSchema>;
|
||||
export type PaymentMethod = z.infer<typeof PaymentMethodSchema>;
|
||||
|
||||
// ✅ Runtime arrays (used in code logic / map / select options)
|
||||
export const paymentStatusOptions = PaymentStatusSchema.options;
|
||||
export const paymentMethodOptions = PaymentMethodSchema.options;
|
||||
export const paymentStatusOptions =
|
||||
makeEnumOptions<
|
||||
typeof PaymentStatusSchema extends z.ZodTypeAny
|
||||
? z.infer<typeof PaymentStatusSchema>
|
||||
: string
|
||||
>(PaymentStatusSchema);
|
||||
export type PaymentStatusOptions =
|
||||
(typeof paymentStatusOptions)[keyof typeof paymentStatusOptions];
|
||||
export const paymentStatusArray = Object.values(
|
||||
paymentStatusOptions
|
||||
) as PaymentStatusOptions[];
|
||||
|
||||
export const paymentMethodOptions =
|
||||
makeEnumOptions<
|
||||
typeof PaymentMethodSchema extends z.ZodTypeAny
|
||||
? z.infer<typeof PaymentMethodSchema>
|
||||
: string
|
||||
>(PaymentMethodSchema);
|
||||
export type PaymentMethodOptions =
|
||||
(typeof paymentMethodOptions)[keyof typeof paymentMethodOptions];
|
||||
export const paymentMethodArray = Object.values(
|
||||
paymentMethodOptions
|
||||
) as PaymentMethodOptions[];
|
||||
|
||||
// ========== INPUT TYPES ==========
|
||||
|
||||
|
||||
35
packages/db/utils/index.ts
Normal file
35
packages/db/utils/index.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Extract enum values from a Zod enum or native enum schema.
|
||||
* Supports z.enum([...]) and z.nativeEnum(SomeTsEnum).
|
||||
*/
|
||||
export function extractEnumValues<T extends string | number>(schema: any): T[] {
|
||||
// z.enum([...]) => schema.options exists
|
||||
if (Array.isArray(schema?.options)) {
|
||||
return schema.options as T[];
|
||||
}
|
||||
|
||||
// z.nativeEnum(SomeEnum) => schema._def?.values may exist or enum is in schema._def?.enum
|
||||
if (Array.isArray(schema?._def?.values)) {
|
||||
return schema._def.values as T[];
|
||||
}
|
||||
|
||||
if (schema?._def?.enum) {
|
||||
// enum object -> values
|
||||
return Object.values(schema._def.enum) as T[];
|
||||
}
|
||||
|
||||
throw new Error("Unsupported Zod schema type for enum extraction");
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a runtime map: { VAL: "VAL", ... } with proper typing
|
||||
* so callers can import paymentStatusOptions.VOID etc.
|
||||
*/
|
||||
export function makeEnumOptions<T extends string | number>(schema: any) {
|
||||
const values = extractEnumValues<T>(schema);
|
||||
const map = {} as Record<string, T>;
|
||||
values.forEach((v) => {
|
||||
map[String(v)] = v;
|
||||
});
|
||||
return map as { [K in T & (string | number)]: K };
|
||||
}
|
||||
Reference in New Issue
Block a user