From cb5601c68b5a9af03ba0c59bc00636a1f068d463 Mon Sep 17 00:00:00 2001 From: Kukks Date: Fri, 24 Jul 2020 08:13:21 +0200 Subject: [PATCH] unarchive endpoint + formatting --- .../Models/CreateInvoiceRequest.cs | 90 ++++--------------- .../Models/UpdateInvoiceRequest.cs | 7 ++ .../GreenField/InvoiceController.cs | 31 +++++-- .../Services/Invoices/InvoiceRepository.cs | 6 +- 4 files changed, 48 insertions(+), 86 deletions(-) create mode 100644 BTCPayServer.Client/Models/UpdateInvoiceRequest.cs diff --git a/BTCPayServer.Client/Models/CreateInvoiceRequest.cs b/BTCPayServer.Client/Models/CreateInvoiceRequest.cs index 04c7acada..103bb5415 100644 --- a/BTCPayServer.Client/Models/CreateInvoiceRequest.cs +++ b/BTCPayServer.Client/Models/CreateInvoiceRequest.cs @@ -8,17 +8,9 @@ namespace BTCPayServer.Client.Models public class CreateInvoiceRequest { [JsonProperty(ItemConverterType = typeof(DecimalDoubleStringJsonConverter))] - public decimal Amount - { - get; - set; - } + public decimal Amount { get; set; } - public string Currency - { - get; - set; - } + public string Currency { get; set; } public ProductInformation Metadata { get; set; } @@ -46,67 +38,31 @@ namespace BTCPayServer.Client.Models public class BuyerInformation { [JsonProperty(PropertyName = "buyerName")] - public string BuyerName - { - get; - set; - } + public string BuyerName { get; set; } [JsonProperty(PropertyName = "buyerEmail")] - public string BuyerEmail - { - get; - set; - } + public string BuyerEmail { get; set; } [JsonProperty(PropertyName = "buyerCountry")] - public string BuyerCountry - { - get; - set; - } + public string BuyerCountry { get; set; } [JsonProperty(PropertyName = "buyerZip")] - public string BuyerZip - { - get; - set; - } + public string BuyerZip { get; set; } [JsonProperty(PropertyName = "buyerState")] - public string BuyerState - { - get; - set; - } + public string BuyerState { get; set; } [JsonProperty(PropertyName = "buyerCity")] - public string BuyerCity - { - get; - set; - } + public string BuyerCity { get; set; } [JsonProperty(PropertyName = "buyerAddress2")] - public string BuyerAddress2 - { - get; - set; - } + public string BuyerAddress2 { get; set; } [JsonProperty(PropertyName = "buyerAddress1")] - public string BuyerAddress1 - { - get; - set; - } + public string BuyerAddress1 { get; set; } [JsonProperty(PropertyName = "buyerPhone")] - public string BuyerPhone - { - get; - set; - } + public string BuyerPhone { get; set; } } public class ProductInformation @@ -114,29 +70,13 @@ namespace BTCPayServer.Client.Models public string OrderId { get; set; } public string PosData { get; set; } - public string ItemDesc - { - get; - set; - } + public string ItemDesc { get; set; } - public string ItemCode - { - get; - set; - } + public string ItemCode { get; set; } - public bool Physical - { - get; - set; - } + public bool Physical { get; set; } - public decimal? TaxIncluded - { - get; - set; - } + public decimal? TaxIncluded { get; set; } } } } diff --git a/BTCPayServer.Client/Models/UpdateInvoiceRequest.cs b/BTCPayServer.Client/Models/UpdateInvoiceRequest.cs new file mode 100644 index 000000000..dea03c3b1 --- /dev/null +++ b/BTCPayServer.Client/Models/UpdateInvoiceRequest.cs @@ -0,0 +1,7 @@ +namespace BTCPayServer.Client.Models +{ + public class UpdateInvoiceRequest + { + public bool Archived { get; set; } + } +} diff --git a/BTCPayServer/Controllers/GreenField/InvoiceController.cs b/BTCPayServer/Controllers/GreenField/InvoiceController.cs index f97be7892..0caf6db2c 100644 --- a/BTCPayServer/Controllers/GreenField/InvoiceController.cs +++ b/BTCPayServer/Controllers/GreenField/InvoiceController.cs @@ -2,6 +2,7 @@ using System; using System.Linq; using System.Threading.Tasks; using BTCPayServer.Client; +using BTCPayServer.Client.Models; using BTCPayServer.Payments; using BTCPayServer.Security; using BTCPayServer.Services.Invoices; @@ -9,7 +10,6 @@ using BTCPayServer.Validation; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.ModelBinding; using NBitcoin; using NBitpayClient; using CreateInvoiceRequest = BTCPayServer.Client.Models.CreateInvoiceRequest; @@ -42,7 +42,11 @@ namespace BTCPayServer.Controllers.GreenField return NotFound(); } - var invoices = await _invoiceRepository.GetInvoices(new InvoiceQuery() {StoreId = new[] {store.Id}}); + var invoices = + await _invoiceRepository.GetInvoices(new InvoiceQuery() + { + StoreId = new[] {store.Id}, IncludeArchived = false + }); return Ok(invoices.Select(ToModel)); } @@ -79,13 +83,7 @@ namespace BTCPayServer.Controllers.GreenField return NotFound(); } - var invoice = await _invoiceRepository.GetInvoice(invoiceId, true); - if (invoice.StoreId != store.Id) - { - return NotFound(); - } - - await _invoiceRepository.ToggleInvoiceArchival(invoiceId, true); + await _invoiceRepository.ToggleInvoiceArchival(invoiceId, true, storeId); return Ok(); } @@ -145,6 +143,21 @@ namespace BTCPayServer.Controllers.GreenField return Ok(ToModel(invoice)); } + [Authorize(Policy = Policies.CanModifyStoreSettings, + AuthenticationSchemes = AuthenticationSchemes.Greenfield)] + [HttpPost("~/api/v1/stores/{storeId}/invoices/{invoiceId}")] + public async Task UpdateInvoice(string storeId, string invoiceId, UpdateInvoiceRequest request) + { + var store = HttpContext.GetStoreData(); + if (store == null) + { + return NotFound(); + } + + await _invoiceRepository.ToggleInvoiceArchival(invoiceId, request.Archived, storeId); + return await GetInvoice(storeId, invoiceId); + } + public InvoiceData ToModel(InvoiceEntity entity) { return new InvoiceData() diff --git a/BTCPayServer/Services/Invoices/InvoiceRepository.cs b/BTCPayServer/Services/Invoices/InvoiceRepository.cs index 59eb21398..0deadd243 100644 --- a/BTCPayServer/Services/Invoices/InvoiceRepository.cs +++ b/BTCPayServer/Services/Invoices/InvoiceRepository.cs @@ -428,12 +428,14 @@ retry: } } - public async Task ToggleInvoiceArchival(string invoiceId, bool archived) + public async Task ToggleInvoiceArchival(string invoiceId, bool archived, string storeId = null) { using (var context = _ContextFactory.CreateContext()) { var invoiceData = await context.FindAsync(invoiceId).ConfigureAwait(false); - if (invoiceData == null || invoiceData.Archived == archived) + if (invoiceData == null || invoiceData.Archived == archived || + (storeId != null && + invoiceData.StoreDataId.Equals(storeId, StringComparison.InvariantCultureIgnoreCase))) return; invoiceData.Archived = archived; await context.SaveChangesAsync().ConfigureAwait(false);