Skip to content

Commit a50c871

Browse files
authored
Fix: Add bother note author and other user details in get all notes apis (#34)
1 parent 1066726 commit a50c871

File tree

4 files changed

+90
-84
lines changed

4 files changed

+90
-84
lines changed

Diff for: .env.example

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ GITHUB_API_URL = https://api.github.com/
55
JWT_KEY = <JWT_KEY>
66
REDIS_URL = 'redis://127.0.0.1:6379'
77
HEROKU_URL= <HEROKU_URL>
8-
WELCOME_URL='https://gitxapp.com/welcome.html'
8+
WELCOME_URL=https://gitxapp.com/welcome.html

Diff for: server/apis/v1/modules/Note/note.service.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,18 @@ async function getNotes(user, noteDetails) {
109109
{ noteType },
110110
{ $or: [{ userId }, { noteVisibility: true }] }
111111
]
112-
});
112+
}).populate("userId author", "userName");
113+
const userDetails = { userName, avatarUrl, githubId };
113114

114115
notes = notes.map(note => {
115116
return {
116-
...note._doc,
117+
_id: note._id,
117118
noteContent: converter.makeHtml(note.noteContent),
118-
userId: { userName, avatarUrl, githubId }
119+
author: note.userId,
120+
createdAt: note.createdAt,
121+
updatedAt: note.updatedAt,
122+
nearestCommentId: note.nearestCommentId,
123+
userDetails
119124
};
120125
});
121126
}

Diff for: server/apis/v1/modules/User/user.service.js

+20-25
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import jwt from 'jsonwebtoken';
1+
import jwt from "jsonwebtoken";
22

3-
import User from './user.model';
3+
import User from "./user.model";
44

5-
import Note from '../Note/note.model';
5+
import Note from "../Note/note.model";
66

77
async function createUser(userDetails) {
88
const user = new User(userDetails);
@@ -12,19 +12,19 @@ async function createUser(userDetails) {
1212
return {
1313
data: user,
1414
status: 200,
15-
message: 'User created',
15+
message: "User created"
1616
};
1717
} catch (err) {
18-
if (err.name === 'MongoError' && err.code === 11000) {
18+
if (err.name === "MongoError" && err.code === 11000) {
1919
return {
2020
status: 401,
21-
message: 'Email Id already registered',
21+
message: "Email Id already registered"
2222
};
2323
}
2424
return {
2525
status: 500,
26-
message: 'User not created',
27-
data: { error: err },
26+
message: "User not created",
27+
data: { error: err }
2828
};
2929
}
3030
}
@@ -38,7 +38,7 @@ async function createOrUpdate({
3838
location,
3939
bio,
4040
authParams,
41-
accessToken,
41+
accessToken
4242
}) {
4343
const userDetails = {
4444
userName,
@@ -49,60 +49,55 @@ async function createOrUpdate({
4949
location,
5050
bio,
5151
authParams,
52-
accessToken,
52+
accessToken
5353
};
5454
try {
55-
console.log('User Details -->', userName, githubId, email);
56-
5755
await User.update({ githubId }, userDetails, { upsert: true });
5856
const updatedUser = await User.findOne({ githubId });
59-
console.log('Updated User -->', updatedUser);
6057

6158
userDetails._id = updatedUser._id;
62-
userDetails.expiresIn = '7d';
59+
userDetails.expiresIn = "7d";
6360
userDetails.token = jwt.sign(userDetails, process.env.JWT_KEY);
6461

6562
return {
6663
data: userDetails,
6764
status: 200,
68-
message: 'Logged in',
65+
message: "Logged in"
6966
};
7067
} catch (err) {
71-
console.log('User upsert Error-->', err);
72-
7368
return {
7469
status: 500,
75-
message: 'User not created',
76-
data: { error: err },
70+
message: "User not created",
71+
data: { error: err }
7772
};
7873
}
7974
}
8075

8176
async function getUsers() {
8277
try {
8378
const users = await User.find({}).select(
84-
'userName name githubId email avatarUrl company location bio',
79+
"userName name githubId email avatarUrl company location bio"
8580
);
8681
const notes = await Note.find({});
8782
return {
8883
status: 200,
8984
data: {
9085
totalUsers: users.length,
9186
totalNotes: notes.length,
92-
allUsers: users,
93-
},
87+
allUsers: users
88+
}
9489
};
9590
} catch (err) {
9691
return {
9792
status: 200,
98-
message: 'Failed to fetch users',
99-
data: { error: err },
93+
message: "Failed to fetch users",
94+
data: { error: err }
10095
};
10196
}
10297
}
10398

10499
export default {
105100
createUser,
106101
createOrUpdate,
107-
getUsers,
102+
getUsers
108103
};

Diff for: src/noteBox.js

+61-55
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,44 @@
1-
import { findTimeAgo } from './helpers';
1+
import { findTimeAgo } from "./helpers";
22

33
/* eslint-disable no-underscore-dangle */
44
function createCommentBox(noteDetail) {
5-
const { userId, createdAt } = noteDetail;
6-
const { userName } = userId;
5+
const { userDetails, createdAt } = noteDetail;
6+
const { userName } = userDetails;
77
const createdTimeAgo = findTimeAgo({ date: new Date(createdAt) });
88

9-
const wrapper = document.createElement('div');
9+
const wrapper = document.createElement("div");
1010
wrapper.classList = [
11-
'timeline-comment-group js-minimizable-comment-group js-targetable-comment TimelineItem-body my-0 ',
11+
"timeline-comment-group js-minimizable-comment-group js-targetable-comment TimelineItem-body my-0 "
1212
];
1313

14-
const innerWrapper1 = document.createElement('div');
15-
innerWrapper1.classList = ['ml-n3 minimized-comment position-relative d-none '];
14+
const innerWrapper1 = document.createElement("div");
15+
innerWrapper1.classList = [
16+
"ml-n3 minimized-comment position-relative d-none "
17+
];
1618

17-
const innerWrapper2 = document.createElement('div');
19+
const innerWrapper2 = document.createElement("div");
1820
innerWrapper2.classList = [
19-
'ml-n3 timeline-comment unminimized-comment comment previewable-edit js-task-list-container editable-comment js-comment timeline-comment--caret reorderable-task-lists current-user',
21+
"ml-n3 timeline-comment unminimized-comment comment previewable-edit js-task-list-container editable-comment js-comment timeline-comment--caret reorderable-task-lists current-user"
2022
];
2123

22-
const timelineWrapper = document.createElement('div');
23-
timelineWrapper.classList = ['timeline-comment-header clearfix'];
24+
const timelineWrapper = document.createElement("div");
25+
timelineWrapper.classList = ["timeline-comment-header clearfix"];
2426

25-
const timeLineAction = document.createElement('div');
26-
timeLineAction.classList = ['timeline-comment-actions js-timeline-comment-actions'];
27+
const timeLineAction = document.createElement("div");
28+
timeLineAction.classList = [
29+
"timeline-comment-actions js-timeline-comment-actions"
30+
];
2731

28-
const privateNoteLabel = document.createElement('span');
32+
const privateNoteLabel = document.createElement("span");
2933
privateNoteLabel.classList = [
30-
'timeline-comment-label tooltipped tooltipped-multiline tooltipped-s pvt-note-label',
34+
"timeline-comment-label tooltipped tooltipped-multiline tooltipped-s pvt-note-label"
3135
];
32-
privateNoteLabel.setAttribute('aria-label', 'This is a private note');
33-
privateNoteLabel.innerText = 'Private note';
36+
privateNoteLabel.setAttribute("aria-label", "This is a private note");
37+
privateNoteLabel.innerText = "Private note";
3438
timeLineAction.append(privateNoteLabel);
35-
const timeLineActionDetails = document.createElement('details');
39+
const timeLineActionDetails = document.createElement("details");
3640
timeLineActionDetails.classList = [
37-
'details-overlay details-reset position-relative d-inline-block',
41+
"details-overlay details-reset position-relative d-inline-block"
3842
];
3943
timeLineActionDetails.innerHTML = `<summary class="btn-link timeline-comment-action" aria-haspopup="menu">
4044
<svg aria-label="Show options" class="octicon octicon-kebab-horizontal" viewBox="0 0 13 16" version="1.1" width="13" height="16" role="img"><path fill-rule="evenodd" d="M1.5 9a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3zm5 0a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3zM13 7.5a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0z"></path></svg>
@@ -46,30 +50,30 @@ function createCommentBox(noteDetail) {
4650
</details-menu>`;
4751
timeLineAction.append(timeLineActionDetails);
4852
timelineWrapper.append(timeLineAction);
49-
const timelineH3 = document.createElement('h3');
50-
timelineH3.classList = ['timeline-comment-header-text f5 text-normal'];
53+
const timelineH3 = document.createElement("h3");
54+
timelineH3.classList = ["timeline-comment-header-text f5 text-normal"];
5155
timelineH3.innerHTML = `<strong class="css-truncate expandable"><a class="author text-inherit css-truncate-target">${userName}</a></strong>`;
52-
const timestamp = document.createElement('span');
53-
timestamp.classList = ['timestamp js-timestamp'];
56+
const timestamp = document.createElement("span");
57+
timestamp.classList = ["timestamp js-timestamp"];
5458

5559
timestamp.innerHTML = ` added ${createdTimeAgo}`;
5660

5761
timelineH3.append(timestamp);
5862
timelineWrapper.append(timelineH3);
5963

6064
innerWrapper2.append(timelineWrapper);
61-
const commentBodyWrapper = document.createElement('div');
62-
commentBodyWrapper.classList = ['edit-comment-hide js-edit-comment-hide'];
63-
const taskList = document.createElement('task-lists');
64-
taskList.setAttribute('sortable', true);
65-
const table = document.createElement('table');
66-
table.classList = ['d-block'];
67-
const tbody = document.createElement('tbody');
68-
tbody.classList = ['d-block'];
69-
const tr = document.createElement('tr');
70-
tr.classList = ['d-block'];
71-
const td = document.createElement('td');
72-
td.classList = ['d-block comment-body markdown-body js-comment-body'];
65+
const commentBodyWrapper = document.createElement("div");
66+
commentBodyWrapper.classList = ["edit-comment-hide js-edit-comment-hide"];
67+
const taskList = document.createElement("task-lists");
68+
taskList.setAttribute("sortable", true);
69+
const table = document.createElement("table");
70+
table.classList = ["d-block"];
71+
const tbody = document.createElement("tbody");
72+
tbody.classList = ["d-block"];
73+
const tr = document.createElement("tr");
74+
tr.classList = ["d-block"];
75+
const td = document.createElement("td");
76+
td.classList = ["d-block comment-body markdown-body js-comment-body"];
7377
td.innerHTML = noteDetail.noteContent;
7478
tr.appendChild(td);
7579
tbody.appendChild(tr);
@@ -83,23 +87,23 @@ function createCommentBox(noteDetail) {
8387
}
8488

8589
function createAvatar({ userName, githubId, avatarUrl }) {
86-
const avatarWrapper = document.createElement('div');
87-
avatarWrapper.classList = ['avatar-parent-child TimelineItem-avatar'];
90+
const avatarWrapper = document.createElement("div");
91+
avatarWrapper.classList = ["avatar-parent-child TimelineItem-avatar"];
8892

8993
// a tag
90-
const avatarA = document.createElement('a');
94+
const avatarA = document.createElement("a");
9195
avatarA.href = `/${userName}`;
92-
avatarA.classList = ['d-inline-block'];
93-
avatarA.setAttribute('data-hovercard-type', 'user');
94-
avatarA.setAttribute('data-hovercard-url', `/hovercards?user_id=${githubId}`);
95-
avatarA.setAttribute('data-octo-click', 'hovercard-link-click');
96-
avatarA.setAttribute('data-octo-dimensions', 'link_type:self');
96+
avatarA.classList = ["d-inline-block"];
97+
avatarA.setAttribute("data-hovercard-type", "user");
98+
avatarA.setAttribute("data-hovercard-url", `/hovercards?user_id=${githubId}`);
99+
avatarA.setAttribute("data-octo-click", "hovercard-link-click");
100+
avatarA.setAttribute("data-octo-dimensions", "link_type:self");
97101

98102
// image tag
99-
const avatarImg = document.createElement('img');
100-
avatarImg.classList = ['avatar rounded-1'];
101-
avatarImg.height = '44';
102-
avatarImg.width = '44';
103+
const avatarImg = document.createElement("img");
104+
avatarImg.classList = ["avatar rounded-1"];
105+
avatarImg.height = "44";
106+
avatarImg.width = "44";
103107
avatarImg.alt = `@${userName}`;
104108
avatarImg.src = `${avatarUrl}?s=180`;
105109
avatarA.appendChild(avatarImg);
@@ -108,16 +112,18 @@ function createAvatar({ userName, githubId, avatarUrl }) {
108112
}
109113

110114
export default function createNoteBox(noteDetail) {
111-
if (!noteDetail.userId) {
112-
noteDetail.userId = {};
115+
if (!noteDetail.userDetails) {
116+
noteDetail.userDetails = {};
113117
}
114-
const { avatarUrl, githubId, userName } = noteDetail.userId;
115-
const noteNode = document.createElement('div');
116-
noteNode.classList = ['js-timeline-item js-timeline-progressive-focus-container private-note'];
117-
noteNode.setAttribute('private-id', noteDetail._id);
118+
const { avatarUrl, githubId, userName } = noteDetail.userDetails;
119+
const noteNode = document.createElement("div");
120+
noteNode.classList = [
121+
"js-timeline-item js-timeline-progressive-focus-container private-note"
122+
];
123+
noteNode.setAttribute("private-id", noteDetail._id);
118124

119-
const noteWrapper = document.createElement('div');
120-
noteWrapper.classList = ['TimelineItem js-comment-container'];
125+
const noteWrapper = document.createElement("div");
126+
noteWrapper.classList = ["TimelineItem js-comment-container"];
121127
noteWrapper.appendChild(createAvatar({ userName, githubId, avatarUrl }));
122128
noteWrapper.appendChild(createCommentBox(noteDetail));
123129

0 commit comments

Comments
 (0)