feat(people): adapt person user flows to status model
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { Prisma } from "@/generated/prisma/client"
|
||||
import { Prisma, UserStatus } from "@/generated/prisma/client"
|
||||
import { normalizeEmail } from "@/lib/email"
|
||||
import prisma from "@/lib/prisma"
|
||||
import { getPasswordHash } from "@/lib/security"
|
||||
import type {
|
||||
@@ -178,6 +179,10 @@ export async function createPersonUserUseCase(
|
||||
}
|
||||
|
||||
// Person + User creation
|
||||
if (!password) {
|
||||
return personError({ password: ["Password is required"] })
|
||||
}
|
||||
|
||||
const person = await PersonService.create(
|
||||
{
|
||||
firstName,
|
||||
@@ -190,15 +195,20 @@ export async function createPersonUserUseCase(
|
||||
)
|
||||
|
||||
const userName = `${firstName} ${lastName}`
|
||||
const hashedPassword = await getPasswordHash(password!)
|
||||
const hashedPassword = await getPasswordHash(password)
|
||||
const status = isActive ? UserStatus.ACTIVE : UserStatus.DISABLED
|
||||
const now = new Date()
|
||||
|
||||
const user = await tx.user.create({
|
||||
data: {
|
||||
name: userName,
|
||||
email,
|
||||
password: hashedPassword,
|
||||
emailNormalized: normalizeEmail(email),
|
||||
passwordHash: hashedPassword,
|
||||
role,
|
||||
isActive,
|
||||
status,
|
||||
...(status === UserStatus.ACTIVE ? { activatedAt: now } : {}),
|
||||
passwordChangedAt: now,
|
||||
},
|
||||
})
|
||||
|
||||
@@ -240,7 +250,7 @@ export async function updatePersonUserUseCase(
|
||||
return await prisma.$transaction(async (tx) => {
|
||||
const existing = await tx.person.findUnique({
|
||||
where: { id },
|
||||
include: { user: true },
|
||||
select: { id: true, userId: true },
|
||||
})
|
||||
|
||||
if (!existing) {
|
||||
@@ -267,17 +277,21 @@ export async function updatePersonUserUseCase(
|
||||
)
|
||||
|
||||
// If the person has a linked user, update User fields.
|
||||
if (existing.userId && existing.user) {
|
||||
if (existing.userId) {
|
||||
const userData: Prisma.UserUpdateInput = {}
|
||||
|
||||
if (role !== undefined) {
|
||||
userData.role = role
|
||||
}
|
||||
if (isActive !== undefined) {
|
||||
userData.isActive = isActive
|
||||
userData.status = isActive ? UserStatus.ACTIVE : UserStatus.DISABLED
|
||||
if (isActive) {
|
||||
userData.activatedAt = new Date()
|
||||
}
|
||||
}
|
||||
if (password && password.length >= 8) {
|
||||
userData.password = await getPasswordHash(password)
|
||||
userData.passwordHash = await getPasswordHash(password)
|
||||
userData.passwordChangedAt = new Date()
|
||||
}
|
||||
|
||||
if (Object.keys(userData).length > 0) {
|
||||
|
||||
Reference in New Issue
Block a user