1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
// lib/owner-companies/service.ts
"use server";
// export const dynamic = "force-dynamic";
// export const runtime = "nodejs";
import db from "@/db/db";
import { ownerCompanies, users } from "@/db/schema";
import { revalidatePath, unstable_noStore } from "next/cache";
import { eq } from "drizzle-orm";
export async function createOwnerCompany(data: { name: string }) {
const [company] = await db
.insert(ownerCompanies)
.values({
name: data.name,
})
.returning();
revalidatePath("/owner-companies");
return { success: true, data: company };
}
export async function updateOwnerCompany(
id: number,
data: { name: string }
) {
const [company] = await db
.update(ownerCompanies)
.set({
name: data.name,
})
.where(eq(ownerCompanies.id, id))
.returning();
revalidatePath("/owner-companies");
revalidatePath(`/owner-companies/${id}`);
return { success: true, data: company };
}
export async function createOwnerCompanyUser(
companyId: number,
data: {
name: string;
email: string;
phone?: string;
employeeNumber?: string;
}
) {
// 이메일 정규화 (다른 유저 생성 로직과 일관성 유지)
const normalizedEmail = data.email.toLowerCase().trim();
// 이메일 중복 체크 (정규화된 이메일로 체크)
const existing = await db
.select()
.from(users)
.where(eq(users.email, normalizedEmail))
.limit(1);
if (existing.length > 0) {
return { success: false, error: "이미 사용 중인 이메일입니다." };
}
const [user] = await db
.insert(users)
.values({
...data,
email: normalizedEmail, // 정규화된 이메일로 저장
ownerCompanyId: companyId,
domain: "partners", // 발주처 도메인
isActive: true,
})
.returning();
revalidatePath(`/evcp/data-room/owner-companies/${companyId}/users`);
return { success: true, data: user };
}
export async function getOwnerCompanyUsers(companyId: number) {
return await db
.select()
.from(users)
.where(eq(users.ownerCompanyId, companyId))
.orderBy(users.createdAt);
}
export const getOwnerCompanyList = async() => {
unstable_noStore()
const companies = await db.select().from(ownerCompanies).orderBy(ownerCompanies.createdAt);
return companies
}
export const getTargetOwnerCompany = async(companyId:number) => {
const [company] = await db
.select()
.from(ownerCompanies)
.where(eq(ownerCompanies.id, companyId))
.limit(1);
return company
}
|