Skip to content
Draft
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
7 changes: 0 additions & 7 deletions accounts/yourViews.ts

This file was deleted.

79 changes: 52 additions & 27 deletions commerce/yourViews/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import DataLoader from "https://esm.sh/dataloader@2.2.2";
import { fetchAPI } from "../../utils/fetchAPI.ts";
import { Ratings, Reviews } from "./types.ts";
import { Rating, Ratings, Reviews } from "./types.ts";

export type ClientYourViews = ReturnType<typeof createClient>;

Expand All @@ -14,42 +15,66 @@ export interface PaginationOptions {
}

const baseUrl = "http://service.yourviews.com.br";
export type RatingFetcher = (productIds: string) => Promise<Rating | undefined>;
const fetcherPool: Record<
string,
RatingFetcher
> = {};

export const createClient = ({ token, appId }: ConfigYourViews) => {
const sameOrder = (
productIds: readonly string[],
elements: Rating[],
): (Rating | undefined)[] => {
const ordered = new Array<Rating | undefined>(productIds.length).fill(
undefined,
);
for (let i = 0; i < productIds.length; i++) {
const ratingIdx = elements.findIndex((e) => e.ProductId === productIds[i]);
if (ratingIdx !== -1) {
ordered[i] = elements[ratingIdx];
elements.splice(ratingIdx, 1); // remove already found rating
}
}
return ordered;
};

const getRatingFetcher = (
{ appId, token }: ConfigYourViews,
): RatingFetcher => {
const key = `${appId}${token}`;
if (fetcherPool[key]) {
return fetcherPool[key];
}
const headers = {
"Authorization": token,
};

/** @description https://yourviews.freshdesk.com/support/solutions/articles/5000756179-buscar-as-estrelas-nas-prateleiras */
const rating = (
{ page = 0, count = 1, productId }: PaginationOptions & {
productId: string;
},
) =>
fetchAPI<Ratings>(
`${baseUrl}/api/${appId}/review/reviewshelf?${new URLSearchParams(
{ page, count, productIds: productId } as unknown as Record<
string,
string
>,
)}`,
{ headers, withProxyCache: true },
);

/** @description https://yourviews.freshdesk.com/support/solutions/articles/5000756179-buscar-as-estrelas-nas-prateleiras */
const ratings = (
{ page = 0, count, productIds }: PaginationOptions & {
productIds: string[];
},
) =>
const dl = new DataLoader((productIds: readonly string[]) =>
fetchAPI<Ratings>(
`${baseUrl}/api/${appId}/review/reviewshelf?${new URLSearchParams({
page,
count: count ?? productIds.length,
page: 0,
count: productIds.length,
productIds: productIds.join(","),
} as unknown as Record<string, string>)}`,
{ headers, withProxyCache: true },
);
).then((rat) => sameOrder(productIds, rat.Element))
);
return fetcherPool[key] = dl.load.bind(dl);
};

export const createClient = ({ token, appId }: ConfigYourViews) => {
const headers = {
"Authorization": token,
};
const batchFetcher = getRatingFetcher({ token, appId });

/** @description https://yourviews.freshdesk.com/support/solutions/articles/5000756179-buscar-as-estrelas-nas-prateleiras */
const rating = batchFetcher;

/** @description https://yourviews.freshdesk.com/support/solutions/articles/5000756179-buscar-as-estrelas-nas-prateleiras */
const ratings = (
productIds: string[],
) => Promise.all(productIds.map(batchFetcher));

/** @description https://yourviews.freshdesk.com/support/solutions/articles/5000740469-buscar-reviews-de-produto */
const review = (
Expand Down
95 changes: 0 additions & 95 deletions commerce/yourViews/withYourViews.ts

This file was deleted.

32 changes: 32 additions & 0 deletions extensions/yourViews.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Product } from "deco-sites/std/commerce/types.ts";
import {
ConfigYourViews,
RatingFetcher,
} from "deco-sites/std/commerce/yourViews/client.ts";
import { ExtensionOf } from "https://denopkg.com/deco-cx/live@3c5ca2344ff1d8168085a3d5685c57100e6bdedb/blocks/extension.ts";
import { createClient } from "../commerce/yourViews/client.ts";

export type Props = ConfigYourViews;

const aggregateRatingFor =
(fetcher: RatingFetcher) => async ({ isVariantOf }: Product) => {
const productId = isVariantOf!.productGroupID;
const rating = await fetcher(productId);

return rating
? {
"@type": "AggregateRating" as const,
ratingCount: rating.TotalRatings,
ratingValue: rating.Rating,
}
: undefined;
};

export default function AddYourViews(config: Props): ExtensionOf<Product> {
const client = createClient(config);
const aggregateRating = aggregateRatingFor(client.rating.bind(client));

return {
aggregateRating,
};
}
2 changes: 1 addition & 1 deletion import_map.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"imports": {
"deco-sites/std/": "./",
"$live/": "https://denopkg.com/deco-cx/live@1.0.0-rc.46/",
"$live/": "https://denopkg.com/deco-cx/live@extension-loaders/",
"partytown/": "https://deno.land/x/partytown@0.2.1/",
"$fresh/": "https://deno.land/x/fresh@1.1.3/",
"preact": "https://esm.sh/preact@10.11.0",
Expand Down
Loading