update email template

This commit is contained in:
baodev0
2026-09-09 16:56:22 +07:00
parent d3d9f76ba0
commit 2365ef72e9
210 changed files with 6335 additions and 4688 deletions
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Tests\Feature\Api\Service;
use App\Enums\Invoice\InvoiceStatus;
use App\Enums\Order\OrderStatus;
use App\Models\Customer;
use App\Models\Invoice;
@@ -123,4 +124,105 @@ class RenewServiceTest extends TestCase
$response->assertStatus(400);
$this->assertSame(0, Invoice::where('customer_id', $this->customer->id)->count());
}
public function test_renew_reuses_original_order_instead_of_creating_a_new_one(): void
{
$service = $this->createServiceWithOldPrice(90_000);
$originalOrderId = $service->orderItem->order_id;
ProductPricings::create([
'product_id' => $this->product->id,
'billing_cycle' => 'monthly',
'price' => 100_000,
'is_active' => true,
]);
$response = $this->actingAsCustomer()->postJson(
"/v1/client/dich-vu/{$service->id}/renew",
['months' => 1],
);
$response->assertOk();
$response->assertJsonPath('data.order_id', $originalOrderId);
// Order 1:N Invoice — no second Order row is ever created for a renewal.
$this->assertSame(1, Order::where('customer_id', $this->customer->id)->count());
$this->assertDatabaseHas('invoices', [
'id' => $response->json('data.invoice_id'),
'order_id' => $originalOrderId,
'invoice_type' => 2,
]);
}
public function test_renewal_invoice_has_a_billing_period_and_is_not_duplicated_for_the_same_period(): void
{
$service = $this->createServiceWithOldPrice(90_000);
ProductPricings::create([
'product_id' => $this->product->id,
'billing_cycle' => 'monthly',
'price' => 100_000,
'is_active' => true,
]);
$response = $this->actingAsCustomer()->postJson(
"/v1/client/dich-vu/{$service->id}/renew",
['months' => 1],
);
$response->assertOk();
$invoiceId = $response->json('data.invoice_id');
$this->assertDatabaseHas('invoices', ['id' => $invoiceId]);
$invoice = Invoice::find($invoiceId);
$this->assertNotNull($invoice->billing_period_start);
$this->assertNotNull($invoice->billing_period_end);
// Calling renew again while the invoice is still open must return the SAME
// invoice, not create a duplicate for the same billing period.
$second = $this->actingAsCustomer()->postJson(
"/v1/client/dich-vu/{$service->id}/renew",
['months' => 1],
);
$second->assertOk();
$second->assertJsonPath('data.invoice_id', $invoiceId);
$this->assertSame(1, Invoice::where('customer_id', $this->customer->id)->where('invoice_type', 2)->count());
}
/**
* Đúng flow đề bài mục 6: invoice gia hạn quá due_date -> OVERDUE, KHÔNG tạo invoice mới.
* Verify khách bấm "Gia hạn" lại LÚC invoice đang OVERDUE (chưa qua billing cycle tiếp theo)
* vẫn nhận về đúng invoice cũ, không bị tạo trùng.
*/
public function test_renew_while_existing_invoice_is_overdue_returns_the_same_invoice(): void
{
$service = $this->createServiceWithOldPrice(90_000);
ProductPricings::create([
'product_id' => $this->product->id,
'billing_cycle' => 'monthly',
'price' => 100_000,
'is_active' => true,
]);
$first = $this->actingAsCustomer()->postJson(
"/v1/client/dich-vu/{$service->id}/renew",
['months' => 1],
);
$first->assertOk();
$invoiceId = $first->json('data.invoice_id');
// Invoice quá due_date -> cron invoices:check-overdue chuyển sang OVERDUE (flow thật).
Invoice::whereKey($invoiceId)->update(['due_date' => now()->subDay()->toDateString()]);
$this->artisan('invoices:check-overdue')->assertExitCode(0);
$this->assertSame(InvoiceStatus::OVERDUE->value, (int) Invoice::find($invoiceId)->status);
// Khách bấm "Gia hạn" lại trong khi invoice đang OVERDUE — không được tạo invoice #2.
$second = $this->actingAsCustomer()->postJson(
"/v1/client/dich-vu/{$service->id}/renew",
['months' => 1],
);
$second->assertOk();
$second->assertJsonPath('data.invoice_id', $invoiceId);
$this->assertSame(1, Invoice::where('customer_id', $this->customer->id)->where('invoice_type', 2)->count());
}
}