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

Feat/vnda prices segment #897

Open
wants to merge 6 commits into
base: main
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
4 changes: 4 additions & 0 deletions vnda/actions/cart/addItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { HttpError } from "../../../utils/http.ts";
import cartLoader, { Cart } from "../../loaders/cart.ts";
import { AppContext } from "../../mod.ts";
import { getCartCookie } from "../../utils/cart.ts";
import { getCC } from "../../utils/segment.ts";

export interface Props {
itemId: string;
Expand All @@ -22,11 +23,14 @@ const action = async (
throw new HttpError(400, "Missing cart cookie");
}

const cc = getCC(req);

await api["POST /api/v2/carts/:cartId/items"]({ cartId }, {
body: {
sku: itemId,
quantity,
extra: attributes,
store_coupon_code: cc,
},
});

Expand Down
32 changes: 17 additions & 15 deletions vnda/loaders/productDetailsPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { STALE } from "../../utils/fetch.ts";
import type { RequestURLParam } from "../../website/functions/requestToParam.ts";
import { AppContext } from "../mod.ts";
import { ProductPrice } from "../utils/client/types.ts";
import { getCC } from "../utils/segment.ts";
import { parseSlug, toProduct } from "../utils/transform.ts";

export interface Props {
Expand Down Expand Up @@ -34,6 +35,7 @@ async function loader(
}

const { id } = fromSlug;
const cc = getCC(req);

const getMaybeProduct = async (id: number) => {
try {
Expand All @@ -54,22 +56,23 @@ async function loader(

// Since the Product by ID request don't return the INTL price, is necessary to search all prices and replace them
const getProductPrice = async (id: number): Promise<ProductPrice | null> => {
if (!priceIntl) {
if (!priceIntl && !cc) {
return null;
} else {
try {
const result = await api["GET /api/v2/products/:productId/price"]({
productId: id,
}, STALE);
return result.json();
} catch (error) {
// Make async rendering work
if (error instanceof DOMException && error.name === "AbortError") {
throw error;
}

return null;
}

try {
const result = await api["GET /api/v2/products/:productId/price"]({
productId: id,
coupon_codes: cc ? [cc] : [],
}, STALE);
return result.json();
} catch (error) {
// Make async rendering work
if (error instanceof DOMException && error.name === "AbortError") {
throw error;
}

return null;
}
};

Expand All @@ -84,7 +87,6 @@ async function loader(
if (!maybeProduct || variantsLength === 0) {
return null;
}

const product = toProduct(maybeProduct, variantId, {
url,
priceCurrency: "BRL",
Expand Down
11 changes: 10 additions & 1 deletion vnda/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { AppMiddlewareContext } from "./mod.ts";
import { equal } from "std/testing/asserts.ts";
import { AppMiddlewareContext } from "./mod.ts";
import {
buildSegmentCookie,
getSegmentFromBag,
getSegmentFromCookie,
setSegmentCookie,
setSegmentInBag,
setStoreCC,
} from "./utils/segment.ts";

export const middleware = (
Expand All @@ -14,9 +15,17 @@ export const middleware = (
ctx: AppMiddlewareContext,
) => {
const segment = getSegmentFromBag(ctx);
const url = new URL(req.url);
const cc = url.searchParams.get("cc");

if (cc) {
setStoreCC(cc, ctx.response.headers);
}

if (!segment) {
const segmentFromRequest = buildSegmentCookie(req);
const segmentFromCookie = getSegmentFromCookie(req);

if (
segmentFromRequest !== null &&
!equal(segmentFromRequest, segmentFromCookie)
Expand Down
39 changes: 34 additions & 5 deletions vnda/utils/segment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ interface Segment {
agent: string;
}

export const STORE_CC_COOKIE_NAME = "store_cc";
export const CART_CC_COOKIE_NAME = "cart_cc";
export const SEGMENT_COOKIE_NAME = "vnda_segment";
const SEGMENT = Symbol("segment");
const SIXTYDAYS = new Date(Date.now() + 60 * 24 * 60 * 60 * 1000);
Expand All @@ -19,11 +21,8 @@ export const parse = (cookie: string) => JSON.parse(atob(cookie));
export const buildSegmentCookie = (req: Request): string | null => {
const url = new URL(req.url);
const param = url.searchParams.get("agent");
if (param) {
const partialSegment: string = param;
return partialSegment;
}
return null;

return param || null;
};

export const getSegmentFromCookie = (
Expand All @@ -49,3 +48,33 @@ export const setSegmentCookie = (

return headers;
};

export const getCC = (
req: Request,
): string | undefined => {
const cookies = getCookies(req.headers);
const cookie = cookies[STORE_CC_COOKIE_NAME];
return cookie;
};

export const setStoreCC = (
cc: string,
headers: Headers = new Headers(),
): Headers => {
setCookie(headers, {
value: cc,
name: STORE_CC_COOKIE_NAME,
path: "/",
secure: true,
httpOnly: true,
});
setCookie(headers, {
value: cc,
name: CART_CC_COOKIE_NAME,
path: "/",
secure: true,
httpOnly: true,
});

return headers;
};
7 changes: 3 additions & 4 deletions vnda/utils/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
ProductVariant,
VariantProductSearch,
} from "./openapi/vnda.openapi.gen.ts";
import { getSegmentFromCookie, parse } from "./segment.ts";
import { getCC } from "./segment.ts";

export type VNDAProductGroup = ProductSearch | OProduct;
type VNDAProduct = VariantProductSearch | ProductVariant;
Expand Down Expand Up @@ -511,13 +511,12 @@ export const fetchAndApplyPrices = async (
req: Request,
ctx: AppContext,
): Promise<Product[]> => {
const segmentCookie = getSegmentFromCookie(req);
const segment = segmentCookie ? parse(segmentCookie) : null;
const cc = getCC(req);

const pricePromises = products.map((product) =>
ctx.api["GET /api/v2/products/:productId/price"]({
productId: product.sku,
coupon_codes: segment?.cc ? [segment.cc] : [],
coupon_codes: cc ? [cc] : [],
}, STALE)
);

Expand Down
Loading