Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option parameters on transaction #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 22 additions & 3 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 @@ -29,10 +41,17 @@ export class CartTransaction {

/* Convert to a JSON object suitable to send to the ERP */
toErpTransaction(): any {
return {
let data: any = {
uuid: this.uuid,
product_id: this.productId,
qty: this.qty,
};
if (this.options === null || Object.keys(this.options).length > 0) {
data = {
...data,
options: this.options,
};
}
return data;
}
}
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