summaryrefslogtreecommitdiff
path: root/lib/shi-api/shi-api-utils.ts
diff options
context:
space:
mode:
authorjoonhoekim <26rote@gmail.com>2025-10-03 13:54:38 +0900
committerjoonhoekim <26rote@gmail.com>2025-10-03 13:54:38 +0900
commit8945be5ea89365f8a686a0e65b5a7d5b61c2ca20 (patch)
treed7ee4acd93bcffacea3c095cb60d5a9c67998be9 /lib/shi-api/shi-api-utils.ts
parentdefda07c0bb4b0bd444ca8dc4fd3f89322bda0ce (diff)
(김준회) 부서별 권한관리, swp 코멘트 기능, 벤더 po, shi-api 동기화 로직 수정
Diffstat (limited to 'lib/shi-api/shi-api-utils.ts')
-rw-r--r--lib/shi-api/shi-api-utils.ts44
1 files changed, 37 insertions, 7 deletions
diff --git a/lib/shi-api/shi-api-utils.ts b/lib/shi-api/shi-api-utils.ts
index bb80c454..e3ab1102 100644
--- a/lib/shi-api/shi-api-utils.ts
+++ b/lib/shi-api/shi-api-utils.ts
@@ -2,9 +2,10 @@
import { nonsapUser, users } from '@/db/schema';
import db from '@/db/db';
+import { eq } from 'drizzle-orm';
import { debugError, debugLog, debugWarn, debugSuccess } from '@/lib/debug-utils';
import { bulkUpsert } from '@/lib/soap/batch-utils';
-import { autoAssignPendingUsersDomains } from '@/lib/users/department-domain/service';
+import { autoAssignUsersDomains, type UserDomain } from '@/lib/users/department-domain/service';
const shiApiBaseUrl = process.env.SHI_API_BASE_URL;
const shiNonsapUserSegment = process.env.SHI_NONSAP_USER_SEGMENT;
@@ -91,11 +92,34 @@ export const getAllNonsapUser = async () => {
for (let i = 0; i < sourceData.length; i += CHUNK_SIZE) {
const chunk = sourceData.slice(i, i + CHUNK_SIZE);
debugLog(`[CHUNK ${Math.floor(i/CHUNK_SIZE) + 1}] Processing ${chunk.length} users (${i + 1}-${Math.min(i + chunk.length, sourceData.length)}/${sourceData.length})`);
-
+
try {
+ // 청크 내 이메일별 기존 domain 정보 조회를 위한 Map 생성
+ const existingDomainMap = new Map<string, string>();
+
+ // 각 사용자에 대해 개별적으로 기존 domain 조회 (메모리 효율성 위해)
+ for (const u of chunk) {
+ if (u.EMAIL_ADR) {
+ try {
+ const existingUser = await db
+ .select({ domain: users.domain })
+ .from(users)
+ .where(eq(users.email, u.EMAIL_ADR))
+ .limit(1);
+
+ if (existingUser.length > 0) {
+ existingDomainMap.set(u.EMAIL_ADR.toLowerCase(), existingUser[0].domain);
+ }
+ } catch (error) {
+ // 조회 실패 시 무시하고 계속 진행
+ console.warn(`Failed to lookup existing user for email ${u.EMAIL_ADR}:`, error);
+ }
+ }
+ }
+
// 청크 단위로 매핑 수행
const mappedChunk: InsertUser[] = [];
-
+
for (const u of chunk) {
const isDeleted = ynToBool(u.DEL_YN); // nonsap user 테이블에서 삭제여부
const isAbsent = ynToBool(u.LOFF_GB); // nonsap user 테이블에서 휴직여부
@@ -104,18 +128,24 @@ export const getAllNonsapUser = async () => {
// S = 정직원
const isRegularEmployee = (u.REGL_ORORD_GB || '').toUpperCase() === 'S';
- const mappedUser: Partial<InsertUser> = {
+ // 기존 사용자인지 확인하여 domain 결정
+ const email = u.EMAIL_ADR;
+ const existingDomain = email ? existingDomainMap.get(email.toLowerCase()) as UserDomain | undefined : undefined;
+ // 기존 사용자(existingDomain !== undefined)는 기존 domain을 무조건 유지, 새 사용자는 pending
+ const domain = existingDomain !== undefined ? existingDomain : 'pending';
+
+ const mappedUser: Partial<InsertUser> = {
// mapped fields
nonsapUserId: u.USR_ID || undefined,
employeeNumber: u.EMPNO || undefined,
knoxId: u.MYSNG_ID || undefined,
name: u.USR_NM || undefined,
- email: u.EMAIL_ADR || undefined,
+ email: email || undefined,
epId: u.MYSNG_USR_ID || undefined,
deptCode: u.DEPTCD || undefined,
deptName: u.DEPTNM || undefined,
phone: u.HP_NO || undefined,
- domain: 'pending', // SHI-API를 통해 동기화되는 사용자는 pending 도메인으로 설정
+ domain, // 기존 사용자는 기존 domain 유지, 새 사용자는 pending
isAbsent,
isDeletedOnNonSap: isDeleted,
isActive,
@@ -160,7 +190,7 @@ export const getAllNonsapUser = async () => {
// ** 4. 사용자 동기화 완료 후 부서별 도메인 자동 할당 처리 **
debugLog('[DOMAIN-AUTO-ASSIGN] SHI-API 동기화 완료 후 부서별 도메인 자동 할당 시작');
try {
- const domainAssignResult = await autoAssignPendingUsersDomains();
+ const domainAssignResult = await autoAssignUsersDomains();
debugSuccess(`[DOMAIN-AUTO-ASSIGN] 부서별 도메인 자동 할당 완료: ${domainAssignResult.message}`);
} catch (domainError) {
debugError('[DOMAIN-AUTO-ASSIGN] 부서별 도메인 자동 할당 실패', domainError);