Back/apps/app.js

228 lines
6.5 KiB
JavaScript

import express, { query } from 'express';
import { createDatabaseConnection } from '../db/database.js'; // db 연결 파일
import { HandlePush } from './push.js';
const cmcd = "/JJ";
const db = "/JJ/db";
const app = express();
app.use(express.json());
const dbConfig = {
host: 'ipStein.myDS.me',
user: 'root',
password: 'JJrootPW',
database: 'dev',
port: 8002
};
/*
// query 클래스 묶음
let query;
query = await getQueryModule('update', 'Update');
if (!query) { return res.status(400).json({ error: 'Invalid parameter' }); }
*/
const getQueryModule = async (type, className) => {
if (type === 'create') {
const module = await import('../../db/queries/create_query.js');
return module[className];
}
else if (type === 'read') {
const module = await import('../../db/queries/read_query.js');
return module[className];
}
else if (type === 'update') {
const module = await import('../../db/queries/update_query.js');
return module[className];
}
else if (type === 'delete') {
const module = await import('../../db/queries/update_query.js');
return module[className];
}
else if (type === 'function') {
const module = await import('../../db/queries/functions_query.js');
return module[className];
}
return ''
};
app.get(`${cmcd}/push`, async (req, res) => {
// HandlePush(req, res);
//3138117983JJ20240611
// const { cid, title, body, cd } = req.query;
const { cid, title, body, badge, parameter } = req.query;
if (!cid || !title || !body) {
return res.status(400).json({ error: 'All parameters (cid, title, body) are required' });
}
let query;
query = await getQueryModule('function', 'Push');
if (!query) { return res.status(400).json({ error: 'Invalid parameter' }); }
try {
const connection = await createDatabaseConnection({ ...dbConfig, database: 'jjungtable_db'});
const [rows] = await connection.query(query.loadFCM_token, [cid]);
await connection.end();
if (rows.length === 0) {
return res.status(404).json({ error: 'No fcm_token found for the provided cid' });
}
const fcmToken = rows[0].fcm_token;
console.log(fcmToken);
// HandlePush 호출 시 필요한 req.body에 fcmToken, title, body 설정
req.body = { fcmToken, title, body, badge, parameter };
HandlePush(req, res);
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Internal server error' });
}
});
// DB 연결 테스트
app.get(`${db}/test`, async (req, res) => {
try {
const connection = await createDatabaseConnection(dbConfig);
const [rows] = await connection.query('SELECT 1 + 1 AS solution');
await connection.end();
res.json({ result: rows[0].solution });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
///////////////////////////////////////////////////////////////
// GET
///////////////////////////////////////////////////////////////
app.get(`${db}/version/read`, async (req, res) => {
const { os_type } = req.query;
if (!os_type) { return res.status(400).json({ error: 'ostype parameter is required' }) }
let query;
query = await getQueryModule('read', 'Read');
if (!query) { return res.status(400).json({ error: 'Invalid parameter' }); }
try {
const connection = await createDatabaseConnection({ ...dbConfig, database: 'jjungtable_db' });
const [rows] = await connection.query(query.version, [os_type]);
await connection.end();
if (rows.length === 0) {
return res.status(404).json({ error: 'No data found' });
}
res.json(rows);
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Internal server error' });
}
});
app.get(`${db}/userInfo/read`, async (req, res) => {
const { id, type } = req.query;
if (!id || !type) { return res.status(400).json({ error: 'ostype parameter is required' }) }
let query;
query = await getQueryModule('read', 'Read');
if (!query) { return res.status(400).json({ error: 'Invalid parameter' }); }
try {
const connection = await createDatabaseConnection({ ...dbConfig, database: 'jjungtable_db' });
const [rows] = await connection.query(query.login_code, [id, type]);
await connection.end();
if (rows.length === 0) {
return res.status(404).json({ error: 'No data found' });
}
res.json(rows);
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Internal server error' });
}
});
///////////////////////////////////////////////////////////////
// POST
///////////////////////////////////////////////////////////////
async function insertLog(table, connection, query, body) {
if (table === 'version') {
const { date, managerId, os_type, final_ver, force_ver, update_ver, update_choice, log } = body;
await connection.query( query, [date, managerId, os_type, final_ver, force_ver, update_ver, update_choice, log] );
}
}
app.post(`${db}/version/update`, async (req, res) => {
const { date, managerId, os_type, final_ver, force_ver, update_ver, update_choice, log } = req.body;
try {
const connection = await createDatabaseConnection({ ...dbConfig});
let query;
// 버전 정보 업데이트
query = await getQueryModule('update', 'Update');
if (!query) { return res.status(400).json({ error: 'Invalid parameter' }); }
await connection.query( query.version,[final_ver, force_ver, update_ver, update_choice, os_type]);
// 로그 생성
query = await getQueryModule('create', 'Log');
if (!query) { return res.status(400).json({ error: 'Invalid parameter' }); }
await insertLog('version', connection, query.version_Log, req.body);
await connection.end();
res.json({ message: 'Update successful' });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
///////////////////////////////////////////////////////////////
// LOG 관련
// DB Log 읽어들이기
app.get(`${db}/log/read`, async (req, res) => {
console.log('[JJ]Log: Version');
const { param } = req.query;
try {
const connection = await createDatabaseConnection({ ...dbConfig });
let query, rows;
// let rows;
if (param === 'version') {
query = await getQueryModule('read', 'Log');
if (!query) { return res.status(400).json({ error: 'Invalid parameter' }); }
[rows] = await connection.query(query.version_Log);
}
await connection.end();
res.json(rows);
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Internal server error' });
}
});
export default app;