Skip to content

Commit

Permalink
feat: Add support for guest, customer, and reserve operations on baskets
Browse files Browse the repository at this point in the history
  • Loading branch information
Antaris committed Feb 13, 2024
1 parent 7d658b0 commit 8eb9435
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 8 deletions.
31 changes: 24 additions & 7 deletions apps/TrybeSDK.ConsoleSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
string basketId = bookingFrame.Data.Basket.Id;

// MA - Add an item to the basket.
var itemResponse = await api.Shop.Baskets.AddBasketItemAsync(
var basket = await api.Shop.Baskets.AddBasketItemAsync(
basketId,
new AddBasketItemRequest
{
Expand All @@ -45,15 +45,32 @@
Guests = bookingFrame.Data.Basket.Guests
});

string basketItemId = itemResponse.Data.Items[0].Id;
var guest = basket.Data.Guests[0];
guest.Name = "Matthew Abbott";
guest.IsLeadBooker = true;

itemResponse = await api.Shop.Baskets.DeleteBasketItemAsync(
await api.Shop.Baskets.UpdateBasketGuestsAsync(
basketId,
basketItemId);
new UpdateBasketGuestsRequest
{
Guests = basket.Data.Guests
});

await api.Shop.Baskets.SetCustomerAsync(
basketId,
new Customer
{
FirstName = "Matthew",
LastName = "Abbott",
Email = "[email protected]",
HasPassword = false,
Phone = "07944747565"
});

var response = await api.Shop.Baskets.ReserveBasketAsync(
basketId);

// MA - Fetch the basket.
var response = await api.Shop.Baskets.GetBasketAsync(bookingFrame.Data!.Basket!.Id);
Console.WriteLine(response.Data);
Console.WriteLine(response);

TrybeSettings GetSettings()
{
Expand Down
39 changes: 39 additions & 0 deletions libs/TrybeSDK/Api/Shop/Basket/BasketOperations.Customer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.

using System;
using System.Collections.Generic;
using System.Text;

namespace TrybeSDK.Api;

partial interface IBasketOperations
{
/// <summary>
/// Sets the customer for the basket.
/// </summary>
/// <param name="basketId">The basket ID.</param>
/// <param name="customer">The customer instance.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The updated basket instance.</returns>
Task<TrybeResponse<Basket>> SetCustomerAsync(
string basketId,
Customer customer,
CancellationToken cancellationToken = default);
}

partial class BasketOperations
{
public async Task<TrybeResponse<Basket>> SetCustomerAsync(
string basketId,
Customer customer,
CancellationToken cancellationToken = default)
{
Ensure.IsNotNullOrEmpty(basketId, nameof(basketId));
Ensure.IsNotNull(customer, nameof(customer));

var request = new TrybeRequest<Customer>(HttpMethod.Post, path + $"/{basketId}/customer", customer);

return await client.FetchAsync<Customer, Basket>(request, cancellationToken);
}
}
38 changes: 38 additions & 0 deletions libs/TrybeSDK/Api/Shop/Basket/BasketOperations.Items.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ Task<TrybeResponse<Basket>> UpdateBasketItemAsync(
string basketItemId,
UpdateBasketItemRequest request,
CancellationToken cancellationToken = default);

/// <summary>
/// Updates guests on the basket.
/// HTTP POST /shop/basket/{basketId}/update-guests
/// </summary>
/// <param name="basketId">The ID of the basket.</param>
/// <param name="request">The request representing the update operation.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The updated basket instance.</returns>
Task<TrybeResponse<Basket>> UpdateBasketGuestsAsync(
string basketId,
UpdateBasketGuestsRequest request,
CancellationToken cancellationToken = default);
}

partial class BasketOperations
Expand Down Expand Up @@ -93,6 +106,19 @@ public async Task<TrybeResponse<Basket>> UpdateBasketItemAsync(

return await client.FetchAsync<UpdateBasketItemRequest, Basket>(request, cancellationToken);
}

public async Task<TrybeResponse<Basket>> UpdateBasketGuestsAsync(
string basketId,
UpdateBasketGuestsRequest guestsRequest,
CancellationToken cancellationToken = default)
{
Ensure.IsNotNullOrEmpty(basketId, nameof(basketId));
Ensure.IsNotNull(guestsRequest, nameof(guestsRequest));

var request = new TrybeRequest<UpdateBasketGuestsRequest>(HttpMethod.Post, path + $"/{basketId}/update-guests", guestsRequest);

return await client.FetchAsync<UpdateBasketGuestsRequest, Basket>(request, cancellationToken);
}
}

/// <summary>
Expand Down Expand Up @@ -202,3 +228,15 @@ public class UpdateBasketItemRequest
[JsonPropertyName("time")]
public TimeSpan? Time { get; set; }
}

/// <summary>
/// Represents a request to update guests for the basket.
/// </summary>
public class UpdateBasketGuestsRequest
{
/// <summary>
/// The set of guests for the basket.
/// </summary>
[JsonPropertyName("guests")]
public required GuestList Guests { get; init; }
}
23 changes: 23 additions & 0 deletions libs/TrybeSDK/Api/Shop/Basket/BasketOperations.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.

using System.Threading;

namespace TrybeSDK.Api;

partial interface IShopOperations
Expand Down Expand Up @@ -33,6 +35,16 @@ public partial interface IBasketOperations
Task<TrybeResponse<Basket>> GetBasketAsync(
string basketId,
CancellationToken cancellationToken = default);

/// <summary>
/// Reserves the given basket.
/// </summary>
/// <param name="basketId">The basket ID.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
Task<TrybeResponse<Basket>> ReserveBasketAsync(
string basketId,
CancellationToken cancellationToken = default);
}

public partial class BasketOperations(PathString path, ApiClient client): IBasketOperations
Expand All @@ -47,4 +59,15 @@ public async Task<TrybeResponse<Basket>> GetBasketAsync(

return await client.FetchAsync<Basket>(request, cancellationToken);
}

public async Task<TrybeResponse<Basket>> ReserveBasketAsync(
string basketId,
CancellationToken cancellationToken = default)
{
Ensure.IsNotNullOrEmpty(basketId, nameof(basketId));

var request = new TrybeRequest(HttpMethod.Post, path + $"/{basketId}/reserve");

return await client.FetchAsync<Basket>(request, cancellationToken);
}
}
149 changes: 149 additions & 0 deletions libs/TrybeSDK/Api/Shop/Basket/Customer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.

using System.Text.Json.Serialization;

namespace TrybeSDK.Api;

public class Customer
{
/// <summary>
/// The ID of the customer.
/// </summary>
/// <value>The ID of the customer.</value>
[JsonPropertyName("id")]
public string? Id { get; set; }

/// <summary>
/// The first name of the customer.
/// </summary>
/// <value>The first name of the customer.</value>
[JsonPropertyName("first_name")]
public string? FirstName { get; set; }

/// <summary>
/// The last name of the customer.
/// </summary>
/// <value>The last name of the customer.</value>
[JsonPropertyName("last_name")]
public string? LastName { get; set; }

/// <summary>
/// The customer's full name
/// </summary>
/// <value>The customer's full name</value>
[JsonPropertyName("full_name")]
public string? FullName { get; set; }

/// <summary>
/// The customer's phone number
/// </summary>
/// <value>The customer's phone number</value>
[JsonPropertyName("phone")]
public string? Phone { get; set; }

/// <summary>
/// Whether the user has a password
/// </summary>
/// <value>Whether the user has a password</value>
[JsonPropertyName("has_password")]
public bool? HasPassword { get; set; }

/// <summary>
/// The customer's date of birth
/// </summary>
/// <value>The customer's date of birth</value>
[JsonPropertyName("dob")]
public DateTime? Dob { get; set; }

/// <summary>
/// The ID of the brand this customer belongs to
/// </summary>
/// <value>The ID of the brand this customer belongs to</value>
[JsonPropertyName("brand_id")]
public string? BrandId { get; set; }

/// <summary>
/// The ID of the site this customer belongs to
/// </summary>
/// <value>The ID of the site this customer belongs to</value>
[JsonPropertyName("site_id")]
public string? SiteId { get; set; }

/// <summary>
/// The Stripe Customer ID for this customer
/// </summary>
/// <value>The Stripe Customer ID for this customer</value>
[JsonPropertyName("stripe_id")]
public string? StripeId { get; set; }

/// <summary>
/// The Stripe Customer ID for this customer if they also exist in Trybe's legacy Stripe integration
/// </summary>
/// <value>The Stripe Customer ID for this customer if they also exist in Trybe's legacy Stripe integration</value>
[JsonPropertyName("express_stripe_id")]
public string? ExpressStripeId { get; set; }

/// <summary>
/// The email address of the customer.
/// </summary>
/// <value>The email address of the customer.</value>
[JsonPropertyName("email")]
public string? Email { get; set; }

/// <summary>
/// The preferred locale of the customer
/// </summary>
/// <value>The preferred locale of the customer</value>
[JsonPropertyName("preferred_locale")]
public string? PreferredLocale { get; set; }

/// <summary>
/// The datetime which the customer was locked, or null if it has not been locked.
/// </summary>
/// <value>The datetime which the customer was locked, or null if it has not been locked.</value>
[JsonPropertyName("locked_at")]
public DateTime? LockedAt { get; set; }

/// <summary>
/// The datetime which the customer was created
/// </summary>
/// <value>The datetime which the customer was created</value>
[JsonPropertyName("created_at")]
public DateTime? CreatedAt { get; set; }

/// <summary>
/// The datetime which the customer was last updated
/// </summary>
/// <value>The datetime which the customer was last updated</value>
[JsonPropertyName("updated_at")]
public DateTime? UpdatedAt { get; set; }

/// <summary>
/// The datetime which the customer was anonymised, or null if it has not been.
/// </summary>
/// <value>The datetime which the customer was anonymised, or null if it has not been.</value>
[JsonPropertyName("deleted_at")]
public DateTime? DeletedAt { get; set; }

/// <summary>
/// The datetime which the customer was last active
/// </summary>
/// <value>The datetime which the customer was last active</value>
[JsonPropertyName("last_active_at")]
public DateTime? LastActiveAt { get; set; }

/// <summary>
/// The datetime which the customer verified their email address
/// </summary>
/// <value>The datetime which the customer verified their email address</value>
[JsonPropertyName("email_verified_at")]
public DateTime? EmailVerifiedAt { get; set; }

/// <summary>
/// An external reference for this customer.
/// </summary>
/// <value>An external reference for this customer.</value>
[JsonPropertyName("external_ref")]
public string? ExternalRef { get; set; }
}
2 changes: 1 addition & 1 deletion libs/TrybeSDK/Api/Shop/Basket/Guest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Guest
/// </summary>
/// <value>The guest's full name.</value>
[JsonPropertyName("name")]
public required string Name { get; init; }
public required string Name { get; set; }

/// <summary>
/// Whether this guest is the lead booker
Expand Down
Loading

0 comments on commit 8eb9435

Please sign in to comment.