feat(checkout): refactor purchase completion flow and clear cart state
This commit is contained in:
@@ -34,7 +34,8 @@ describe('CartService', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
tenantServiceMock = {
|
||||
getTenantApiUrl: vi.fn().mockReturnValue('http://api.test/tenants/acme')
|
||||
getTenantApiUrl: vi.fn().mockReturnValue('http://api.test/tenants/acme'),
|
||||
tenant: vi.fn().mockReturnValue({ codigo: 'acme' })
|
||||
};
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
@@ -74,6 +75,18 @@ describe('CartService', () => {
|
||||
req.flush({ data: mockCart });
|
||||
});
|
||||
|
||||
it('should clear cart state locally', () => {
|
||||
service.clearCart();
|
||||
|
||||
expect(service.cart()).toEqual({
|
||||
id: null,
|
||||
tenant_codigo: 'acme',
|
||||
status: 'active',
|
||||
items: [],
|
||||
subtotal: '0.00'
|
||||
});
|
||||
});
|
||||
|
||||
it('should add item and update signal', () => {
|
||||
service.addItem(10, 2).subscribe((res) => {
|
||||
expect(res.data).toEqual(mockCart);
|
||||
|
||||
@@ -23,6 +23,16 @@ export class CartService {
|
||||
return this.tenantService.getTenantApiUrl();
|
||||
}
|
||||
|
||||
clearCart(): void {
|
||||
this.cartState.set({
|
||||
id: null,
|
||||
tenant_codigo: this.tenantService.tenant()?.codigo ?? '',
|
||||
status: 'active',
|
||||
items: [],
|
||||
subtotal: '0.00'
|
||||
});
|
||||
}
|
||||
|
||||
loadCart(): Observable<Cart> {
|
||||
return this.http
|
||||
.get<ApiResponse<Cart>>(`${this.tenantApiUrl}/cart`, {
|
||||
|
||||
@@ -13,7 +13,6 @@ export interface CreatePurchasePayload {
|
||||
}
|
||||
|
||||
export interface PurchaseStatusResponse {
|
||||
payment_status: string | null;
|
||||
status: string | null;
|
||||
}
|
||||
|
||||
@@ -47,9 +46,9 @@ export class CheckoutService {
|
||||
return response;
|
||||
}
|
||||
|
||||
async finalizePurchase(tenantCode: string, purchaseId: number): Promise<PurchaseStatusResponse> {
|
||||
async completePurchase(tenantCode: string, purchaseId: number): Promise<PurchaseStatusResponse> {
|
||||
const response = await firstValueFrom(
|
||||
this.http.post<{ data?: PurchaseStatusResponse } | PurchaseStatusResponse>(`${environment.url}tenants/${tenantCode}/compras/${purchaseId}/finalize`, {})
|
||||
this.http.post<{ data?: PurchaseStatusResponse } | PurchaseStatusResponse>(`${environment.url}tenants/${tenantCode}/compras/${purchaseId}/complete`, {})
|
||||
);
|
||||
|
||||
const purchase = this.extractResponseData<PurchaseStatusResponse>(response);
|
||||
@@ -59,7 +58,6 @@ export class CheckoutService {
|
||||
}
|
||||
|
||||
return {
|
||||
payment_status: purchase.payment_status ?? null,
|
||||
status: purchase.status ?? null
|
||||
};
|
||||
}
|
||||
@@ -76,7 +74,6 @@ export class CheckoutService {
|
||||
}
|
||||
|
||||
return {
|
||||
payment_status: purchase.payment_status ?? null,
|
||||
status: purchase.status ?? null
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user