Skip to content

Commit

Permalink
Updated the Tax rates page in Ops.
Browse files Browse the repository at this point in the history
  • Loading branch information
uncheckederror committed Nov 2, 2024
1 parent c3bc1c7 commit 02e56a4
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 244 deletions.
231 changes: 110 additions & 121 deletions NumberSearch.DataAccess/InvoiceNinja/Client.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using Flurl.Http;

using System;
using System.Threading.Tasks;

namespace NumberSearch.DataAccess.InvoiceNinja
{
public class Client
public readonly record struct Client(ClientDatum[] data)
{
public ClientDatum[] data { get; set; } = [];

// Disabled because the Array versus object formatting for the ClientLinks is inconsistent and breaks the JSON serialization.
// And we don't need this data because we're not a JavaScript front-end client.
//public ClientMeta meta { get; set; }
Expand Down Expand Up @@ -63,61 +62,58 @@ public static async Task<ClientDatum> GetByIdAsync(string clientId, string token
/// The JSON body returned by the API is a JSON object with a single field called "data" that contains a single object "Datum".
/// Which is just different enough from the standard JSON body that we need this DTO to handle it.
/// </summary>
public class ClientSingle
{
public ClientDatum data { get; set; } = new();
}

public class ClientDatum
{

public string id { get; set; } = string.Empty;
public string user_id { get; set; } = string.Empty;
public string assigned_user_id { get; set; } = string.Empty;
public string name { get; set; } = string.Empty;
public string website { get; set; } = string.Empty;
public string private_notes { get; set; } = string.Empty;
public decimal balance { get; set; }
public string group_settings_id { get; set; } = string.Empty;
public decimal paid_to_date { get; set; }
public decimal credit_balance { get; set; }
public int last_login { get; set; }
public string size_id { get; set; } = string.Empty;
public string public_notes { get; set; } = string.Empty;
public string client_hash { get; set; } = string.Empty;
public string address1 { get; set; } = string.Empty;
public string address2 { get; set; } = string.Empty;
public string phone { get; set; } = string.Empty;
public string city { get; set; } = string.Empty;
public string state { get; set; } = string.Empty;
public string postal_code { get; set; } = string.Empty;
public string country_id { get; set; } = string.Empty;
public string industry_id { get; set; } = string.Empty;
public string custom_value1 { get; set; } = string.Empty;
public string custom_value2 { get; set; } = string.Empty;
public string custom_value3 { get; set; } = string.Empty;
public string custom_value4 { get; set; } = string.Empty;
public string shipping_address1 { get; set; } = string.Empty;
public string shipping_address2 { get; set; } = string.Empty;
public string shipping_city { get; set; } = string.Empty;
public string shipping_state { get; set; } = string.Empty;
public string shipping_postal_code { get; set; } = string.Empty;
public string shipping_country_id { get; set; } = string.Empty;
public ClientSettings settings { get; set; } = new();
public bool is_deleted { get; set; }
public string vat_number { get; set; } = string.Empty;
public string id_number { get; set; } = string.Empty;
public int updated_at { get; set; }
public int archived_at { get; set; }
public int created_at { get; set; }
public string display_name { get; set; } = string.Empty;
public string number { get; set; } = string.Empty;
public ClientContact[] contacts { get; set; } = [];
public object[] documents { get; set; } = [];
public ClientGateway_Tokens[] gateway_tokens { get; set; } = [];
public InvoiceDatum[] invoices { get; set; } = [];

public async Task<ClientDatum> PostAsync(string token)
public readonly record struct ClientSingle (ClientDatum data);

public readonly record struct ClientDatum
(
string id,
string user_id,
string assigned_user_id,
string name,
string website,
string private_notes,
decimal balance,
string group_settings_id,
decimal paid_to_date,
decimal credit_balance,
int last_login,
string size_id,
string public_notes,
string client_hash,
string address1,
string address2,
string phone,
string city,
string state,
string postal_code,
string country_id,
string industry_id,
string custom_value1,
string custom_value2,
string custom_value3,
string custom_value4,
string shipping_address1,
string shipping_address2,
string shipping_city,
string shipping_state,
string shipping_postal_code,
string shipping_country_id,
ClientSettings settings,
bool is_deleted,
string vat_number,
string id_number,
int updated_at,
int archived_at,
int created_at,
string display_name,
string number,
ClientContact[] contacts,
object[] documents,
ClientGateway_Tokens[] gateway_tokens,
InvoiceDatum[] invoices
){

public async Task<ClientDatum> PostAsync(ReadOnlyMemory<char> token)
{
string baseUrl = "https://billing.acceleratenetworks.com/api/v1/";
string endpoint = "clients";
Expand All @@ -130,14 +126,13 @@ public async Task<ClientDatum> PostAsync(string token)
.WithHeader(tokenHeader, token)
.WithHeader(contentHeader, contentHeaderValue)
.PostJsonAsync(new { name })
.ReceiveJson<ClientSingle>()
.ConfigureAwait(false);
.ReceiveJson<ClientSingle>();

// Unwrap the data we want from the single-field parent object.
return result?.data ?? new();
return result.data;
}

public async Task<ClientDatum> PutAsync(string token)
public async Task<ClientDatum> PutAsync(ReadOnlyMemory<char> token)
{
string baseUrl = "https://billing.acceleratenetworks.com/api/v1/";
string endpoint = "clients";
Expand All @@ -151,14 +146,13 @@ public async Task<ClientDatum> PutAsync(string token)
.WithHeader(tokenHeader, token)
.WithHeader(contentHeader, contentHeaderValue)
.PutJsonAsync(new { name, contacts, address1, address2, city, state, postal_code })
.ReceiveJson<ClientSingle>()
.ConfigureAwait(false);
.ReceiveJson<ClientSingle>();

// Unwrap the data we want from the single-field parent object.
return result?.data ?? new();
return result.data;
}

public async Task<ClientDatum> DeleteAsync(string token)
public async Task<ClientDatum> DeleteAsync(ReadOnlyMemory<char> token)
{
string baseUrl = "https://billing.acceleratenetworks.com/api/v1/";
string endpoint = "clients";
Expand All @@ -172,64 +166,59 @@ public async Task<ClientDatum> DeleteAsync(string token)
.WithHeader(tokenHeader, token)
.WithHeader(contentHeader, contentHeaderValue)
.DeleteAsync()
.ReceiveJson<ClientSingle>()
.ConfigureAwait(false);
.ReceiveJson<ClientSingle>();

// Unwrap the data we want from the single-field parent object.
return result?.data ?? new();
return result.data;
}
}


public class ClientSettings
{
public string currency_id { get; set; } = string.Empty;
}

public class ClientContact
{
public string id { get; set; } = string.Empty;
public string first_name { get; set; } = string.Empty;
public string last_name { get; set; } = string.Empty;
public string email { get; set; } = string.Empty;
public int created_at { get; set; }
public int updated_at { get; set; }
public int archived_at { get; set; }
public bool is_primary { get; set; }
public bool is_locked { get; set; }
public string phone { get; set; } = string.Empty;
public string custom_value1 { get; set; } = string.Empty;
public string custom_value2 { get; set; } = string.Empty;
public string custom_value3 { get; set; } = string.Empty;
public string custom_value4 { get; set; } = string.Empty;
public string contact_key { get; set; } = string.Empty;
public bool send_email { get; set; }
public int last_login { get; set; }
public string password { get; set; } = string.Empty;
public string link { get; set; } = string.Empty;
}

public class ClientGateway_Tokens
{
public string id { get; set; } = string.Empty;
public string token { get; set; } = string.Empty;
public string gateway_customer_reference { get; set; } = string.Empty;
public string gateway_type_id { get; set; } = string.Empty;
public string company_gateway_id { get; set; } = string.Empty;
public bool is_default { get; set; }
public ClientMeta meta { get; set; } = new();
public int created_at { get; set; }
public int updated_at { get; set; }
public int archived_at { get; set; }
public bool is_deleted { get; set; }
}

public class ClientMeta
{
public string exp_month { get; set; } = string.Empty;
public string exp_year { get; set; } = string.Empty;
public string brand { get; set; } = string.Empty;
public string last4 { get; set; } = string.Empty;
public int type { get; set; }
}
public readonly record struct ClientSettings(string currency_id);

public readonly record struct ClientContact
(
string id,
string first_name,
string last_name,
string email,
int created_at,
int updated_at,
int archived_at,
bool is_primary,
bool is_locked,
string phone,
string custom_value1,
string custom_value2,
string custom_value3,
string custom_value4,
string contact_key,
bool send_email,
int last_login,
string password,
string link
);

public readonly record struct ClientGateway_Tokens
(
string id,
string token,
string gateway_customer_reference,
string gateway_type_id,
string company_gateway_id,
bool is_default,
ClientMeta meta,
int created_at,
int updated_at,
int archived_at,
bool is_deleted
);

public readonly record struct ClientMeta
(
string exp_month,
string exp_year,
string brand,
string last4,
int type
);
}
46 changes: 20 additions & 26 deletions NumberSearch.DataAccess/InvoiceNinja/TaxRate.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using Flurl.Http;

using System;
using System.Threading.Tasks;

namespace NumberSearch.DataAccess.InvoiceNinja
{
public class TaxRate
public readonly record struct TaxRate(TaxRateDatum[] data)
{
public TaxRateDatum[] data { get; set; } = [];

public static async Task<TaxRate> GetAllAsync(string token)
public static async Task<TaxRate> GetAllAsync(ReadOnlyMemory<char> token)
{
string baseUrl = "https://billing.acceleratenetworks.com/api/v1/";
string endpoint = "tax_rates";
Expand All @@ -18,28 +17,24 @@ public static async Task<TaxRate> GetAllAsync(string token)

return await url
.WithHeader(tokenHeader, token)
.GetJsonAsync<TaxRate>()
.ConfigureAwait(false);
.GetJsonAsync<TaxRate>();
}
}

public class TaxSingle
{
public TaxRateDatum data { get; set; } = new();
}

public class TaxRateDatum
{
public string account_key { get; set; } = string.Empty;
public bool is_owner { get; set; }
public string id { get; set; } = string.Empty;
public string name { get; set; } = string.Empty;
public decimal rate { get; set; } = 0M;
public bool is_inclusive { get; set; }
public int updated_at { get; set; }
public object archived_at { get; set; } = new();

public async Task<TaxRateDatum> PostAsync(string token)
public readonly record struct TaxSingle(TaxRateDatum data);

public readonly record struct TaxRateDatum
(
string account_key,
bool is_owner,
string id,
string name,
decimal rate,
bool is_inclusive,
int updated_at,
object archived_at
) {
public async Task<TaxRateDatum> PostAsync(ReadOnlyMemory<char> token)
{
string baseUrl = "https://billing.acceleratenetworks.com/api/v1/";
string endpoint = "tax_rates";
Expand All @@ -52,11 +47,10 @@ public async Task<TaxRateDatum> PostAsync(string token)
.WithHeader(tokenHeader, token)
.WithHeader(contentHeader, contentHeaderValue)
.PostJsonAsync(new { name, rate })
.ReceiveJson<TaxSingle>()
.ConfigureAwait(false);
.ReceiveJson<TaxSingle>();

// Unwrap the data we want from the single-field parent object.
return result?.data ?? new();
return result.data;
}
}
}
8 changes: 4 additions & 4 deletions NumberSearch.DataAccess/LCGuide/RateCenterLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ public static async Task<RateCenterLookup> GetAsync(ReadOnlyMemory<char> npa, Re
string nxxParameter = $"&nxx={nxx}";
string route = $"{baseUrl}{npaParameter}{nxxParameter}";

var result = await route.GetStringAsync().ConfigureAwait(false);
var result = await route.GetStringAsync();

if (result.Contains("</rc>") && result.Contains("<region>"))
{
var rateCenterStart = result.IndexOf("<rc>") + "<rc>".Length;
var rateCenterEnd = result.IndexOf("</rc>");
var rateCenterText = result[rateCenterStart..rateCenterEnd];
ReadOnlyMemory<char> rateCenterText = result.AsMemory()[rateCenterStart..rateCenterEnd];

var regionStart = result.IndexOf("<region>") + "<region>".Length;
var regionEnd = result.IndexOf("</region>");
var regionText = result[regionStart..regionEnd];
ReadOnlyMemory<char> regionText = result.AsMemory()[regionStart..regionEnd];

return new RateCenterLookup(rateCenterText.AsMemory(), regionText.AsMemory());
return new RateCenterLookup(rateCenterText, regionText);
}
else
{
Expand Down
Loading

0 comments on commit 02e56a4

Please sign in to comment.