round money
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { CacheService } from 'src/modules/utils/cache.service';
|
||||
import { roundQuantity } from 'src/modules/products/utils/quantity.utils';
|
||||
import { Cart } from '../interfaces/cart.interface';
|
||||
|
||||
@Injectable()
|
||||
@@ -20,7 +21,7 @@ export class CartRepository {
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(cachedCart);
|
||||
if (this.isCart(parsed) && parsed.userId === userId && parsed.shopId === shopId) {
|
||||
return parsed;
|
||||
return this.normalizeCartQuantities(parsed);
|
||||
}
|
||||
} catch {
|
||||
// If parsing fails, return null
|
||||
@@ -53,6 +54,17 @@ export class CartRepository {
|
||||
return `${this.CART_KEY_PREFIX}:${userId}:${shopId}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Coerce cached item quantities to numbers (guards against string concat bugs).
|
||||
*/
|
||||
private normalizeCartQuantities(cart: Cart): Cart {
|
||||
for (const item of cart.items) {
|
||||
item.quantity = roundQuantity(item.quantity);
|
||||
}
|
||||
cart.totalItems = roundQuantity(cart.totalItems);
|
||||
return cart;
|
||||
}
|
||||
|
||||
/**
|
||||
* Type guard to check if an object is a Cart
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user