Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Get seller from salechannel selected #99

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions dotnet/Data/AvailabilityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,18 @@ public async Task<bool> 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)}' ");
}

Expand Down
59 changes: 55 additions & 4 deletions dotnet/GraphQL/Mutation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using Vtex.Api.Context;
using System.Net;

using System;

namespace AvailabilityNotify.GraphQL
{
[GraphQLMetadata("Mutation")]
Expand All @@ -15,25 +17,74 @@ public Mutation(IIOServiceContext contextService, IVtexAPIService vtexApiService
{
Name = "Mutation";

Field<BooleanGraphType>(
FieldAsync<BooleanGraphType>(
"availabilitySubscribe",
arguments: new QueryArguments(
new QueryArgument<StringGraphType> {Name = "name"},
new QueryArgument<StringGraphType> {Name = "email"},
new QueryArgument<StringGraphType> {Name = "skuId"},
new QueryArgument<StringGraphType> {Name = "locale"},
new QueryArgument<StringGraphType> {Name = "saleChannel"},
new QueryArgument<NonNullGraphType<SellerObjInputType>> {Name = "sellerObj"}
),
resolve: context =>
resolve: async context =>
{
var name = context.GetArgument<string>("name");
var email = context.GetArgument<string>("email");
var skuId = context.GetArgument<string>("skuId");
var locale = context.GetArgument<string>("locale");
var saleChannel = context.GetArgument<string>("saleChannel");
var sellerObj = context.GetArgument<SellerObj>("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<BooleanGraphType>(
Expand Down
112 changes: 112 additions & 0 deletions dotnet/Models/GetSellerName.cs
Original file line number Diff line number Diff line change
@@ -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; }
}

}
12 changes: 12 additions & 0 deletions dotnet/Models/sellerDataResponse.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
1 change: 1 addition & 0 deletions dotnet/Services/IVtexAPIService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace AvailabilityNotify.Services
public interface IVtexAPIService
{
Task<bool> AvailabilitySubscribe(string email, string sku, string name, string locale, SellerObj sellerObj);
Task<SellerDataResponse> GetSellerName(string saleChannel);
Task<bool> ProcessNotification(AffiliateNotification notification);
Task<bool> ProcessNotification(BroadcastNotification notification);
Task ProcessNotification(AllStatesNotification notification);
Expand Down
79 changes: 79 additions & 0 deletions dotnet/Services/VtexAPIService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -491,11 +491,90 @@ public async Task<bool> 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<SellerDataResponse> 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<GetSellerNameResponse>(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<bool> ProcessNotification(AffiliateNotification notification)
{
bool success = true;
Expand Down
1 change: 1 addition & 0 deletions graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type Mutation {
email: String
skuId: String
locale: String
saleChannel: String
sellerObj: SellerObjInputType!
): Boolean

Expand Down
3 changes: 2 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions react/AvailabilityNotifier.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -18,6 +19,7 @@ interface MutationVariables {
email: string
locale: string
sellerObj: SellerObj
saleChannel: string
}

interface Props {
Expand Down Expand Up @@ -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 = {
Expand All @@ -91,6 +95,7 @@ function AvailabilityNotifier(props: Props) {
name,
email,
locale,
saleChannel,
sellerObj: {
sellerId: sellerObj.sellerId,
sellerName: sellerObj.sellerName,
Expand Down
2 changes: 2 additions & 0 deletions react/graphql/addToAvailabilityNotifierMutation.gql
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ mutation AvailabilitySubscribe(
$email: String
$skuId: String
$locale: String
$saleChannel: String
$sellerObj: SellerObjInputType!
) {
availabilitySubscribe(
name: $name
email: $email
skuId: $skuId
locale: $locale
saleChannel: $saleChannel
sellerObj: $sellerObj
)
}
Loading