summaryrefslogtreecommitdiff
path: root/lib/vendor-document-list/sync-service.ts
diff options
context:
space:
mode:
authordujinkim <dujin.kim@dtsolution.co.kr>2025-08-14 11:54:47 +0000
committerdujinkim <dujin.kim@dtsolution.co.kr>2025-08-14 11:54:47 +0000
commit969c25b56f6d29d7ffa4bc2ce04c5fb4e5846b34 (patch)
tree551d335e850e6163792ded0e7a75fa41d96d612a /lib/vendor-document-list/sync-service.ts
parentdd20ba9785cdbd3d61f6b014d003d3bd9646ad13 (diff)
(대표님) 정규벤더등록, 벤더문서관리, 벤더데이터입력, 첨부파일관리
Diffstat (limited to 'lib/vendor-document-list/sync-service.ts')
-rw-r--r--lib/vendor-document-list/sync-service.ts65
1 files changed, 46 insertions, 19 deletions
diff --git a/lib/vendor-document-list/sync-service.ts b/lib/vendor-document-list/sync-service.ts
index 0544ce06..cdc22e11 100644
--- a/lib/vendor-document-list/sync-service.ts
+++ b/lib/vendor-document-list/sync-service.ts
@@ -8,6 +8,8 @@ import {
} from "@/db/schema/vendorDocu"
import { documents, revisions, documentAttachments } from "@/db/schema/vendorDocu"
import { eq, and, lt, desc, sql, inArray } from "drizzle-orm"
+import { getServerSession } from "next-auth/next"
+import { authOptions } from "@/app/api/auth/[...nextauth]/route"
export interface SyncableEntity {
entityType: 'document' | 'revision' | 'attachment'
@@ -42,7 +44,7 @@ class SyncService {
* 변경사항을 change_logs에 기록
*/
async logChange(
- projectId: number,
+ vendorId: number,
entityType: 'document' | 'revision' | 'attachment',
entityId: number,
action: 'CREATE' | 'UPDATE' | 'DELETE',
@@ -56,7 +58,7 @@ class SyncService {
const changedFields = this.detectChangedFields(oldValues, newValues)
await db.insert(changeLogs).values({
- projectId,
+ vendorId,
entityType,
entityId,
action,
@@ -99,7 +101,7 @@ class SyncService {
* 동기화할 변경사항 조회 (증분)
*/
async getPendingChanges(
- projectId: number,
+ vendorId: number,
targetSystem: string = 'DOLCE',
limit?: number
): Promise<ChangeLog[]> {
@@ -107,7 +109,7 @@ class SyncService {
.select()
.from(changeLogs)
.where(and(
- eq(changeLogs.projectId, projectId),
+ eq(changeLogs.vendorId, vendorId),
eq(changeLogs.isSynced, false),
lt(changeLogs.syncAttempts, 3),
sql`(${changeLogs.targetSystems} IS NULL OR ${changeLogs.targetSystems} @> ${JSON.stringify([targetSystem])})`
@@ -136,14 +138,14 @@ class SyncService {
* 동기화 배치 생성
*/
async createSyncBatch(
- projectId: number,
+ vendorId: number,
targetSystem: string,
changeLogIds: number[]
): Promise<number> {
const [batch] = await db
.insert(syncBatches)
.values({
- projectId,
+ vendorId,
targetSystem,
batchSize: changeLogIds.length,
changeLogIds,
@@ -168,8 +170,16 @@ class SyncService {
throw new Error(`Sync not enabled for ${targetSystem}`)
}
+ const session = await getServerSession(authOptions)
+ if (!session?.user?.companyId) {
+ throw new Error("인증이 필요합니다.")
+ }
+
+ const vendorId = Number(session.user.companyId)
+
+
// 2. 대기 중인 변경사항 조회 (전체)
- const pendingChanges = await this.getPendingChanges(projectId, targetSystem)
+ const pendingChanges = await this.getPendingChanges(vendorId, targetSystem)
if (pendingChanges.length === 0) {
return {
@@ -182,7 +192,7 @@ class SyncService {
// 3. 배치 생성
const batchId = await this.createSyncBatch(
- projectId,
+ vendorId,
targetSystem,
pendingChanges.map(c => c.id)
)
@@ -446,11 +456,20 @@ class SyncService {
*/
async getSyncStatus(projectId: number, targetSystem: string = 'DOLCE') {
try {
+
+ const session = await getServerSession(authOptions)
+ if (!session?.user?.companyId) {
+ throw new Error("인증이 필요합니다.")
+ }
+
+ const vendorId = Number(session.user.companyId)
+
+
// 대기 중인 변경사항 수 조회
const pendingCount = await db.$count(
changeLogs,
and(
- eq(changeLogs.projectId, projectId),
+ eq(changeLogs.vendorId, vendorId),
eq(changeLogs.isSynced, false),
lt(changeLogs.syncAttempts, 3),
sql`(${changeLogs.targetSystems} IS NULL OR ${changeLogs.targetSystems} @> ${JSON.stringify([targetSystem])})`
@@ -461,7 +480,7 @@ class SyncService {
const syncedCount = await db.$count(
changeLogs,
and(
- eq(changeLogs.projectId, projectId),
+ eq(changeLogs.vendorId, vendorId),
eq(changeLogs.isSynced, true),
sql`(${changeLogs.targetSystems} IS NULL OR ${changeLogs.targetSystems} @> ${JSON.stringify([targetSystem])})`
)
@@ -471,7 +490,7 @@ class SyncService {
const failedCount = await db.$count(
changeLogs,
and(
- eq(changeLogs.projectId, projectId),
+ eq(changeLogs.vendorId, vendorId),
eq(changeLogs.isSynced, false),
sql`${changeLogs.syncAttempts} >= 3`,
sql`(${changeLogs.targetSystems} IS NULL OR ${changeLogs.targetSystems} @> ${JSON.stringify([targetSystem])})`
@@ -483,7 +502,7 @@ class SyncService {
.select()
.from(syncBatches)
.where(and(
- eq(syncBatches.projectId, projectId),
+ eq(syncBatches.vendorId, vendorId),
eq(syncBatches.targetSystem, targetSystem),
eq(syncBatches.status, 'SUCCESS')
))
@@ -491,7 +510,7 @@ class SyncService {
.limit(1)
return {
- projectId,
+ vendorId,
targetSystem,
totalChanges: pendingCount + syncedCount + failedCount,
pendingChanges: pendingCount,
@@ -511,11 +530,19 @@ class SyncService {
*/
async getRecentSyncBatches(projectId: number, targetSystem: string = 'DOLCE', limit: number = 10) {
try {
+
+ const session = await getServerSession(authOptions)
+ if (!session?.user?.companyId) {
+ throw new Error("인증이 필요합니다.")
+ }
+
+ const vendorId = Number(session.user.companyId)
+
const batches = await db
.select()
.from(syncBatches)
.where(and(
- eq(syncBatches.projectId, projectId),
+ eq(syncBatches.vendorId, vendorId),
eq(syncBatches.targetSystem, targetSystem)
))
.orderBy(desc(syncBatches.createdAt))
@@ -524,7 +551,7 @@ class SyncService {
// Date 객체를 문자열로 변환
return batches.map(batch => ({
id: Number(batch.id),
- projectId: batch.projectId,
+ vendorId: batch.vendorId,
targetSystem: batch.targetSystem,
batchSize: batch.batchSize,
status: batch.status,
@@ -561,7 +588,7 @@ export async function logDocumentChange(
}
export async function logRevisionChange(
- projectId: number,
+ vendorId: number,
revisionId: number,
action: 'CREATE' | 'UPDATE' | 'DELETE',
newValues?: any,
@@ -570,11 +597,11 @@ export async function logRevisionChange(
userName?: string,
targetSystems: string[] = ["DOLCE", "SWP"]
) {
- return syncService.logChange(projectId, 'revision', revisionId, action, newValues, oldValues, userId, userName, targetSystems)
+ return syncService.logChange(vendorId, 'revision', revisionId, action, newValues, oldValues, userId, userName, targetSystems)
}
export async function logAttachmentChange(
- projectId: number,
+ vendorId: number,
attachmentId: number,
action: 'CREATE' | 'UPDATE' | 'DELETE',
newValues?: any,
@@ -583,5 +610,5 @@ export async function logAttachmentChange(
userName?: string,
targetSystems: string[] = ["DOLCE", "SWP"]
) {
- return syncService.logChange(projectId, 'attachment', attachmentId, action, newValues, oldValues, userId, userName, targetSystems)
+ return syncService.logChange(vendorId, 'attachment', attachmentId, action, newValues, oldValues, userId, userName, targetSystems)
} \ No newline at end of file