feat: 平台管理员创建企业客户功能 + deploy.sh 跳过 tsc
This commit is contained in:
@@ -136,6 +136,71 @@ router.get('/orgs', async (req: AuthRequest, res, next) => {
|
||||
}
|
||||
})
|
||||
|
||||
/**
|
||||
* 创建企业客户(含管理员账号)
|
||||
*/
|
||||
router.post('/orgs', async (req: AuthRequest, res, next) => {
|
||||
try {
|
||||
const {
|
||||
name, plan, maxEmployees, city, contactName, contactPhone,
|
||||
adminName, adminPhone, adminPassword,
|
||||
} = req.body as {
|
||||
name: string; plan?: string; maxEmployees?: number
|
||||
city?: string; contactName?: string; contactPhone?: string
|
||||
adminName?: string; adminPhone: string; adminPassword: string
|
||||
}
|
||||
|
||||
if (!name || !adminPhone || !adminPassword) {
|
||||
return res.status(400).json({ success: false, error: { code: 'VALIDATION', message: '企业名称、管理员手机号、密码不能为空' } })
|
||||
}
|
||||
|
||||
if (adminPassword.length < 8) {
|
||||
return res.status(400).json({ success: false, error: { code: 'VALIDATION', message: '密码至少8位' } })
|
||||
}
|
||||
|
||||
const existing = await prisma.user.findUnique({ where: { phone: adminPhone } })
|
||||
if (existing) {
|
||||
return res.status(400).json({ success: false, error: { code: 'DUPLICATE', message: '该手机号已存在' } })
|
||||
}
|
||||
|
||||
// 创建企业
|
||||
const org = await prisma.organization.create({
|
||||
data: {
|
||||
name,
|
||||
plan: (plan as any) || 'FREE',
|
||||
maxEmployees: maxEmployees || 20,
|
||||
city: city || null,
|
||||
contactName: contactName || null,
|
||||
contactPhone: contactPhone || null,
|
||||
},
|
||||
})
|
||||
|
||||
// 创建管理员账号
|
||||
const bcrypt = await import('bcryptjs')
|
||||
const passwordHash = await bcrypt.default.hash(adminPassword, 10)
|
||||
const admin = await prisma.user.create({
|
||||
data: {
|
||||
orgId: org.id,
|
||||
phone: adminPhone,
|
||||
name: adminName || '管理员',
|
||||
passwordHash,
|
||||
role: 'ADMIN',
|
||||
},
|
||||
select: { id: true, name: true, phone: true, role: true },
|
||||
})
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
org: { id: org.id, name: org.name, plan: org.plan, maxEmployees: org.maxEmployees, city: org.city },
|
||||
admin,
|
||||
},
|
||||
})
|
||||
} catch (err) {
|
||||
next(err)
|
||||
}
|
||||
})
|
||||
|
||||
/**
|
||||
* 企业详情
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user