|
|
|
@ -1,4 +1,6 @@ |
|
|
|
const prisma = require("../utils/prisma"); |
|
|
|
const { User } = require("../models/user"); |
|
|
|
const { Dept } = require("../models/dept"); |
|
|
|
|
|
|
|
/** |
|
|
|
* @typedef {Object} DeptUser |
|
|
|
@ -43,31 +45,92 @@ const DeptUsers = { |
|
|
|
* @returns {Promise<{ deptUser: DeptUser | null, error: string | null }>} |
|
|
|
*/ |
|
|
|
create: async function (data) { |
|
|
|
console.log("55555555555555555", data); |
|
|
|
try { |
|
|
|
const validatedData = {}; |
|
|
|
for (const key of this.writable) { |
|
|
|
if (data[key] !== undefined) { |
|
|
|
if (this.validations[key]) { |
|
|
|
validatedData[key] = this.validations[key](data[key]); |
|
|
|
} else { |
|
|
|
validatedData[key] = this.castColumnValue(key, data[key]); |
|
|
|
} |
|
|
|
const { userId, deptId } = data.data; |
|
|
|
console.log("创建组织机构用户关联:", userId); |
|
|
|
console.log("创建组织机构用户关联:", deptId); |
|
|
|
|
|
|
|
// 检查 userId 和 deptId 是否存在
|
|
|
|
const userExists = await User.get({ |
|
|
|
id: userId |
|
|
|
}); |
|
|
|
if (!userExists) { |
|
|
|
throw new Error(`用户 ID ${userId} 不存在`); |
|
|
|
} |
|
|
|
|
|
|
|
const deptExists = await Dept.get({ |
|
|
|
deptId: parseInt(deptId) |
|
|
|
}); |
|
|
|
if (!deptExists) { |
|
|
|
throw new Error(`部门 ID ${deptId} 不存在`); |
|
|
|
} |
|
|
|
|
|
|
|
// 创建 dept_users 记录
|
|
|
|
const deptUser = await prisma.dept_users.create({ |
|
|
|
data: { |
|
|
|
// userId: userId,
|
|
|
|
// deptId: parseInt(deptId),
|
|
|
|
createdAt: new Date(), |
|
|
|
updatedAt: new Date(), |
|
|
|
user: { |
|
|
|
connect: { |
|
|
|
id: userId, |
|
|
|
}, |
|
|
|
}, |
|
|
|
dept: { |
|
|
|
connect: { |
|
|
|
deptId: parseInt(deptId), |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
}); |
|
|
|
|
|
|
|
return deptUser; |
|
|
|
} catch (error) { |
|
|
|
console.error("创建 dept_users 记录时出错:", error.message); |
|
|
|
throw error; |
|
|
|
} |
|
|
|
}, |
|
|
|
createNew: async function (data,prisma) { |
|
|
|
try { |
|
|
|
const { userId, deptId } = data.data; |
|
|
|
// 检查 userId 和 deptId 是否存在
|
|
|
|
const userExists = await User.getNew({ |
|
|
|
id: userId, |
|
|
|
},prisma); |
|
|
|
if (!userExists) { |
|
|
|
throw new Error(`用户 ID ${userId} 不存在`); |
|
|
|
} |
|
|
|
const deptExists = await Dept.get({ |
|
|
|
deptId: parseInt(deptId), |
|
|
|
}); |
|
|
|
if (!deptExists) { |
|
|
|
throw new Error(`部门 ID ${deptId} 不存在`); |
|
|
|
} |
|
|
|
// 创建 dept_users 记录
|
|
|
|
const deptUser = await prisma.dept_users.create({ |
|
|
|
data: { |
|
|
|
...validatedData, |
|
|
|
// userId: userId,
|
|
|
|
// deptId: parseInt(deptId),
|
|
|
|
createdAt: new Date(), |
|
|
|
updatedAt: new Date(), |
|
|
|
user: { |
|
|
|
connect: { |
|
|
|
id: userId, |
|
|
|
}, |
|
|
|
}, |
|
|
|
dept: { |
|
|
|
connect: { |
|
|
|
deptId: parseInt(deptId), |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
}); |
|
|
|
|
|
|
|
return { deptUser, error: null }; |
|
|
|
return deptUser; |
|
|
|
} catch (error) { |
|
|
|
console.error("FAILED TO CREATE DEPT USER.", error.message); |
|
|
|
return { deptUser: null, error: error.message }; |
|
|
|
console.error("创建 dept_users 记录时出错:", error.message); |
|
|
|
throw error; |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
|