Skip to content

Commit 9302bc2

Browse files
committed
fixed conflicts
2 parents ec20b7b + 8f92e6b commit 9302bc2

File tree

10 files changed

+99
-52
lines changed

10 files changed

+99
-52
lines changed

app.js

+30-19
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,30 @@ const notificationRouter = require('./app/routes/notification')
2424
const proposalRouter = require('./app/routes/proposal')
2525
const app = express()
2626
const server = require('http').Server(app)
27-
server.listen(process.env.SOCKET_PORT || 8810)
27+
if (process.env.NODE_ENV !== 'testing') {
28+
server.listen(process.env.SOCKET_PORT || 8810)
29+
}
2830
// WARNING: app.listen(80) will NOT work here!
2931
const io = socket.listen(server)
3032

3133
class ServerBuilder {
3234
constructor () {
33-
this.initDB()
34-
this.initMiddleware()
35-
this.initViewEngine()
36-
this.initLogger()
37-
this.initSocket()
38-
this.initRouter()
39-
this.initErrorHandler()
35+
this.#initDB()
36+
this.#initMiddleware()
37+
this.#initViewEngine()
38+
this.#initLogger()
39+
this.#initSocket()
40+
this.#initRouter()
41+
this.#initErrorHandler()
4042
}
4143

42-
initDB () {
43-
const db = new Connection().getInstance()
44-
db.connect()
44+
// PRIVATE METHOD (ES6)
45+
#initDB = async () => {
46+
await Connection.connect()
4547
}
4648

47-
initMiddleware () {
49+
// PRIVATE METHOD (ES6)
50+
#initMiddleware = () => {
4851
app.use(cors())
4952

5053
app.use(bodyParser.json({ limit: '200mb' }))
@@ -59,13 +62,15 @@ class ServerBuilder {
5962
app.use(cookieParser())
6063
}
6164

62-
initViewEngine () {
65+
// PRIVATE METHOD (ES6)
66+
#initViewEngine = () => {
6367
// view engine setup
6468
app.set('views', path.join(__dirname, 'views'))
6569
app.set('view engine', 'ejs')
6670
}
6771

68-
initLogger () {
72+
// PRIVATE METHOD (ES6)
73+
#initLogger = () => {
6974
morgan.token('data', (req, res) => {
7075
return JSON.stringify(req.body)
7176
})
@@ -78,7 +83,8 @@ class ServerBuilder {
7883
)
7984
}
8085

81-
initSocket () {
86+
// PRIVATE METHOD (ES6)
87+
#initSocket = () => {
8288
let count = 0
8389
io.on('connection', (socket) => {
8490
console.log('socket connected count ', count++)
@@ -91,7 +97,8 @@ class ServerBuilder {
9197
})
9298
}
9399

94-
initRouter () {
100+
// PRIVATE METHOD (ES6)
101+
#initRouter = () => {
95102
app.use('/notification', notificationRouter)
96103
app.use('/', indexRouter)
97104
app.use('/auth', authRouter)
@@ -105,7 +112,8 @@ class ServerBuilder {
105112
app.use('/proposal', proposalRouter)
106113
}
107114

108-
initErrorHandler () {
115+
// PRIVATE METHOD (ES6)
116+
#initErrorHandler = () => {
109117
// catch 404 and forward to error handler
110118
app.use(function (req, res, next) {
111119
next(createError(404, "route doesn't exist"))
@@ -134,8 +142,11 @@ class ServerBuilder {
134142
next()
135143
})
136144
}
145+
static startServer() {
146+
return new ServerBuilder()
147+
}
137148
}
138149

139-
// ANONYMOUS OBJECT
140-
new ServerBuilder()
150+
// STATIC METHOD CALLED
151+
ServerBuilder.startServer()
141152
module.exports = { app, io }

app/controllers/auth.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ const HttpStatus = require('http-status-codes')
33
class Auth {
44
constructor (UserModel) {
55
this.UserModel = UserModel
6-
this.initBinding()
6+
this.#initBinding()
77
}
88

9-
initBinding () {
9+
// PRIVATE ES6
10+
#initBinding = () => {
1011
this.authenticateUser = this.authenticateUser.bind(this)
1112
this.logout = this.logout.bind(this)
1213
this.logoutAll = this.logoutAll.bind(this)

app/controllers/comment.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ const helper = require('../utils/paginate')
66
class Comment {
77
constructor(CommentModel) {
88
this.CommentModel = CommentModel
9-
this.initBinding()
9+
this.#initBinding()
1010
}
1111

12-
initBinding () {
12+
// PRIVATE ES6
13+
#initBinding = () => {
1314
this.comment = this.comment.bind(this)
1415
this.update = this.update.bind(this)
1516
this.delete = this.delete.bind(this)

app/controllers/notification.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ const ProposalNotifications = require('../models/ProposalNotification')
77

88
class NotificationProvider {
99
constructor(NotificationModel, UserModel, ProposalModel) {
10-
this.initModels(NotificationModel, UserModel, ProposalModel)
11-
this.initBinding()
10+
this.#initModels(NotificationModel, UserModel, ProposalModel)
11+
this.#initBinding()
1212
}
1313

14-
initModels (NotificationModel, UserModel, ProposalModel) {
14+
// PRIVATE ES6
15+
#initModels = (NotificationModel, UserModel, ProposalModel) => {
1516
this.UserModel = UserModel
1617
this.NotificationModel = NotificationModel
1718
this.ProposalModel = ProposalModel
1819
}
1920

20-
initBinding () {
21+
// PRIVATE ES6
22+
#initBinding = () => {
2123
this.getOrgNotifications = this.getOrgNotifications.bind(this)
2224
this.getUserNotification = this.getUserNotification.bind(this)
2325
this.getProposalNotifications = this.getProposalNotifications.bind(this)

app/controllers/user.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class UserController extends Notification {
9191
'phone',
9292
'info',
9393
'about',
94-
'isDeactivated'
94+
'socialMedia'
9595
]
9696
// added control as per org settings
9797
if (settingHelper.canChangeName()) {
@@ -158,7 +158,7 @@ class UserController extends Notification {
158158
'Forgot password!',
159159
'Password successfully updated!',
160160
TAGS.UPDATE)
161-
notificationHelper.addToNotificationForUser(id, res, newNotif, next)
161+
await notificationHelper.addToNotificationForUser(id, res, newNotif, next)
162162
return res.status(HttpStatus.OK).json({ updated: true })
163163
} else {
164164
res.status(HttpStatus.BAD_REQUEST).json({ error: 'Token expired' })
@@ -215,7 +215,7 @@ class UserController extends Notification {
215215
'Account successfully activated!',
216216
TAGS.ACTIVATE
217217
)
218-
notificationHelper.addToNotificationForUser(user._id, res, newNotif, next)
218+
await notificationHelper.addToNotificationForUser(user._id, res, newNotif, next)
219219
return res.status(HttpStatus.OK).json({ msg: 'Succesfully activated!' })
220220
}
221221
} catch (Error) {
@@ -299,7 +299,7 @@ class UserController extends Notification {
299299
`${req.user.name.firstName} started following you!`,
300300
TAGS.FOLLOWER
301301
)
302-
notificationHelper.addToNotificationForUser(user._id, res, newNotif, next)
302+
await notificationHelper.addToNotificationForUser(user._id, res, newNotif, next)
303303
const userData = await this.UserModel.findById(req.user._id)
304304
.populate('followings', ['name.firstName', 'name.lastName', 'info.about.designation', '_id', 'isAdmin'])
305305
.populate('followers', ['name.firstName', 'name.lastName', 'info.about.designation', '_id', 'isAdmin'])

app/models/Proposal.js

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ const proposalSchema = new Schema(
77
type: String,
88
required: true
99
},
10+
organization: {
11+
type: String
12+
},
1013
content: {
1114
type: String,
1215
required: true

config/mongoose.js

+1-13
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,5 @@ class Connection {
2020
})
2121
}
2222
}
23-
2423
// SINGLETON CLASS
25-
class Singleton {
26-
constructor () {
27-
if (!Singleton.instance) {
28-
Singleton.instance = new Connection()
29-
}
30-
}
31-
32-
getInstance () {
33-
return Singleton.instance
34-
}
35-
}
36-
module.exports = Singleton
24+
module.exports = new Connection()

test/organisation.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,14 @@ describe('POST /org/', () => {
108108
.set('Authorization', `Bearer ${token}`)
109109
.send(testOrg)
110110
.expect(HttpStatus.CREATED)
111-
orgId = response.body.org._id
111+
orgId = response.body.orgData._id
112112
/** DB must be changed **/
113-
const org = await Organization.findById(response.body.org._id)
113+
const org = await Organization.findById(response.body.orgData._id)
114114
expect(org).not.toBeNull()
115115

116116
/** Check the response **/
117117
expect(response.body).toMatchObject({
118-
org: {
118+
orgData: {
119119
isArchived: false,
120120
_id: `${orgId}`,
121121
name: `${testOrg.name}`,

test/proposal.test.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const app = require('../app')
1+
const app = require('../app').app
22
const mongoose = require('mongoose')
33
const jwt = require('jsonwebtoken')
44
const HttpStatus = require('http-status-codes')
@@ -195,3 +195,12 @@ test('Should return the proposal by the given Id', async (done) => {
195195

196196
done()
197197
})
198+
199+
afterAll(async () => {
200+
// avoid jest open handle error
201+
await new Promise((resolve) => setTimeout(() => resolve(), 500))
202+
// close server
203+
await server.close()
204+
// Closing the DB connection allows Jest to exit successfully.
205+
await mongoose.connection.close()
206+
})

test/user.test.js

+36-4
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,38 @@ test('Should not get profile for unauthenticated user', async () => {
185185
.expect(HttpStatus.UNAUTHORIZED)
186186
})
187187

188+
/** Should update user profile */
189+
test('Should update profile or authenticated user', async () => {
190+
await request(app)
191+
.patch('/user/me')
192+
.set('Authorization', `Bearer ${testUser.tokens[0].token}`)
193+
.send({
194+
195+
})
196+
.expect(HttpStatus.OK)
197+
})
198+
199+
/** Should fail to make updates that are not allowed to user profile */
200+
test('Should be able to make only allowed updates to authenticated user', async () => {
201+
await request(app)
202+
.patch('/user/me')
203+
.set('Authorization', `Bearer ${testUser.tokens[0].token}`)
204+
.send({
205+
gender: 'Male'
206+
})
207+
.expect(HttpStatus.BAD_REQUEST)
208+
})
209+
210+
/** Should Fail updating profile of unauthenticate user */
211+
test('Should not update profile or unauthenticated user', async () => {
212+
await request(app)
213+
.patch('/user/me')
214+
.send({
215+
216+
})
217+
.expect(HttpStatus.UNAUTHORIZED)
218+
})
219+
188220
/** Delete authenticated user profile */
189221
test('Should delete profile of authenticated user', async () => {
190222
await request(app)
@@ -209,7 +241,7 @@ test('Should not delete profile of unauthenticated user', async () => {
209241
/** Forgot password request **/
210242
test('Should send the request to change the password ', async () => {
211243
const response = await request(app)
212-
.post('/user/password_reset')
244+
.patch('/user/password_reset/request')
213245
.send({
214246
email: `${testUser.email}`
215247
})
@@ -221,7 +253,7 @@ test('Should send the request to change the password ', async () => {
221253
/* Password update */
222254
test('Should update the password ', async () => {
223255
await request(app)
224-
.post(`/user/password_reset/${passwordToken}`)
256+
.patch(`/user/password_reset/${passwordToken}`)
225257
.send({
226258
password: 'newPassword',
227259
id: testUserId
@@ -243,7 +275,7 @@ test('Should activate the account ', async (done) => {
243275
/* Get invite link */
244276
test('Should generate an invite link and send', async () => {
245277
const response = await request(app)
246-
.get('/user/invite')
278+
.get('/user/invite?role=user')
247279
.set('Authorization', `Bearer ${testUser.tokens[0].token}`)
248280
.send()
249281
.expect(HttpStatus.OK)
@@ -258,7 +290,7 @@ test('Should validate the invite link token ', async () => {
258290
await request(app)
259291
.get(`/user/invite/${inviteToken}`)
260292
.send()
261-
.expect(HttpStatus.OK)
293+
.expect(HttpStatus.MOVED_TEMPORARILY)
262294
})
263295

264296
/* Logout the user */

0 commit comments

Comments
 (0)