Skip to content

Commit

Permalink
[IMP] Add option parameters on transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultrey committed Feb 5, 2024
1 parent 8a5de3c commit 76d02bf
Show file tree
Hide file tree
Showing 7 changed files with 4,408 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Shopinvader async cart library",
"license": "MIT",
"author": "ACSONE SA/NV",
"version": "0.1.0",
"version": "0.2.3",
"type": "module",
"main": "dist/src/index.js",
"module": "dist/src/index.js",
Expand Down
14 changes: 11 additions & 3 deletions src/cartData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { CartLineData } from './cartLineData.js';
import { CartTransaction } from './cartTransaction.js';
import { isEqual } from './utils.js';

export class CartData {
public hasPendingTransactions: boolean = false;
Expand Down Expand Up @@ -30,13 +31,20 @@ export class CartData {
this.lines.push(line);
}

getLine(productId: number): CartLineData | undefined {
return this.lines.find(line => line.productId === productId);
getLine(productId: number, options?: any): CartLineData | undefined {
return this.lines.find(
line =>
line.productId === productId &&
isEqual(line?.options || null, options || null)
);
}

applyTransactions(transactions: CartTransaction[]) {
for (const transaction of transactions) {
const cartLineData = this.getLine(transaction.productId);
const cartLineData = this.getLine(
transaction.productId,
transaction?.options || null
);
if (!cartLineData) {
this.addLine(CartLineData.fromTransaction(transaction));
} else {
Expand Down
12 changes: 11 additions & 1 deletion src/cartLineData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ export class CartLineData {

public qty: number;

public options: any | null = null;

public erpCartLine: any;

constructor(
hasPendingTransactions: boolean,
productId: number,
qty: number,
options?: any,
erpCartLine?: any
) {
this.hasPendingTransactions = hasPendingTransactions;
this.productId = productId;
this.qty = qty;
this.options = options || null;
this.erpCartLine = erpCartLine;
}

Expand All @@ -28,12 +32,18 @@ export class CartLineData {
false,
erpCartLine.product_id,
erpCartLine.qty,
erpCartLine.options,
erpCartLine
);
}

static fromTransaction(transaction: CartTransaction): CartLineData {
return new this(true, transaction.productId, transaction.qty);
return new this(
true,
transaction.productId,
transaction.qty,
transaction.options
);
}

applyTransaction(transaction: CartTransaction) {
Expand Down
17 changes: 15 additions & 2 deletions src/cartTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) ACSONE SA/NV 2022

import { v4 as uuidv4 } from 'uuid';
import { isEqual } from './utils.js';

export class CartTransaction {
public readonly uuid: string;
Expand All @@ -9,14 +10,25 @@ export class CartTransaction {

public qty: number;

constructor(productId: number, qty: number, uuid?: string) {
public options: any | null = null;

constructor(
productId: number,
qty: number,
uuid?: string | null,
options?: any
) {
this.productId = productId;
this.qty = qty || 0;
this.uuid = uuid || uuidv4();
this.options = options || null;
}

isForSameCartLine(other: CartTransaction): boolean {
return other.productId === this.productId;
return (
other.productId === this.productId &&
isEqual(other?.options || null, this.options)
);
}

merge(other: CartTransaction): boolean {
Expand All @@ -33,6 +45,7 @@ export class CartTransaction {
uuid: this.uuid,
product_id: this.productId,
qty: this.qty,
options: this.options,
};
}
}
22 changes: 22 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* function to compare deeply two values and return true if they are equal */

export const isEqual = (objA: any, objB: any) => {
const keysA = typeof objA === 'object' ? Object.keys(objA || {}) : [];
const keysB = typeof objB === 'object' ? Object.keys(objB || {}) : [];

if (keysA.length !== keysB.length) {
return false;
}

for (const key of keysA) {
if (typeof objA[key] === 'object' && typeof objB[key] === 'object') {
if (!isEqual(objA[key], objB[key])) {
return false;
}
}
if (objA[key] !== objB[key]) {
return false;
}
}
return true;
};
2 changes: 1 addition & 1 deletion src/webStorageCartStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class WebStorageCartStorage implements CartStorage {
try {
const res = JSON.parse(data) as Data;
res.transactions = res.transactions.map(
t => new CartTransaction(t.productId, t.qty, t.uuid)
t => new CartTransaction(t.productId, t.qty, t.uuid, t?.options || {})
);
return res;
} catch (error) {
Expand Down
Loading

0 comments on commit 76d02bf

Please sign in to comment.