public async Task <IActionResult> ReceivedBuyer(ReceivedBuyerDTO receivedBuyerDTO) { try { var resp = await _buyOrderServices.ReceivedBuyer(receivedBuyerDTO); if (!resp) { return(NotFound(resp)); } return(StatusCode(StatusCodes.Status204NoContent, resp)); } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, new { message = e.Message })); } }
public async Task <bool> ReceivedBuyer(ReceivedBuyerDTO receivedBuyerDTO) { try { using (var db = new SocialNetworkDeveloperContext()) { using (var transaction = db.Database.BeginTransaction()) { try { var buyOrder = await db.OrdenesCompras.FirstOrDefaultAsync(x => x.IdOrdenCompra == receivedBuyerDTO.IdBuyOrder); if (buyOrder == null) { return(false); } var buyer = await _userServices.GetUserByID((int)buyOrder.IdUsuario); buyOrder.EstadoOrdenCompra = 4; //recibido db.Update(buyOrder).State = EntityState.Modified; await db.SaveChangesAsync(); //actualizacion orden de compra var saleOrder = await db.OrdenesVentas.FirstOrDefaultAsync(x => x.IdOrdenVenta == receivedBuyerDTO.IdSaleOrder); if (saleOrder != null) { saleOrder.EstadoOrdenVenta = 4; //entregado db.Update(saleOrder).State = EntityState.Modified; await db.SaveChangesAsync(); } //inserto raiting de publicacion Rating rating = new Rating() { Rating1 = receivedBuyerDTO.Raiting, FechaHoraCreacion = DateTime.Now, IdPublicacion = buyOrder.IdPublicacion, IdUsuario = buyOrder.IdUsuario }; db.Ratings.Add(rating); await db.SaveChangesAsync(); //actualizar publicación //var publication = await _publicationServices.GetPublicationById((int)buyOrder.IdPublicacion); var publication = await db.Publicaciones.FirstOrDefaultAsync(x => x.IdPublicacion == buyOrder.IdPublicacion); //if (publication != null) //{ // publication.Raiting = receivedBuyerDTO.Raiting; // db.Update(publication).State = EntityState.Modified; // await _context.SaveChangesAsync(); //} var seller = await _userServices.GetUserByID((int)publication.IdUsuario); EmailDTO emailDTO = new EmailDTO() { EmailBuyer = buyer.CorreoElectronico, EmailSeller = seller.CorreoElectronico, Title = string.Format("Su compra/venta de la Publicacion {0} fue Entregada", publication.Titulo), MessagesSeller = string.Format(@"Estimado/a <b>{0}</b> el comprador acaba de confirmar de recibido la mercaderia, gracias por utilizar SNB&S, <br/> Detalle de la Venta <br/> Publicación: <b>{1}</b> <br/> Cantidad: <b>{2}</b> <br/> Cliente: <b>{3}</b> <br/> Telefono: <b>{4}</b> <br/> Total: <b>{5}</b>" , seller.NombreCompleto, publication.Titulo, saleOrder.Cantidad, buyer.NombreCompleto, buyer.TelefonoContacto, saleOrder.TotalVenta), MessagesBuyer = string.Format(@"Estimado/a <b>{0}</b> acaba de confirmar de recibido la mercaderia de su compra, gracias por utilizar SNB&S, <br/> Detalle De la Compra <br/> Publicación: <b>{1}</b> <br/> Cantidad: <b>{2}</b> <br/> Total: <b>{3}</b> <br/> Vendedor: <b>{4}</b> <br/> Telefono: <b>{5}</b>" , buyer.NombreCompleto, publication.Titulo, saleOrder.Cantidad, Math.Round((decimal)saleOrder.TotalVenta, 2), seller.NombreCompleto, seller.TelefonoContacto), }; //si falla envio correo no registrar la aprobacion var email = await _emailServices.SendEmail(emailDTO); if (!email) { throw new Exception(); } //envio sms twilio var smsSeller = await _twilioServices.SendSMS(seller.TelefonoContacto, "Una Venta fue Entregada con Exito en SNB&S"); if (!smsSeller) { throw new Exception(); } var smsBuyer = await _twilioServices.SendSMS(buyer.TelefonoContacto, "Acabas de Confirmar de Recibido tu Compra en SNB&S"); if (!smsBuyer) { throw new Exception(); } await transaction.CommitAsync(); return(true); } catch (Exception e) { transaction.Rollback(); log.ErrorFormat("Error al ejecutar transaccion para confirmar de recibido la mercaderia ReceivedBuyer() {0} : {1} ", e.Source, e.Message); return(false); } } } } catch (Exception e) { log.ErrorFormat("Error al actualizar orden de compra a recibido ReceivedBuyer() {0} : {1} ", e.Source, e.Message); return(false); throw; } }