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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
|
'use server'
import { revalidateTag, unstable_cache ,unstable_noStore} from "next/cache"
import {count, desc, asc, and, or, gte, lte, ilike, eq, inArray, sql } from "drizzle-orm"
import { filterColumns } from "@/lib/filter-columns"
import db from "@/db/db"
import { vendorResponseDetailView,
attachmentRevisionHistoryView,
rfqProgressSummaryView,
vendorResponseAttachmentsEnhanced ,Incoterm, RfqDashboardView, Vendor, VendorAttachmentResponse, bRfqAttachmentRevisions, bRfqs, bRfqsAttachments, incoterms, initialRfq, initialRfqDetailView, projects, users, vendorAttachmentResponses, vendors,
vendorResponseAttachmentsB} from "@/db/schema" // 실제 스키마 import 경로에 맞게 수정
import { rfqDashboardView } from "@/db/schema" // 뷰 import
import type { SQL } from "drizzle-orm"
import { AttachmentRecord, BulkEmailInput, CreateRfqInput, DeleteAttachmentsInput, GetInitialRfqDetailSchema, GetRFQDashboardSchema, GetRfqAttachmentsSchema, GetVendorResponsesSchema, RemoveInitialRfqsSchema, RequestRevisionResult, ResponseStatus, UpdateInitialRfqSchema, VendorRfqResponseSummary, attachmentRecordSchema, bulkEmailSchema, createRfqServerSchema, deleteAttachmentsSchema, removeInitialRfqsSchema, requestRevisionSchema, updateInitialRfqSchema } from "./validations"
import { getServerSession } from "next-auth/next"
import { authOptions } from "@/app/api/auth/[...nextauth]/route"
import { unlink } from "fs/promises"
import { getErrorMessage } from "../handle-error"
import { AddInitialRfqFormData } from "./initial/add-initial-rfq-dialog"
import { sendEmail } from "../mail/sendEmail"
import { RfqType } from "../rfqs/validations"
const tag = {
initialRfqDetail:"initial-rfq",
rfqDashboard: 'rfq-dashboard',
rfq: (id: number) => `rfq-${id}`,
rfqAttachments: (rfqId: number) => `rfq-attachments-${rfqId}`,
attachmentRevisions: (attId: number) => `attachment-revisions-${attId}`,
vendorResponses: (
attId: number,
type: 'INITIAL' | 'FINAL' = 'INITIAL',
) => `vendor-responses-${attId}-${type}`,
} as const;
export async function getRFQDashboard(input: GetRFQDashboardSchema) {
try {
const offset = (input.page - 1) * input.perPage;
const rfqFilterMapping = createRFQFilterMapping();
const joinedTables = getRFQJoinedTables();
console.log(input, "견적 인풋")
// 1) 고급 필터 조건
let advancedWhere: SQL<unknown> | undefined = undefined;
if (input.filters && input.filters.length > 0) {
advancedWhere = filterColumns({
table: rfqDashboardView,
filters: input.filters,
joinOperator: input.joinOperator || 'and',
joinedTables,
customColumnMapping: rfqFilterMapping,
});
}
// 2) 기본 필터 조건
let basicWhere: SQL<unknown> | undefined = undefined;
if (input.basicFilters && input.basicFilters.length > 0) {
basicWhere = filterColumns({
table: rfqDashboardView,
filters: input.basicFilters,
joinOperator: input.basicJoinOperator || 'and',
joinedTables,
customColumnMapping: rfqFilterMapping,
});
}
// 3) 글로벌 검색 조건
let globalWhere: SQL<unknown> | undefined = undefined;
if (input.search) {
const s = `%${input.search}%`;
const validSearchConditions: SQL<unknown>[] = [];
const rfqCodeCondition = ilike(rfqDashboardView.rfqCode, s);
if (rfqCodeCondition) validSearchConditions.push(rfqCodeCondition);
const descriptionCondition = ilike(rfqDashboardView.description, s);
if (descriptionCondition) validSearchConditions.push(descriptionCondition);
const projectNameCondition = ilike(rfqDashboardView.projectName, s);
if (projectNameCondition) validSearchConditions.push(projectNameCondition);
const projectCodeCondition = ilike(rfqDashboardView.projectCode, s);
if (projectCodeCondition) validSearchConditions.push(projectCodeCondition);
const picNameCondition = ilike(rfqDashboardView.picName, s);
if (picNameCondition) validSearchConditions.push(picNameCondition);
const packageNoCondition = ilike(rfqDashboardView.packageNo, s);
if (packageNoCondition) validSearchConditions.push(packageNoCondition);
const packageNameCondition = ilike(rfqDashboardView.packageName, s);
if (packageNameCondition) validSearchConditions.push(packageNameCondition);
if (validSearchConditions.length > 0) {
globalWhere = or(...validSearchConditions);
}
}
// 6) 최종 WHERE 조건 생성
const whereConditions: SQL<unknown>[] = [];
if (advancedWhere) whereConditions.push(advancedWhere);
if (basicWhere) whereConditions.push(basicWhere);
if (globalWhere) whereConditions.push(globalWhere);
const finalWhere = whereConditions.length > 0 ? and(...whereConditions) : undefined;
// 7) 전체 데이터 수 조회
const totalResult = await db
.select({ count: count() })
.from(rfqDashboardView)
.where(finalWhere);
const total = totalResult[0]?.count || 0;
if (total === 0) {
return { data: [], pageCount: 0, total: 0 };
}
console.log(total)
// 8) 정렬 및 페이징 처리된 데이터 조회
const orderByColumns = input.sort.map((sort) => {
const column = sort.id as keyof typeof rfqDashboardView.$inferSelect;
return sort.desc ? desc(rfqDashboardView[column]) : asc(rfqDashboardView[column]);
});
if (orderByColumns.length === 0) {
orderByColumns.push(desc(rfqDashboardView.createdAt));
}
const rfqData = await db
.select()
.from(rfqDashboardView)
.where(finalWhere)
.orderBy(...orderByColumns)
.limit(input.perPage)
.offset(offset);
const pageCount = Math.ceil(total / input.perPage);
return { data: rfqData, pageCount, total };
} catch (err) {
console.error("Error in getRFQDashboard:", err);
return { data: [], pageCount: 0, total: 0 };
}
}
// 헬퍼 함수들
function createRFQFilterMapping() {
return {
// 뷰의 컬럼명과 실제 필터링할 컬럼 매핑
rfqCode: rfqDashboardView.rfqCode,
description: rfqDashboardView.description,
status: rfqDashboardView.status,
projectName: rfqDashboardView.projectName,
projectCode: rfqDashboardView.projectCode,
picName: rfqDashboardView.picName,
packageNo: rfqDashboardView.packageNo,
packageName: rfqDashboardView.packageName,
dueDate: rfqDashboardView.dueDate,
overallProgress: rfqDashboardView.overallProgress,
createdAt: rfqDashboardView.createdAt,
};
}
function getRFQJoinedTables() {
return {
// 조인된 테이블 정보 (뷰이므로 실제로는 사용되지 않을 수 있음)
projects,
users,
};
}
// ================================================================
// 3. RFQ Dashboard 타입 정의
// ================================================================
async function generateNextSerial(picCode: string): Promise<string> {
try {
// 해당 picCode로 시작하는 RFQ 개수 조회
const existingCount = await db
.select({ count: count() })
.from(bRfqs)
.where(eq(bRfqs.picCode, picCode))
const nextSerial = (existingCount[0]?.count || 0) + 1
return nextSerial.toString().padStart(5, '0') // 5자리로 패딩
} catch (error) {
console.error("시리얼 번호 생성 오류:", error)
return "00001" // 기본값
}
}
export async function createRfqAction(input: CreateRfqInput) {
try {
// 입력 데이터 검증
const validatedData = createRfqServerSchema.parse(input)
// RFQ 코드 자동 생성: N + picCode + 시리얼5자리
const serialNumber = await generateNextSerial(validatedData.picCode)
const rfqCode = `N${validatedData.picCode}${serialNumber}`
// 데이터베이스에 삽입
const result = await db.insert(bRfqs).values({
rfqCode,
projectId: validatedData.projectId,
dueDate: validatedData.dueDate,
status: "DRAFT",
picCode: validatedData.picCode,
picName: validatedData.picName || null,
EngPicName: validatedData.engPicName || null,
packageNo: validatedData.packageNo || null,
packageName: validatedData.packageName || null,
remark: validatedData.remark || null,
projectCompany: validatedData.projectCompany || null,
projectFlag: validatedData.projectFlag || null,
projectSite: validatedData.projectSite || null,
createdBy: validatedData.createdBy,
updatedBy: validatedData.updatedBy,
}).returning({
id: bRfqs.id,
rfqCode: bRfqs.rfqCode,
})
return {
success: true,
data: result[0],
message: "RFQ가 성공적으로 생성되었습니다",
}
} catch (error) {
console.error("RFQ 생성 오류:", error)
return {
success: false,
error: "RFQ 생성에 실패했습니다",
}
}
}
// RFQ 코드 중복 확인 액션
export async function checkRfqCodeExists(rfqCode: string) {
try {
const existing = await db.select({ id: bRfqs.id })
.from(bRfqs)
.where(eq(bRfqs.rfqCode, rfqCode))
.limit(1)
return existing.length > 0
} catch (error) {
console.error("RFQ 코드 확인 오류:", error)
return false
}
}
// picCode별 다음 예상 RFQ 코드 미리보기
export async function previewNextRfqCode(picCode: string) {
try {
const serialNumber = await generateNextSerial(picCode)
return `N${picCode}${serialNumber}`
} catch (error) {
console.error("RFQ 코드 미리보기 오류:", error)
return `N${picCode}00001`
}
}
const getBRfqById = async (id: number): Promise<RfqDashboardView | null> => {
// 1) RFQ 단건 조회
const rfqsRes = await db
.select()
.from(rfqDashboardView)
.where(eq(rfqDashboardView.rfqId, id))
.limit(1);
if (rfqsRes.length === 0) return null;
const rfqRow = rfqsRes[0];
// 3) RfqWithItems 형태로 반환
const result: RfqDashboardView = {
...rfqRow,
};
return result;
};
export const findBRfqById = async (id: number): Promise<RfqDashboardView | null> => {
try {
const rfq = await getBRfqById(id);
return rfq;
} catch (error) {
throw new Error('Failed to fetch user');
}
};
export async function getRfqAttachments(
input: GetRfqAttachmentsSchema,
rfqId: number
) {
try {
const offset = (input.page - 1) * input.perPage
// Advanced Filter 처리 (메인 테이블 기준)
const advancedWhere = filterColumns({
table: bRfqsAttachments,
filters: input.filters,
joinOperator: input.joinOperator,
})
// 전역 검색 (첨부파일 + 리비전 파일명 검색)
let globalWhere
if (input.search) {
const s = `%${input.search}%`
globalWhere = or(
ilike(bRfqsAttachments.serialNo, s),
ilike(bRfqsAttachments.description, s),
ilike(bRfqsAttachments.currentRevision, s),
ilike(bRfqAttachmentRevisions.fileName, s),
ilike(bRfqAttachmentRevisions.originalFileName, s)
)
}
// 기본 필터
let basicWhere
if (input.attachmentType.length > 0 || input.fileType.length > 0) {
basicWhere = and(
input.attachmentType.length > 0
? inArray(bRfqsAttachments.attachmentType, input.attachmentType)
: undefined,
input.fileType.length > 0
? inArray(bRfqAttachmentRevisions.fileType, input.fileType)
: undefined
)
}
// 최종 WHERE 절
const finalWhere = and(
eq(bRfqsAttachments.rfqId, rfqId), // RFQ ID 필수 조건
advancedWhere,
globalWhere,
basicWhere
)
// 정렬 (메인 테이블 기준)
const orderBy = input.sort.length > 0
? input.sort.map((item) =>
item.desc ? desc(bRfqsAttachments[item.id as keyof typeof bRfqsAttachments]) : asc(bRfqsAttachments[item.id as keyof typeof bRfqsAttachments])
)
: [desc(bRfqsAttachments.createdAt)]
// 트랜잭션으로 데이터 조회
const { data, total } = await db.transaction(async (tx) => {
// 메인 데이터 조회 (첨부파일 + 최신 리비전 조인)
const data = await tx
.select({
// 첨부파일 메인 정보
id: bRfqsAttachments.id,
attachmentType: bRfqsAttachments.attachmentType,
serialNo: bRfqsAttachments.serialNo,
rfqId: bRfqsAttachments.rfqId,
currentRevision: bRfqsAttachments.currentRevision,
latestRevisionId: bRfqsAttachments.latestRevisionId,
description: bRfqsAttachments.description,
createdBy: bRfqsAttachments.createdBy,
createdAt: bRfqsAttachments.createdAt,
updatedAt: bRfqsAttachments.updatedAt,
// 최신 리비전 파일 정보
fileName: bRfqAttachmentRevisions.fileName,
originalFileName: bRfqAttachmentRevisions.originalFileName,
filePath: bRfqAttachmentRevisions.filePath,
fileSize: bRfqAttachmentRevisions.fileSize,
fileType: bRfqAttachmentRevisions.fileType,
revisionComment: bRfqAttachmentRevisions.revisionComment,
// 생성자 정보
createdByName: users.name,
})
.from(bRfqsAttachments)
.leftJoin(
bRfqAttachmentRevisions,
and(
eq(bRfqsAttachments.latestRevisionId, bRfqAttachmentRevisions.id),
eq(bRfqAttachmentRevisions.isLatest, true)
)
)
.leftJoin(users, eq(bRfqsAttachments.createdBy, users.id))
.where(finalWhere)
.orderBy(...orderBy)
.limit(input.perPage)
.offset(offset)
// 전체 개수 조회
const totalResult = await tx
.select({ count: count() })
.from(bRfqsAttachments)
.leftJoin(
bRfqAttachmentRevisions,
eq(bRfqsAttachments.latestRevisionId, bRfqAttachmentRevisions.id)
)
.where(finalWhere)
const total = totalResult[0]?.count ?? 0
return { data, total }
})
const pageCount = Math.ceil(total / input.perPage)
// 각 첨부파일별 벤더 응답 통계 조회
const attachmentIds = data.map(item => item.id)
let responseStatsMap: Record<number, any> = {}
if (attachmentIds.length > 0) {
responseStatsMap = await getAttachmentResponseStats(attachmentIds)
}
// 통계 데이터 병합
const dataWithStats = data.map(attachment => ({
...attachment,
responseStats: responseStatsMap[attachment.id] || {
totalVendors: 0,
respondedCount: 0,
pendingCount: 0,
waivedCount: 0,
responseRate: 0
}
}))
return { data: dataWithStats, pageCount }
} catch (err) {
console.error("getRfqAttachments error:", err)
return { data: [], pageCount: 0 }
}
}
// 첨부파일별 벤더 응답 통계 조회
async function getAttachmentResponseStats(attachmentIds: number[]) {
try {
const stats = await db
.select({
attachmentId: vendorAttachmentResponses.attachmentId,
totalVendors: count(),
respondedCount: sql<number>`count(case when ${vendorAttachmentResponses.responseStatus} = 'RESPONDED' then 1 end)`,
pendingCount: sql<number>`count(case when ${vendorAttachmentResponses.responseStatus} = 'NOT_RESPONDED' then 1 end)`,
waivedCount: sql<number>`count(case when ${vendorAttachmentResponses.responseStatus} = 'WAIVED' then 1 end)`,
})
.from(vendorAttachmentResponses)
.where(inArray(vendorAttachmentResponses.attachmentId, attachmentIds))
.groupBy(vendorAttachmentResponses.attachmentId)
// 응답률 계산해서 객체로 변환
const statsMap: Record<number, any> = {}
stats.forEach(stat => {
const activeVendors = stat.totalVendors - stat.waivedCount
const responseRate = activeVendors > 0
? Math.round((stat.respondedCount / activeVendors) * 100)
: 0
statsMap[stat.attachmentId] = {
totalVendors: stat.totalVendors,
respondedCount: stat.respondedCount,
pendingCount: stat.pendingCount,
waivedCount: stat.waivedCount,
responseRate
}
})
return statsMap
} catch (error) {
console.error("getAttachmentResponseStats error:", error)
return {}
}
}
// 특정 첨부파일에 대한 벤더 응답 현황 상세 조회
export async function getVendorResponsesForAttachment(
attachmentId: number,
rfqType: 'INITIAL' | 'FINAL' = 'INITIAL'
) {
try {
// 1. 기본 벤더 응답 정보 가져오기
const responses = await db
.select({
id: vendorAttachmentResponses.id,
attachmentId: vendorAttachmentResponses.attachmentId,
vendorId: vendorAttachmentResponses.vendorId,
vendorCode: vendors.vendorCode,
vendorName: vendors.vendorName,
vendorCountry: vendors.country,
rfqType: vendorAttachmentResponses.rfqType,
rfqRecordId: vendorAttachmentResponses.rfqRecordId,
responseStatus: vendorAttachmentResponses.responseStatus,
currentRevision: vendorAttachmentResponses.currentRevision,
respondedRevision: vendorAttachmentResponses.respondedRevision,
responseComment: vendorAttachmentResponses.responseComment,
vendorComment: vendorAttachmentResponses.vendorComment,
// 새로 추가된 필드들
revisionRequestComment: vendorAttachmentResponses.revisionRequestComment,
revisionRequestedAt: vendorAttachmentResponses.revisionRequestedAt,
requestedAt: vendorAttachmentResponses.requestedAt,
respondedAt: vendorAttachmentResponses.respondedAt,
updatedAt: vendorAttachmentResponses.updatedAt,
})
.from(vendorAttachmentResponses)
.leftJoin(vendors, eq(vendorAttachmentResponses.vendorId, vendors.id))
.where(
and(
eq(vendorAttachmentResponses.attachmentId, attachmentId),
eq(vendorAttachmentResponses.rfqType, rfqType)
)
)
.orderBy(vendors.vendorName);
// 2. 각 응답에 대한 파일 정보 가져오기
const responseIds = responses.map(r => r.id);
let responseFiles: any[] = [];
if (responseIds.length > 0) {
responseFiles = await db
.select({
id: vendorResponseAttachmentsB.id,
vendorResponseId: vendorResponseAttachmentsB.vendorResponseId,
fileName: vendorResponseAttachmentsB.fileName,
originalFileName: vendorResponseAttachmentsB.originalFileName,
filePath: vendorResponseAttachmentsB.filePath,
fileSize: vendorResponseAttachmentsB.fileSize,
fileType: vendorResponseAttachmentsB.fileType,
description: vendorResponseAttachmentsB.description,
uploadedAt: vendorResponseAttachmentsB.uploadedAt,
})
.from(vendorResponseAttachmentsB)
.where(inArray(vendorResponseAttachmentsB.vendorResponseId, responseIds))
.orderBy(desc(vendorResponseAttachmentsB.uploadedAt));
}
// 3. 응답에 파일 정보 병합
const enhancedResponses = responses.map(response => ({
...response,
files: responseFiles.filter(file => file.vendorResponseId === response.id),
totalFiles: responseFiles.filter(file => file.vendorResponseId === response.id).length,
latestFile: responseFiles
.filter(file => file.vendorResponseId === response.id)
.sort((a, b) => new Date(b.uploadedAt).getTime() - new Date(a.uploadedAt).getTime())[0] || null,
}));
return enhancedResponses;
} catch (err) {
console.error("getVendorResponsesForAttachment error:", err);
return [];
}
}
export async function confirmDocuments(rfqId: number) {
try {
const session = await getServerSession(authOptions)
if (!session?.user?.id) {
throw new Error("인증이 필요합니다.")
}
// TODO: RFQ 상태를 "Doc. Confirmed"로 업데이트
await db
.update(bRfqs)
.set({
status: "Doc. Confirmed",
updatedBy: Number(session.user.id),
updatedAt: new Date(),
})
.where(eq(bRfqs.id, rfqId))
return {
success: true,
message: "문서가 확정되었습니다.",
}
} catch (error) {
console.error("confirmDocuments error:", error)
return {
success: false,
message: error instanceof Error ? error.message : "문서 확정 중 오류가 발생했습니다.",
}
}
}
// TBE 요청 서버 액션
export async function requestTbe(rfqId: number, attachmentIds?: number[]) {
try {
const session = await getServerSession(authOptions)
if (!session?.user?.id) {
throw new Error("인증이 필요합니다.")
}
// attachmentIds가 제공된 경우 해당 첨부파일들만 처리
let targetAttachments = []
if (attachmentIds && attachmentIds.length > 0) {
// 선택된 첨부파일들 조회
targetAttachments = await db
.select({
id: bRfqsAttachments.id,
serialNo: bRfqsAttachments.serialNo,
attachmentType: bRfqsAttachments.attachmentType,
currentRevision: bRfqsAttachments.currentRevision,
})
.from(bRfqsAttachments)
.where(
and(
eq(bRfqsAttachments.rfqId, rfqId),
inArray(bRfqsAttachments.id, attachmentIds)
)
)
if (targetAttachments.length === 0) {
throw new Error("선택된 첨부파일을 찾을 수 없습니다.")
}
} else {
// 전체 RFQ의 모든 첨부파일 처리
targetAttachments = await db
.select({
id: bRfqsAttachments.id,
serialNo: bRfqsAttachments.serialNo,
attachmentType: bRfqsAttachments.attachmentType,
currentRevision: bRfqsAttachments.currentRevision,
})
.from(bRfqsAttachments)
.where(eq(bRfqsAttachments.rfqId, rfqId))
}
if (targetAttachments.length === 0) {
throw new Error("TBE 요청할 첨부파일이 없습니다.")
}
// TODO: TBE 요청 로직 구현
// 1. RFQ 상태를 "TBE started"로 업데이트 (선택적)
// 2. 선택된 첨부파일들에 대해 벤더들에게 TBE 요청 이메일 발송
// 3. vendorAttachmentResponses 테이블에 TBE 요청 레코드 생성
// 4. TBE 관련 메타데이터 업데이트
// 예시: 선택된 첨부파일들에 대한 벤더 응답 레코드 생성
await db.transaction(async (tx) => {
const [updatedRfq] = await tx
.update(bRfqs)
.set({
status: "TBE started",
updatedBy: Number(session.user.id),
updatedAt: new Date(),
})
.where(eq(bRfqs.id, rfqId))
.returning()
// 각 첨부파일에 대해 벤더 응답 레코드 생성 또는 업데이트
for (const attachment of targetAttachments) {
// TODO: 해당 첨부파일과 연관된 벤더들에게 TBE 요청 처리
console.log(`TBE 요청 처리: ${attachment.serialNo} (${attachment.currentRevision})`)
}
})
const attachmentCount = targetAttachments.length
const attachmentList = targetAttachments
.map(a => `${a.serialNo} (${a.currentRevision})`)
.join(', ')
return {
success: true,
message: `${attachmentCount}개 문서에 대한 TBE 요청이 전송되었습니다.\n대상: ${attachmentList}`,
targetAttachments,
}
} catch (error) {
console.error("requestTbe error:", error)
return {
success: false,
message: error instanceof Error ? error.message : "TBE 요청 중 오류가 발생했습니다.",
}
}
}
// 다음 시리얼 번호 생성
async function getNextSerialNo(rfqId: number): Promise<string> {
try {
// 해당 RFQ의 기존 첨부파일 개수 조회
const [result] = await db
.select({ count: count() })
.from(bRfqsAttachments)
.where(eq(bRfqsAttachments.rfqId, rfqId))
const nextNumber = (result?.count || 0) + 1
// 001, 002, 003... 형태로 포맷팅
return nextNumber.toString().padStart(3, '0')
} catch (error) {
console.error("getNextSerialNo error:", error)
// 에러 발생 시 타임스탬프 기반으로 fallback
return Date.now().toString().slice(-3)
}
}
export async function addRfqAttachmentRecord(record: AttachmentRecord) {
try {
const session = await getServerSession(authOptions)
if (!session?.user?.id) {
throw new Error("인증이 필요합니다.")
}
const validatedRecord = attachmentRecordSchema.parse(record)
const userId = Number(session.user.id)
const result = await db.transaction(async (tx) => {
// 1. 시리얼 번호 생성
const [countResult] = await tx
.select({ count: count() })
.from(bRfqsAttachments)
.where(eq(bRfqsAttachments.rfqId, validatedRecord.rfqId))
const serialNo = (countResult.count + 1).toString().padStart(3, '0')
// 2. 메인 첨부파일 레코드 생성
const [attachment] = await tx
.insert(bRfqsAttachments)
.values({
rfqId: validatedRecord.rfqId,
attachmentType: validatedRecord.attachmentType,
serialNo: serialNo,
currentRevision: "Rev.0",
description: validatedRecord.description,
createdBy: userId,
})
.returning()
// 3. 초기 리비전 (Rev.0) 생성
const [revision] = await tx
.insert(bRfqAttachmentRevisions)
.values({
attachmentId: attachment.id,
revisionNo: "Rev.0",
fileName: validatedRecord.fileName,
originalFileName: validatedRecord.originalFileName,
filePath: validatedRecord.filePath,
fileSize: validatedRecord.fileSize,
fileType: validatedRecord.fileType,
revisionComment: validatedRecord.revisionComment,
isLatest: true,
createdBy: userId,
})
.returning()
// 4. 메인 테이블의 latest_revision_id 업데이트
await tx
.update(bRfqsAttachments)
.set({
latestRevisionId: revision.id,
updatedAt: new Date(),
})
.where(eq(bRfqsAttachments.id, attachment.id))
return { attachment, revision }
})
return {
success: true,
message: `파일이 성공적으로 등록되었습니다. (시리얼: ${result.attachment.serialNo}, 리비전: Rev.0)`,
attachment: result.attachment,
revision: result.revision,
}
} catch (error) {
console.error("addRfqAttachmentRecord error:", error)
return {
success: false,
message: error instanceof Error ? error.message : "첨부파일 등록 중 오류가 발생했습니다.",
}
}
}
// 리비전 추가 (기존 첨부파일에 새 버전 추가)
export async function addRevisionToAttachment(
attachmentId: number,
revisionData: {
fileName: string;
originalFileName: string;
filePath: string;
fileSize: number;
fileType: string;
revisionComment?: string;
},
) {
try {
const session = await getServerSession(authOptions);
if (!session?.user?.id) throw new Error('인증이 필요합니다.');
const userId = Number(session.user.id);
// ────────────────────────────────────────────────────────────────────────────
// 0. 첨부파일의 rfqId 사전 조회 (태그 무효화를 위해 필요)
// ────────────────────────────────────────────────────────────────────────────
const [attInfo] = await db
.select({ rfqId: bRfqsAttachments.rfqId })
.from(bRfqsAttachments)
.where(eq(bRfqsAttachments.id, attachmentId))
.limit(1);
if (!attInfo) throw new Error('첨부파일을 찾을 수 없습니다.');
const rfqId = attInfo.rfqId;
// ────────────────────────────────────────────────────────────────────────────
// 1‑5. 리비전 트랜잭션
// ────────────────────────────────────────────────────────────────────────────
const newRevision = await db.transaction(async (tx) => {
// 1. 현재 최신 리비전 조회
const [latestRevision] = await tx
.select({ revisionNo: bRfqAttachmentRevisions.revisionNo })
.from(bRfqAttachmentRevisions)
.where(
and(
eq(bRfqAttachmentRevisions.attachmentId, attachmentId),
eq(bRfqAttachmentRevisions.isLatest, true),
),
);
if (!latestRevision) throw new Error('기존 첨부파일을 찾을 수 없습니다.');
// 2. 새 리비전 번호 생성
const currentNum = parseInt(latestRevision.revisionNo.replace('Rev.', ''));
const newRevisionNo = `Rev.${currentNum + 1}`;
// 3. 기존 리비전 isLatest → false
await tx
.update(bRfqAttachmentRevisions)
.set({ isLatest: false })
.where(
and(
eq(bRfqAttachmentRevisions.attachmentId, attachmentId),
eq(bRfqAttachmentRevisions.isLatest, true),
),
);
// 4. 새 리비전 INSERT
const [inserted] = await tx
.insert(bRfqAttachmentRevisions)
.values({
attachmentId,
revisionNo: newRevisionNo,
fileName: revisionData.fileName,
originalFileName: revisionData.originalFileName,
filePath: revisionData.filePath,
fileSize: revisionData.fileSize,
fileType: revisionData.fileType,
revisionComment: revisionData.revisionComment ?? `${newRevisionNo} 업데이트`,
isLatest: true,
createdBy: userId,
})
.returning();
// 5. 메인 첨부파일 row 업데이트
await tx
.update(bRfqsAttachments)
.set({
currentRevision: newRevisionNo,
latestRevisionId: inserted.id,
updatedAt: new Date(),
})
.where(eq(bRfqsAttachments.id, attachmentId));
return inserted;
});
return {
success: true,
message: `새 리비전(${newRevision.revisionNo})이 성공적으로 추가되었습니다.`,
revision: newRevision,
};
} catch (error) {
console.error('addRevisionToAttachment error:', error);
return {
success: false,
message: error instanceof Error ? error.message : '리비전 추가 중 오류가 발생했습니다.',
};
}
}
// 특정 첨부파일의 모든 리비전 조회
export async function getAttachmentRevisions(attachmentId: number) {
try {
const revisions = await db
.select({
id: bRfqAttachmentRevisions.id,
revisionNo: bRfqAttachmentRevisions.revisionNo,
fileName: bRfqAttachmentRevisions.fileName,
originalFileName: bRfqAttachmentRevisions.originalFileName,
filePath: bRfqAttachmentRevisions.filePath,
fileSize: bRfqAttachmentRevisions.fileSize,
fileType: bRfqAttachmentRevisions.fileType,
revisionComment: bRfqAttachmentRevisions.revisionComment,
isLatest: bRfqAttachmentRevisions.isLatest,
createdBy: bRfqAttachmentRevisions.createdBy,
createdAt: bRfqAttachmentRevisions.createdAt,
createdByName: users.name,
})
.from(bRfqAttachmentRevisions)
.leftJoin(users, eq(bRfqAttachmentRevisions.createdBy, users.id))
.where(eq(bRfqAttachmentRevisions.attachmentId, attachmentId))
.orderBy(desc(bRfqAttachmentRevisions.createdAt))
return {
success: true,
revisions,
}
} catch (error) {
console.error("getAttachmentRevisions error:", error)
return {
success: false,
message: "리비전 조회 중 오류가 발생했습니다.",
revisions: [],
}
}
}
// 첨부파일 삭제 (리비전 포함)
export async function deleteRfqAttachments(input: DeleteAttachmentsInput) {
try {
const session = await getServerSession(authOptions)
if (!session?.user?.id) {
throw new Error("인증이 필요합니다.")
}
const validatedInput = deleteAttachmentsSchema.parse(input)
const result = await db.transaction(async (tx) => {
// 1. 삭제할 첨부파일들의 정보 조회 (파일 경로 포함)
const attachmentsToDelete = await tx
.select({
id: bRfqsAttachments.id,
rfqId: bRfqsAttachments.rfqId,
serialNo: bRfqsAttachments.serialNo,
})
.from(bRfqsAttachments)
.where(inArray(bRfqsAttachments.id, validatedInput.ids))
if (attachmentsToDelete.length === 0) {
throw new Error("삭제할 첨부파일을 찾을 수 없습니다.")
}
// 2. 관련된 모든 리비전 파일 경로 조회
const revisionFilePaths = await tx
.select({ filePath: bRfqAttachmentRevisions.filePath })
.from(bRfqAttachmentRevisions)
.where(inArray(bRfqAttachmentRevisions.attachmentId, validatedInput.ids))
// 3. DB에서 리비전 삭제 (CASCADE로 자동 삭제되지만 명시적으로)
await tx
.delete(bRfqAttachmentRevisions)
.where(inArray(bRfqAttachmentRevisions.attachmentId, validatedInput.ids))
// 4. DB에서 첨부파일 삭제
await tx
.delete(bRfqsAttachments)
.where(inArray(bRfqsAttachments.id, validatedInput.ids))
// 5. 실제 파일 삭제 (비동기로 처리)
Promise.all(
revisionFilePaths.map(async ({ filePath }) => {
try {
if (filePath) {
const fullPath = `${process.cwd()}/public${filePath}`
await unlink(fullPath)
}
} catch (fileError) {
console.warn(`Failed to delete file: ${filePath}`, fileError)
}
})
).catch(error => {
console.error("Some files failed to delete:", error)
})
return {
deletedCount: attachmentsToDelete.length,
rfqIds: [...new Set(attachmentsToDelete.map(a => a.rfqId))],
attachments: attachmentsToDelete,
}
})
return {
success: true,
message: `${result.deletedCount}개의 첨부파일이 삭제되었습니다.`,
deletedAttachments: result.attachments,
}
} catch (error) {
console.error("deleteRfqAttachments error:", error)
return {
success: false,
message: error instanceof Error ? error.message : "첨부파일 삭제 중 오류가 발생했습니다.",
}
}
}
//Initial RFQ
export async function getInitialRfqDetail(input: GetInitialRfqDetailSchema, rfqId?: number) {
try {
const offset = (input.page - 1) * input.perPage;
// 1) 고급 필터 조건
let advancedWhere: SQL<unknown> | undefined = undefined;
if (input.filters && input.filters.length > 0) {
advancedWhere = filterColumns({
table: initialRfqDetailView,
filters: input.filters,
joinOperator: input.joinOperator || 'and',
});
}
// 2) 기본 필터 조건
let basicWhere: SQL<unknown> | undefined = undefined;
if (input.basicFilters && input.basicFilters.length > 0) {
basicWhere = filterColumns({
table: initialRfqDetailView,
filters: input.basicFilters,
joinOperator: input.basicJoinOperator || 'and',
});
}
let rfqIdWhere: SQL<unknown> | undefined = undefined;
if (rfqId) {
rfqIdWhere = eq(initialRfqDetailView.rfqId, rfqId);
}
// 3) 글로벌 검색 조건
let globalWhere: SQL<unknown> | undefined = undefined;
if (input.search) {
const s = `%${input.search}%`;
const validSearchConditions: SQL<unknown>[] = [];
const rfqCodeCondition = ilike(initialRfqDetailView.rfqCode, s);
if (rfqCodeCondition) validSearchConditions.push(rfqCodeCondition);
const vendorNameCondition = ilike(initialRfqDetailView.vendorName, s);
if (vendorNameCondition) validSearchConditions.push(vendorNameCondition);
const vendorCodeCondition = ilike(initialRfqDetailView.vendorCode, s);
if (vendorCodeCondition) validSearchConditions.push(vendorCodeCondition);
const vendorCountryCondition = ilike(initialRfqDetailView.vendorCountry, s);
if (vendorCountryCondition) validSearchConditions.push(vendorCountryCondition);
const incotermsDescriptionCondition = ilike(initialRfqDetailView.incotermsDescription, s);
if (incotermsDescriptionCondition) validSearchConditions.push(incotermsDescriptionCondition);
const classificationCondition = ilike(initialRfqDetailView.classification, s);
if (classificationCondition) validSearchConditions.push(classificationCondition);
const sparepartCondition = ilike(initialRfqDetailView.sparepart, s);
if (sparepartCondition) validSearchConditions.push(sparepartCondition);
if (validSearchConditions.length > 0) {
globalWhere = or(...validSearchConditions);
}
}
// 5) 최종 WHERE 조건 생성
const whereConditions: SQL<unknown>[] = [];
if (advancedWhere) whereConditions.push(advancedWhere);
if (basicWhere) whereConditions.push(basicWhere);
if (globalWhere) whereConditions.push(globalWhere);
if (rfqIdWhere) whereConditions.push(rfqIdWhere);
const finalWhere = whereConditions.length > 0 ? and(...whereConditions) : undefined;
// 6) 전체 데이터 수 조회
const totalResult = await db
.select({ count: count() })
.from(initialRfqDetailView)
.where(finalWhere);
const total = totalResult[0]?.count || 0;
if (total === 0) {
return { data: [], pageCount: 0, total: 0 };
}
console.log(totalResult);
console.log(total);
// 7) 정렬 및 페이징 처리된 데이터 조회
const orderByColumns = input.sort.map((sort) => {
const column = sort.id as keyof typeof initialRfqDetailView.$inferSelect;
return sort.desc ? desc(initialRfqDetailView[column]) : asc(initialRfqDetailView[column]);
});
if (orderByColumns.length === 0) {
orderByColumns.push(desc(initialRfqDetailView.createdAt));
}
const initialRfqData = await db
.select()
.from(initialRfqDetailView)
.where(finalWhere)
.orderBy(...orderByColumns)
.limit(input.perPage)
.offset(offset);
const pageCount = Math.ceil(total / input.perPage);
return { data: initialRfqData, pageCount, total };
} catch (err) {
console.error("Error in getInitialRfqDetail:", err);
return { data: [], pageCount: 0, total: 0 };
}
}
export async function getVendorsForSelection() {
try {
const vendorsData = await db
.select({
id: vendors.id,
vendorName: vendors.vendorName,
vendorCode: vendors.vendorCode,
taxId: vendors.taxId,
country: vendors.country,
status: vendors.status,
})
.from(vendors)
// .where(
// and(
// ne(vendors.status, "BLACKLISTED"),
// ne(vendors.status, "REJECTED")
// )
// )
.orderBy(vendors.vendorName)
return vendorsData.map(vendor => ({
id: vendor.id,
vendorName: vendor.vendorName || "",
vendorCode: vendor.vendorCode || "",
country: vendor.country || "",
status: vendor.status,
}))
} catch (error) {
console.error("Error fetching vendors:", error)
throw new Error("Failed to fetch vendors")
}
}
export async function addInitialRfqRecord(data: AddInitialRfqFormData & { rfqId: number }) {
try {
console.log('Incoming data:', data);
const [newRecord] = await db
.insert(initialRfq)
.values({
rfqId: data.rfqId,
vendorId: data.vendorId,
initialRfqStatus: data.initialRfqStatus,
dueDate: data.dueDate,
validDate: data.validDate,
incotermsCode: data.incotermsCode,
gtc: data.gtc,
gtcValidDate: data.gtcValidDate,
classification: data.classification,
sparepart: data.sparepart,
shortList: data.shortList,
returnYn: data.returnYn,
cpRequestYn: data.cpRequestYn,
prjectGtcYn: data.prjectGtcYn,
returnRevision: data.returnRevision,
})
.returning()
return {
success: true,
message: "초기 RFQ가 성공적으로 추가되었습니다.",
data: newRecord,
}
} catch (error) {
console.error("Error adding initial RFQ:", error)
return {
success: false,
message: "초기 RFQ 추가에 실패했습니다.",
error,
}
}
}
export async function getIncotermsForSelection() {
try {
const incotermData = await db
.select({
code: incoterms.code,
description: incoterms.description,
})
.from(incoterms)
.orderBy(incoterms.code)
return incotermData
} catch (error) {
console.error("Error fetching incoterms:", error)
throw new Error("Failed to fetch incoterms")
}
}
export async function removeInitialRfqs(input: RemoveInitialRfqsSchema) {
unstable_noStore ()
try {
const { ids } = removeInitialRfqsSchema.parse(input)
await db.transaction(async (tx) => {
await tx.delete(initialRfq).where(inArray(initialRfq.id, ids))
})
return {
data: null,
error: null,
}
} catch (err) {
return {
data: null,
error: getErrorMessage(err),
}
}
}
interface ModifyInitialRfqInput extends UpdateInitialRfqSchema {
id: number
}
export async function modifyInitialRfq(input: ModifyInitialRfqInput) {
unstable_noStore ()
try {
const { id, ...updateData } = input
// validation
updateInitialRfqSchema.parse(updateData)
await db.transaction(async (tx) => {
const existingRfq = await tx
.select()
.from(initialRfq)
.where(eq(initialRfq.id, id))
.limit(1)
if (existingRfq.length === 0) {
throw new Error("초기 RFQ를 찾을 수 없습니다.")
}
await tx
.update(initialRfq)
.set({
...updateData,
// Convert empty strings to null for optional fields
incotermsCode: updateData.incotermsCode || null,
gtc: updateData.gtc || null,
gtcValidDate: updateData.gtcValidDate || null,
classification: updateData.classification || null,
sparepart: updateData.sparepart || null,
validDate: updateData.validDate || null,
updatedAt: new Date(),
})
.where(eq(initialRfq.id, id))
})
return {
data: null,
error: null,
}
} catch (err) {
return {
data: null,
error: getErrorMessage(err),
}
}
}
// 이메일 발송용 데이터 타입
interface EmailData {
rfqCode: string
projectName: string
projectCompany: string
projectFlag: string
projectSite: string
classification: string
incotermsCode: string
incotermsDescription: string
dueDate: string
validDate: string
sparepart: string
vendorName: string
picName: string
picEmail: string
warrantyPeriod: string
packageName: string
rfqRevision: number
emailType: string
}
export async function sendBulkInitialRfqEmails(input: BulkEmailInput) {
unstable_noStore()
try {
const session = await getServerSession(authOptions)
if (!session?.user?.id) {
throw new Error("인증이 필요합니다.")
}
const { initialRfqIds, language } = bulkEmailSchema.parse(input)
// 1. 선택된 초기 RFQ들의 상세 정보 조회
const initialRfqDetails = await db
.select({
// initialRfqDetailView 필드들을 명시적으로 선택
rfqId: initialRfqDetailView.rfqId,
rfqCode: initialRfqDetailView.rfqCode,
rfqStatus: initialRfqDetailView.rfqStatus,
initialRfqId: initialRfqDetailView.initialRfqId,
initialRfqStatus: initialRfqDetailView.initialRfqStatus,
vendorId: initialRfqDetailView.vendorId,
vendorCode: initialRfqDetailView.vendorCode,
vendorName: initialRfqDetailView.vendorName,
vendorCategory: initialRfqDetailView.vendorCategory,
vendorCountry: initialRfqDetailView.vendorCountry,
vendorBusinessSize: initialRfqDetailView.vendorBusinessSize,
dueDate: initialRfqDetailView.dueDate,
validDate: initialRfqDetailView.validDate,
incotermsCode: initialRfqDetailView.incotermsCode,
incotermsDescription: initialRfqDetailView.incotermsDescription,
shortList: initialRfqDetailView.shortList,
returnYn: initialRfqDetailView.returnYn,
cpRequestYn: initialRfqDetailView.cpRequestYn,
prjectGtcYn: initialRfqDetailView.prjectGtcYn,
returnRevision: initialRfqDetailView.returnRevision,
rfqRevision: initialRfqDetailView.rfqRevision,
gtc: initialRfqDetailView.gtc,
gtcValidDate: initialRfqDetailView.gtcValidDate,
classification: initialRfqDetailView.classification,
sparepart: initialRfqDetailView.sparepart,
createdAt: initialRfqDetailView.createdAt,
updatedAt: initialRfqDetailView.updatedAt,
// bRfqs에서 추가로 필요한 필드들
picName: bRfqs.picName,
picCode: bRfqs.picCode,
packageName: bRfqs.packageName,
packageNo: bRfqs.packageNo,
projectCompany: bRfqs.projectCompany,
projectFlag: bRfqs.projectFlag,
projectSite: bRfqs.projectSite,
})
.from(initialRfqDetailView)
.leftJoin(bRfqs, eq(initialRfqDetailView.rfqId, bRfqs.id))
.where(inArray(initialRfqDetailView.initialRfqId, initialRfqIds))
if (initialRfqDetails.length === 0) {
return {
success: false,
message: "선택된 초기 RFQ를 찾을 수 없습니다.",
}
}
// 2. 각 RFQ에 대한 첨부파일 조회
const rfqIds = [...new Set(initialRfqDetails.map(rfq => rfq.rfqId))].filter((id): id is number => id !== null)
const attachments = await db
.select()
.from(bRfqsAttachments)
.where(inArray(bRfqsAttachments.rfqId, rfqIds))
// 3. 벤더 이메일 정보 조회 (모든 이메일 주소 포함)
const vendorIds = [...new Set(initialRfqDetails.map(rfq => rfq.vendorId))].filter((id): id is number => id !== null)
const vendorsWithAllEmails = await db
.select({
id: vendors.id,
vendorName: vendors.vendorName,
email: vendors.email,
representativeEmail: vendors.representativeEmail,
// 연락처 이메일들을 JSON 배열로 집계
contactEmails: sql<string[]>`
COALESCE(
(SELECT json_agg(contact_email)
FROM vendor_contacts
WHERE vendor_id = ${vendors.id}
AND contact_email IS NOT NULL
AND contact_email != ''
),
'[]'::json
)
`.as("contact_emails")
})
.from(vendors)
.where(inArray(vendors.id, vendorIds))
// 각 벤더의 모든 유효한 이메일 주소를 정리하는 함수
function getAllVendorEmails(vendor: typeof vendorsWithAllEmails[0]): string[] {
const emails: string[] = []
// 벤더 기본 이메일
if (vendor.email) {
emails.push(vendor.email)
}
// 대표자 이메일
if (vendor.representativeEmail && vendor.representativeEmail !== vendor.email) {
emails.push(vendor.representativeEmail)
}
// 연락처 이메일들
if (vendor.contactEmails && Array.isArray(vendor.contactEmails)) {
vendor.contactEmails.forEach(contactEmail => {
if (contactEmail && !emails.includes(contactEmail)) {
emails.push(contactEmail)
}
})
}
return emails.filter(email => email && email.trim() !== '')
}
const results = []
const errors = []
// 4. 각 초기 RFQ에 대해 처리
for (const rfqDetail of initialRfqDetails) {
try {
// vendorId null 체크
if (!rfqDetail.vendorId) {
errors.push(`벤더 ID가 없습니다: RFQ ID ${rfqDetail.initialRfqId}`)
continue
}
// 해당 RFQ의 첨부파일들
const rfqAttachments = attachments.filter(att => att.rfqId === rfqDetail.rfqId)
// 벤더 정보
const vendor = vendorsWithAllEmails.find(v => v.id === rfqDetail.vendorId)
if (!vendor) {
errors.push(`벤더 정보를 찾을 수 없습니다: RFQ ID ${rfqDetail.initialRfqId}`)
continue
}
// 해당 벤더의 모든 이메일 주소 수집
const vendorEmails = getAllVendorEmails(vendor)
if (vendorEmails.length === 0) {
errors.push(`벤더 이메일 주소가 없습니다: ${vendor.vendorName}`)
continue
}
// 5. 기존 vendorAttachmentResponses 조회하여 리비전 상태 확인
const currentRfqRevision = rfqDetail.rfqRevision || 0
let emailType: "NEW" | "RESEND" | "REVISION" = "NEW"
let revisionToUse = currentRfqRevision
// 첫 번째 첨부파일을 기준으로 기존 응답 조회 (리비전 상태 확인용)
if (rfqAttachments.length > 0 && rfqDetail.initialRfqId) {
const existingResponses = await db
.select()
.from(vendorAttachmentResponses)
.where(
and(
eq(vendorAttachmentResponses.vendorId, rfqDetail.vendorId),
eq(vendorAttachmentResponses.rfqType, "INITIAL"),
eq(vendorAttachmentResponses.rfqRecordId, rfqDetail.initialRfqId)
)
)
if (existingResponses.length > 0) {
// 기존 응답이 있음
const existingRevision = parseInt(existingResponses[0].currentRevision?.replace("Rev.", "") || "0")
if (currentRfqRevision > existingRevision) {
// RFQ 리비전이 올라감 → 리비전 업데이트
emailType = "REVISION"
revisionToUse = currentRfqRevision
} else {
// 동일하거나 낮음 → 재전송
emailType = "RESEND"
revisionToUse = existingRevision
}
} else {
// 기존 응답이 없음 → 신규 전송
emailType = "NEW"
revisionToUse = currentRfqRevision
}
}
// 6. vendorAttachmentResponses 레코드 생성/업데이트
for (const attachment of rfqAttachments) {
const existingResponse = await db
.select()
.from(vendorAttachmentResponses)
.where(
and(
eq(vendorAttachmentResponses.attachmentId, attachment.id),
eq(vendorAttachmentResponses.vendorId, rfqDetail.vendorId),
eq(vendorAttachmentResponses.rfqType, "INITIAL")
)
)
.limit(1)
if (existingResponse.length === 0) {
// 새 응답 레코드 생성
await db.insert(vendorAttachmentResponses).values({
attachmentId: attachment.id,
vendorId: rfqDetail.vendorId,
rfqType: "INITIAL",
rfqRecordId: rfqDetail.initialRfqId,
responseStatus: "NOT_RESPONDED",
currentRevision: `Rev.${revisionToUse}`,
requestedAt: new Date(),
})
} else {
// 기존 레코드 업데이트
await db
.update(vendorAttachmentResponses)
.set({
currentRevision: `Rev.${revisionToUse}`,
requestedAt: new Date(),
// 리비전 업데이트인 경우 응답 상태 초기화
responseStatus: emailType === "REVISION" ? "NOT_RESPONDED" : existingResponse[0].responseStatus,
})
.where(eq(vendorAttachmentResponses.id, existingResponse[0].id))
}
}
const formatDateSafely = (date: Date | string | null | undefined): string => {
if (!date) return ""
try {
// Date 객체로 변환하고 포맷팅
const dateObj = new Date(date)
// 유효한 날짜인지 확인
if (isNaN(dateObj.getTime())) return ""
return dateObj.toLocaleDateString('en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit'
})
} catch (error) {
console.error("Date formatting error:", error)
return ""
}
}
// 7. 이메일 발송
const emailData: EmailData = {
name:vendor.vendorName,
rfqCode: rfqDetail.rfqCode || "",
projectName: rfqDetail.rfqCode || "", // 실제 프로젝트명이 있다면 사용
projectCompany: rfqDetail.projectCompany || "",
projectFlag: rfqDetail.projectFlag || "",
projectSite: rfqDetail.projectSite || "",
classification: rfqDetail.classification || "ABS",
incotermsCode: rfqDetail.incotermsCode || "FOB",
incotermsDescription: rfqDetail.incotermsDescription || "FOB Finland Port",
dueDate: rfqDetail.dueDate ? formatDateSafely(rfqDetail.dueDate) : "",
validDate: rfqDetail.validDate ?formatDateSafely(rfqDetail.validDate) : "",
sparepart: rfqDetail.sparepart || "One(1) year operational spare parts",
vendorName: vendor.vendorName,
picName: session.user.name || rfqDetail.picName || "Procurement Manager",
picEmail: session.user.email || "procurement@samsung.com",
warrantyPeriod: "Refer to commercial package attached",
packageName: rfqDetail.packageName || "",
rfqRevision: revisionToUse, // 리비전 정보 추가
emailType: emailType, // 이메일 타입 추가
}
// 이메일 제목 생성 (타입에 따라 다르게)
let emailSubject = ""
const revisionText = revisionToUse > 0 ? ` Rev.${revisionToUse}` : ""
switch (emailType) {
case "NEW":
emailSubject = `[SHI RFQ] ${rfqDetail.rfqCode}${revisionText} Invitation to Bidder for ${emailData.packageName} * ${vendor.vendorName} * RFQ No. ${rfqDetail.rfqCode}`
break
case "RESEND":
emailSubject = `[SHI RFQ - RESEND] ${rfqDetail.rfqCode}${revisionText} Invitation to Bidder for ${emailData.packageName} * ${vendor.vendorName} * RFQ No. ${rfqDetail.rfqCode}`
break
case "REVISION":
emailSubject = `[SHI RFQ - REVISED] ${rfqDetail.rfqCode}${revisionText} Invitation to Bidder for ${emailData.packageName} * ${vendor.vendorName} * RFQ No. ${rfqDetail.rfqCode}`
break
}
// nodemailer로 모든 이메일 주소에 한번에 발송
await sendEmail({
// from: session.user.email || undefined,
to: vendorEmails.join(", "), // 콤마+공백으로 구분
subject: emailSubject,
template: "initial-rfq-invitation", // hbs 템플릿 파일명
context: {
...emailData,
language,
}
})
// 8. 초기 RFQ 상태 업데이트 (리비전은 변경하지 않음 - 이미 DB에 저장된 값 사용)
if(rfqDetail.initialRfqId && rfqDetail.rfqId){
// Promise.all로 두 테이블 동시 업데이트
await Promise.all([
// initialRfq 테이블 업데이트
db
.update(initialRfq)
.set({
initialRfqStatus: "Init. RFQ Sent",
updatedAt: new Date(),
})
.where(eq(initialRfq.id, rfqDetail.initialRfqId)),
// bRfqs 테이블 status도 함께 업데이트
db
.update(bRfqs)
.set({
status: "Init. RFQ Sent",
// updatedBy: session.user.id,
updatedAt: new Date(),
})
.where(eq(bRfqs.id, rfqDetail.rfqId))
]);
}
results.push({
initialRfqId: rfqDetail.initialRfqId,
vendorName: vendor.vendorName,
vendorEmails: vendorEmails, // 발송된 모든 이메일 주소 기록
emailCount: vendorEmails.length,
emailType: emailType,
rfqRevision: revisionToUse,
success: true,
})
} catch (error) {
console.error(`Error processing RFQ ${rfqDetail.initialRfqId}:`, error)
errors.push(`RFQ ${rfqDetail.initialRfqId} 처리 중 오류: ${getErrorMessage(error)}`)
}
}
return {
success: true,
message: `${results.length}개의 RFQ 이메일이 발송되었습니다.`,
results,
errors: errors.length > 0 ? errors : undefined,
}
} catch (err) {
console.error("Bulk email error:", err)
return {
success: false,
message: getErrorMessage(err),
}
}
}
// 개별 RFQ 이메일 재발송
export async function resendInitialRfqEmail(initialRfqId: number) {
unstable_noStore()
try {
const result = await sendBulkInitialRfqEmails({
initialRfqIds: [initialRfqId],
language: "en",
})
return result
} catch (err) {
return {
success: false,
message: getErrorMessage(err),
}
}
}
export type VendorResponseDetail = VendorAttachmentResponse & {
attachment: {
id: number;
attachmentType: string;
serialNo: string;
description: string | null;
currentRevision: string;
};
vendor: {
id: number;
vendorCode: string;
vendorName: string;
country: string | null;
businessSize: string | null;
};
rfq: {
id: number;
rfqCode: string | null;
description: string | null;
status: string;
dueDate: Date;
};
};
export async function getVendorRfqResponses(input: GetVendorResponsesSchema, vendorId?: string, rfqId?: string) {
try {
// 페이지네이션 설정
const page = input.page || 1;
const perPage = input.perPage || 10;
const offset = (page - 1) * perPage;
// 기본 조건
let whereConditions = [];
// 벤더 ID 조건
if (vendorId) {
whereConditions.push(eq(vendorAttachmentResponses.vendorId, Number(vendorId)));
}
// RFQ 타입 조건
// if (input.rfqType !== "ALL") {
// whereConditions.push(eq(vendorAttachmentResponses.rfqType, input.rfqType as RfqType));
// }
// 날짜 범위 조건
if (input.from && input.to) {
whereConditions.push(
and(
gte(vendorAttachmentResponses.requestedAt, new Date(input.from)),
lte(vendorAttachmentResponses.requestedAt, new Date(input.to))
)
);
}
const baseWhere = whereConditions.length > 0 ? and(...whereConditions) : undefined;
// 그룹핑된 응답 요약 데이터 조회
const groupedResponses = await db
.select({
vendorId: vendorAttachmentResponses.vendorId,
rfqRecordId: vendorAttachmentResponses.rfqRecordId,
rfqType: vendorAttachmentResponses.rfqType,
// 통계 계산 (조건부 COUNT 수정)
totalAttachments: count(),
respondedCount: sql<number>`SUM(CASE WHEN ${vendorAttachmentResponses.responseStatus} = 'RESPONDED' THEN 1 ELSE 0 END)`,
pendingCount: sql<number>`SUM(CASE WHEN ${vendorAttachmentResponses.responseStatus} = 'NOT_RESPONDED' THEN 1 ELSE 0 END)`,
revisionRequestedCount: sql<number>`SUM(CASE WHEN ${vendorAttachmentResponses.responseStatus} = 'REVISION_REQUESTED' THEN 1 ELSE 0 END)`,
waivedCount: sql<number>`SUM(CASE WHEN ${vendorAttachmentResponses.responseStatus} = 'WAIVED' THEN 1 ELSE 0 END)`,
// 날짜 정보
requestedAt: sql<Date>`MIN(${vendorAttachmentResponses.requestedAt})`,
lastRespondedAt: sql<Date | null>`MAX(${vendorAttachmentResponses.respondedAt})`,
// 코멘트 여부
hasComments: sql<boolean>`BOOL_OR(${vendorAttachmentResponses.responseComment} IS NOT NULL OR ${vendorAttachmentResponses.vendorComment} IS NOT NULL)`,
})
.from(vendorAttachmentResponses)
.where(baseWhere)
.groupBy(
vendorAttachmentResponses.vendorId,
vendorAttachmentResponses.rfqRecordId,
vendorAttachmentResponses.rfqType
)
.orderBy(desc(sql`MIN(${vendorAttachmentResponses.requestedAt})`))
.offset(offset)
.limit(perPage);
// 벤더 정보와 RFQ 정보를 별도로 조회
const vendorIds = [...new Set(groupedResponses.map(r => r.vendorId))];
const rfqRecordIds = [...new Set(groupedResponses.map(r => r.rfqRecordId))];
// 벤더 정보 조회
const vendorsData = await db.query.vendors.findMany({
where: or(...vendorIds.map(id => eq(vendors.id, id))),
columns: {
id: true,
vendorCode: true,
vendorName: true,
country: true,
businessSize: true,
}
});
// RFQ 정보 조회 (초기 RFQ와 최종 RFQ 모두)
const [initialRfqs] = await Promise.all([
db.query.initialRfq.findMany({
where: or(...rfqRecordIds.map(id => eq(initialRfq.id, id))),
with: {
rfq: {
columns: {
id: true,
rfqCode: true,
description: true,
status: true,
dueDate: true,
}
}
}
})
]);
// 데이터 조합 및 변환
const transformedResponses: VendorRfqResponseSummary[] = groupedResponses.map(response => {
const vendor = vendorsData.find(v => v.id === response.vendorId);
let rfqInfo = null;
if (response.rfqType === "INITIAL") {
const initialRfq = initialRfqs.find(r => r.id === response.rfqRecordId);
rfqInfo = initialRfq?.rfq || null;
}
// 응답률 계산
const responseRate = Number(response.totalAttachments) > 0
? Math.round((Number(response.respondedCount) / Number(response.totalAttachments)) * 100)
: 0;
// 완료율 계산 (응답완료 + 포기)
const completionRate = Number(response.totalAttachments) > 0
? Math.round(((Number(response.respondedCount) + Number(response.waivedCount)) / Number(response.totalAttachments)) * 100)
: 0;
// 전체 상태 결정
let overallStatus: ResponseStatus = "NOT_RESPONDED";
if (Number(response.revisionRequestedCount) > 0) {
overallStatus = "REVISION_REQUESTED";
} else if (completionRate === 100) {
overallStatus = Number(response.waivedCount) === Number(response.totalAttachments) ? "WAIVED" : "RESPONDED";
} else if (Number(response.respondedCount) > 0) {
overallStatus = "RESPONDED"; // 부분 응답
}
return {
id: `${response.vendorId}-${response.rfqRecordId}-${response.rfqType}`,
vendorId: response.vendorId,
rfqRecordId: response.rfqRecordId,
rfqType: response.rfqType,
rfq: rfqInfo,
vendor: vendor || null,
totalAttachments: Number(response.totalAttachments),
respondedCount: Number(response.respondedCount),
pendingCount: Number(response.pendingCount),
revisionRequestedCount: Number(response.revisionRequestedCount),
waivedCount: Number(response.waivedCount),
responseRate,
completionRate,
overallStatus,
requestedAt: response.requestedAt,
lastRespondedAt: response.lastRespondedAt,
hasComments: response.hasComments,
};
});
// 전체 개수 조회 (그룹핑 기준) - PostgreSQL 호환 방식
const totalCountResult = await db
.select({
totalCount: sql<number>`COUNT(DISTINCT (${vendorAttachmentResponses.vendorId}, ${vendorAttachmentResponses.rfqRecordId}, ${vendorAttachmentResponses.rfqType}))`
})
.from(vendorAttachmentResponses)
.where(baseWhere);
const totalCount = Number(totalCountResult[0].totalCount);
const pageCount = Math.ceil(totalCount / perPage);
return {
data: transformedResponses,
pageCount,
totalCount
};
} catch (err) {
console.error("getVendorRfqResponses 에러:", err);
return { data: [], pageCount: 0, totalCount: 0 };
}
}
/**
* 특정 RFQ의 첨부파일별 응답 상세 조회 (상세 페이지용)
*/
export async function getRfqAttachmentResponses(vendorId: string, rfqRecordId: string) {
try {
// 해당 RFQ의 모든 첨부파일 응답 조회
const responses = await db.query.vendorAttachmentResponses.findMany({
where: and(
eq(vendorAttachmentResponses.vendorId, Number(vendorId)),
eq(vendorAttachmentResponses.rfqRecordId, Number(rfqRecordId)),
),
with: {
attachment: {
with: {
rfq: {
columns: {
id: true,
rfqCode: true,
description: true,
status: true,
dueDate: true,
// 추가 정보
picCode: true,
picName: true,
EngPicName: true,
packageNo: true,
packageName: true,
projectId: true,
projectCompany: true,
projectFlag: true,
projectSite: true,
remark: true,
},
with: {
project: {
columns: {
id: true,
code: true,
name: true,
type: true,
}
}
}
}
}
},
vendor: {
columns: {
id: true,
vendorCode: true,
vendorName: true,
country: true,
businessSize: true,
}
},
responseAttachments: true,
},
orderBy: [asc(vendorAttachmentResponses.attachmentId)]
});
return {
data: responses,
rfqInfo: responses[0]?.attachment?.rfq || null,
vendorInfo: responses[0]?.vendor || null,
};
} catch (err) {
console.error("getRfqAttachmentResponses 에러:", err);
return { data: [], rfqInfo: null, vendorInfo: null };
}
}
export async function getVendorResponseStatusCounts(vendorId?: string, rfqId?: string, rfqType?: RfqType) {
try {
const initial: Record<ResponseStatus, number> = {
NOT_RESPONDED: 0,
RESPONDED: 0,
REVISION_REQUESTED: 0,
WAIVED: 0,
};
// 조건 설정
let whereConditions = [];
// 벤더 ID 조건
if (vendorId) {
whereConditions.push(eq(vendorAttachmentResponses.vendorId, Number(vendorId)));
}
// RFQ ID 조건
if (rfqId) {
const attachmentIds = await db
.select({ id: bRfqsAttachments.id })
.from(bRfqsAttachments)
.where(eq(bRfqsAttachments.rfqId, Number(rfqId)));
if (attachmentIds.length > 0) {
whereConditions.push(
or(...attachmentIds.map(att => eq(vendorAttachmentResponses.attachmentId, att.id)))
);
}
}
// RFQ 타입 조건
if (rfqType) {
whereConditions.push(eq(vendorAttachmentResponses.rfqType, rfqType));
}
const whereCondition = whereConditions.length > 0 ? and(...whereConditions) : undefined;
// 상태별 그룹핑 쿼리
const rows = await db
.select({
status: vendorAttachmentResponses.responseStatus,
count: count(),
})
.from(vendorAttachmentResponses)
.where(whereCondition)
.groupBy(vendorAttachmentResponses.responseStatus);
// 결과 처리
const result = rows.reduce<Record<ResponseStatus, number>>((acc, { status, count }) => {
if (status) {
acc[status as ResponseStatus] = Number(count);
}
return acc;
}, initial);
return result;
} catch (err) {
console.error("getVendorResponseStatusCounts 에러:", err);
return {} as Record<ResponseStatus, number>;
}
}
/**
* RFQ별 벤더 응답 요약 조회
*/
export async function getRfqResponseSummary(rfqId: string, rfqType?: RfqType) {
try {
// RFQ의 첨부파일 목록 조회 (relations 사용)
const attachments = await db.query.bRfqsAttachments.findMany({
where: eq(bRfqsAttachments.rfqId, Number(rfqId)),
columns: {
id: true,
attachmentType: true,
serialNo: true,
description: true,
}
});
if (attachments.length === 0) {
return {
totalAttachments: 0,
totalVendors: 0,
responseRate: 0,
completionRate: 0,
statusCounts: {} as Record<ResponseStatus, number>
};
}
// 조건 설정
let whereConditions = [
or(...attachments.map(att => eq(vendorAttachmentResponses.attachmentId, att.id)))
];
if (rfqType) {
whereConditions.push(eq(vendorAttachmentResponses.rfqType, rfqType));
}
const whereCondition = and(...whereConditions);
// 벤더 수 및 응답 통계 조회
const [vendorStats, statusCounts] = await Promise.all([
// 전체 벤더 수 및 응답 벤더 수 (조건부 COUNT 수정)
db
.select({
totalVendors: count(),
respondedVendors: sql<number>`SUM(CASE WHEN ${vendorAttachmentResponses.responseStatus} = 'RESPONDED' THEN 1 ELSE 0 END)`,
completedVendors: sql<number>`SUM(CASE WHEN ${vendorAttachmentResponses.responseStatus} = 'RESPONDED' OR ${vendorAttachmentResponses.responseStatus} = 'WAIVED' THEN 1 ELSE 0 END)`,
})
.from(vendorAttachmentResponses)
.where(whereCondition),
// 상태별 개수
db
.select({
status: vendorAttachmentResponses.responseStatus,
count: count(),
})
.from(vendorAttachmentResponses)
.where(whereCondition)
.groupBy(vendorAttachmentResponses.responseStatus)
]);
const stats = vendorStats[0];
const statusCountsMap = statusCounts.reduce<Record<ResponseStatus, number>>((acc, { status, count }) => {
if (status) {
acc[status as ResponseStatus] = Number(count);
}
return acc;
}, {
NOT_RESPONDED: 0,
RESPONDED: 0,
REVISION_REQUESTED: 0,
WAIVED: 0,
});
const responseRate = stats.totalVendors > 0
? Math.round((Number(stats.respondedVendors) / Number(stats.totalVendors)) * 100)
: 0;
const completionRate = stats.totalVendors > 0
? Math.round((Number(stats.completedVendors) / Number(stats.totalVendors)) * 100)
: 0;
return {
totalAttachments: attachments.length,
totalVendors: Number(stats.totalVendors),
responseRate,
completionRate,
statusCounts: statusCountsMap
};
} catch (err) {
console.error("getRfqResponseSummary 에러:", err);
return {
totalAttachments: 0,
totalVendors: 0,
responseRate: 0,
completionRate: 0,
statusCounts: {} as Record<ResponseStatus, number>
};
}
}
/**
* 벤더별 응답 진행률 조회
*/
export async function getVendorResponseProgress(vendorId: string) {
try {
let whereConditions = [eq(vendorAttachmentResponses.vendorId, Number(vendorId))];
const whereCondition = and(...whereConditions);
const progress = await db
.select({
totalRequests: count(),
responded: sql<number>`SUM(CASE WHEN ${vendorAttachmentResponses.responseStatus} = 'RESPONDED' THEN 1 ELSE 0 END)`,
pending: sql<number>`SUM(CASE WHEN ${vendorAttachmentResponses.responseStatus} = 'NOT_RESPONDED' THEN 1 ELSE 0 END)`,
revisionRequested: sql<number>`SUM(CASE WHEN ${vendorAttachmentResponses.responseStatus} = 'REVISION_REQUESTED' THEN 1 ELSE 0 END)`,
waived: sql<number>`SUM(CASE WHEN ${vendorAttachmentResponses.responseStatus} = 'WAIVED' THEN 1 ELSE 0 END)`,
})
.from(vendorAttachmentResponses)
.where(whereCondition);
console.log(progress,"progress")
const stats = progress[0];
const responseRate = Number(stats.totalRequests) > 0
? Math.round((Number(stats.responded) / Number(stats.totalRequests)) * 100)
: 0;
const completionRate = Number(stats.totalRequests) > 0
? Math.round(((Number(stats.responded) + Number(stats.waived)) / Number(stats.totalRequests)) * 100)
: 0;
return {
totalRequests: Number(stats.totalRequests),
responded: Number(stats.responded),
pending: Number(stats.pending),
revisionRequested: Number(stats.revisionRequested),
waived: Number(stats.waived),
responseRate,
completionRate,
};
} catch (err) {
console.error("getVendorResponseProgress 에러:", err);
return {
totalRequests: 0,
responded: 0,
pending: 0,
revisionRequested: 0,
waived: 0,
responseRate: 0,
completionRate: 0,
};
}
}
export async function getRfqAttachmentResponsesWithRevisions(vendorId: string, rfqRecordId: string) {
try {
// 1. 벤더 응답 상세 정보 조회 (뷰 사용)
const responses = await db
.select()
.from(vendorResponseDetailView)
.where(
and(
eq(vendorResponseDetailView.vendorId, Number(vendorId)),
eq(vendorResponseDetailView.rfqRecordId, Number(rfqRecordId))
)
)
.orderBy(asc(vendorResponseDetailView.attachmentId));
// 2. RFQ 진행 현황 요약 조회
const progressSummaryResult = await db
.select()
.from(rfqProgressSummaryView)
.where(eq(rfqProgressSummaryView.rfqId, responses[0]?.rfqId || 0))
.limit(1);
const progressSummary = progressSummaryResult[0] || null;
// 3. 각 응답의 첨부파일 리비전 히스토리 조회
const attachmentHistories = await Promise.all(
responses.map(async (response) => {
const history = await db
.select()
.from(attachmentRevisionHistoryView)
.where(eq(attachmentRevisionHistoryView.attachmentId, response.attachmentId))
.orderBy(desc(attachmentRevisionHistoryView.clientRevisionCreatedAt));
return {
attachmentId: response.attachmentId,
revisions: history
};
})
);
// 4. 벤더 응답 파일들 조회 (향상된 정보 포함)
const responseFiles = await Promise.all(
responses.map(async (response) => {
const files = await db
.select()
.from(vendorResponseAttachmentsEnhanced)
.where(eq(vendorResponseAttachmentsEnhanced.vendorResponseId, response.responseId))
.orderBy(desc(vendorResponseAttachmentsEnhanced.uploadedAt));
return {
responseId: response.responseId,
files: files
};
})
);
// 5. 데이터 변환 및 통합
const enhancedResponses = responses.map(response => {
const attachmentHistory = attachmentHistories.find(h => h.attachmentId === response.attachmentId);
const responseFileData = responseFiles.find(f => f.responseId === response.responseId);
return {
...response,
// 첨부파일 정보에 리비전 히스토리 추가
attachment: {
id: response.attachmentId,
attachmentType: response.attachmentType,
serialNo: response.serialNo,
description: response.attachmentDescription,
currentRevision: response.currentRevision,
// 모든 리비전 정보
revisions: attachmentHistory?.revisions?.map(rev => ({
id: rev.clientRevisionId,
revisionNo: rev.clientRevisionNo,
fileName: rev.clientFileName,
originalFileName: rev.clientFileName,
filePath: rev.clientFilePath, // 파일 경로 추가
fileSize: rev.clientFileSize,
revisionComment: rev.clientRevisionComment,
createdAt: rev.clientRevisionCreatedAt?.toISOString() || new Date().toISOString(),
isLatest: rev.isLatestClientRevision
})) || []
},
// 벤더 응답 파일들
responseAttachments: responseFileData?.files?.map(file => ({
id: file.responseAttachmentId,
fileName: file.fileName,
originalFileName: file.originalFileName,
filePath: file.filePath,
fileSize: file.fileSize,
description: file.description,
uploadedAt: file.uploadedAt?.toISOString() || new Date().toISOString(),
isLatestResponseFile: file.isLatestResponseFile,
fileSequence: file.fileSequence
})) || [],
// 리비전 분석 정보
isVersionMatched: response.isVersionMatched,
versionLag: response.versionLag,
needsUpdate: response.needsUpdate,
hasMultipleRevisions: response.hasMultipleRevisions,
// 새로 추가된 필드들
revisionRequestComment: response.revisionRequestComment,
revisionRequestedAt: response.revisionRequestedAt?.toISOString() || null,
};
});
// RFQ 기본 정보 (첫 번째 응답에서 추출)
const rfqInfo = responses[0] ? {
id: responses[0].rfqId,
rfqCode: responses[0].rfqCode,
// 추가 정보는 기존 방식대로 별도 조회 필요
description: "",
dueDate: progressSummary?.dueDate || new Date(),
status: progressSummary?.rfqStatus || "DRAFT",
// ... 기타 필요한 정보들
} : null;
// 벤더 정보
const vendorInfo = responses[0] ? {
id: responses[0].vendorId,
vendorCode: responses[0].vendorCode,
vendorName: responses[0].vendorName,
country: responses[0].vendorCountry,
} : null;
// 통계 정보 계산
const calculateStats = (responses: typeof enhancedResponses) => {
const total = responses.length;
const responded = responses.filter(r => r.responseStatus === "RESPONDED").length;
const pending = responses.filter(r => r.responseStatus === "NOT_RESPONDED").length;
const revisionRequested = responses.filter(r => r.responseStatus === "REVISION_REQUESTED").length;
const waived = responses.filter(r => r.responseStatus === "WAIVED").length;
const versionMismatch = responses.filter(r => r.effectiveStatus === "VERSION_MISMATCH").length;
const upToDate = responses.filter(r => r.effectiveStatus === "UP_TO_DATE").length;
return {
total,
responded,
pending,
revisionRequested,
waived,
versionMismatch,
upToDate,
responseRate: total > 0 ? Math.round((responded / total) * 100) : 0,
completionRate: total > 0 ? Math.round(((responded + waived) / total) * 100) : 0,
versionMatchRate: responded > 0 ? Math.round((upToDate / responded) * 100) : 100
};
};
const statistics = calculateStats(enhancedResponses);
return {
data: enhancedResponses,
rfqInfo,
vendorInfo,
statistics,
progressSummary: progressSummary ? {
totalAttachments: progressSummary.totalAttachments,
attachmentsWithMultipleRevisions: progressSummary.attachmentsWithMultipleRevisions,
totalClientRevisions: progressSummary.totalClientRevisions,
totalResponseFiles: progressSummary.totalResponseFiles,
daysToDeadline: progressSummary.daysToDeadline
} : null
};
} catch (err) {
console.error("getRfqAttachmentResponsesWithRevisions 에러:", err);
return {
data: [],
rfqInfo: null,
vendorInfo: null,
statistics: {
total: 0,
responded: 0,
pending: 0,
revisionRequested: 0,
waived: 0,
versionMismatch: 0,
upToDate: 0,
responseRate: 0,
completionRate: 0,
versionMatchRate: 100
},
progressSummary: null
};
}
}
// 첨부파일 리비전 히스토리 조회
export async function getAttachmentRevisionHistory(attachmentId: number) {
try {
const history = await db
.select()
.from(attachmentRevisionHistoryView)
.where(eq(attachmentRevisionHistoryView.attachmentId, attachmentId))
.orderBy(desc(attachmentRevisionHistoryView.clientRevisionCreatedAt));
return history;
} catch (err) {
console.error("getAttachmentRevisionHistory 에러:", err);
return [];
}
}
// RFQ 전체 진행 현황 조회
export async function getRfqProgressSummary(rfqId: number) {
try {
const summaryResult = await db
.select()
.from(rfqProgressSummaryView)
.where(eq(rfqProgressSummaryView.rfqId, rfqId))
.limit(1);
return summaryResult[0] || null;
} catch (err) {
console.error("getRfqProgressSummary 에러:", err);
return null;
}
}
// 벤더 응답 파일 상세 조회 (향상된 정보 포함)
export async function getVendorResponseFiles(vendorResponseId: number) {
try {
const files = await db
.select()
.from(vendorResponseAttachmentsEnhanced)
.where(eq(vendorResponseAttachmentsEnhanced.vendorResponseId, vendorResponseId))
.orderBy(desc(vendorResponseAttachmentsEnhanced.uploadedAt));
return files;
} catch (err) {
console.error("getVendorResponseFiles 에러:", err);
return [];
}
}
// 타입 정의 확장
export type EnhancedVendorResponse = {
// 기본 응답 정보
responseId: number;
rfqId: number;
rfqCode: string;
rfqType: "INITIAL" | "FINAL";
rfqRecordId: number;
// 첨부파일 정보
attachmentId: number;
attachmentType: string;
serialNo: string;
attachmentDescription?: string;
// 벤더 정보
vendorId: number;
vendorCode: string;
vendorName: string;
vendorCountry: string;
// 응답 상태
responseStatus: "NOT_RESPONDED" | "RESPONDED" | "REVISION_REQUESTED" | "WAIVED";
currentRevision: string;
respondedRevision?: string;
effectiveStatus: string;
// 코멘트 관련 필드들 (새로 추가된 필드 포함)
responseComment?: string; // 벤더가 응답할 때 작성하는 코멘트
vendorComment?: string; // 벤더 내부 메모
revisionRequestComment?: string; // 발주처가 수정 요청할 때 작성하는 사유 (새로 추가)
// 날짜 관련 필드들 (새로 추가된 필드 포함)
requestedAt: string;
respondedAt?: string;
revisionRequestedAt?: string; // 수정 요청 날짜 (새로 추가)
// 발주처 최신 리비전 정보
latestClientRevisionNo?: string;
latestClientFileName?: string;
latestClientFileSize?: number;
latestClientRevisionComment?: string;
// 리비전 분석
isVersionMatched: boolean;
versionLag?: number;
needsUpdate: boolean;
hasMultipleRevisions: boolean;
// 응답 파일 통계
totalResponseFiles: number;
latestResponseFileName?: string;
latestResponseFileSize?: number;
latestResponseUploadedAt?: string;
// 첨부파일 정보 (리비전 히스토리 포함)
attachment: {
id: number;
attachmentType: string;
serialNo: string;
description?: string;
currentRevision: string;
revisions: Array<{
id: number;
revisionNo: string;
fileName: string;
originalFileName: string;
filePath?: string;
fileSize?: number;
revisionComment?: string;
createdAt: string;
isLatest: boolean;
}>;
};
// 벤더 응답 파일들
responseAttachments: Array<{
id: number;
fileName: string;
originalFileName: string;
filePath: string;
fileSize?: number;
description?: string;
uploadedAt: string;
isLatestResponseFile: boolean;
fileSequence: number;
}>;
};
export async function requestRevision(
responseId: number,
revisionReason: string
): Promise<RequestRevisionResult> {
try {
// 입력값 검증
const validatedData = requestRevisionSchema.parse({
responseId,
revisionReason,
});
// 현재 응답 정보 조회
const existingResponse = await db
.select()
.from(vendorAttachmentResponses)
.where(eq(vendorAttachmentResponses.id, validatedData.responseId))
.limit(1);
if (existingResponse.length === 0) {
return {
success: false,
message: "해당 응답을 찾을 수 없습니다",
error: "NOT_FOUND",
};
}
const response = existingResponse[0];
// 응답 상태 확인 (이미 응답되었거나 포기된 상태에서만 수정 요청 가능)
if (response.responseStatus !== "RESPONDED") {
return {
success: false,
message: "응답된 상태의 항목에서만 수정을 요청할 수 있습니다",
error: "INVALID_STATUS",
};
}
// 응답 상태를 REVISION_REQUESTED로 업데이트
const updateResult = await db
.update(vendorAttachmentResponses)
.set({
responseStatus: "REVISION_REQUESTED",
revisionRequestComment: validatedData.revisionReason, // 새로운 필드에 저장
revisionRequestedAt: new Date(), // 수정 요청 시간 저장
updatedAt: new Date(),
})
.where(eq(vendorAttachmentResponses.id, validatedData.responseId))
.returning();
if (updateResult.length === 0) {
return {
success: false,
message: "수정 요청 업데이트에 실패했습니다",
error: "UPDATE_FAILED",
};
}
return {
success: true,
message: "수정 요청이 성공적으로 전송되었습니다",
};
} catch (error) {
console.error("Request revision server action error:", error);
return {
success: false,
message: "내부 서버 오류가 발생했습니다",
error: "INTERNAL_ERROR",
};
}
}
|