Skip to content

Commit

Permalink
fix: removed converted function to reviews ordenation
Browse files Browse the repository at this point in the history
  • Loading branch information
aline-pereira committed Jan 7, 2025
1 parent c93ac15 commit ee31c2f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 51 deletions.
20 changes: 11 additions & 9 deletions konfidency/actions/submitReviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ export interface Props {
review: WriteReview;
}

export default async function action(props: Props, _req: Request, ctx: AppContext): Promise<ResponseWriteReview | null> {

const { customer, api } = ctx
const { review, sku } = props
export default async function action(
props: Props,
_req: Request,
ctx: AppContext,
): Promise<ResponseWriteReview | null> {
const { customer, api } = ctx;
const { review, sku } = props;

try {
const response = await api[`POST /:customer/:sku/review`]({
Expand All @@ -23,13 +26,12 @@ export default async function action(props: Props, _req: Request, ctx: AppContex
body: {
...review,
},
}).then((r) => r.json())
return await response
}).then((r) => r.json());
return await response;
} catch (error) {
const errorObj = error as { name: string; message: string };
logger.error(`{ errorName: ${errorObj.name},
errorMessage: ${errorObj.message} }`);
return null
return null;
}

}
}
24 changes: 13 additions & 11 deletions konfidency/loaders/productDetailsPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { toReview } from "../utils/transform.ts";
import { logger } from "@deco/deco/o11y";
export interface Props {
/**
* @description Rating type, default: helpfulScore
* @default "helpfulScore"
*/
* @description Rating type, default: helpfulScore
* @default "helpfulScore"
*/
sortField?: "helpfulScore" | "created" | "rating";
/**
* @description Default value: asc
Expand All @@ -24,7 +24,8 @@ export interface Props {
page?: number;
}
export default function productDetailsPage(
{ pageSize = 5, page = 1, sortField = "helpfulScore", sortOrder = "asc" }: Props,
{ pageSize = 5, page = 1, sortField = "helpfulScore", sortOrder = "asc" }:
Props,
_req: Request,
ctx: AppContext,
): ExtensionOf<ProductDetailsPage | null> {
Expand All @@ -34,13 +35,14 @@ export default function productDetailsPage(
return null;
}
try {
const reviews = await api["GET /:customer/:sku/summary/:sortField,:sortOrder"]({
customer,
"sortField,:sortOrder": `${sortField},${sortOrder}`,
page,
pageSize,
sku: productDetailsPage.product.inProductGroupWithID as string,
}).then((res) => res.json());
const reviews = await api
["GET /:customer/:sku/summary/:sortField,:sortOrder"]({
customer,
"sortField,:sortOrder": `${sortField},${sortOrder}`,
page,
pageSize,
sku: productDetailsPage.product.inProductGroupWithID as string,
}).then((res) => res.json());
const { aggregateRating, review } = toReview(reviews.reviews[0]);
return {
...productDetailsPage,
Expand Down
2 changes: 1 addition & 1 deletion konfidency/manifest.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// This file SHOULD be checked into source version control.
// This file is automatically updated during development when running `dev.ts`.

import * as $$$0 from "./loaders/productDetailsPage.ts";
import * as $$$$$$$$$0 from "./actions/submitReviews.ts";
import * as $$$0 from "./loaders/productDetailsPage.ts";

const manifest = {
"loaders": {
Expand Down
6 changes: 3 additions & 3 deletions konfidency/utils/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PDPReview, WriteReview, ResponseWriteReview } from "./types.ts";
import type { PDPReview, ResponseWriteReview, WriteReview } from "./types.ts";

export interface API {
"GET /:customer/:sku/summary/:sortField,:sortOrder": {
Expand All @@ -10,7 +10,7 @@ export interface API {
};

"POST /:customer/:sku/review": {
response: ResponseWriteReview
response: ResponseWriteReview;
body: WriteReview;
}
};
}
28 changes: 14 additions & 14 deletions konfidency/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ export interface WriteReview {
}

export interface ResponseWriteReview {
helpful: number;
unhelpful: number;
verified: boolean;
status: string;
_id: string;
created: string;
customer: string;
userId: string;
name: string;
sku: string;
text: string;
recommended: boolean;
rating: number;
}
helpful: number;
unhelpful: number;
verified: boolean;
status: string;
_id: string;
created: string;
customer: string;
userId: string;
name: string;
sku: string;
text: string;
recommended: boolean;
rating: number;
}
15 changes: 2 additions & 13 deletions verified-reviews/utils/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@ export interface PaginationOptions {
| "helpfulrating_DESC";
}

// creating an object to keep backward compatibility
const orderMap = {
date_desc: "date_desc",
date_ASC: "date_asc",
rate_DESC: "rate_desc",
rate_ASC: "rate_asc",
helpfulrating_DESC: "most_helpful",
} as const;

const MessageError = {
ratings:
"🔴⭐ Error on call ratings of Verified Review - probably unidentified product",
Expand Down Expand Up @@ -96,14 +87,12 @@ export const createClient = (params: ConfigVerifiedReviews | undefined) => {
};
/** @description https://documenter.getpostman.com/view/2336519/SVzw6MK5#daf51360-c79e-451a-b627-33bdd0ef66b8 */
const reviews = (
{ productId, count = 5, offset = 0, order: _order = "date_desc" }:
{ productId, count, offset = 0, order = "date_desc" }:
& PaginationOptions
& {
productId: string | string[];
},
) => {
const order = orderMap[_order];

const payload = {
query: "reviews",
product: Array.isArray(productId) ? productId : [productId],
Expand All @@ -122,7 +111,7 @@ export const createClient = (params: ConfigVerifiedReviews | undefined) => {

const fullReview = async ({
productId,
count = 5,
count,
offset = 0,
order,
}: PaginationOptions & {
Expand Down

0 comments on commit ee31c2f

Please sign in to comment.