Whenever you're ready, walk me through how you'd approach this.
Xero's invoicing service has a method that finalizes an invoice: it converts the subtotal to NZD, applies GST, writes the total back to the database, and alerts finance if the invoice is unusually large. Nobody has ever written a unit test against it, and a new hire has been asked to refactor it and say exactly where test coverage should go.
public class InvoiceService
{
public decimal FinalizeInvoice(int invoiceId, decimal subtotal, string currencyCode)
{
var connection = new SqlConnection("Server=prod-sql;Database=Xero;Trusted_Connection=True;");
connection.Open();
decimal converted = CurrencyConverter.ConvertToNZD(subtotal, currencyCode);
decimal gst = converted * 0.15m;
decimal total = converted + gst;
var query = "UPDATE Invoices SET Total = " + total + " WHERE Id = " + invoiceId;
var cmd = new SqlCommand(query, connection);
cmd.ExecuteNonQuery();
if (total > 10000)
{
EmailNotifier.SendHighValueAlert(invoiceId, total);
}
Console.WriteLine($"Invoice {invoiceId} finalized at {DateTime.Now}");
return total;
}
}
Refactor this method and explain, point by point, everything that currently makes it impossible to unit test, where you would add coverage once it's testable, and any other issue you notice along the way.