Example #1
0
 def post(self, id, accion):
     n = NotificacionTemplateModel.find_by_id(id)
     if not n:
         return {"message": "No se encontro la notificación"}, 404
     response_envio = NotificacionTemplateModel.send_to_participantes(n)
     if response_envio["status"] == 404:
         return {"message": "No se pudo crear o enviar la notificacion."}, 404
     if accion == 'ninguna':
             return {"message": "Notificacion reenviada con éxito.",
                 "Número de destinatarios:": response_envio["total"]}
     elif accion == 'premio':
         p = PremioModel.find_by_id(n.link)
         if not p:
             return {"message": "No se encontro el premio"}, 404
         response_envio = PremioModel.send_to_participantes(n)
         if response_envio["status"] == 404:
             return {"message": "No se pudo crear o enviar la notificacion."}, 404
         return {"message": "Notificacion reenviada con éxito.",
             "Número de destinatarios:": response_envio["total"]}
     elif accion == 'encuesta':
         n = NotificacionTemplateModel.find_by_id(id)
         pprint(n)
         if not n:
             return {"message": "No se encontro la notificacion"}, 404
         en = EncuestaModel.find_by_id(n.link)
         if not en:
             return {"message": "No se encontro la encuesta"}, 404
         response_envio = EncuestaModel.send_to_participantes(n)
         if response_envio["status"] == 404:
             return {"message": "No se pudo crear o enviar la notificacion."}, 404
         return {"message": "Notificacion reenviada con éxito.",
             "Número de destinatarios:": response_envio["total"]}
Example #2
0
 def delete(self, id, accion):
     if accion == 'ninguna':
         n = NotificacionTemplateModel.find_by_id(id)
         if not n:
             return {"message": "No se encontro la notificación"}, 404
         try:
             for p in NotificacionModel.objects.raw({"id_notificacion": n._id}):
                 p.delete()
             n.delete()
         except:
             return {"message": "No se pudo efectuar esta operación"},404 
         return {"message": "Notificacion eliminada"}, 200
     elif accion == 'premio':
         n = NotificacionTemplateModel.find_by_id(id)
         pprint(n)
         if not n:
             return {"message": "No se encontro la notificacion"}, 404
         if n.link != "null" and n.link:
             p = PremioModel.find_by_id(n.link)
             if not p:
                 return {"message": "No se encontro el premio"}, 404
         else: 
             p = None
         try:
             for np in NotificacionModel.objects.raw({"id_notificacion": n._id}):
                 np.delete()
             if n.link != "null" and n.link:
                 for pp in PremioParticipanteModel.objects.raw({"id_premio": p._id}):
                     pp.delete()
                 p.delete()
             n.delete()
         except:
             return {"message": "No se pudo efectuar esta operación"},404 
         return {"message": "Notificación y premio eliminados"}, 200               
     elif accion == 'encuesta':
         n = NotificacionTemplateModel.find_by_id(id)
         pprint(n)
         if not n:
             return {"message": "No se encontro la notificacion"}, 404
         en = EncuestaModel.find_by_id(n.link)
         if not en:
             return {"message": "No se encontro la encuesta"}, 404
         try:
             for np in NotificacionModel.objects.raw({"id_notificacion": n._id}):
                 np.delete()
             for ep in ParticipantesEncuestaModel.objects.raw({"id_encuesta": en._id}):
                 ep.delete()
             n.delete()
             en.delete()
         except:
             return {"message": "No se pudo efectuar esta operación"},404 
         return {"message": "Notificación y encuesta eliminados"}, 200   
Example #3
0
 def get(self, id):
     print("Admin")
     n = NotificacionTemplateModel.find_by_id(id)
     if not n:
         print("no se encontro")
         return {"message": "No se encontro el la notificación!"}, 404
     return NotificacionTemplateSchema(
         only=(
         "_id",
         "titulo",
         "mensaje",
         "fecha",
         "imagenIcon",
         "bar_text",
         "tipo_notificacion",
         "link",
         "filtros"
         )).dump(n), 200
Example #4
0
 def patch(self, id):
     n = NotificacionTemplateModel.find_by_id(id)
     if not n:
         return {"message": "No se encontro la notificación!"}, 404
     noti_json = request.get_json()
     # print(user_json)
     noti = not_schema_template.load(noti_json)
     try:
         if "tipo_notificacion" in noti:
             n.tipo_notificacion=noti["tipo_notificacion"]
         if "imagenIcon" in noti:
             n.imagenIcon=noti["imagenIcon"]
         if "imagenDisplay" in noti:
             n.imagenDisplay=noti["imagenDisplay"]
         if "titulo" in noti:
             n.titulo=noti["titulo"]
         if "fecha" in noti:
             n.fecha=noti["fecha"]
         if "bar_text" in noti:
             n.bar_text=noti["bar_text"]
         if "mensaje" in noti:
             n.mensaje=noti["mensaje"]
         if "link" in noti:
             n.link=noti["link"]
         n.save()
     except ValidationError as exc:
         print(exc.message)
         return {"message": "No se pudo actualizar la notificación."}, 404
     return NotificacionTemplateSchema(
         only=(
         "_id",
         "titulo",
         "mensaje",
         "fecha",
         "imagenIcon",
         "imagenDisplay",
         "bar_text",
         "tipo_notificacion",
         "link",
         )).dump(n), 200
Example #5
0
 def post(self, id, accion):
     if accion == 'ninguna':
         n = NotificacionTemplateModel.find_by_id(id)
         if not n:
             return {"message": "No se encontro la notificación"}, 404
         try:
             filtersObids=[]
             if not "filtros" in n:
                 return {"message": "Error: Sin destinatarios, debe haber al menos un participante a quien enviarle esta acción"}
             for fil in n["filtros"]:
                 filtersObids.append(ObjectId(fil))
             # Enviar a todos los participantes
             for p in ParticipanteModel.objects.raw({"_id": { "$in": filtersObids}}):
                 # part_id = ObjectId(id)
                 notif = NotificacionModel(
                 id_participante=p._id,
                 id_notificacion=n._id,
                 estado=0,
                 # Estado puede servir para actualizar tambien OJO! ahora esta fijo, pero podrías ser variable
                 ).save()            
                 # PYMODM no tiene soporte transaccional, en un futuro migrar a PYMONGO, que sí tiene soporte
         # return {"message": "Notificacion guardada con éxito."}
         except ValidationError as exc:
             print(exc.message)
             return {"message": "No se pudo crear o enviar la notificacion."}, 404
         return {"message": "Notificacion reenviada con éxito.",
                 "Número de destinatarios:": len(filtersObids)}
     elif accion == 'premio':
         n = NotificacionTemplateModel.find_by_id(id)
         pprint(n)
         if not n:
             return {"message": "No se encontro el premio"}, 404
         p = PremioModel.find_by_id(n.link)
         if not p:
             return {"message": "No se encontro el premio"}, 404
         try:
             filtersObids=[]
             if not "filtros" in n:
                 return {"message": "Error: Sin destinatarios, debe haber al menos un participante a quien enviarle esta acción"}
             for fil in n["filtros"]:
                 filtersObids.append(ObjectId(fil))
             # Enviar a todos los participantes
             for par in ParticipanteModel.objects.raw({"_id": { "$in": filtersObids}}):
                 # part_id = ObjectId(id)
                 notif = PremioParticipanteModel(
                 id_participante=par._id,
                 # id_notificacion=n._id,
                 fecha_creacion=dt.datetime.now(),
                 estado=0,
                 # Estado puede servir para actualizar tambien OJO! ahora esta fijo, pero podrías ser variable
                 ).save()        
             filtersObids=[]
             if not "filtros" in n:
                 return {"message": "Error: Sin destinatarios, debe haber al menos un participante a quien enviarle esta acción"}
             for fil in n["filtros"]:
                 filtersObids.append(ObjectId(fil))
             # Enviar a todos los participantes
             for p in ParticipanteModel.objects.raw({"_id": { "$in": filtersObids}}):
                 # part_id = ObjectId(id)
                 notif = NotificacionModel(
                 id_participante=p._id,
                 id_notificacion=n._id,
                 estado=0,
                 # Estado puede servir para actualizar tambien OJO! ahora esta fijo, pero podrías ser variable
             ).save()            
             # PYMODM no tiene soporte transaccional, en un futuro migrar a PYMONGO, que sí tiene soporte
     # return {"message": "Notificacion guardada con éxito."}
         except ValidationError as exc:
             print(exc.message)
             return {"message": "No se pudo crear o enviar la notificacion."}, 404
         return {"message": "Notificacion reenviada con éxito.",
                 "Número de destinatarios:": len(filtersObids)}               
     elif accion == 'encuesta':
         n = NotificacionTemplateModel.find_by_id(id)
         pprint(n)
         if not n:
             return {"message": "No se encontro la notificacion"}, 404
         en = EncuestaModel.find_by_id(n.link)
         if not en:
             return {"message": "No se encontro la encuesta"}, 404
         try:
              filtersObids=[]
             if not "filtros" in n:
                 return {"message": "Error: Sin destinatarios, debe haber al menos un participante a quien enviarle esta acción"}
             for fil in n["filtros"]:
                 filtersObids.append(ObjectId(fil))
             # Enviar a todos los participantes
             for par in ParticipanteModel.objects.raw({"_id": { "$in": filtersObids}}):
                 # part_id = ObjectId(id)
                 notif = PremioParticipanteModel(
                 id_participante=par._id,
                 # id_notificacion=n._id,
                 fecha_creacion=dt.datetime.now(),
                 estado=0,
                 # Estado puede servir para actualizar tambien OJO! ahora esta fijo, pero podrías ser variable
                 ).save()        
             filtersObids=[]
             if not "filtros" in n:
                 return {"message": "Error: Sin destinatarios, debe haber al menos un participante a quien enviarle esta acción"}
             for fil in n["filtros"]:
                 filtersObids.append(ObjectId(fil))
             # Enviar a todos los participantes
             for p in ParticipanteModel.objects.raw({"_id": { "$in": filtersObids}}):
                 # part_id = ObjectId(id)
                 notif = NotificacionModel(
                 id_participante=p._id,
                 id_notificacion=n._id,
                 estado=0,
                 # Estado puede servir para actualizar tambien OJO! ahora esta fijo, pero podrías ser variable
             ).save()         
            
         except:
Example #6
0
 def put(self, id, accion):
     if accion == 'ninguna':
         n = NotificacionTemplateModel.find_by_id(id)
         if not n:
             return {"message": "No se encontro la notificación!"}, 404
         noti_json = request.get_json()
         # pprint(noti_json["notificacion"])
         noti = not_schema_template.load(noti_json["notificacion"])
         try:
             if "tipo_notificacion" in noti:
                 n.tipo_notificacion=noti["tipo_notificacion"]
             if "imagenIcon" in noti:
                 n.imagenIcon=noti["imagenIcon"]
             if "titulo" in noti:
                 n.titulo=noti["titulo"]
             if "fecha" in noti:
                 n.fecha=noti["fecha"]
             if "bar_text" in noti:
                 n.bar_text=noti["bar_text"]
             if "mensaje" in noti:
                 n.mensaje=noti["mensaje"]
             if "link" in noti:
                 n.link=noti["link"]
             n.save()
         except ValidationError as exc:
             print(exc.message)
             return {"message": "No se pudo actualizar la notificación."}, 404
         return {"notificacion": NotificacionTemplateSchema(
             only=(
             "_id",
             "titulo",
             "mensaje",
             "fecha",
             "imagenIcon",
             "bar_text",
             "tipo_notificacion",
             "link",
             )).dump(n)}, 200
         # /admin/notificaciones/<string:id> patch! ya existe!
     elif accion == 'premio':
         n = NotificacionTemplateModel.find_by_id(id)
         pprint(n)
         if not n:
             return {"message": "No se encontro la notificacion"}, 404
         noti_json = request.get_json()
         # pprint(noti_json["notificacion"])
         noti = not_schema_template.load(noti_json["notificacion"])
         # pprint(noti)
         try:
             if "tipo_notificacion" in noti:
                 n.tipo_notificacion=noti["tipo_notificacion"]
             if "imagenIcon" in noti:
                 n.imagenIcon=noti["imagenIcon"]
             if "titulo" in noti:
                 n.titulo=noti["titulo"]
             if "fecha" in noti:
                 n.fecha=noti["fecha"]
             if "bar_text" in noti:
                 n.bar_text=noti["bar_text"]
             if "mensaje" in noti:
                 n.mensaje=noti["mensaje"]
             if "link" in noti:
                 n.link=noti["link"] # link no se debe actualizar, pero bueno! jaja
             n.save()
         except ValidationError as exc:
             print(exc.message)
             return {"message": "No se pudo actualizar la notificación."}, 404
         p = PremioModel.find_by_id(n.link)
         if not p:
             return {"message": "No se encontro el premio"}, 404
         # p_req = request.get_json()
         # pprint(p_req)
         premio = premio_schema.load(noti_json["premio"])
         # pprint(p_req["premio"])
         pprint(premio)
         try:
             if "nombre" in premio:
                 p.nombre = premio["nombre"] 
             if "puntos" in premio:
                 p.puntos = premio["puntos"] 
             if "codigo_barras" in premio:
                 p.codigo_barras = premio["codigo_barras"] 
             if "codigo_qr" in premio:
                 p.codigo_barras = premio["codigo_barras"] 
             if "imagen_icon" in premio:
                 p.imagen_icon = premio["imagen_icon"] 
             if "imagen_display" in premio:
                 p.imagen_icon = premio["imagen_icon"] 
             if "fecha_creacion" in premio:
                 p.imagen_icon = premio["imagen_icon"] 
             if "fecha_vigencia" in premio:
                 p.fecha_vigencia = premio["fecha_vigencia"] 
             if "fecha_redencion" in premio:
                 p.fecha_redencion = premio["fecha_redencion"] 
             p.save()
         except ValidationError as exc:
             print(exc.message)
             return {"message": "No se pudo actualizar el premio."}, 400
         return {"notificacion": NotificacionTemplateSchema(
             only=(
             "_id",
             "titulo",
             "mensaje",
             "fecha",
             "imagenIcon",
             "bar_text",
             "tipo_notificacion",
             "link",
             )).dump(n),
          "premio":  PremioSchema(
                 only=(
                     "_id",
                     "nombre", 
                     "puntos", 
                     "codigo_barras", 
                     "codigo_qr",
                     "imagen_icon",
                     "imagen_display",
                     "fecha_creacion", 
                     "fecha_vigencia", 
                     # "fecha_redencion",
                     # "id_producto",
                     "id_participante"
                 )).dump(p)
         }, 200               
     elif accion == 'encuesta':
         n = NotificacionTemplateModel.find_by_id(id)
         if not n:
             return {"message": "No se encontro la notificación!"}, 404
         e = EncuestaModel.find_by_id(n.link)
         if not e:
             return {"message": "No se encontro la encuesta"}, 404
         noti_json = request.get_json()
         noti = not_schema_template.load(noti_json["notificacion"])
         # encuesta_json = request.get_json()
         encuesta = EncuestaSchema().load(noti_json["encuesta"])
         try:
             if "tipo_notificacion" in noti:
                 n.tipo_notificacion=noti["tipo_notificacion"]
             if "imagenIcon" in noti:
                 n.imagenIcon=noti["imagenIcon"]
             if "titulo" in noti:
                 n.titulo=noti["titulo"]
             if "fecha" in noti:
                 n.fecha=noti["fecha"]
             if "bar_text" in noti:
                 n.bar_text=noti["bar_text"]
             if "mensaje" in noti:
                 n.mensaje=noti["mensaje"]
             if "link" in noti:
                 n.link=noti["link"]
             # Encuesta
             if "titulo" in encuesta:
                 e.titulo=encuesta["titulo"]
             if "categoria" in encuesta:
                 e.categoria=encuesta["categoria"]
             e.fecha_creacion=dt.datetime.now()
             if "metrica" in encuesta:
                 e.metrica=encuesta["metrica"]
             if "puntos" in encuesta:
                 e.puntos=encuesta["puntos"]
             if "paginas" in encuesta:
                 e.paginas=encuesta["paginas"]
                 pprint(e.paginas)
                 # for pagina in e.paginas:
                 #     print(1)
             e.save()
             n.save()
         except ValidationError as exc:
             print(exc.message)
             return {"message": "No se pudo actualizar la notificación."}, 404
         return {
             "notificacion": NotificacionTemplateSchema(
                 only=(
                 "_id",
                 "titulo",
                 "mensaje",
                 "fecha",
                 "imagenIcon",
                 "bar_text",
                 "tipo_notificacion",
                 "link"
                 )).dump(n),
             "encuesta": EncuestaSchema(
                     only=(
                         "_id",
                         "titulo",
                         "categoria",
                         "fecha_creacion",
                         "fecha_respuesta",
                         "metrica",
                         "puntos",
                         "paginas",
                     )).dump(e)
         }, 200
Example #7
0
 def get(self, id, accion):
     if accion == 'ninguna':
         n = NotificacionTemplateModel.find_by_id(id)
         if not n:
             return {"message": "No se encontro la notificación"}, 404
         return {"notificacion": NotificacionTemplateSchema(
             only=(
             "_id",
             "titulo",
             "mensaje",
             "fecha",
             "imagenIcon",
             "bar_text",
             "tipo_notificacion",
             "link",
             "filtros"
             )).dump(n)}, 200
     elif accion == 'premio':
         n = NotificacionTemplateModel.find_by_id(id)
         pprint(n)
         if not n:
             return {"message": "No se encontro el premio"}, 404
         p = PremioModel.find_by_id(n.link)
         if not p:
             return {"message": "No se encontro el premio"}, 404
         return {"notificacion": NotificacionTemplateSchema(
             only=(
             "_id",
             "titulo",
             "mensaje",
             "fecha",
             "imagenIcon",
             "bar_text",
             "tipo_notificacion",
             "link",
             "filtros"
             )).dump(n),
          "premio":  PremioSchema(
                 only=(
                     "_id",
                     "nombre", 
                     "puntos", 
                     "codigo_barras", 
                     "codigo_qr",
                     "imagen_icon",
                     "imagen_display",
                     "fecha_creacion", 
                     "fecha_vigencia", 
                     # "fecha_redencion",
                     # "id_producto",
                     "id_participante"
                 )).dump(p)
         }, 200               
     elif accion == 'encuesta':
         n = NotificacionTemplateModel.find_by_id(id)
         pprint(n)
         if not n:
             return {"message": "No se encontro la notificacion"}, 404
         en = EncuestaModel.find_by_id(n.link)
         if not en:
             return {"message": "No se encontro la encuesta"}, 404
         return {
             "notificacion": NotificacionTemplateSchema(
                 only=(
                 "_id",
                 "titulo",
                 "mensaje",
                 "fecha",
                 "imagenIcon",
                 "bar_text",
                 "tipo_notificacion",
                 "link",
                 "filtros"
                 )).dump(n),
             "encuesta": EncuestaSchema(
                     only=(
                         "_id",
                         "titulo",
                         "categoria",
                         "fecha_creacion",
                         "fecha_respuesta",
                         "metrica",
                         "puntos",
                         "paginas",
                     )).dump(en)
         }, 200
Example #8
0
 def post(self):
     notificacion_json = request.get_json()
     # print(notificacion_json)
     n = not_schema_template.load(notificacion_json)
     pprint(n)
     # print("loaded")
     try:
         template = NotificacionTemplateModel()
         if "titulo" in n:
             template.titulo=n["titulo"]
         if "mensaje" in n:
             template.mensaje=n["mensaje"]
         if "imagenIcon" in n:
             template.imagenIcon=n["imagenIcon"]
         if "bar_text" in n:
             template.bar_text=n["bar_text"]
         if "fecha" in n:
             template.fecha=n["fecha"]
         else:
             template.fecha=dt.datetime.now()
         if  "tipo_notificacion" in n: 
             template.tipo_notificacion=n["tipo_notificacion"]
         if "link" in n:
             template.link=n["link"]
         if "filtros" in n:
             template.filtros = n["filtros"]
         else: 
             template.filtros = []
         template.save()
         filtersObids=[]
         for fil in n["filtros"]:
             filtersObids.append(ObjectId(fil))
         # Enviar a todos los participantes
         for p in ParticipanteModel.objects.raw({"_id": { "$in": filtersObids}}):
             # part_id = ObjectId(id)
             notif = NotificacionModel(
             id_participante=p._id,
             id_notificacion=template._id,
             estado=0,
             # Estado puede servir para actualizar tambien OJO! ahora esta fijo, pero podrías ser variable
             ).save()            
             # PYMODM no tiene soporte transaccional, en un futuro migrar a PYMONGO, que sí tiene soporte
         # return {"message": "Notificacion guardada con éxito."}
     except ValidationError as exc:
         print(exc.message)
         return {"message": "No se pudo crear o enviar la notificacion."}, 404
     return {"message": "Notificacion guardada con éxito.",
                 "_id": str(template._id),
             "Número de destinatarios:": len(template.filtros)}
Example #9
0
 def post(self, id):
     # Get premios
     bir_all = BirthdayModel.objects.all()
     # pprint(bir)
     if not bir_all:
         return {"message": "No se encontró el premio de cumpleaños"}, 404
     # item_json = request.get_json()
     # item = BirthdaySchema().load(item_json)
     # Get notificacion
     # TODO: Obtener la última configuración
     for birthday in bir_all:
         bir = birthday
     notificacion = NotificacionTemplateModel.find_by_id(
         bir.id_notificacion)
     if not notificacion:
         return {"message": "No se encontró la notificacion"}, 404
     # Enviar notificaciones
     # Enviar premio
     notificacion_id_premio = notificacion.link
     current_date = datetime.now()
     print("current_date", current_date)
     current_day = current_date.day
     current_month = current_date.month
     current_year = current_date.year
     maxdays = 0
     count_sends = 0
     premio = None
     for p in ParticipanteModel.objects.all():
         if p.fecha_nacimiento and p.fecha_nacimiento != "null":
             maxdays = bir.trigger
             bmonth = p.fecha_nacimiento.month
             r_maxdate = current_date + relativedelta(days=+maxdays)
             r_mindate = current_date - relativedelta(days=+maxdays)
             r_participante_birthdate = p.fecha_nacimiento.replace(
                 year=current_year)
             r_antiguedad = current_date - p.fecha_antiguedad
             r_antiguedad = r_antiguedad.days
             #  Verificar si no se ha enviado antes el premio al participante
             # print("data id_par:{}, id_premio: {}".format(str(p._id), notificacion_id_premio))
             tienePremio = PremioParticipanteModel.find_by_two_fields(
                 'id_participante', str(p._id), 'id_premio',
                 notificacion_id_premio)
             # Verificar si en ESTE año no ha canjeado el premio, en caso de que no, enviar premio
             canjeado = False
             if tienePremio and tienePremio.fechas_redencion:
                 if len(tienePremio.fechas_redencion) > 0:
                     for fecha in tienePremio.fechas_redencion:
                         if fecha.year == current_date.year:
                             canjeado = True
             #OLD: tieneNotificacion = NotificacionModel.find_by_field('id_notificacion', str(notificacion._id))
             if r_mindate < r_participante_birthdate < r_maxdate and r_antiguedad >= bir.antiguedad:
                 if not canjeado and not tienePremio:
                     # Realizar envío de un nuevo premio
                     if (notificacion.tipo_notificacion == "premio"):
                         premio = PremioParticipanteModel(
                             id_participante=str(p._id),
                             id_premio=notificacion_id_premio,
                             estado=0,
                             fecha_creacion=datetime.now(),
                             id_promocion=bir.id_promocion,
                         ).save()
                         notif = NotificacionModel(
                             id_participante=str(p._id),
                             id_notificacion=str(notificacion._id),
                             estado=0).save()
                         # print("Envio", list(premio), str(premio._id))
                         if premio:
                             count_sends += 1
     return {"Total de envios:": count_sends}, 200
Example #10
0
    def post(self, id):
        def diff(first, second):
            second = set(second)
            return [item for item in first if item not in second]

        ticket = VentaModel.find_by_field('id_ticket_punto_venta', id)
        if ticket:
            return {"message": "El ticket que desea ingresar ya ha sido registrado antes"}, 400
        req_json = request.get_json()
        req = VentaSchema().load(req_json)
        # print("detalle_save", req_json)
        # print("detalle_save req_json",req)
        try:
            ticket = VentaModel()
            ticket.id_ticket_punto_venta = id
            if "total" in req:
                ticket.total = req["total"]
            if "descuento" in req:
                ticket.descuento = req["descuento"]
            if "fecha" in req:
                ticket.fecha = req["fecha"]
            if "id_participante" in req:
                ticket.id_participante = req["id_participante"]
            if "promociones" in req:
                ticket.promociones = req["promociones"]
            if "detalle_venta" in req:
                ticket.detalle_venta = req["detalle_venta"]
            # ticket.save()
            # print("detalle_save",ticket.detalle_venta)
        except ValidationError as exc:
            print(exc.message)
            return {"message": "No se pudo capturar el ticket."}, 504   
        # Buscar al participante
        p = ParticipanteModel.find_by_id(ticket.id_participante)
        if not p:
            return {'message': f"No participante with id{ str(ticket.id_participante) }"}, 404 
        card_id = TarjetaSellosModel.get_tarjeta_sellos_actual()
        # Transaccion de sellos
        new_notificacion_sello = None
        if card_id.trigger == 'producto' or card_id.trigger == 'cantidad': 
            if card_id.trigger == 'producto':
                bonificacion_sellos = TarjetaSellosModel.calcular_sellos_por_productos(ticket.detalle_venta)
            elif card_id.trigger == 'cantidad':
                bonificacion_sellos = TarjetaSellosModel.calcular_sellos_por_cantidad(ticket, card_id.cantidad_trigger)
            is_historial_sellos_new_element = 0
            notificaciones_enviadas_por_sellos = 0
            encuestas_enviadas_por_sellos = 0
            premios_enviados_por_sellos = 0
            if bonificacion_sellos:
                # Quitar puntos que han caducado
                    # 1. Verificar si ha caducado sus sellos
                current_date = dt.datetime.now()
                if current_date < card_id.fecha_inicio and current_date > card_id.fecha_inicio:
                    # 2. Si sí, quitar sellos
                    p.sellos = 0
                    p.save()
                p.sellos += bonificacion_sellos
                print("participante sellos: ", p.sellos)
                # resetear sellos, liberar premio
                tarjeta_sellos_actual = TarjetaSellosModel.get_tarjeta_sellos_actual()
                if p.sellos >= tarjeta_sellos_actual.num_sellos:
                    # Verificar el número de premios que se obtienen con los sellos obtenidos en la compra efectuada
                    total_sellos_obtenidos = int(p.sellos // tarjeta_sellos_actual.num_sellos) 
                    # Enviar premio y notificacion si se amerita
                    for prem in range(total_sellos_obtenidos): 
                        new_notificacion_sello = NotificacionModel(
                            id_participante = str(p._id),
                            id_notificacion = str(tarjeta_sellos_actual.id_notificacion),
                            estado = 0
                        ).save() 
                        # Agregar el id de la notificación (movimiento) al ticket.
                        if new_notificacion_sello:
                            notificaciones_enviadas_por_sellos += 1 
                            ticket.id_notificacion_obtenidas_list.append(str(new_notificacion_sello._id))
                        # Buscar el esquema de la notificación de la tarjeta de sellos
                        tarjeta_sellos_notificacion = NotificacionTemplateModel.find_by_id(tarjeta_sellos_actual.id_notificacion)
                        if tarjeta_sellos_notificacion and tarjeta_sellos_notificacion.link and tarjeta_sellos_notificacion.link != "null":
                            # Envio de premio o encuesta
                            if tarjeta_sellos_notificacion.tipo_notificacion == "premio":
                                new_bonificacion_link = PremioParticipanteModel(
                                # TODO: Modificación en id_promoción
                                    id_participante = str(p._id),
                                    id_premio = tarjeta_sellos_notificacion.link,
                                    estado = 0,
                                    fecha_creacion = dt.datetime.now()
                                ).save()
                                if new_bonificacion_link:
                                    premios_enviados_por_sellos += 1
                            if tarjeta_sellos_notificacion.tipo_notificacion == "encuesta":
                                new_bonificacion_link = ParticipantesEncuestaModel(
                                    id_participante = str(p._id),
                                    id_encuesta = tarjeta_sellos_notificacion.link,
                                    estado = 0, 
                                    fecha_creacion = dt.datetime.now() 
                                ).save()
                                if new_bonificacion_link:
                                    encuestas_enviadas_por_sellos += 1
                    p.sellos %= tarjeta_sellos_actual.num_sellos
                    new_sello_historial = HistorialTarjetaSellos.add_movimiento(str(p._id), str(tarjeta_sellos_actual._id))
                    if new_sello_historial:
                        is_historial_sellos_new_element = 1
        # Transacción de Puntos
        # Puntos: 1. Verificar si el participante llego a un nuevo nivel
        bonificacion_puntos = ConfigModel.calcular_puntos(ticket.total) 
        nivel_actual = TarjetaPuntosTemplateModel.get_level(p.saldo)
        nivel_sig = TarjetaPuntosTemplateModel.get_level(p.saldo + bonificacion_puntos)
        bonificacion_niveles = diff(nivel_sig, nivel_actual)
        print(bonificacion_niveles)
        print(len(bonificacion_niveles))
        notificaciones_enviadas_por_puntos = 0
        encuestas_enviadas_por_puntos = 0
        premios_enviados_por_puntos = 0
        premios_enviados = 0
        if len(bonificacion_niveles): 
            for nivel in bonificacion_niveles:
                new_nivel = TarjetaPuntosTemplateModel.find_by_id(nivel)
                if new_nivel.id_notificacion:
        # Puntos: 2. Habilitado de niveles: Notificacion y premio
                    # Integración de fecha_vencimiento del sistema de niveles:
                    if new_nivel.fecha_vencimiento and dt.datetime.now() < new_nivel.fecha_vencimiento:
                        trigger_notificacion = NotificacionModel.add_notificacion(new_nivel.id_notificacion, p._id)
                        notificacion = NotificacionTemplateModel.find_by_id(new_nivel.id_notificacion)
                        print(notificacion)
                        print(notificacion.tipo_notificacion)
                        print(notificacion.link)
                        if notificacion and notificacion.link and notificacion.link != "null":
                            # Envio de premio o encuesta
                            if notificacion.tipo_notificacion == "premio":
                                new_bonificacion_link = PremioParticipanteModel(
                                # TODO: Modificación en id_promoción
                                    id_participante = str(p._id),
                                    id_premio = notificacion.link,
                                    estado = 0,
                                    fecha_creacion = dt.datetime.now()
                                ).save()
                                if new_bonificacion_link:
                                    # Añadir los premios enviados en el ticket por el sistema de niveles
                                    ticket.id_premios_obtenidos_list.append(str(new_bonificacion_link._id))
                                    premios_enviados_por_puntos += 1
                            if notificacion.tipo_notificacion == "encuesta":
                                new_bonificacion_link = ParticipantesEncuestaModel(
                                    id_participante = str(p._id),
                                    id_encuesta = notificacion.link,
                                    estado = 0, 
                                    fecha_creacion = dt.datetime.now() 
                                ).save()
                                if new_bonificacion_link:
                                    encuestas_enviadas_por_puntos += 1
                        if trigger_notificacion:
                            # Añadir las notificaciones enviadas por el sistema de niveles
                            ticket.id_notificacion_obtenidas_list.append(str(trigger_notificacion._id))
                            notificaciones_enviadas_por_puntos += 1 
        # Puntos: 3. Transaccion de puntos
        if bonificacion_puntos:
            p.saldo += bonificacion_puntos
        try:    
            p.save()
        except :
            print()
            return {"message": "No se pudieron agregar los puntos al participante"}, 504
        print("saldo del participante:", p.saldo)
        print("bonificacion_puntos:", bonificacion_puntos)
        # Transaccion de movimientos y quema de cupones, sólo hay uno en la parte superior "new_sello_historial"
        new_movimiento = MovimientoAppModel.add_movimiento(str(p._id), "Compra", "entrada", ticket.total, "ayuda4.png")
        if new_movimiento:
            movimientos_enviados = 1
        # TODO: Añadir los diferentes tipos de movimientos existentes! !
        try: 
            ticket.sellos_otorgados = bonificacion_sellos
            # TODO: Notificación por puntos
            ticket.puntos_otorgados = bonificacion_puntos
            # if new_notificacion_sello:
            #     ticket.id_notificacion_obtenidas_list.append(str(new_notificacion_sello._id))
            ticket.save()
        except ValidationError as exc:            
            print(exc.message)
            return {"message": "No se pudo guardar el ticket."}, 504   

        # Retorno
        return {
            'message': "Ticket aceptado con éxito",
            'captura del ticket': 'Exitosa',
            'Busqueda del participante': 'Exitosa',
            'Bonificacion de sellos': '{} sello(s)'.format(bonificacion_sellos),
            'Habilitación de un nuevo nivel': '{} nivel(es) desbloqueados'.format(len(bonificacion_niveles)),
            'Notificaciones enviadas por tarjeta de sellos': notificaciones_enviadas_por_sellos,
            'Notificaciones enviadas por sistema de niveles(Puntos)': notificaciones_enviadas_por_puntos,
            'Nuevas Encuestas por sellos': encuestas_enviadas_por_sellos,
            'Nuevas Encuestas por puntos': encuestas_enviadas_por_puntos,
            'Movimientos enviados': movimientos_enviados,
            'Nuevos premios por sellos': premios_enviados_por_sellos,
            'Nuevos premios por puntos': premios_enviados_por_puntos,
            'Bonificación de puntos': '{} puntos bonificados'.format(bonificacion_puntos),
            '_id': str(ticket._id)
        }, 200