@misiki/gocommerce-connector 0.16.0 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2854,6 +2854,71 @@ var ReviewService = class extends BaseService {
2854
2854
  };
2855
2855
  var reviewService = new ReviewService();
2856
2856
 
2857
+ // src/services/google-review-service.ts
2858
+ var PLUGIN = "google-reviews";
2859
+ var GoogleReviewService = class extends BaseService {
2860
+ facts = new StoreFacts(this._fetch);
2861
+ /**
2862
+ * Google's rating and its most relevant reviews, read live. Google's terms
2863
+ * forbid keeping them, so nothing here caches them either, and every call
2864
+ * is one the store pays Google for: call it when the section is in view.
2865
+ */
2866
+ async place() {
2867
+ if (!await this.switchOn("show_place")) return null;
2868
+ const p = await this.orNull(() => this.data("/x/google-reviews/place"));
2869
+ if (!p?.user_rating_count) return null;
2870
+ return {
2871
+ rating: p.rating,
2872
+ count: p.user_rating_count,
2873
+ mapsUrl: p.google_maps_uri ?? "",
2874
+ reviews: (p.reviews ?? []).map((r) => ({
2875
+ author: r.author?.name ?? "",
2876
+ authorUrl: r.author?.uri ?? "",
2877
+ authorPhoto: r.author?.photo_uri ?? "",
2878
+ rating: r.rating,
2879
+ text: r.text ?? "",
2880
+ language: r.language ?? "",
2881
+ when: r.relative_time ?? "",
2882
+ visited: r.visit_date ?? "",
2883
+ url: r.google_maps_uri ?? "",
2884
+ reportUrl: r.flag_uri ?? ""
2885
+ }))
2886
+ };
2887
+ }
2888
+ /**
2889
+ * What Google Customer Reviews' opt-in is rendered with, for an order placed
2890
+ * in this browser: its access token is kept here, and nowhere else can ask.
2891
+ * Every field is the order's own; the keys are Google's, to pass unchanged
2892
+ * to `gapi.surveyoptin.render`.
2893
+ */
2894
+ async survey(orderNumber) {
2895
+ const kept = orderNumber ? placedOrder(orderNumber) : void 0;
2896
+ if (!kept || !await this.switchOn("customer_reviews")) return null;
2897
+ return this.orNull(
2898
+ () => this.data(`/x/google-reviews/survey/${encodeURIComponent(orderNumber)}`, {
2899
+ query: { token: kept.token }
2900
+ })
2901
+ );
2902
+ }
2903
+ async switchOn(setting) {
2904
+ try {
2905
+ return (await this.facts.plugins()).get(PLUGIN)?.settings?.[setting] === true;
2906
+ } catch {
2907
+ return false;
2908
+ }
2909
+ }
2910
+ /** Off, used up for the day, or refused by Google: nothing to show. Anything else still throws. */
2911
+ async orNull(call) {
2912
+ try {
2913
+ return await call();
2914
+ } catch (err) {
2915
+ if (err instanceof GoCommerceError && [404, 429, 502].includes(err.status)) return null;
2916
+ throw err;
2917
+ }
2918
+ }
2919
+ };
2920
+ var googleReviewService = new GoogleReviewService();
2921
+
2857
2922
  // src/services/menu-service.ts
2858
2923
  function linkFor(item) {
2859
2924
  const t = item.target ?? "";
@@ -3075,6 +3140,14 @@ var StoreService = class extends BaseService {
3075
3140
  ...basePlugins.isProductReviewsAndRatings,
3076
3141
  ...flag(plugins.has("product-reviews"))
3077
3142
  },
3143
+ // Google Customer Reviews (ext/google-reviews): the confirmation page's survey offer,
3144
+ // whose fields the engine builds from the order, and Google's store widget, which
3145
+ // belongs to the same programme and so needs the survey on.
3146
+ googleReviewsOptIn: {
3147
+ ...basePlugins.googleReviewsOptIn,
3148
+ ...flag(googleSetting(plugins, "customer_reviews") === true)
3149
+ },
3150
+ googleStoreWidget: storeWidget(plugins),
3078
3151
  // The footer's sign-up box posts to ext/newsletter, and the theme editor's footer
3079
3152
  // switch (D131) can only show it while that is on.
3080
3153
  newsletter: { ...basePlugins.newsletter, ...flag(plugins.has("newsletter")) },
@@ -3117,6 +3190,12 @@ function regionName(code) {
3117
3190
  return code;
3118
3191
  }
3119
3192
  }
3193
+ var googleSetting = (plugins, key) => plugins.get("google-reviews")?.settings?.[key];
3194
+ function storeWidget(plugins) {
3195
+ const where = googleSetting(plugins, "store_widget");
3196
+ if (googleSetting(plugins, "customer_reviews") !== true || !where) return { active: false };
3197
+ return { active: true, position: where === "Bottom left" ? "LEFT_BOTTOM" : "RIGHT_BOTTOM" };
3198
+ }
3120
3199
  var LOGIN_TYPES = { phone_otp: "PHONE", email_or_phone: "BOTH", email_password: "EMAIL" };
3121
3200
 
3122
3201
  // src/services/page-service.ts
@@ -3822,6 +3901,7 @@ exports.EnquiryService = EnquiryService;
3822
3901
  exports.FaqService = FaqService;
3823
3902
  exports.FeedbackService = FeedbackService;
3824
3903
  exports.GalleryService = GalleryService;
3904
+ exports.GoogleReviewService = GoogleReviewService;
3825
3905
  exports.HomeService = HomeService;
3826
3906
  exports.InitService = InitService;
3827
3907
  exports.LayoutService = LayoutService;
@@ -3876,6 +3956,7 @@ exports.enquiryService = enquiryService;
3876
3956
  exports.faqService = faqService;
3877
3957
  exports.feedbackService = feedbackService;
3878
3958
  exports.galleryService = galleryService;
3959
+ exports.googleReviewService = googleReviewService;
3879
3960
  exports.homeService = homeService;
3880
3961
  exports.initService = initService;
3881
3962
  exports.layoutService = layoutService;