Skip to content

Commit

Permalink
Merge pull request #24 from udistrital/release/0.0.1
Browse files Browse the repository at this point in the history
Fix: Correccion de modelos y filtro cors
  • Loading branch information
a52290451 committed Mar 8, 2022
2 parents 764ebd4 + 1a22cf6 commit 4063871
Show file tree
Hide file tree
Showing 14 changed files with 469 additions and 119 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# notificacion_mid
Servicio para envío de notificaciónes por difusión en AWS SNS
Servicio para envío de notificaciónes por difusión en AWS SNS.

Para instrucciones de implementación del sistema de notificaciones consulta la [documentacion](https://drive.google.com/file/d/1ZlXdKAyooyftamyCZX8Btmd5nhDR5YEX/view?usp=drivesdk)

## Especificaciones Técnicas

Expand Down
83 changes: 73 additions & 10 deletions controllers/colas.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package controllers

import (
"encoding/json"
"errors"
"strconv"
"strings"

"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
Expand Down Expand Up @@ -76,7 +78,7 @@ func (c *ColasController) RecibirMensajes() {
if err := recover(); err != nil {
logs.Error(err)
localError := err.(map[string]interface{})
c.Data["message"] = (beego.AppConfig.String("appname") + "/GetTopics/" + (localError["funcion"]).(string))
c.Data["message"] = (beego.AppConfig.String("appname") + "/RecibirMensaje/" + (localError["funcion"]).(string))
c.Data["data"] = (localError["err"])
if status, ok := localError["status"]; ok {
c.Abort(status.(string))
Expand All @@ -97,8 +99,8 @@ func (c *ColasController) RecibirMensajes() {
}

numMaxStr := c.GetString("numMax")
if tiempoOcultoStr == "" {
tiempoOcultoStr = "1"
if numMaxStr == "" {
numMaxStr = "1"
}

numMax, err := strconv.Atoi(numMaxStr)
Expand All @@ -115,14 +117,76 @@ func (c *ColasController) RecibirMensajes() {
c.ServeJSON()
}

// EsperarMensajes ...
// @Title EsperarMensajes
// @Description Espera por un tiempo determinado a que los mensajes estén disponibles y devuelve los recibidos en ese intervalo de tiempo
// @Param nombre query string true "Nombre de la cola"
// @Param tiempoEspera query int true "Tiempo de espera del api por mensajes"
// @Param cantidad query int false "Cantidad máxima de mensajes a recibir. Esta cantidad debe ser menor a diez veces el tiempo de espera, ya que se pueden obtener máximo 10 mensajes por segundo. Por defecto, se recibirán todos"
// @Param filtro query string false "Recepción de mensajes filtrados por metadata. Tiene el funcionamiento de un and, por lo tanto sólo devuelve los valores que cumplan con todo el filtro"
// @Success 201 {object} models.Mensaje
// @Failure 400 Error en parametros ingresados
// @router /mensajes/espera [get]
func (c *ColasController) EsperarMensajes() {
filtro := make(map[string]string)

defer func() {
if err := recover(); err != nil {
logs.Error(err)
localError := err.(map[string]interface{})
c.Data["message"] = (beego.AppConfig.String("appname") + "/EsperarMensajes/" + (localError["funcion"]).(string))
c.Data["data"] = (localError["err"])
if status, ok := localError["status"]; ok {
c.Abort(status.(string))
} else {
c.Abort("404")
}
}
}()

tiempoEsperaStr := c.GetString("tiempoEspera")
cantidadStr := c.GetString("cantidad")

if v := c.GetString("filtro"); v != "" {
for _, cond := range strings.Split(v, ",") {
kv := strings.SplitN(cond, ":", 2)
if len(kv) != 2 {
c.Data["json"] = errors.New("error: atributos invalidos")
c.ServeJSON()
return
}
k, v := kv[0], kv[1]
filtro[k] = v
}
}

tiempoEspera, err := strconv.Atoi(tiempoEsperaStr)
if err != nil {
panic(map[string]interface{}{"funcion": "EsperarMensajes", "err": "Error en parámetros de ingresos", "status": "400"})
}

cantidad, err := strconv.Atoi(cantidadStr)
if (err != nil && cantidadStr != "") || cantidad > tiempoEspera*10 {
panic(map[string]interface{}{"funcion": "EsperarMensajes", "err": "Error en parámetros de ingresos", "status": "400"})
}

if respuesta, err := helpers.EsperarMensajes(c.GetString("nombre"), tiempoEspera, cantidad, filtro); err == nil {
c.Ctx.Output.SetStatus(200)
c.Data["json"] = map[string]interface{}{"Success": true, "Status": "200", "Message": "Successful", "Data": respuesta}
} else {
panic(err)
}
c.ServeJSON()
}

// BorrarMensaje ...
// @Title BorrarMensaje
// @Description Borra la notificación de la cola
// @Param cola path string true "Nombre de la cola en donde está el mensaje"
// @Param mensaje body models.Mensaje true "Mensaje a borrar"
// @Success 200 {string} Mensaje eliminado
// @Failure 404 not found resource
// @router /mensajes/:cola [delete]
// @router /mensajes/:cola [post]
func (c *ColasController) BorrarMensaje() {
colaStr := c.Ctx.Input.Param(":cola")
var mensaje models.Mensaje
Expand All @@ -145,9 +209,9 @@ func (c *ColasController) BorrarMensaje() {
panic(map[string]interface{}{"funcion": "BorrarMensaje", "err": "Error en parámetros de ingresos", "status": "400"})
}

if err := helpers.BorrarMensaje(colaStr, mensaje); err == nil {
if conteo, err := helpers.BorrarMensaje(colaStr, mensaje); err == nil {
c.Ctx.Output.SetStatus(200)
c.Data["json"] = map[string]interface{}{"Success": true, "Status": "200", "Message": "Successful", "Data": "Mensaje eliminado"}
c.Data["json"] = map[string]interface{}{"Success": true, "Status": "200", "Message": "Successful", "Data": map[string]interface{}{"MensajesEliminados": conteo}}
} else {
panic(err)
}
Expand All @@ -160,7 +224,7 @@ func (c *ColasController) BorrarMensaje() {
// @Param filtro body models.Filtro true "Filtro de los mensajes a borrar"
// @Success 200 {string} Mensaje eliminado
// @Failure 404 not found resource
// @router /mensajes [delete]
// @router /mensajes [post]
func (c *ColasController) BorrarMensajeFiltro() {
var filtro models.Filtro

Expand All @@ -179,14 +243,13 @@ func (c *ColasController) BorrarMensajeFiltro() {
}()

json.Unmarshal(c.Ctx.Input.RequestBody, &filtro)

if filtro.NombreCola == "" {
panic(map[string]interface{}{"funcion": "BorrarMensajeFiltro", "err": "Error en parámetros de ingresos", "status": "400"})
}

if err := helpers.BorrarMensajeFiltro(filtro); err == nil {
if conteo, err := helpers.BorrarMensajeFiltro(filtro); err == nil {
c.Ctx.Output.SetStatus(200)
c.Data["json"] = map[string]interface{}{"Success": true, "Status": "200", "Message": "Successful", "Data": "Mensajes eliminados"}
c.Data["json"] = map[string]interface{}{"Success": true, "Status": "200", "Message": "Successful", "Data": map[string]interface{}{"MensajesEliminados": conteo}}
} else {
panic(err)
}
Expand Down
7 changes: 5 additions & 2 deletions controllers/notificaciones.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *NotificacionController) PostOneNotif() {
}()

json.Unmarshal(c.Ctx.Input.RequestBody, &notif)
if notif.RemitenteId == "" || len(notif.DestinatarioId) == 0 || notif.Asunto == "" || notif.Mensaje == "" {
if notif.RemitenteId == "" || (len(notif.DestinatarioId) == 0 && len(notif.Atributos) == 0) || notif.Asunto == "" || notif.Mensaje == "" || notif.ArnTopic == "" {
panic(map[string]interface{}{"funcion": "PostOneNotif", "err": "Error en parámetros de ingresos", "status": "400"})
}

Expand Down Expand Up @@ -94,7 +94,10 @@ func (c *NotificacionController) Subscribe() {
for _, subscriptor := range sub.Suscritos {
prot := subscriptor.Protocolo
if prot != "kinesis" && prot != "lambda" && prot != "sqs" && prot != "email" && prot != "email-json" && prot != "http" && prot != "https" && prot != "application" && prot != "sms" && prot != "firehouse" {
panic(map[string]interface{}{"funcion": "PostOneNotif", "err": "Protocolo invalido", "status": "400"})
panic(map[string]interface{}{"funcion": "Subscribe", "err": "Protocolo invalido", "status": "400"})
}
if subscriptor.Id == "" && len(subscriptor.Atributos) == 0 {
panic(map[string]interface{}{"funcion": "Subscribe", "err": "Mensaje sin destino", "status": "400"})
}
}

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
require (
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.0.0 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
)

require (
Expand All @@ -35,6 +36,7 @@ require (
github.com/prometheus/procfs v0.6.0 // indirect
github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/udistrital/auditoria v0.0.0-20200115201815-9680ae9c2515
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420 // indirect
golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ github.com/openzipkin/zipkin-go v0.2.1/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnh
github.com/openzipkin/zipkin-go v0.2.2/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnhQw8ySjnjRyN4=
github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/pelletier/go-toml v1.0.1/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
Expand Down Expand Up @@ -347,6 +349,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/syndtr/goleveldb v0.0.0-20160425020131-cfa635847112/go.mod h1:Z4AUp2Km+PwemOoO/VB5AOx9XSsIItzFjoJlOSiYmn0=
github.com/syndtr/goleveldb v0.0.0-20181127023241-353a9fca669c/go.mod h1:Z4AUp2Km+PwemOoO/VB5AOx9XSsIItzFjoJlOSiYmn0=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/udistrital/auditoria v0.0.0-20200115201815-9680ae9c2515 h1:HTZDLLQ5QiGANHPjsz2rtNoryMncPISVrQ4DIjveWI4=
github.com/udistrital/auditoria v0.0.0-20200115201815-9680ae9c2515/go.mod h1:2g8J932brTzbNOcSHob/vjt+/GFfMQuRreLZhvGiUro=
github.com/udistrital/utils_oas v0.0.0-20211011160436-7fa8127363aa h1:nzKR3AWWkX5b1A1hzNNHKLDnugkNWH8ll/Mb6mbdUMs=
github.com/udistrital/utils_oas v0.0.0-20211011160436-7fa8127363aa/go.mod h1:ctr3PgpZazF32ukxlLZP3AVjq8b2zaZ4beG4cqkx5ZA=
github.com/ugorji/go v0.0.0-20171122102828-84cb69a8af83/go.mod h1:hnLbHMwcvSihnDhEfx2/BzKp2xb0Y+ErdfYcrs9tkJQ=
Expand Down
Loading

0 comments on commit 4063871

Please sign in to comment.