diff --git a/dotnet/Data/AvailabilityRepository.cs b/dotnet/Data/AvailabilityRepository.cs index 9aae050f..037a64c6 100644 --- a/dotnet/Data/AvailabilityRepository.cs +++ b/dotnet/Data/AvailabilityRepository.cs @@ -99,10 +99,18 @@ public async Task SaveNotifyRequest(NotifyRequest notifyRequest, RequestCo { // PATCH https://{{accountName}}.vtexcommercestable.com.br/api/dataentities/{{data_entity_name}}/documents + Console.WriteLine("SaveNotifyRequest"); + + string url = $"http://{requestContext.Account}.vtexcommercestable.com.br/api/dataentities/{Constants.DATA_ENTITY}/documents"; ResponseWrapper responseWrapper = await this.SendRequest(url, HttpMethod.Put, notifyRequest); + + Console.WriteLine(responseWrapper.IsSuccess); + if (!responseWrapper.IsSuccess) { + Console.WriteLine("errrorrrrrr save"); + Console.WriteLine(responseWrapper.IsSuccess); _context.Vtex.Logger.Error("SaveNotifyRequest", null, $"Failed to save '{JsonConvert.SerializeObject(notifyRequest)}' "); } diff --git a/dotnet/GraphQL/Mutation.cs b/dotnet/GraphQL/Mutation.cs index 907e5772..d9af2d6b 100644 --- a/dotnet/GraphQL/Mutation.cs +++ b/dotnet/GraphQL/Mutation.cs @@ -6,6 +6,8 @@ using Vtex.Api.Context; using System.Net; +using System; + namespace AvailabilityNotify.GraphQL { [GraphQLMetadata("Mutation")] @@ -15,25 +17,74 @@ public Mutation(IIOServiceContext contextService, IVtexAPIService vtexApiService { Name = "Mutation"; - Field( + FieldAsync( "availabilitySubscribe", arguments: new QueryArguments( new QueryArgument {Name = "name"}, new QueryArgument {Name = "email"}, new QueryArgument {Name = "skuId"}, new QueryArgument {Name = "locale"}, + new QueryArgument {Name = "saleChannel"}, new QueryArgument> {Name = "sellerObj"} ), - resolve: context => + resolve: async context => { var name = context.GetArgument("name"); var email = context.GetArgument("email"); var skuId = context.GetArgument("skuId"); var locale = context.GetArgument("locale"); + var saleChannel = context.GetArgument("saleChannel"); var sellerObj = context.GetArgument("sellerObj"); - contextService.Vtex.Logger.Debug("GraphQL", null, $"AvailabilitySubscribe Mutation called: '{name}' '{email}' '{skuId}' '{locale}'"); - return vtexApiService.AvailabilitySubscribe(email, skuId, name, locale, sellerObj); + Console.WriteLine("saleChannel"); + Console.WriteLine(saleChannel); + + if (saleChannel != "1" && saleChannel != null) { + Console.WriteLine("llegaaaaaaaaaaaa"); + Console.WriteLine(saleChannel); + var sellerData = await vtexApiService.GetSellerName(saleChannel); + Console.WriteLine("sellerDataaaa"); + Console.WriteLine(sellerData.Id); + Console.WriteLine(sellerData.Name); + + contextService.Vtex.Logger.Debug("GraphQL", null, $"AvailabilitySubscribe Mutation called: '{name}' '{email}' '{skuId}' '{locale}'"); + + if(sellerData.Id != "" && sellerData.Name != "") { + + SellerObj SellerObjBetterScope = new SellerObj + { + sellerId = sellerData.Id, + sellerName = sellerData.Name, + addToCartLink = sellerObj.addToCartLink, + sellerDefault = sellerObj.sellerDefault + }; + + Console.WriteLine("New Object SellerObjBetterScope"); + + Console.WriteLine(SellerObjBetterScope.sellerId); + Console.WriteLine(SellerObjBetterScope.sellerName); + Console.WriteLine(SellerObjBetterScope.addToCartLink); + Console.WriteLine(SellerObjBetterScope.sellerDefault); + + + return vtexApiService.AvailabilitySubscribe(email, skuId, name, locale, SellerObjBetterScope); + + } else { + return vtexApiService.AvailabilitySubscribe(email, skuId, name, locale, sellerObj); + } + } else { + + Console.WriteLine("New Object sellerObj"); + + Console.WriteLine(sellerObj); + Console.WriteLine(sellerObj.sellerId); + Console.WriteLine(sellerObj.sellerName); + Console.WriteLine(sellerObj.addToCartLink); + Console.WriteLine(sellerObj.sellerDefault); + + return vtexApiService.AvailabilitySubscribe(email, skuId, name, locale, sellerObj); + } + }); FieldAsync( diff --git a/dotnet/Models/GetSellerName.cs b/dotnet/Models/GetSellerName.cs new file mode 100644 index 00000000..220ca7d0 --- /dev/null +++ b/dotnet/Models/GetSellerName.cs @@ -0,0 +1,112 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Text; + +namespace AvailabilityNotify.Models +{ + public class GetSellerNameResponse + { + [JsonProperty("paging")] + public Paging Paging { get; set; } + + [JsonProperty("items")] + public Items[] Items { get; set; } + + } + + public partial class Paging + { + [JsonProperty("from")] + public long? From { get; set; } + + [JsonProperty("to")] + public long? To { get; set; } + + [JsonProperty("total")] + public long? Total { get; set; } + } + + public partial class Items + { + [JsonProperty("id")] + public string Id { get; set; } + + [JsonProperty("logo")] + public string? Logo { get; set; } + + [JsonProperty("taxCode")] + public string? TaxCode { get; set; } + + [JsonProperty("email")] + public string? Email { get; set; } + + [JsonProperty("description")] + public string? Description { get; set; } + + [JsonProperty("sellerCommissionConfiguration")] + public string? SellerCommissionConfiguration { get; set; } + + [JsonProperty("catalogSystemEndpoint")] + public string? CatalogSystemEndpoint { get; set; } + + [JsonProperty("CSCIdentification")] + public string? CSCIdentification { get; set; } + + [JsonProperty("account")] + public string? Account { get; set; } + + [JsonProperty("channel")] + public string? Channel { get; set; } + + [JsonProperty("salesChannel")] + public string? SalesChannel { get; set; } + + [JsonProperty("score")] + public long? Score { get; set; } + + [JsonProperty("exchangeReturnPolicy")] + public string? ExchangeReturnPolicy { get; set; } + + [JsonProperty("deliveryPolicy")] + public string? DeliveryPolicy { get; set; } + + [JsonProperty("securityPrivacyPolicy")] + public string? SecurityPrivacyPolicy { get; set; } + + [JsonProperty("fulfillmentSellerId")] + public string FulfillmentSellerId { get; set; } + + [JsonProperty("name")] + public string Name { get; set; } + + [JsonProperty("allowHybridPayments")] + public bool AllowHybridPayments { get; set; } + + [JsonProperty("isBetterScope")] + public bool IsBetterScope { get; set; } + + [JsonProperty("sellerType")] + public long SellerType { get; set; } + + [JsonProperty("availableSalesChannels")] + public AvailableSalesChannels[] AvailableSalesChannels { get; set; } + + [JsonProperty("isVtex")] + public bool IsVtex { get; set; } + + [JsonProperty("trustPolicy")] + public string TrustPolicy { get; set; } + + [JsonProperty("policies")] + public string[] Policies { get; set; } + + } + + public partial class AvailableSalesChannels + { + [JsonProperty("id")] + public long? Id { get; set; } + } + +} diff --git a/dotnet/Models/sellerDataResponse.cs b/dotnet/Models/sellerDataResponse.cs new file mode 100644 index 00000000..95da6750 --- /dev/null +++ b/dotnet/Models/sellerDataResponse.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace AvailabilityNotify.Models +{ + public class SellerDataResponse + { + public string Id { get; set; } + public string Name { get; set; } + } +} diff --git a/dotnet/Services/IVtexAPIService.cs b/dotnet/Services/IVtexAPIService.cs index 75d4387f..dcc0558c 100644 --- a/dotnet/Services/IVtexAPIService.cs +++ b/dotnet/Services/IVtexAPIService.cs @@ -8,6 +8,7 @@ namespace AvailabilityNotify.Services public interface IVtexAPIService { Task AvailabilitySubscribe(string email, string sku, string name, string locale, SellerObj sellerObj); + Task GetSellerName(string saleChannel); Task ProcessNotification(AffiliateNotification notification); Task ProcessNotification(BroadcastNotification notification); Task ProcessNotification(AllStatesNotification notification); diff --git a/dotnet/Services/VtexAPIService.cs b/dotnet/Services/VtexAPIService.cs index 9dfb5d86..d005b3f6 100644 --- a/dotnet/Services/VtexAPIService.cs +++ b/dotnet/Services/VtexAPIService.cs @@ -491,11 +491,90 @@ public async Task AvailabilitySubscribe(string email, string sku, string n Seller = sellerObj }; + Console.WriteLine("AvailabilitySubscribe notifyRequest"); + Console.WriteLine(notifyRequest.Email); + success = await _availabilityRepository.SaveNotifyRequest(notifyRequest, requestContext); + Console.WriteLine("AvailabilitySubscribe success"); + + Console.WriteLine(success); + return success; } + public async Task GetSellerName(string saleChannel) + { + Console.WriteLine("saleChannel GetSellerName"); + Console.WriteLine(saleChannel); + + SellerDataResponse sellerData = new SellerDataResponse + { + Id = "", + Name = "" + }; + + try + { + var request = new HttpRequestMessage + { + Method = HttpMethod.Get, + RequestUri = new Uri($"http://{_context.Vtex.Account}.{Constants.ENVIRONMENT}.com.br/api/seller-register/pvt/sellers?sc={saleChannel}") + }; + + request.Headers.Add(Constants.USE_HTTPS_HEADER_NAME, "true"); + string authToken = _context.Vtex.AuthToken; + Console.WriteLine(authToken); + if (authToken != null) + { + request.Headers.Add(Constants.AUTHORIZATION_HEADER_NAME, authToken); + request.Headers.Add(Constants.VTEX_ID_HEADER_NAME, authToken); + request.Headers.Add(Constants.PROXY_AUTHORIZATION_HEADER_NAME, authToken); + } + + var client = _clientFactory.CreateClient(); + var response = await client.SendAsync(request); + string responseContent = await response.Content.ReadAsStringAsync(); + Console.WriteLine(response.IsSuccessStatusCode); + if (response.IsSuccessStatusCode) + { + GetSellerNameResponse sellerNameResponse = JsonConvert.DeserializeObject(responseContent); + Console.WriteLine("sellerNameResponse"); + Console.WriteLine(sellerNameResponse.Items[0].Id); + + var sellerBetterScope = sellerNameResponse.Items.Where(c => c.IsBetterScope).ToArray(); + Console.WriteLine(sellerBetterScope[0].Id); + + if( sellerBetterScope[0].Id != null && sellerBetterScope[0].Name != null) { + sellerData.Id = sellerBetterScope[0].Id; + sellerData.Name = sellerBetterScope[0].Name; + } + + + } + else + { + Console.WriteLine("Errorrr 1"); + _context.Vtex.Logger.Warn("Error getting seller 1"); + } + + } + catch (Exception ex) + { + Console.WriteLine("Error 2"); + Console.WriteLine(ex); + _context.Vtex.Logger.Error("Error getting seller"); + } + + Console.WriteLine("sellerData response"); + Console.WriteLine(sellerData.Id); + Console.WriteLine(sellerData.Name); + + + return sellerData; + + } + public async Task ProcessNotification(AffiliateNotification notification) { bool success = true; diff --git a/graphql/schema.graphql b/graphql/schema.graphql index 6126b01a..74bf307c 100644 --- a/graphql/schema.graphql +++ b/graphql/schema.graphql @@ -6,6 +6,7 @@ type Mutation { email: String skuId: String locale: String + saleChannel: String sellerObj: SellerObjInputType! ): Boolean diff --git a/manifest.json b/manifest.json index b578d762..7e9e9d08 100644 --- a/manifest.json +++ b/manifest.json @@ -42,7 +42,8 @@ "vtex.store-image": "0.x", "vtex.store-resources": "0.x", "vtex.styleguide": "9.x", - "vtex.broadcaster": "0.x" + "vtex.broadcaster": "0.x", + "vtex.session-client": "1.x" }, "settingsSchema": { "title": "Availability Notifier", diff --git a/react/AvailabilityNotifier.tsx b/react/AvailabilityNotifier.tsx index 54f5bd9e..ad99cb6d 100644 --- a/react/AvailabilityNotifier.tsx +++ b/react/AvailabilityNotifier.tsx @@ -5,6 +5,7 @@ import { useMutation } from 'react-apollo' import { Button, Input } from 'vtex.styleguide' import { useProduct } from 'vtex.product-context' import { useRuntime } from 'vtex.render-runtime' +import { useRenderSession } from 'vtex.session-client' import type { Seller } from './utils/sellers' import ADD_TO_AVAILABILITY_SUBSCRIBER_MUTATION from './graphql/addToAvailabilityNotifierMutation.gql' @@ -18,6 +19,7 @@ interface MutationVariables { email: string locale: string sellerObj: SellerObj + saleChannel: string } interface Props { @@ -69,6 +71,8 @@ function AvailabilityNotifier(props: Props) { const available = props.available ?? isAvailable(seller?.commertialOffer) const skuId = props.skuId ?? productContext?.selectedItem?.itemId const { locale } = useRuntime().culture + const { session } = useRenderSession() + let saleChannel = session?.namespaces?.store?.channel?.value // console.log('Seller =>', seller) // const sellerObj = seller as SellerObj const sellerObj = { @@ -91,6 +95,7 @@ function AvailabilityNotifier(props: Props) { name, email, locale, + saleChannel, sellerObj: { sellerId: sellerObj.sellerId, sellerName: sellerObj.sellerName, diff --git a/react/graphql/addToAvailabilityNotifierMutation.gql b/react/graphql/addToAvailabilityNotifierMutation.gql index 4b7af7fc..6e4ed893 100644 --- a/react/graphql/addToAvailabilityNotifierMutation.gql +++ b/react/graphql/addToAvailabilityNotifierMutation.gql @@ -3,6 +3,7 @@ mutation AvailabilitySubscribe( $email: String $skuId: String $locale: String + $saleChannel: String $sellerObj: SellerObjInputType! ) { availabilitySubscribe( @@ -10,6 +11,7 @@ mutation AvailabilitySubscribe( email: $email skuId: $skuId locale: $locale + saleChannel: $saleChannel sellerObj: $sellerObj ) }