Skip to content

Commit

Permalink
Merge pull request #5 from DemocraciaEnRed/1.3.0
Browse files Browse the repository at this point in the history
Ready for 1.3.0
  • Loading branch information
guillecro authored Jul 8, 2019
2 parents af209ee + 4c97edf commit 4ea031f
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 25 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# CHANGELOG

### 1.3.0

* Se hizo un FIX donde el link a la propuesta se parseaba como `Object` y habia que sacar el `id`
* DERLA-58 Se agrego el feature de que se envia un correo al diputado al recibir un comentario tanto en fundamentacion como contextual.
* Se hizo un FIX en el saludo, donde a veces no existia nombre a quien

Compatible con:
* `leyesabiertas-core:1.3.0`
* `leyesabiertas-web:1.3.0`

#### 1.1.2
- Fixing sending of comments notifications
- Fixing sending of closing topics
Expand Down
81 changes: 81 additions & 0 deletions api/controllers/comment-new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const { INTERNAL_SERVER_ERROR, OK } = require('http-status')
const { ObjectID } = require('mongodb')
const mongo = require('../../services/db')
const notification = require('../../core/notification-strategies')

exports.post = async (req, res) => {
try {
const { type, comment } = req.body
let commentInfo = await mongo.getDB().collection('comments').aggregate([
{ $match: { _id: ObjectID(comment) } },
{
$lookup: {
from: 'users',
localField: 'user',
foreignField: '_id',
as: 'user'
}
},
{
$lookup: {
from: 'documentversions',
localField: 'version',
foreignField: '_id',
as: 'version'
}
},
{
$lookup: {
from: 'documents',
localField: 'document',
foreignField: '_id',
as: 'document'
}
},
{
$lookup: {
from: 'users',
localField: 'document.author',
foreignField: '_id',
as: 'author'
}
},
{ $project: {
'user.avatar': 0,
'author.avatar': 0,
'version.content.fundation': 0,
'version.content.articles': 0,
'decoration': 0
}
}
]).toArray()
// Prepare email props
let emailProps = {
author: {
id: commentInfo[0].user[0]._id,
name: commentInfo[0].user[0].name,
fullname: commentInfo[0].user[0].fullname,
email: commentInfo[0].user[0].email
},
document: {
id: commentInfo[0].document[0]._id,
title: commentInfo[0].version[0].content.title
},
comment: {
content: commentInfo[0].content
},
reply: commentInfo[0].reply || null,
authorDocument: commentInfo[0].author[0].email
}
// Send notification
notification.sendEmail(type, emailProps)
res.status(OK).json({
message: 'Email scheduled'
})
} catch (err) {
res.status(INTERNAL_SERVER_ERROR).json({
message: 'An error ocurred trying to schedule the email.',
reason: err.message
})
}
}
2 changes: 1 addition & 1 deletion api/controllers/send-email.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ exports.post = async (req, res) => {
email: commentInfo[0].user[0].email
},
document: {
id: commentInfo[0].document,
id: commentInfo[0].document[0]._id,
title: commentInfo[0].version[0].content.title
},
comment: {
Expand Down
7 changes: 7 additions & 0 deletions api/routes/comment-new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const express = require('express')
const router = express.Router()
const CommentNew = require('../controllers/comment-new')

router.post('/', CommentNew.post)

module.exports = router
53 changes: 32 additions & 21 deletions api/routes/dev-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,38 +40,49 @@ router.post('/test', async (req, res, next) => {
},
{
$lookup: {
from: 'documentversions',
localField: 'version',
from: 'documents',
localField: 'document',
foreignField: '_id',
as: 'version'
as: 'document'
}
},
{
$lookup: {
from: 'users',
localField: 'document.author',
foreignField: '_id',
as: 'author'
}
},
{ $project: {
'user.avatar': 0,
'author.avatar': 0,
'version.content.fundation': 0,
'version.content.articles': 0,
'decoration': 0
}
}
]).toArray()
let emailProps = {
author: {
id: commentInfo[0].user[0]._id,
name: commentInfo[0].user[0].name,
fullname: commentInfo[0].user[0].fullname,
email: commentInfo[0].user[0].email
},
document: {
id: commentInfo[0].document,
title: commentInfo[0].version[0].content.title
},
comment: {
content: commentInfo[0].content
},
reply: commentInfo[0].reply || null
}
const template = buildTemplate(type, emailProps)
res.send(template)
// console.log(commentInfo[0].document[0])
// let emailProps = {
// author: {
// id: commentInfo[0].user[0]._id,
// name: commentInfo[0].user[0].name,
// fullname: commentInfo[0].user[0].fullname,
// email: commentInfo[0].user[0].email
// },
// document: {
// id: commentInfo[0].document,
// title: commentInfo[0].document[0].author
// },
// comment: {
// content: commentInfo[0].content
// },
// reply: commentInfo[0].reply || null
// }
res.send(commentInfo[0])
// const template = buildTemplate(type, emailProps)
// res.send(template)
} catch (err) {
res.status(INTERNAL_SERVER_ERROR).json({
message: 'An error ocurred',
Expand Down
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require('dotenv').config()

const sendEmailRoutes = require('./api/routes/send-email')
const setCloseEvent = require('./api/routes/document-closes')
const setNewCommentEvent = require('./api/routes/comment-new')

const { NODE_ENV } = process.env

Expand All @@ -27,6 +28,7 @@ app.use((req, res, next) => {

app.use('/api/send-email', sendEmailRoutes)
app.use('/api/set-document-closes', setCloseEvent)
app.use('/api/comment-new', setNewCommentEvent)

if (NODE_ENV === 'development') app.use('/views', require('./api/routes/dev-view'))

Expand Down
6 changes: 6 additions & 0 deletions core/notification-strategies.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const buildTemplate = (fileName, props) => {
return reactTemplate({ ...props })
}

const commentNew = (info) => {
const template = buildTemplate('comment-new', info)
execute(info.authorDocument, '¡Comentario nuevo!', template)
}

const commentResolved = (info) => {
const template = buildTemplate('comment-resolved', info)
execute(info.author.email, '¡Comentario resuelto!', template)
Expand All @@ -40,6 +45,7 @@ const commentContribution = (info) => {
}

const strategies = [
['comment-new', commentNew],
['comment-resolved', commentResolved],
['comment-liked', commentLiked],
['comment-replied', commentReplied],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "democracyos-api-notifier",
"version": "1.0.0",
"version": "1.3.0",
"description": "API Notifications",
"main": "server.js",
"scripts": {
Expand Down
55 changes: 55 additions & 0 deletions templates/comment-new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const React = require('react')
const { Email, Item, Span, A, renderEmail, Box, Image } = require('react-html-email')
const MailHeader = require('./header')
const MailFooter = require('./footer')
const Content = require('./content')
const Style = require('./styles')
const CommentContainerStyle = require('./commentContainerStyle')
const { ORGANIZATION_NAME, ORGANIZATION_URL, ORGANIZATION_API_URL } = process.env

const CommentRead = (props) => {
return (
<Email title='Comentario resuelto' style={{ width: '100%', maxWidth: '700px' }}>
<MailHeader />
<Content name={props.author.name} style={{ width: '100%' }}>
<Item style={Style.itemStyle}>
<Span {...Style.defaultContentStyle}>
Hay un nuevo comentario en el proyecto <b>{props.document.title}</b> en <A href={`${ORGANIZATION_URL}/propuesta?id=${props.document.id.toString()}`}>{ORGANIZATION_NAME}</A>
</Span>
</Item>
<Item style={Style.itemStyle}>
<Span {...Style.defaultContentStyle}>
Este fue el comentario:
</Span>
</Item>
<Item style={Style.itemStyle}>
<A href={`${ORGANIZATION_URL}/propuesta?id=${props.document.id}`} textDecoration='none'>
<Box align='center' style={CommentContainerStyle.cardStyle}>
<Item>
<div style={CommentContainerStyle.leftColumn} >
<Image src={`${ORGANIZATION_URL}/static/assets/emails/check.png`} style={CommentContainerStyle.cardIconImg} />
</div>
<div style={CommentContainerStyle.cardContentStyle}>
<div style={CommentContainerStyle.userContainerStyle}>
<Span {...CommentContainerStyle.userNameStyle}>
<Image src={`${ORGANIZATION_API_URL}/api/v1/users/${props.author.id}`} style={CommentContainerStyle.userAvatarStyle} />
{props.author.fullname}
</Span>
</div>
<div>
<Span {...CommentContainerStyle.theCommentStyle}>
{props.comment.content}
</Span>
</div>
</div>
</Item>
</Box>
</A>
</Item>
</Content>
<MailFooter />
</Email>
)
}

module.exports = (props) => renderEmail(<CommentRead {...props} />)
4 changes: 2 additions & 2 deletions templates/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ const Content = (props) => {
props.showName === undefined
? <Item style={Styles.itemStyle}>
<Span {...Styles.defaultContentStyle}>
<b>Hola {props.name}</b>,
<b>Hola</b>,
</Span>
</Item>
: props.showName && <Item style={Styles.itemStyle}>
<Span {...Styles.defaultContentStyle}>
<b>Hola {props.name}</b>,
<b>Hola</b>,
</Span>
</Item>
}
Expand Down

0 comments on commit 4ea031f

Please sign in to comment.