summaryrefslogtreecommitdiff
path: root/lib/vendors/service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'lib/vendors/service.ts')
-rw-r--r--lib/vendors/service.ts45
1 files changed, 40 insertions, 5 deletions
diff --git a/lib/vendors/service.ts b/lib/vendors/service.ts
index fb834814..7c6ac15d 100644
--- a/lib/vendors/service.ts
+++ b/lib/vendors/service.ts
@@ -30,6 +30,7 @@ import {
selectVendorsWithTypes,
countVendorsWithTypes,
countVendorMaterials,
+ selectVendorMaterials,
insertVendorMaterial,
} from "./repository";
@@ -56,7 +57,7 @@ import { promises as fsPromises } from 'fs';
import { sendEmail } from "../mail/sendEmail";
import { PgTransaction } from "drizzle-orm/pg-core";
import { items, materials } from "@/db/schema/items";
-import { users } from "@/db/schema/users";
+import { roles, userRoles, users } from "@/db/schema/users";
import { getServerSession } from "next-auth";
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
import { contracts, contractsDetailView, projects, vendorPQSubmissions, vendorProjectPQs, vendorsLogs } from "@/db/schema";
@@ -306,7 +307,8 @@ export type CreateVendorData = {
creditRating?: string
cashFlowRating?: string
corporateRegistrationNumber?: string
-
+ businessSize?: string
+
country?: string
status?: "PENDING_REVIEW" | "IN_REVIEW" | "IN_PQ" | "PQ_FAILED" | "APPROVED" | "ACTIVE" | "INACTIVE" | "BLACKLISTED" | "PQ_SUBMITTED"
}
@@ -1468,7 +1470,7 @@ interface ApproveVendorsInput {
*/
export async function approveVendors(input: ApproveVendorsInput & { userId: number }) {
unstable_noStore();
-
+
try {
// 트랜잭션 내에서 협력업체 상태 업데이트, 유저 생성 및 이메일 발송
const result = await db.transaction(async (tx) => {
@@ -1507,7 +1509,7 @@ export async function approveVendors(input: ApproveVendorsInput & { userId: numb
if (!vendor.email) return; // 이메일이 없으면 스킵
// 이미 존재하는 유저인지 확인
- const existingUser = await db.query.users.findFirst({
+ const existingUser = await tx.query.users.findFirst({
where: eq(users.email, vendor.email),
columns: {
id: true
@@ -1516,11 +1518,42 @@ export async function approveVendors(input: ApproveVendorsInput & { userId: numb
// 유저가 존재하지 않는 경우에만 생성
if (!existingUser) {
- await tx.insert(users).values({
+ // 유저 생성
+ const [newUser] = await tx.insert(users).values({
name: vendor.vendorName,
email: vendor.email,
companyId: vendor.id,
domain: "partners", // 기본값으로 이미 설정되어 있지만 명시적으로 지정
+ }).returning({ id: users.id });
+
+ // "Vendor Admin" 역할 찾기 또는 생성
+ let vendorAdminRole = await tx.query.roles.findFirst({
+ where: and(
+ eq(roles.name, "Vendor Admin"),
+ eq(roles.domain, "partners"),
+ eq(roles.companyId, vendor.id)
+ ),
+ columns: {
+ id: true
+ }
+ });
+
+ // "Vendor Admin" 역할이 없다면 생성
+ if (!vendorAdminRole) {
+ const [newRole] = await tx.insert(roles).values({
+ name: "Vendor Admin",
+ domain: "partners",
+ companyId: vendor.id,
+ description: "Vendor Administrator role",
+ }).returning({ id: roles.id });
+
+ vendorAdminRole = newRole;
+ }
+
+ // userRoles 테이블에 관계 생성
+ await tx.insert(userRoles).values({
+ userId: newUser.id,
+ roleId: vendorAdminRole.id,
});
}
})
@@ -1580,6 +1613,8 @@ export async function approveVendors(input: ApproveVendorsInput & { userId: numb
revalidateTag("vendors");
revalidateTag("vendor-status-counts");
revalidateTag("users"); // 유저 캐시도 무효화
+ revalidateTag("roles"); // 역할 캐시도 무효화
+ revalidateTag("user-roles"); // 유저 역할 캐시도 무효화
return { data: result, error: null };
} catch (err) {