import common from "../common/polling.mjs";
import {
DEFAULT_LIMIT,
MAX_LIMIT,
} from "../../common/constants.mjs";
export default {
...common,
key: "trustpilot-new-product-reviews",
name: "New Product Reviews",
description: "Emit new event when a customer posts a new product review on Trustpilot. This source periodically polls the Trustpilot API to detect new product reviews. Each event contains the complete review data including star rating, review text, product information, consumer details, and timestamps. Perfect for monitoring product feedback, analyzing customer satisfaction trends, and triggering automated responses or alerts for specific products.",
version: "0.1.0",
type: "source",
dedupe: "unique",
methods: {
...common.methods,
generateSummary(review) {
const stars = review.stars || "N/A";
const consumerName = review.consumer?.name || "Anonymous";
const productName = review.product?.name || "Unknown Product";
const businessUnit = this.businessUnitId || "Unknown";
return `New ${stars}-star product review by ${consumerName} for "${productName}" (${businessUnit})`;
},
getFetchParams() {
return {
businessUnitId: this.businessUnitId,
perPage: DEFAULT_LIMIT,
page: 1,
};
},
async fetchReviews($, params) {
const perPage = params.perPage ?? DEFAULT_LIMIT;
let page = params.page ?? 1;
let result = await this.trustpilot.fetchProductReviews($, {
...params,
page,
});
let all = Array.isArray(result.reviews)
? result.reviews
: [];
let lastPageSize = all.length;
while (lastPageSize === perPage && all.length < MAX_LIMIT) {
page += 1;
const next = await this.trustpilot.fetchProductReviews($, {
...params,
page,
});
const chunk = Array.isArray(next.reviews) ?
next.reviews :
[];
if (chunk.length === 0) break;
all = all.concat(chunk);
lastPageSize = chunk.length;
result = next;
}
result.reviews = all.slice(0, MAX_LIMIT);
return result;
},
filterNewReviews(reviews, lastReviewTime) {
const lastTs = Number(lastReviewTime) || 0;
const toMs = (d) => new Date(d).getTime();
return lastTs
? reviews.filter((r) => toMs(r.createdAt) > lastTs)
: reviews;
},
_getLastReviewTime() {
return this.db.get("lastReviewTime");
},
_setLastReviewTime(time) {
const timeMs = typeof time === "string"
? new Date(time).getTime()
: time;
this.db.set("lastReviewTime", timeMs);
},
},
};