Skip to content

Commit 2de1b13

Browse files
committed
trustpilot fixes
1 parent bd527ae commit 2de1b13

File tree

4 files changed

+88
-24
lines changed

4 files changed

+88
-24
lines changed

components/trustpilot/actions/fetch-product-reviews/fetch-product-reviews.mjs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ export default {
1414
"businessUnitId",
1515
],
1616
},
17+
sku: {
18+
propDefinition: [
19+
trustpilot,
20+
"sku",
21+
],
22+
},
23+
productUrl: {
24+
propDefinition: [
25+
trustpilot,
26+
"productUrl",
27+
],
28+
},
1729
stars: {
1830
propDefinition: [
1931
trustpilot,
@@ -62,6 +74,8 @@ export default {
6274
async run({ $ }) {
6375
const {
6476
businessUnitId,
77+
sku,
78+
productUrl,
6579
stars,
6680
sortBy,
6781
limit,
@@ -74,6 +88,8 @@ export default {
7488
try {
7589
const result = await this.trustpilot.getProductReviews({
7690
businessUnitId,
91+
sku,
92+
productUrl,
7793
stars,
7894
sortBy,
7995
limit,

components/trustpilot/common/constants.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const WEBHOOK_EVENTS = {
1111

1212
export const ENDPOINTS = {
1313
// Business Units
14-
BUSINESS_UNITS: "/business-units",
14+
BUSINESS_UNITS: "/business-units/search",
1515
BUSINESS_UNIT_BY_ID: "/business-units/{businessUnitId}",
1616

1717
// Public Reviews

components/trustpilot/trustpilot.app.mjs

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ export default defineApp({
3131
type: "string",
3232
label: "Business Unit ID",
3333
description: "The unique identifier for your business unit on Trustpilot",
34-
async options() {
34+
async options(page, prevContext, query) {
3535
try {
3636
const businessUnits = await this.searchBusinessUnits({
37-
query: "",
38-
limit: 20,
37+
query,
38+
page,
3939
});
4040
return businessUnits.map(({
4141
id, displayName, name: { identifying },
@@ -54,6 +54,18 @@ export default defineApp({
5454
label: "Review ID",
5555
description: "The unique identifier for a review",
5656
},
57+
sku: {
58+
type: "string",
59+
label: "SKU",
60+
description: "Filter by SKU",
61+
optional: true,
62+
},
63+
productUrl: {
64+
type: "string",
65+
label: "Product URL",
66+
description: "Filter by product URL",
67+
optional: true,
68+
},
5769
stars: {
5870
type: "integer",
5971
label: "Star Rating",
@@ -106,25 +118,50 @@ export default defineApp({
106118
},
107119
methods: {
108120
// Authentication and base request methods
109-
_getAuthHeaders() {
121+
_isPrivateURL(url) {
122+
return url.includes("private");
123+
},
124+
125+
_getAuthHeadersForPrivateURL() {
126+
if (!this.$auth?.oauth_access_token) {
127+
throw new Error("Authentication required: OAuth token is required for private requests");
128+
} else {
129+
return {
130+
"Authorization": `Bearer ${this.$auth.oauth_access_token}`,
131+
};
132+
}
133+
},
134+
135+
_getAuthHeadersForPublicURL() {
136+
if (!this.$auth?.api_key) {
137+
throw new Error("Authentication required: API key is required for public requests");
138+
} else {
139+
return {
140+
"apikey": this.$auth.api_key,
141+
};
142+
}
143+
},
144+
145+
_getAuthHeaders(url) {
110146
const headers = {
111147
"Content-Type": "application/json",
112148
"User-Agent": "Pipedream/1.0",
113149
};
114150

115-
if (!this.$auth?.api_key && !this.$auth?.oauth_access_token) {
116-
throw new Error("Authentication required: Configure either API key or OAuth token");
117-
}
118-
119-
if (this.$auth?.api_key) {
120-
headers["apikey"] = this.$auth.api_key;
121-
}
151+
const isPrivate = this._isPrivateURL(url);
152+
console.log("isPrivate", isPrivate);
122153

123-
if (this.$auth?.oauth_access_token) {
124-
headers["Authorization"] = `Bearer ${this.$auth.oauth_access_token}`;
154+
if (isPrivate) {
155+
return {
156+
...headers,
157+
...this._getAuthHeadersForPrivateURL(),
158+
};
159+
} else {
160+
return {
161+
...headers,
162+
...this._getAuthHeadersForPublicURL(),
163+
};
125164
}
126-
127-
return headers;
128165
},
129166

130167
async _makeRequest({
@@ -182,14 +219,13 @@ export default defineApp({
182219
},
183220

184221
async searchBusinessUnits({
185-
query = "", limit = DEFAULT_LIMIT, offset = 0,
222+
query = "", page = 1,
186223
} = {}) {
187224
const response = await this._makeRequest({
188225
endpoint: ENDPOINTS.BUSINESS_UNITS,
189226
params: {
190227
query,
191-
limit,
192-
offset,
228+
page,
193229
},
194230
});
195231

@@ -265,6 +301,8 @@ export default defineApp({
265301
async _getReviews({
266302
endpoint,
267303
businessUnitId,
304+
sku = null,
305+
productUrl = null,
268306
stars = null,
269307
sortBy = SORT_OPTIONS.CREATED_AT_DESC,
270308
limit = DEFAULT_LIMIT,
@@ -277,7 +315,13 @@ export default defineApp({
277315
throw new Error("Invalid business unit ID");
278316
}
279317

318+
if (sku === null && productUrl === null) {
319+
throw new Error("Either SKU or product URL is required");
320+
}
321+
280322
const params = {
323+
sku,
324+
productUrl,
281325
stars,
282326
orderBy: sortBy,
283327
perPage: limit,

pnpm-lock.yaml

Lines changed: 9 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)