Skip to content

Commit 4b30e27

Browse files
authored
Merge branch 'master' into tastekim/sequelize
2 parents c579c5a + 9a40c66 commit 4b30e27

File tree

4 files changed

+205
-52
lines changed

4 files changed

+205
-52
lines changed
Lines changed: 60 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,68 @@
11
import { createConnection } from 'mysql2/promise';
22
import { utils, writeFile } from 'xlsx';
33
import config from '../../config/dbconfig.js';
4+
import { utmFilters } from './utmFilter.js';
45

56
async function exportDataToExcel(req, res) {
6-
try {
7-
// db 연결용 config
8-
const DB_CONFIG = config.test_db_config;
9-
10-
// 앞으로 많이 수정하게 될 쿼리문
11-
const SQL_QUERY = 'SELECT * FROM member';
12-
13-
const today = new Date();
14-
const year = today.getFullYear(); // 년도
15-
const month = today.getMonth() + 1; // 월
16-
const date = today.getDate(); // 날짜
17-
const hours = today.getHours(); // 시
18-
const minutes = today.getMinutes(); // 분
19-
const seconds = today.getSeconds(); // 초
20-
const nowTime =
21-
year + '-' + month + '-' + date + ' ' + hours + '-' + minutes + '-' + seconds;
22-
const file_name = '김성현의 테스트 엑셀파일 데이터';
23-
24-
// 파일경로이자 마지막 / 뒤에는 내가 저장하게될 파일 이름 + 확장자 현재경로는 바탕하면으로 할 예정
25-
const FILE_PATH = `/Users/User/${file_name} + ${nowTime}.xlsx`;
26-
27-
// db 연결
28-
const connection = await createConnection(DB_CONFIG);
29-
30-
// 쿼리 실행 후 rows 에 담기
31-
const [rows] = await connection.execute(SQL_QUERY);
32-
33-
// 새로운 파일이랑 워크시트 생성
34-
const workbook = utils.book_new();
35-
const worksheet = utils.json_to_sheet(rows);
36-
// console.log(worksheet);
37-
38-
// 워크북에 워크시트 추가
39-
utils.book_append_sheet(workbook, worksheet);
40-
41-
// 해당 경로에 워크북 파일 생성
42-
writeFile(workbook, FILE_PATH);
43-
44-
// console.log(`파일 내보내기가 잘 실행되었습니다. 파일이 저장된 경로는 -> ${FILE_PATH}`);
45-
46-
return res.status(200).json({
47-
isSuccess: true,
48-
msg: '파일 내보내기가 잘 실행되었습니다.',
49-
path: '파일이 생성된 위치는 ' + FILE_PATH + ' 입니다.',
50-
});
51-
} catch (error) {
52-
return res.status(400)({
53-
msg: '유효하지 않은 값입니다. 에러내용을 확인해주세요',
54-
error,
55-
});
56-
}
7+
const { path, file_name, query } = req.body;
8+
9+
// db 연결용 config
10+
const DB_CONFIG = config.test_db_config;
11+
const SQL_QUERY = await utmFilters(query);
12+
console.log('여기는 쿼리 함수 돌고 돌아오 결과문:', SQL_QUERY);
13+
14+
// const SQL_QUERY = utmFilters()
15+
/*
16+
// db 연결용 config
17+
const DB_CONFIG = config.test_db_config;
18+
19+
// 앞으로 많이 수정하게 될 쿼리문
20+
const SQL_QUERY = 'SELECT * FROM kshexportdb.utmtest_2';
21+
22+
const today = new Date();
23+
const year = today.getFullYear(); // 년도
24+
const month = today.getMonth() + 1; // 월
25+
const date = today.getDate(); // 날짜
26+
const hours = today.getHours(); // 시
27+
const minutes = today.getMinutes(); // 분
28+
const seconds = today.getSeconds(); // 초
29+
const nowTime =
30+
year + '-' + month + '-' + date + ' ' + hours + '-' + minutes + '-' + seconds;
31+
const file_name = '김성현의 테스트 엑셀파일 데이터';
32+
33+
// 파일경로이자 마지막 / 뒤에는 내가 저장하게될 파일 이름 + 확장자 현재경로는 바탕하면으로 할 예정
34+
const FILE_PATH = `/Users/User/${file_name} + ${nowTime}.xlsx`;
35+
*/
36+
37+
const FILE_PATH = `${path}${file_name}.xlsx`;
38+
39+
// db 연결
40+
const connection = await createConnection(DB_CONFIG);
41+
42+
// 쿼리 실행 후 rows 에 담기
43+
const [rows] = await connection.execute(SQL_QUERY);
44+
45+
// // 쿼리 실행 후 rows 에 담기
46+
// const [rows] = await connection.execute(SQL_QUERY);
47+
48+
// 새로운 파일이랑 워크시트 생성
49+
const workbook = utils.book_new();
50+
const worksheet = utils.json_to_sheet(rows);
51+
// console.log(worksheet);
52+
53+
// 워크북에 워크시트 추가
54+
utils.book_append_sheet(workbook, worksheet);
55+
56+
// 해당 경로에 워크북 파일 생성
57+
writeFile(workbook, FILE_PATH);
58+
59+
// console.log(`파일 내보내기가 잘 실행되었습니다. 파일이 저장된 경로는 -> ${FILE_PATH}`);
60+
61+
return res.status(200).json({
62+
isSuccess: true,
63+
msg: '파일 내보내기가 잘 실행되었습니다.',
64+
path: '파일이 생성된 위치는 ' + FILE_PATH + ' 입니다.',
65+
});
5766
}
5867

5968
export { exportDataToExcel };

src/controllers/utm/utmFilter.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
async function utmFilters(req) {
2+
const query = req;
3+
4+
let sql_query = 'SELECT * FROM kshexportdb.utmtest_2';
5+
6+
// const data = {
7+
// 생성날짜: '2023.02.22',
8+
// url: 'https://www.naver.com/',
9+
// 캠페인ID: 'naver테스트',
10+
// 소스: null,
11+
// 미디움: 'paidsearching',
12+
// 캠페인이름: '2022_유렉카 기획',
13+
// 캠페인텀: 'iphone12',
14+
// 캠페인콘텐츠: 'image1',
15+
// shortenUrl: 'www.naver.com',
16+
// utm: null,
17+
// };
18+
19+
// const createdat = data['생성날짜'];
20+
// const url = data['url'];
21+
// const campaignId = data['캠페인ID'];
22+
// const source = data['소스'];
23+
// const campaignTerm = data['캠페인텀'];
24+
// const content = data['캠페인콘텐츠'];
25+
// const shortenUrl = data['shortenUrl'];
26+
// const utm = data['utm'];
27+
28+
let andStack = 1;
29+
30+
if (!query) {
31+
sql_query;
32+
} else {
33+
sql_query = sql_query + ' where ';
34+
35+
const createdat = query['created_at'];
36+
const url = query['utm_url'];
37+
const campaignId = query['utm_campaign_id'];
38+
const source = query['utm_source'];
39+
const medium = query['utm_medium'];
40+
const campaigName = query['utm_campaign_name'];
41+
const campaignTerm = query['utm_term'];
42+
const content = query['utm_content'];
43+
const fullUrl = query['full_url'];
44+
const shortenUrl = query['shorten_url'];
45+
46+
if (createdat != null) {
47+
andStack--;
48+
if (andStack < 0) sql_query = sql_query + 'and';
49+
sql_query = sql_query + `createdat like '%${query['created_at']}%'`;
50+
}
51+
if (url != null) {
52+
andStack--;
53+
if (andStack < 0) sql_query = sql_query + ' and ';
54+
sql_query = sql_query + `url like '%${query['utm_url']}%'`;
55+
}
56+
if (campaignId != null) {
57+
andStack--;
58+
if (andStack < 0) sql_query = sql_query + ' and ';
59+
sql_query = sql_query + `campaignId like '%${query['utm_campaign_id']}%'`;
60+
}
61+
if (source != null) {
62+
andStack--;
63+
if (andStack < 0) sql_query = sql_query + ' and ';
64+
sql_query = sql_query + `source like '%${query['utm_source']}%'`;
65+
}
66+
if (medium != null) {
67+
andStack--;
68+
if (andStack < 0) sql_query = sql_query + ' and ';
69+
sql_query = sql_query + `source like '%${query['utm_medium']}%'`;
70+
}
71+
if (campaigName != null) {
72+
andStack--;
73+
if (andStack < 0) sql_query = sql_query + ' and ';
74+
sql_query = sql_query + `source like '%${query['utm_campaign_name']}%'`;
75+
}
76+
if (campaignTerm != null) {
77+
andStack--;
78+
if (andStack < 0) sql_query = sql_query + ' and ';
79+
sql_query = sql_query + `term like '%${query['utm_term']}%'`;
80+
}
81+
if (content != null) {
82+
andStack--;
83+
if (andStack < 0) sql_query = sql_query + ' and ';
84+
sql_query = sql_query + `content like '%${query['utm_content']}%'`;
85+
}
86+
if (shortenUrl != null) {
87+
andStack--;
88+
if (andStack < 0) sql_query = sql_query + ' and ';
89+
sql_query = sql_query + `shortenUrl like '%${query['shorten_url']}%'`;
90+
}
91+
if (fullUrl != null) {
92+
andStack--;
93+
if (andStack < 0) sql_query = sql_query + ' and ';
94+
sql_query = sql_query + `shortenUrl like '%${query['full_url']}%'`;
95+
}
96+
97+
sql_query = sql_query + ';';
98+
andStack = 1;
99+
}
100+
console.log(sql_query);
101+
102+
return sql_query;
103+
// return res.status(200).json({
104+
// isSuccess: true,
105+
// msg: '필터링에 따른 쿼리가 완성되었습니다',
106+
// sql_query,
107+
// });
108+
}
109+
110+
export { utmFilters };

src/controllers/utm/utmMemo.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { createConnection } from 'mysql2/promise.js';
2+
import config from '../../config/dbconfig.js';
3+
4+
async function utmMemo(req, res) {
5+
const inputMemo = req.body;
6+
// const inputMemo = {
7+
// id: '6',
8+
// memo: ' "수정할 메모의 내용을 입력" ',
9+
// };
10+
console.log(inputMemo);
11+
12+
const utm_id = inputMemo['id'];
13+
const utm_memo = inputMemo['memo'];
14+
const DB_CONFIG = config.test_db_config;
15+
16+
const sql_query = `UPDATE kshexportdb.utmtest_2 SET memo = '${utm_memo}' where id=${utm_id} ; `;
17+
// db 연결
18+
const connection = await createConnection(DB_CONFIG);
19+
20+
await connection.execute(sql_query);
21+
22+
return res.status(200).json({
23+
isSuccess: true,
24+
msg: '메모 수정이 완료되었습니다',
25+
update_utm_id: inputMemo['id'],
26+
});
27+
}
28+
29+
export { utmMemo };

src/routes/utmRouter.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import express from 'express';
22
import { asyncWrapper } from '../../utils/middleware.js';
33
import { exportDataToExcel } from '../controllers/utm/exportDataToExcel.js';
44
import { createUtm } from '../controllers/utm/utm-crud.js';
5+
import { utmFilters } from '../controllers/utm/utmFilter.js';
6+
import { utmMemo } from '../controllers/utm/utmMemo.js';
57

68
const router = express.Router();
79

@@ -16,6 +18,9 @@ router.post('/api/utm', () => {});
1618
router.post('/api/utm', () => {});
1719

1820
// UTM 데이터 추출 관련
19-
router.get('/export/excell', asyncWrapper(exportDataToExcel));
21+
router.post('/api/utms/excell', asyncWrapper(exportDataToExcel));
22+
router.post('/api/utms/filter', asyncWrapper(utmFilters));
23+
router.patch('/api/utm/memo', asyncWrapper(utmMemo));
24+
2025

2126
export { router };

0 commit comments

Comments
 (0)