Skip to content

Commit

Permalink
post render bug and part of async-labs#17
Browse files Browse the repository at this point in the history
  • Loading branch information
tima101 committed Apr 30, 2019
1 parent 865fd8f commit cf9c266
Show file tree
Hide file tree
Showing 14 changed files with 967 additions and 31 deletions.
2 changes: 2 additions & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"dependencies": {
"@types/mongoose": "5.3.23",
"@types/node": "11.11.6",
"@types/socket.io": "^2.1.2",
"aws-sdk": "2.428.0",
"compression": "1.7.4",
"connect-mongo": "2.0.3",
Expand All @@ -46,6 +47,7 @@
"passport-google-oauth": "2.0.0",
"qs": "6.7.0",
"request": "2.88.0",
"socket.io": "^2.2.0",
"stripe": "6.28.0",
"typescript": "3.3.4000",
"winston": "3.2.1"
Expand Down
39 changes: 30 additions & 9 deletions api/server/api/team-member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import Post from '../models/Post';
import Team from '../models/Team';
import User from '../models/User';

import {
discussionAdded,
discussionDeleted,
discussionEdited,
postAdded,
postDeleted,
postEdited,
} from '../realtime';

const router = express.Router();

router.use((req, res, next) => {
Expand Down Expand Up @@ -105,7 +114,7 @@ router.get('/teams', async (req, res, next) => {

router.post('/discussions/add', async (req, res, next) => {
try {
const { name, teamId, memberIds = [], notificationType } = req.body;
const { name, teamId, memberIds = [], notificationType, socketId } = req.body;

const discussion = await Discussion.add({
userId: req.user.id,
Expand All @@ -115,6 +124,8 @@ router.post('/discussions/add', async (req, res, next) => {
notificationType,
});

discussionAdded({ socketId, discussion });

res.json({ discussion });
} catch (err) {
next(err);
Expand All @@ -123,16 +134,18 @@ router.post('/discussions/add', async (req, res, next) => {

router.post('/discussions/edit', async (req, res, next) => {
try {
const { name, id, memberIds = [], notificationType } = req.body;
const { name, id, memberIds = [], notificationType, socketId } = req.body;

await Discussion.edit({
const updatedDiscussion = await Discussion.edit({
userId: req.user.id,
name,
id,
memberIds,
notificationType,
});

discussionEdited({ socketId, discussion: updatedDiscussion });

res.json({ done: 1 });
} catch (err) {
next(err);
Expand All @@ -141,9 +154,11 @@ router.post('/discussions/edit', async (req, res, next) => {

router.post('/discussions/delete', async (req, res, next) => {
try {
const { id } = req.body;
const { id, socketId } = req.body;

const { teamId } = await Discussion.delete({ userId: req.user.id, id });

await Discussion.delete({ userId: req.user.id, id });
discussionDeleted({ socketId, teamId, id });

res.json({ done: 1 });
} catch (err) {
Expand All @@ -170,10 +185,12 @@ router.get('/discussions/list', async (req, res, next) => {

router.post('/posts/add', async (req, res, next) => {
try {
const { content, discussionId } = req.body;
const { content, discussionId, socketId } = req.body;

const post = await Post.add({ userId: req.user.id, content, discussionId });

postAdded({ socketId, post });

res.json({ post });
} catch (err) {
next(err);
Expand All @@ -182,9 +199,11 @@ router.post('/posts/add', async (req, res, next) => {

router.post('/posts/edit', async (req, res, next) => {
try {
const { content, id } = req.body;
const { content, id, socketId } = req.body;

const updatedPost = await Post.edit({ userId: req.user.id, content, id });

await Post.edit({ userId: req.user.id, content, id });
postEdited({ socketId, post: updatedPost });

res.json({ done: 1 });
} catch (err) {
Expand All @@ -194,10 +213,12 @@ router.post('/posts/edit', async (req, res, next) => {

router.post('/posts/delete', async (req, res, next) => {
try {
const { id } = req.body;
const { id, socketId, discussionId } = req.body;

await Post.delete({ userId: req.user.id, id });

postDeleted({ socketId, id, discussionId });

res.json({ done: 1 });
} catch (err) {
next(err);
Expand Down
5 changes: 5 additions & 0 deletions api/server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import * as dotenv from 'dotenv';
import * as express from 'express';
import * as session from 'express-session';
import * as helmet from 'helmet';
import * as httpModule from 'http';
import * as mongoose from 'mongoose';
import * as path from 'path';

import api from './api';
import { signRequestForLoad } from './aws-s3';
import auth from './google';
import { setup as realtime } from './realtime';
import { stripeWebHooks } from './stripe';

import logger from './logs';
Expand Down Expand Up @@ -77,6 +79,9 @@ server.use(sessionMiddleware);
auth({ server, ROOT_URL });
api(server);

const http = new httpModule.Server(server);
realtime({ http, origin: PRODUCTION_URL_APP, sessionMiddleware });

server.get('/uploaded-file', async (req, res) => {
if (!req.user) {
res.redirect(dev ? 'http://localhost:3000/login' : `${PRODUCTION_URL_APP}/login`);
Expand Down
7 changes: 4 additions & 3 deletions api/server/models/Discussion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ interface IDiscussionModel extends mongoose.Model<IDiscussionDocument> {
name: string;
memberIds: string[];
notificationType: string;
}): Promise<{ teamId: string }>;
}): Promise<IDiscussionDocument>;

delete({ userId, id }: { userId: string; id: string }): Promise<{ teamId: string }>;
}
Expand Down Expand Up @@ -160,16 +160,17 @@ class DiscussionClass extends mongoose.Model {
throw new Error('Permission denied. Only author or team leader can edit Discussion.');
}

await this.updateOne(
const updatedObj = await this.findOneAndUpdate(
{ _id: id },
{
name,
memberIds: uniq([userId, ...memberIds]),
notificationType,
},
{ runValidators: true, new: true },
);

return { teamId: discussion.teamId };
return updatedObj;
}

public static async delete({ userId, id }) {
Expand Down
7 changes: 4 additions & 3 deletions api/server/models/Post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ interface IPostModel extends mongoose.Model<IPostDocument> {
content: string;
userId: string;
id: string;
}): Promise<{ discussionId: string; htmlContent: string }>;
}): Promise<IPostDocument>;

uploadFile({
userId,
Expand Down Expand Up @@ -199,12 +199,13 @@ class PostClass extends mongoose.Model {

const htmlContent = markdownToHtml(content);

await this.updateOne(
const updatedObj = await this.findOneAndUpdate(
{ _id: id },
{ content, htmlContent, isEdited: true, lastUpdatedAt: new Date() },
{ runValidators: true, new: true },
);

return { discussionId: post.discussionId, htmlContent };
return updatedObj;
}

public static async delete({ userId, id }) {
Expand Down
Loading

0 comments on commit cf9c266

Please sign in to comment.