102 lines
2.3 KiB
TypeScript
102 lines
2.3 KiB
TypeScript
import type {
|
|
PersonDepartment,
|
|
PrismaClient,
|
|
UserRole,
|
|
} from "@/generated/prisma/client"
|
|
import { UserStatus } from "@/generated/prisma/client"
|
|
import { normalizeEmail } from "@/lib/email"
|
|
|
|
let sequence = 0
|
|
|
|
function nextSuffix() {
|
|
sequence += 1
|
|
return `${Date.now()}-${sequence}`
|
|
}
|
|
|
|
export async function createTestUser(
|
|
prisma: PrismaClient,
|
|
overrides: Partial<{
|
|
email: string
|
|
name: string
|
|
role: UserRole
|
|
isActive: boolean
|
|
}> = {},
|
|
) {
|
|
const suffix = nextSuffix()
|
|
const email = overrides.email ?? `test-user-${suffix}@example.test`
|
|
const status =
|
|
(overrides.isActive ?? true) ? UserStatus.ACTIVE : UserStatus.DISABLED
|
|
|
|
return prisma.user.create({
|
|
data: {
|
|
email,
|
|
emailNormalized: normalizeEmail(email),
|
|
name: overrides.name ?? "Test User",
|
|
passwordHash: "hashed-password",
|
|
role: overrides.role ?? "ADMIN",
|
|
status,
|
|
...(status === UserStatus.ACTIVE ? { activatedAt: new Date() } : {}),
|
|
passwordChangedAt: new Date(),
|
|
},
|
|
})
|
|
}
|
|
|
|
export async function createTestCategory(
|
|
prisma: PrismaClient,
|
|
overrides: Partial<{ name: string }> = {},
|
|
) {
|
|
const suffix = nextSuffix()
|
|
|
|
return prisma.category.create({
|
|
data: {
|
|
name: overrides.name ?? `Test Category ${suffix}`,
|
|
},
|
|
})
|
|
}
|
|
|
|
export async function createTestPerson(
|
|
prisma: PrismaClient,
|
|
overrides: Partial<{
|
|
firstName: string
|
|
lastName: string
|
|
department: PersonDepartment
|
|
email: string | null
|
|
phone: string | null
|
|
}> = {},
|
|
) {
|
|
const suffix = nextSuffix()
|
|
|
|
return prisma.person.create({
|
|
data: {
|
|
firstName: overrides.firstName ?? "Test",
|
|
lastName: overrides.lastName ?? `Person-${suffix}`,
|
|
department: overrides.department ?? "OTHER",
|
|
email: overrides.email ?? null,
|
|
phone: overrides.phone ?? null,
|
|
},
|
|
})
|
|
}
|
|
|
|
export async function createTestItem(
|
|
prisma: PrismaClient,
|
|
overrides: Partial<{
|
|
name: string
|
|
stock: number
|
|
categoryId: string
|
|
}> = {},
|
|
) {
|
|
const categoryId =
|
|
overrides.categoryId ?? (await createTestCategory(prisma)).id
|
|
const suffix = nextSuffix()
|
|
|
|
return prisma.item.create({
|
|
data: {
|
|
sku: `TEST-SKU-${suffix}`,
|
|
name: overrides.name ?? `Test Item ${suffix}`,
|
|
trackingType: "QUANTITY",
|
|
stock: overrides.stock ?? 0,
|
|
category: { connect: { id: categoryId } },
|
|
},
|
|
})
|
|
}
|