This repository was archived by the owner on Oct 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
ADCIO-2907) feat: pub-sub add-to-cart #38
Open
hanchchch
wants to merge
7
commits into
develop
Choose a base branch
from
fix/ADCIO-2907
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b68ce68
fix: gen-api and change idOnStore to id
hanchchch 68aca38
refactor: lib/constants/error to lib/error
hanchchch 0e65547
feat: wrap action_basket function to detect add-to-cart, and apply pu…
hanchchch 3e67b61
fix: rollback id to idOnStore
hanchchch 28be992
Merge branch 'develop' into fix/ADCIO-2907
hanchchch 943990c
Merge branch 'develop' into fix/ADCIO-2907
hanchchch
10000
Mar 28, 2024
e01b92a
fix: add type hint for action_basket
hanchchch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { | ||
AdcioAnalyticsOnAddToCartParams, | ||
AdcioAnalyticsOnImpressionParams, | ||
AdcioAnalyticsOnPurchaseParams, | ||
} from "lib/analytics"; | ||
|
||
export const EVENT_IMPRESSION = "adcio.impression"; | ||
export const EVENT_ADD_TO_CART = "adcio.add_to_cart"; | ||
export const EVENT_PURCHASE = "adcio.purchase"; | ||
|
||
function dispatch<T>(eventName: string, params: T) { | ||
document.dispatchEvent( | ||
new CustomEvent(eventName, { | ||
detail: params, | ||
}), | ||
); | ||
} | ||
|
||
function listen<T>(eventName: string, callback: (params: T) => void) { | ||
document.addEventListener(eventName, (event) => { | ||
const { detail: params } = event as CustomEvent; | ||
if (!params) { | ||
return; | ||
} | ||
callback(params); | ||
}); | ||
} | ||
|
||
export abstract class AdcioEvent<T> { | ||
protected params: T; | ||
|
||
constructor(params: T) { | ||
this.params = params; | ||
} | ||
|
||
abstract dispatch(): void; | ||
} | ||
|
||
export class ImpressionEvent extends AdcioEvent<AdcioAnalyticsOnImpressionParams> { | ||
static eventName = EVENT_IMPRESSION; | ||
|
||
dispatch() { | ||
dispatch(ImpressionEvent.eventName, this.params); | ||
} | ||
|
||
static listen(callback: (params: AdcioAnalyticsOnImpressionParams) => void) { | ||
listen(ImpressionEvent.eventName, callback); | ||
} | ||
} | ||
|
||
export class AddToCartEvent extends AdcioEvent<AdcioAnalyticsOnAddToCartParams> { | ||
static eventName = EVENT_ADD_TO_CART; | ||
|
||
dispatch() { | ||
dispatch(AddToCartEvent.eventName, this.params); | ||
} | ||
|
||
static listen(callback: (params: AdcioAnalyticsOnAddToCartParams) => void) { | ||
listen(AddToCartEvent.eventName, callback); | ||
} | ||
} | ||
|
||
export class PurchaseEvent extends AdcioEvent<AdcioAnalyticsOnPurchaseParams> { | ||
static eventName = EVENT_PURCHASE; | ||
|
||
dispatch() { | ||
dispatch(PurchaseEvent.eventName, this.params); | ||
} | ||
|
||
static listen(callback: (params: AdcioAnalyticsOnPurchaseParams) => void) { | ||
listen(PurchaseEvent.eventName, callback); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { AdcioAnalyticsOnAddToCartParams } from "lib/analytics"; | ||
|
||
interface ICafe24ActionBasket { | ||
selected_item: string[]; // `${quantity}||${model_name}` | ||
needed: string[]; | ||
optionIds: string[]; | ||
basket_type: string; | ||
ch_ref: string; | ||
command: string; | ||
delvType: string; | ||
display_group: number; | ||
has_option: "T" | "F"; | ||
is_direct_buy: "T" | "F"; | ||
is_individual: "T" | "F"; | ||
main_cate_no: number; | ||
multi_option_data: string; | ||
multi_option_schema: string; | ||
option1: string; | ||
option2: string; | ||
option_type: "T" | "F"; | ||
prd_detail_ship_type: string; | ||
product_max: number; | ||
product_max_type: string; | ||
product_min: number; | ||
product_name: string; | ||
product_no: number; | ||
product_price: number; | ||
quantity: number; | ||
redirect: number; | ||
relation_product: "yes" | "no"; | ||
} | ||
|
||
export class Cafe24ActionBasket { | ||
private data: ICafe24ActionBasket; | ||
|
||
constructor(data: ICafe24ActionBasket) { | ||
this.data = data; | ||
} | ||
|
||
static fromArg(arg: string) { | ||
const data = decodeURI(arg) | ||
.split("&") | ||
.reduce((acc, cur) => { | ||
const [key, value] = cur.split("="); | ||
if (key.endsWith("[]")) { | ||
const trimmedKey = key.slice(0, -2) as keyof ICafe24ActionBasket; | ||
return { | ||
...acc, | ||
[trimmedKey]: [...((acc[trimmedKey] as string[]) || []), value], | ||
}; | ||
} | ||
return { | ||
...acc, | ||
[key]: isNaN(Number(value)) ? value : Number(value), | ||
}; | ||
}, {} as ICafe24ActionBasket); | ||
return new Cafe24ActionBasket(data); | ||
} | ||
|
||
toCart(): AdcioAnalyticsOnAddToCartParams { | ||
return { | ||
cartId: String(Date.now()), | ||
productIdOnStore: String(this.data.product_no), | ||
categoryIdOnStore: String(this.data.main_cate_no), | ||
quantity: this.data.selected_item.reduce( | ||
(acc, cur) => acc + Number(cur.split("||")[0]), | ||
0, | ||
), | ||
}; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
import { AddToCartEvent } from "lib/analytics/events"; | ||
import { getMeta, getQuery } from "lib/utils"; | ||
import { Cafe24ActionBasket } from "./cafe24-action-basket"; | ||
import { ICAFE24, ICAFE24API } from "./cafe24.interface"; | ||
import { Cart, Customer, ClientAPI, Order } from "../client-api.interface"; | ||
import { Customer, ClientAPI, Order } from "../client-api.interface"; | ||
|
||
const CORCA_CAFE24_CLIENT_ID = "8HE5BizGD9agkHIObMfXRF"; | ||
const CORCA_CAFE24_API_VERSION = "2023-06-01"; | ||
const ACTION_BASKET_NAME = `action_basket_${CORCA_CAFE24_CLIENT_ID}`; | ||
|
||
export class Cafe24API implements ClientAPI { | ||
private authorized: boolean; | ||
|
@@ -28,6 +31,28 @@ export class Cafe24API implements ClientAPI { | |
console.warn("Failed to initialize cafe24 api", e); | ||
this.authorized = false; | ||
} | ||
this.observeAddToCart(); | ||
} | ||
|
||
observeAddToCart() { | ||
if ((window as any)[ACTION_BASKET_NAME]) { | ||
return; | ||
} | ||
(window as any)[ACTION_BASKET_NAME] = (window as any).action_basket; | ||
(window as any).action_basket = ( | ||
...args: [ | ||
sType: number, | ||
sGroup: string, | ||
sAction: string, | ||
sParam: string, | ||
aBasketType: string, | ||
bNonDuplicateChk: unknown, | ||
] | ||
) => { | ||
const actionBasket = Cafe24ActionBasket.fromArg(args[3]); // sParam | ||
new AddToCartEvent(actionBasket.toCart()).dispatch(); | ||
(window as any)[ACTION_BASKET_NAME](...args); | ||
}; | ||
} | ||
|
||
getCustomer() { | ||
|
@@ -72,27 +97,6 @@ export class Cafe24API implements ClientAPI { | |
return { idOnStore }; | ||
} | ||
|
||
getCarts() { | ||
return new Promise<Cart[] | null>((resolve, reject) => { | ||
this.api.getCartList((err, res) => { | ||
if (err) { | ||
reject(err); | ||
} else if (!res.carts) { | ||
resolve(null); | ||
} else { | ||
resolve( | ||
res.carts.map((cart) => ({ | ||
id: `${cart.basket_product_no}`, | ||
productIdOnStore: `${cart.product_no}`, | ||
productPrice: cart.product_price, | ||
quantity: Number(cart.quantity), | ||
})), | ||
); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
getOrder() { | ||
return new Promise<Order | null>((resolve) => { | ||
const { order_id, order_product, payed_amount, total_basic_ship_fee } = | ||
|
@@ -102,6 +106,7 @@ export class Cafe24API implements ClientAPI { | |
id: order_id, | ||
products: order_product.map((product) => ({ | ||
idOnStore: `${product.product_no}`, | ||
// categoryIdOnStore: `${product.category_no_1}`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 혹시 여기가 주석처리된 이유가 있을까요? 주석처리된 상태로 남겨둔다면, 간단히 이유를 남겨줘도 좋을 듯 해요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 우리가 원하는 카테고리(구매 플로우에서 처음에 접근한 상품분류 페이지)가 맞는지 확실하지 않아서 주석처리했습니다. |
||
quantity: product.quantity, | ||
price: product.product_price, | ||
subTotalPrice: product.sub_total_price, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,17 @@ | ||
export { APIError } from "./api-error"; | ||
|
||
export const ERROR_CODE = { | ||
SUGGESTION: { | ||
PLACEMENT_NOT_FOUND: 12_001, | ||
INVALID_PLACEMENT_TYPE: 12_003, | ||
NO_ACTIVATED_PLACEMENT: 12_004, | ||
}, | ||
}; | ||
|
||
export const PLACEMENT_ERROR_MESSAGE = { | ||
PLACEMENT_NOT_FOUND: | ||
"Failed to suggestions: The placement id is not registered", | ||
NO_ACTIVATED_PLACEMENT: "Failed to suggestions: The placement is not active", | ||
NOT_UUID: "placementId must be a UUID", | ||
UNKNOWN_ERROR: "Failed to suggestions: An unknown error occurred", | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
URL 객체써서 요렇게 파싱하는건 어떠세요
const data = new URL(arg);
Object.fromEntries(data.searchParams)