Skip to content

Commit 4698a34

Browse files
authored
Merge pull request #131 from boostcamp-2020/dev
Dev -> master : 신규 유저 random채널로 이동
2 parents 1d5be62 + 33b5175 commit 4698a34

File tree

5 files changed

+32
-11
lines changed

5 files changed

+32
-11
lines changed

server/src/lib/passport.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import config from '@/config';
22
import { Strategy as GoogleStrategy } from 'passport-google-oauth20';
33
import passport from 'passport';
4-
import { userModel } from '@/models';
4+
import { channelModel, userModel } from '@/models';
55
import { User } from '@/types';
66

77
export default () => {
@@ -32,6 +32,7 @@ export default () => {
3232
}
3333

3434
const [{ insertId }] = await userModel.addOAuthUser({ email, displayName });
35+
await channelModel.setUserChannel({ userId: insertId });
3536
done(undefined, { id: insertId, displayName, email });
3637
return;
3738
} catch (err) {

server/src/lib/socket.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,6 @@ export const bindSocketServer = (server: http.Server): void => {
189189
const namespace = io.of('/');
190190

191191
namespace.on(CONNECT, (socket: Socket) => {
192-
console.log('메인 채널 연결됨 socketID : ', socket.id);
193-
// io.to(socket.id).emit(MESSAGE, { socketId: socket.id });
194-
195192
socket.on(MESSAGE, async (data: SocketEvent) => {
196193
if (isThreadEvent(data)) {
197194
const { type, subType, room, thread } = data;
@@ -381,14 +378,10 @@ export const bindSocketServer = (server: http.Server): void => {
381378
});
382379

383380
socket.on(ENTER_ROOM, (data: RoomEvent) => {
384-
console.log('enter');
385-
console.dir(data, { depth: null });
386381
socket.join(data.room);
387382
});
388383

389384
socket.on(LEAVE_ROOM, (data: RoomEvent) => {
390-
console.log('leave');
391-
console.dir(data, { depth: null });
392385
socket.leave(data.room);
393386
});
394387

server/src/models/channels.model.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,29 @@ export const channelModel: Model = {
9595
const sql = `UPDATE user_channel SET unread = ? WHERE user_id = ? AND channel_id = ?;`;
9696
return pool.execute(sql, [unread, userId, channelId]);
9797
},
98+
async setUserChannel({
99+
userId,
100+
channelId = 1,
101+
}: {
102+
userId: number;
103+
channelId?: number;
104+
}): Promise<{ err?: Error }> {
105+
const conn = await pool.getConnection();
106+
await conn.beginTransaction();
107+
try {
108+
const sql1 = `INSERT INTO user_channel (user_id, channel_id) VALUES (?, ?);`;
109+
const sql2 = `UPDATE channel SET member_count = member_count + 1 WHERE id = ?;`;
110+
111+
conn.execute(sql1, [userId, channelId]);
112+
conn.execute(sql2, [channelId]);
113+
conn.commit();
114+
return {};
115+
} catch (err) {
116+
console.error(err);
117+
conn.rollback();
118+
return { err };
119+
} finally {
120+
conn.release();
121+
}
122+
},
98123
};

server/src/routes/api/auth/auth.controller.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
decrypt,
1010
sendEmail,
1111
} from '@/utils/utils';
12-
import { userModel } from '@/models';
12+
import { channelModel, userModel } from '@/models';
1313
import config from '@/config';
1414
import { TIME, TOKEN_TYPE, ERROR_MESSAGE } from '@/utils/constants';
1515
import isEmail from 'validator/lib/isEmail';
@@ -71,7 +71,8 @@ export const signup = async (req: Request, res: Response, next: NextFunction): P
7171
if (verifyRequestData([email, pw, displayName])) {
7272
try {
7373
const hashPw = await bcrypt.hash(pw, 10);
74-
await userModel.addUser({ email, pw: hashPw, displayName });
74+
const [{ insertId }] = await userModel.addUser({ email, pw: hashPw, displayName });
75+
await channelModel.setUserChannel({ userId: insertId });
7576

7677
res.status(200).end();
7778
return;

server/src/routes/api/oauth/oauth.controller.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ERROR_MESSAGE, TIME } from '@/utils/constants';
66
import { User } from '@/types';
77
import axios from 'axios';
88
import { verifyRequestData } from '@/utils/utils';
9-
import { userModel } from '@/models';
9+
import { channelModel, userModel } from '@/models';
1010

1111
/**
1212
* GET /api/oauth/google
@@ -67,6 +67,7 @@ export const googleSignup = async (
6767
userInfo = { email: user.email, displayName: user.displayName, id: user.id };
6868
} else {
6969
const [{ insertId }] = await userModel.addOAuthUser({ email, displayName: name });
70+
await channelModel.setUserChannel({ userId: insertId });
7071
userInfo = { email, displayName: name, id: insertId };
7172
}
7273

0 commit comments

Comments
 (0)