summaryrefslogtreecommitdiff
path: root/lib/users/service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'lib/users/service.ts')
-rw-r--r--lib/users/service.ts147
1 files changed, 137 insertions, 10 deletions
diff --git a/lib/users/service.ts b/lib/users/service.ts
index e32d450e..7a635113 100644
--- a/lib/users/service.ts
+++ b/lib/users/service.ts
@@ -560,12 +560,112 @@ export async function getUsersAllbyVendor(input: GetUsersSchema, domain: string)
}
export async function assignUsersToRole(roleId: number, userIds: number[]) {
- unstable_noStore(); // 캐싱 방지(Next.js 서버 액션용)
- try{
+ unstable_noStore() // 캐싱 방지(Next.js 서버 액션용)
+
+ try {
+ if (userIds.length === 0) {
+ return { data: null, error: "선택된 사용자가 없습니다." }
+ }
+
+ await db.transaction(async (tx) => {
+ // 1) 이미 할당된 사용자들 확인
+ const existingAssignments = await tx
+ .select({ userId: userRoles.userId })
+ .from(userRoles)
+ .where(
+ and(
+ eq(userRoles.roleId, roleId),
+ inArray(userRoles.userId, userIds)
+ )
+ )
+
+ const existingUserIds = existingAssignments.map(item => item.userId)
+
+ // 2) 새로 할당할 사용자들만 필터링
+ const newUserIds = userIds.filter(uid => !existingUserIds.includes(uid))
+
+ // 3) 새로운 할당만 추가
+ if (newUserIds.length > 0) {
+ await tx.insert(userRoles).values(
+ newUserIds.map((uid) => ({
+ userId: uid,
+ roleId: roleId
+ }))
+ )
+ }
+ })
+
+ revalidateTag("users")
+ revalidateTag("roles")
+
+ return {
+ data: {
+ assignedCount: userIds.length,
+ message: `${userIds.length}명의 사용자가 성공적으로 할당되었습니다.`
+ },
+ error: null
+ }
+ } catch (err) {
+ return {
+ data: null,
+ error: getErrorMessage(err)
+ }
+ }
+}
+
+/**
+ * 특정 롤에서 사용자들을 제거합니다
+ */
+export async function removeUsersFromRole(roleId: number, userIds: number[]) {
+ unstable_noStore() // 캐싱 방지(Next.js 서버 액션용)
+
+ try {
+ if (userIds.length === 0) {
+ return { data: null, error: "선택된 사용자가 없습니다." }
+ }
+
+ await db.transaction(async (tx) => {
+ // 해당 롤에서 특정 사용자들만 삭제
+ await tx
+ .delete(userRoles)
+ .where(
+ and(
+ eq(userRoles.roleId, roleId),
+ inArray(userRoles.userId, userIds)
+ )
+ )
+ })
+
+ revalidateTag("users")
+ revalidateTag("roles")
+
+ return {
+ data: {
+ removedCount: userIds.length,
+ message: `${userIds.length}명의 사용자가 성공적으로 제거되었습니다.`
+ },
+ error: null
+ }
+ } catch (err) {
+ return {
+ data: null,
+ error: getErrorMessage(err)
+ }
+ }
+}
+
+/**
+ * 롤의 모든 사용자 할당을 재설정합니다 (기존 함수와 동일)
+ * 기존 할당을 모두 삭제하고 새로운 할당으로 교체합니다
+ */
+export async function replaceRoleAssignments(roleId: number, userIds: number[]) {
+ unstable_noStore() // 캐싱 방지(Next.js 서버 액션용)
+
+ try {
await db.transaction(async (tx) => {
// 1) 기존 userRoles 레코드 삭제
await tx.delete(userRoles).where(eq(userRoles.roleId, roleId))
-
+
// 2) 새로 넣기
if (userIds.length > 0) {
await tx.insert(userRoles).values(
@@ -573,15 +673,41 @@ export async function assignUsersToRole(roleId: number, userIds: number[]) {
)
}
})
- revalidateTag("users");
- revalidateTag("roles");
-
- return { data: null, error: null };
- } catch (err){
- return { data: null, error: getErrorMessage(err) };
-
+
+ revalidateTag("users")
+ revalidateTag("roles")
+
+ return { data: null, error: null }
+ } catch (err) {
+ return { data: null, error: getErrorMessage(err) }
}
+}
+/**
+ * 특정 롤에 할당된 사용자 목록을 가져옵니다
+ */
+export async function getUsersAssignedToRole(roleId: number) {
+ unstable_noStore()
+
+ try {
+ const assignedUsers = await db
+ .select({
+ userId: userRoles.userId,
+ // 필요한 다른 사용자 정보들도 join해서 가져올 수 있습니다
+ })
+ .from(userRoles)
+ .where(eq(userRoles.roleId, roleId))
+
+ return {
+ data: assignedUsers.map(u => u.userId),
+ error: null
+ }
+ } catch (err) {
+ return {
+ data: [],
+ error: getErrorMessage(err)
+ }
+ }
}
@@ -767,3 +893,4 @@ export async function getUserRoles(userId: number): Promise<string[]> {
return []
}
}
+