diff --git a/config/_development.json b/config/_development.json index 62aff48..a0739d1 100644 --- a/config/_development.json +++ b/config/_development.json @@ -1,7 +1,26 @@ { - "cron": {"jobs": []}, + "cron": { + "secret": "VLgL7EJZ4yXPAjce", + "jobs": [ + { + "name": "daily-summary", + "config": "0 0 * * * ?", + "url": "/services/jobs/daily-summary" + } + ] + }, "settings": {"public": {}}, - "smtp": {}, + "smtp": { + "default": { + "enabled": true, + "host": "serua.veks.net", + "port": 465, + "ssl": true, + "from": "academy-test@netuno.org", + "username": "academy-test@netuno.org", + "password": "gDj1pwQ5T#0#" + } + }, "name": "social_ma", "setup": { "schema": { diff --git a/dbs/social_ma.mv.db b/dbs/social_ma.mv.db index 705181f..20d98f2 100644 Binary files a/dbs/social_ma.mv.db and b/dbs/social_ma.mv.db differ diff --git a/server/services/jobs/daily-summary.js b/server/services/jobs/daily-summary.js new file mode 100644 index 0000000..7257018 --- /dev/null +++ b/server/services/jobs/daily-summary.js @@ -0,0 +1,61 @@ + +const dbComunidades = _db.query(` + SELECT + id, + nome + FROM comunidade + WHERE active = true + ORDER BY nome +`) +for (const dbComunidade of dbComunidades) { + const dbPublicacoes = _db.queryFirst(` + SELECT + count(id) AS total + FROM publicacao + WHERE momento > ? + AND comunidade_id = ? + `, _time.localDateTime().minusDays(1), dbComunidade.getInt("id")) + + if (dbPublicacoes.getInt("total") <= 0) { + continue; + } + + const dbPessoas = _db.query(` + SELECT + nome, email + FROM pessoa + INNER JOIN pessoa_comunidade ON pessoa.id = pessoa_comunidade.pessoa_id + WHERE + pessoa_comunidade.comunidade_id = ? + AND pessoa.active = true + `, dbComunidade.getInt("id")) + + for (const dbPessoa of dbPessoas) { + + const smtp = _smtp.init() + + smtp.to = dbPessoa.getString("email") + + smtp.subject = `Plataforma Social - ${dbComunidade.getString("nome")}` + + smtp.text = ` + Olá ${dbPessoa.getString("nome")}, + + Hoje houve ${dbPublicacoes.getInt("total")} novas publicações. + ` + + smtp.html = _template.getOutput( + "emails/daily-summary", + _val.map() + .set("comunidade", dbComunidade.getString("nome")) + .set("pessoa", dbPessoa.getString("nome")) + .set("total", dbPublicacoes.getInt("total")) + ) + + smtp.send() + + _log.info("Pessoa:", dbPessoa) + } +} + +_log.info("Cron Daily Summary E-mail Enviado") diff --git a/server/templates/emails/daily-summary.html b/server/templates/emails/daily-summary.html new file mode 100644 index 0000000..895407a --- /dev/null +++ b/server/templates/emails/daily-summary.html @@ -0,0 +1,7 @@ +
Olá _{req=pessoa},
+ Hoje houve _{data=total} novas publicações. +