cart
This commit is contained in:
@@ -7,19 +7,19 @@ export class CartRepository {
|
||||
private readonly CART_TTL = 3600; // 1 hour in seconds
|
||||
private readonly CART_KEY_PREFIX = 'cart';
|
||||
|
||||
constructor(private readonly cacheService: CacheService) {}
|
||||
constructor(private readonly cacheService: CacheService) { }
|
||||
|
||||
/**
|
||||
* Get cart by user and shop
|
||||
*/
|
||||
async findByUserAndRestaurant(userId: string, restaurantId: string): Promise<Cart | null> {
|
||||
const cacheKey = this.getCacheKey(userId, restaurantId);
|
||||
async findByUserAndShop(userId: string, shopId: string): Promise<Cart | null> {
|
||||
const cacheKey = this.getCacheKey(userId, shopId);
|
||||
const cachedCart = await this.cacheService.get<string>(cacheKey);
|
||||
|
||||
if (cachedCart) {
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(cachedCart);
|
||||
if (this.isCart(parsed) && parsed.userId === userId && parsed.restaurantId === restaurantId) {
|
||||
if (this.isCart(parsed) && parsed.userId === userId && parsed.shopId === shopId) {
|
||||
return parsed;
|
||||
}
|
||||
} catch {
|
||||
@@ -34,23 +34,23 @@ export class CartRepository {
|
||||
* Save cart to cache
|
||||
*/
|
||||
async save(cart: Cart): Promise<void> {
|
||||
const cacheKey = this.getCacheKey(cart.userId, cart.restaurantId);
|
||||
const cacheKey = this.getCacheKey(cart.userId, cart.shopId);
|
||||
await this.cacheService.set(cacheKey, JSON.stringify(cart), this.CART_TTL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete cart from cache
|
||||
*/
|
||||
async delete(userId: string, restaurantId: string): Promise<void> {
|
||||
const cacheKey = this.getCacheKey(userId, restaurantId);
|
||||
async delete(userId: string, shopId: string): Promise<void> {
|
||||
const cacheKey = this.getCacheKey(userId, shopId);
|
||||
await this.cacheService.del(cacheKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate cache key for cart
|
||||
*/
|
||||
private getCacheKey(userId: string, restaurantId: string): string {
|
||||
return `${this.CART_KEY_PREFIX}:${userId}:${restaurantId}`;
|
||||
private getCacheKey(userId: string, shopId: string): string {
|
||||
return `${this.CART_KEY_PREFIX}:${userId}:${shopId}`;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,7 +63,7 @@ export class CartRepository {
|
||||
const cart = obj as Record<string, unknown>;
|
||||
return (
|
||||
typeof cart.userId === 'string' &&
|
||||
typeof cart.restaurantId === 'string' &&
|
||||
typeof cart.shopId === 'string' &&
|
||||
Array.isArray(cart.items) &&
|
||||
typeof cart.subTotal === 'number' &&
|
||||
typeof cart.itemsDiscount === 'number' &&
|
||||
|
||||
Reference in New Issue
Block a user