Skip to content

Commit

Permalink
[Wake] Feat: add partner token cookie and use in every request that s…
Browse files Browse the repository at this point in the history
…uports It (#917)

* feat: add checkout partner association and partner product listing

* fix: change console to logger

* feat: add partnerToken cookie and use in every req that suports it

* keep compatibility

---------

Co-authored-by: decobot <[email protected]>
  • Loading branch information
IncognitaDev and decobot authored Nov 21, 2024
1 parent 40ace8b commit cb00f7c
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 105 deletions.
6 changes: 5 additions & 1 deletion wake/actions/cart/partnerAssociate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CheckoutPartnerAssociateMutationVariables,
} from "../../utils/graphql/storefront.graphql.gen.ts";
import { parseHeaders } from "../../utils/parseHeaders.ts";
import { setPartnerCookie } from "../../utils/partner.ts";

export interface Props {
partnerAccessToken: string;
Expand All @@ -21,6 +22,7 @@ const action = async (
const { storefront } = ctx;
const cartId = getCartCookie(req.headers);
const headers = parseHeaders(req.headers);
const { partnerAccessToken } = props;

if (!cartId) {
throw new HttpError(400, "Missing cart cookie");
Expand All @@ -30,7 +32,7 @@ const action = async (
CheckoutPartnerAssociateMutation,
CheckoutPartnerAssociateMutationVariables
>({
variables: { checkoutId: cartId, ...props },
variables: { checkoutId: cartId, partnerAccessToken },
...CheckoutPartnerAssociate,
}, { headers });

Expand All @@ -40,6 +42,8 @@ const action = async (
setCartCookie(ctx.response.headers, checkoutId);
}

setPartnerCookie(ctx.response.headers, partnerAccessToken);

return data.checkout ?? {};
};

Expand Down
3 changes: 3 additions & 0 deletions wake/actions/cart/partnerDisassociate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CheckoutPartnerDisassociateMutationVariables,
} from "../../utils/graphql/storefront.graphql.gen.ts";
import { parseHeaders } from "../../utils/parseHeaders.ts";
import { deletePartnerCookie } from "../../utils/partner.ts";

const action = async (
_props: unknown,
Expand Down Expand Up @@ -36,6 +37,8 @@ const action = async (
setCartCookie(ctx.response.headers, checkoutId);
}

deletePartnerCookie(ctx.response.headers);

return data.checkout ?? {};
};

Expand Down
105 changes: 60 additions & 45 deletions wake/loaders/productDetailsPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
GetProductQueryVariables,
} from "../utils/graphql/storefront.graphql.gen.ts";
import { parseHeaders } from "../utils/parseHeaders.ts";
import { getPartnerCookie } from "../utils/partner.ts";
import { parseSlug, toBreadcrumbList, toProduct } from "../utils/transform.ts";

export interface Props {
Expand All @@ -25,12 +26,14 @@ export interface Props {
async function loader(
props: Props,
req: Request,
ctx: AppContext,
ctx: AppContext
): Promise<ProductDetailsPage | null> {
const url = new URL(req.url);
const { slug, buyTogether, includeSameParent } = props;
const { storefront } = ctx;

const partnerAccessToken = getPartnerCookie(req.headers);

const headers = parseHeaders(req.headers);

if (!slug) return null;
Expand All @@ -45,23 +48,24 @@ async function loader(
const { buyList: wakeBuyList } = await storefront.query<
BuyListQuery,
BuyListQueryVariables
>({
variables: { id: productId },
...GetBuyList,
}, {
headers,
});
>(
{
variables: { id: productId, partnerAccessToken },
...GetBuyList,
},
{
headers,
}
);

const buyListProducts = await Promise.all(
wakeBuyList?.buyListProducts?.map(async (
buyListProduct,
) => {
wakeBuyList?.buyListProducts?.map(async (buyListProduct) => {
if (!buyListProduct) return;

const { productId, includeSameParent, quantity } = buyListProduct;

const buyListProductPage = await ctx.invoke.wake.loaders
.productDetailsPage({
const buyListProductPage =
await ctx.invoke.wake.loaders.productDetailsPage({
// 'slug' its just to fit the parse function of loader
slug: `slug-${productId}`,
includeSameParent,
Expand All @@ -77,72 +81,83 @@ async function loader(
});

return buyListProductPage.product;
}) ?? [],
}) ?? []
).then((maybeProductList) =>
maybeProductList.filter((node): node is Product => Boolean(node))
);

const { product: wakeProduct } = await storefront.query<
GetProductQuery,
GetProductQueryVariables
>({
variables: { productId, includeParentIdVariants: includeSameParent },
...GetProduct,
}, {
headers,
});
>(
{
variables: {
productId,
includeParentIdVariants: includeSameParent,
partnerAccessToken,
},
...GetProduct,
},
{
headers,
}
);

const wakeProductOrBuyList = wakeProduct || wakeBuyList;

if (!wakeProductOrBuyList) {
return null;
}

const variantsItems = await ctx.invoke.wake.loaders.productList({
first: MAXIMUM_REQUEST_QUANTITY,
sortDirection: "ASC",
sortKey: "RANDOM",
filters: { productId: [productId] },
}) ?? [];
const variantsItems =
(await ctx.invoke.wake.loaders.productList({
first: MAXIMUM_REQUEST_QUANTITY,
sortDirection: "ASC",
sortKey: "RANDOM",
filters: { productId: [productId] },
})) ?? [];

const buyTogetherItens =
buyTogether && !!wakeProductOrBuyList.buyTogether?.length
? await ctx.invoke.wake.loaders.productList({
first: MAXIMUM_REQUEST_QUANTITY,
sortDirection: "ASC",
sortKey: "RANDOM",
filters: {
productId: wakeProductOrBuyList.buyTogether?.map((bt) =>
bt!.productId
),
mainVariant: true,
},
getVariations: true,
}) ?? []
? (await ctx.invoke.wake.loaders.productList({
first: MAXIMUM_REQUEST_QUANTITY,
sortDirection: "ASC",
sortKey: "RANDOM",
filters: {
productId: wakeProductOrBuyList.buyTogether?.map(
(bt) => bt!.productId
),
mainVariant: true,
},
getVariations: true,
})) ?? []
: [];

const product = toProduct(
wakeProductOrBuyList,
{ base: url },
variantsItems,
variantId,
variantId
);
return {
"@type": "ProductDetailsPage",
breadcrumbList: toBreadcrumbList(wakeProductOrBuyList.breadcrumbs ?? [], {
base: url,
}, product),
breadcrumbList: toBreadcrumbList(
wakeProductOrBuyList.breadcrumbs ?? [],
{
base: url,
},
product
),
product: {
...product,
isAccessoryOrSparePartFor: buyListProducts,
isRelatedTo: buyTogetherItens?.map(
(buyItem) => {
isRelatedTo:
buyTogetherItens?.map((buyItem) => {
return {
...buyItem,
additionalType: "BuyTogether",
};
},
) ?? [],
}) ?? [],
},
seo: {
canonical: product.isVariantOf?.url ?? "",
Expand Down
4 changes: 3 additions & 1 deletion wake/loaders/productList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ProductFragment,
} from "../utils/graphql/storefront.graphql.gen.ts";
import { parseHeaders } from "../utils/parseHeaders.ts";
import { getPartnerCookie } from "../utils/partner.ts";
import { toProduct } from "../utils/transform.ts";

export interface StockFilter {
Expand Down Expand Up @@ -141,14 +142,15 @@ const productListLoader = async (
): Promise<Product[] | null> => {
const url = new URL(req.url);
const { storefront } = ctx;
const partnerAccessToken = getPartnerCookie(req.headers);

const headers = parseHeaders(req.headers);

const data = await storefront.query<
GetProductsQuery,
GetProductsQueryVariables
>({
variables: props,
variables: { ...props, partnerAccessToken },
...GetProducts,
}, {
headers,
Expand Down
Loading

0 comments on commit cb00f7c

Please sign in to comment.