diff --git a/server/src/lib/passport.ts b/server/src/lib/passport.ts index 5b7487f2..83bff868 100644 --- a/server/src/lib/passport.ts +++ b/server/src/lib/passport.ts @@ -1,7 +1,7 @@ import config from '@/config'; import { Strategy as GoogleStrategy } from 'passport-google-oauth20'; import passport from 'passport'; -import { userModel } from '@/models'; +import { channelModel, userModel } from '@/models'; import { User } from '@/types'; export default () => { @@ -32,6 +32,7 @@ export default () => { } const [{ insertId }] = await userModel.addOAuthUser({ email, displayName }); + await channelModel.setUserChannel({ userId: insertId }); done(undefined, { id: insertId, displayName, email }); return; } catch (err) { diff --git a/server/src/lib/socket.ts b/server/src/lib/socket.ts index 68ed7a2d..bd7401f8 100644 --- a/server/src/lib/socket.ts +++ b/server/src/lib/socket.ts @@ -189,9 +189,6 @@ export const bindSocketServer = (server: http.Server): void => { const namespace = io.of('/'); namespace.on(CONNECT, (socket: Socket) => { - console.log('메인 채널 연결됨 socketID : ', socket.id); - // io.to(socket.id).emit(MESSAGE, { socketId: socket.id }); - socket.on(MESSAGE, async (data: SocketEvent) => { if (isThreadEvent(data)) { const { type, subType, room, thread } = data; @@ -381,14 +378,10 @@ export const bindSocketServer = (server: http.Server): void => { }); socket.on(ENTER_ROOM, (data: RoomEvent) => { - console.log('enter'); - console.dir(data, { depth: null }); socket.join(data.room); }); socket.on(LEAVE_ROOM, (data: RoomEvent) => { - console.log('leave'); - console.dir(data, { depth: null }); socket.leave(data.room); }); diff --git a/server/src/models/channels.model.ts b/server/src/models/channels.model.ts index e302df57..de645015 100644 --- a/server/src/models/channels.model.ts +++ b/server/src/models/channels.model.ts @@ -95,4 +95,29 @@ export const channelModel: Model = { const sql = `UPDATE user_channel SET unread = ? WHERE user_id = ? AND channel_id = ?;`; return pool.execute(sql, [unread, userId, channelId]); }, + async setUserChannel({ + userId, + channelId = 1, + }: { + userId: number; + channelId?: number; + }): Promise<{ err?: Error }> { + const conn = await pool.getConnection(); + await conn.beginTransaction(); + try { + const sql1 = `INSERT INTO user_channel (user_id, channel_id) VALUES (?, ?);`; + const sql2 = `UPDATE channel SET member_count = member_count + 1 WHERE id = ?;`; + + conn.execute(sql1, [userId, channelId]); + conn.execute(sql2, [channelId]); + conn.commit(); + return {}; + } catch (err) { + console.error(err); + conn.rollback(); + return { err }; + } finally { + conn.release(); + } + }, }; diff --git a/server/src/routes/api/auth/auth.controller.ts b/server/src/routes/api/auth/auth.controller.ts index d1122ef7..7289268f 100644 --- a/server/src/routes/api/auth/auth.controller.ts +++ b/server/src/routes/api/auth/auth.controller.ts @@ -9,7 +9,7 @@ import { decrypt, sendEmail, } from '@/utils/utils'; -import { userModel } from '@/models'; +import { channelModel, userModel } from '@/models'; import config from '@/config'; import { TIME, TOKEN_TYPE, ERROR_MESSAGE } from '@/utils/constants'; import isEmail from 'validator/lib/isEmail'; @@ -71,7 +71,8 @@ export const signup = async (req: Request, res: Response, next: NextFunction): P if (verifyRequestData([email, pw, displayName])) { try { const hashPw = await bcrypt.hash(pw, 10); - await userModel.addUser({ email, pw: hashPw, displayName }); + const [{ insertId }] = await userModel.addUser({ email, pw: hashPw, displayName }); + await channelModel.setUserChannel({ userId: insertId }); res.status(200).end(); return; diff --git a/server/src/routes/api/oauth/oauth.controller.ts b/server/src/routes/api/oauth/oauth.controller.ts index 70c3ccab..a559a926 100644 --- a/server/src/routes/api/oauth/oauth.controller.ts +++ b/server/src/routes/api/oauth/oauth.controller.ts @@ -6,7 +6,7 @@ import { ERROR_MESSAGE, TIME } from '@/utils/constants'; import { User } from '@/types'; import axios from 'axios'; import { verifyRequestData } from '@/utils/utils'; -import { userModel } from '@/models'; +import { channelModel, userModel } from '@/models'; /** * GET /api/oauth/google @@ -67,6 +67,7 @@ export const googleSignup = async ( userInfo = { email: user.email, displayName: user.displayName, id: user.id }; } else { const [{ insertId }] = await userModel.addOAuthUser({ email, displayName: name }); + await channelModel.setUserChannel({ userId: insertId }); userInfo = { email, displayName: name, id: insertId }; }