@misiki/gocommerce-connector 0.4.3 → 0.4.5

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.
@@ -2025,12 +2025,31 @@ var OrderService = class extends BaseService {
2025
2025
  async listPublic(_args = {}) {
2026
2026
  return emptyPage();
2027
2027
  }
2028
- async getOrderByEmailAndOTP() {
2029
- throw new NotSupportedError(
2030
- "OrderService",
2031
- "getOrderByEmailAndOTP",
2032
- "the engine sends no one-time codes; a guest reads an order with its access token"
2028
+ /**
2029
+ * Sends a 4-digit code to the email or phone on the order, when `contact`
2030
+ * is that email or phone. The engine answers the same either way, so this
2031
+ * resolves whether or not anything was sent.
2032
+ */
2033
+ async requestOrderCode({ orderNo, contact }) {
2034
+ const res = await this.data(
2035
+ `/api/orders/${encodeURIComponent(orderNo.trim())}/code`,
2036
+ { method: "POST", body: { contact: contact.trim() } }
2037
+ );
2038
+ return { expiresInMinutes: res?.expires_in_minutes ?? 10 };
2039
+ }
2040
+ /**
2041
+ * Opens an order with the code `requestOrderCode` sent. The token it answers
2042
+ * is kept like an order placed in this browser, so the order page and
2043
+ * `getOrder` read it again without another code.
2044
+ */
2045
+ async getOrderByEmailAndOTP({ orderNo, otp }) {
2046
+ const number = orderNo.trim();
2047
+ const res = await this.data(
2048
+ `/api/orders/${encodeURIComponent(number)}/code/verify`,
2049
+ { method: "POST", body: { code: String(otp).trim() } }
2033
2050
  );
2051
+ rememberOrder({ number: res.order.number, token: res.access_token, cartId: "" });
2052
+ return { data: [toOrder(res.order)] };
2034
2053
  }
2035
2054
  };
2036
2055
  var clean2 = (v) => v && v !== "undefined" && v !== "null" ? v : void 0;
@@ -2167,6 +2186,19 @@ var UserService = class extends BaseService {
2167
2186
  }
2168
2187
  };
2169
2188
  var AuthService = class extends UserService {
2189
+ facts = new StoreFacts(this._fetch);
2190
+ /**
2191
+ * How this store signs customers in: `email_password` or `phone_otp`.
2192
+ * A store whose accounts module says nothing signs in by email.
2193
+ */
2194
+ async loginMethod() {
2195
+ try {
2196
+ const method = (await this.facts.plugins()).get("identity")?.settings?.login_method;
2197
+ return method === "phone_otp" ? "phone_otp" : "email_password";
2198
+ } catch {
2199
+ return "email_password";
2200
+ }
2201
+ }
2170
2202
  async forgotPassword({ email }) {
2171
2203
  return this.account("/x/identity/password-reset", {
2172
2204
  method: "POST",
@@ -2193,11 +2225,21 @@ var AuthService = class extends UserService {
2193
2225
  return { errorCode: err.code ?? "invalid_token", message: err.message };
2194
2226
  }
2195
2227
  }
2196
- async getOtp() {
2197
- throw new NotSupportedError("AuthService", "getOtp", "sign in with an email and password");
2228
+ /** Texts a 4-digit sign-in code to `phone`, which needs its country code. */
2229
+ async getOtp({ phone }) {
2230
+ const res = await this.account("/x/identity/otp", {
2231
+ method: "POST",
2232
+ body: { phone: String(phone ?? "").trim() }
2233
+ });
2234
+ return { expiresInMinutes: res?.expires_in_minutes ?? 10 };
2198
2235
  }
2199
- async verifyOtp() {
2200
- throw new NotSupportedError("AuthService", "verifyOtp", "sign in with an email and password");
2236
+ /** Signs in the account on `phone` with the code it was sent, opening one the first time. */
2237
+ async verifyOtp({ phone, otp }) {
2238
+ const auth = await this.account("/x/identity/otp/verify", {
2239
+ method: "POST",
2240
+ body: { phone: String(phone ?? "").trim(), code: String(otp ?? "").trim() }
2241
+ });
2242
+ return this.signedIn(auth);
2201
2243
  }
2202
2244
  async joinAsAdmin() {
2203
2245
  throw new NotSupportedError("AuthService", "joinAsAdmin", "operators are invited from the admin panel");
@@ -2552,7 +2594,11 @@ var StoreService = class extends BaseService {
2552
2594
  ...flag(plugins.has("product-reviews"))
2553
2595
  }
2554
2596
  },
2555
- ...menus?.length ? { menu: menus } : {}
2597
+ ...menus?.length ? { menu: menus } : {},
2598
+ // The one way this store signs customers in (D116), which the sign-in
2599
+ // form reads as PHONE or EMAIL. Without the accounts module the static
2600
+ // record says.
2601
+ ...plugins.has("identity") ? { loginType: plugins.get("identity")?.settings?.login_method === "phone_otp" ? "PHONE" : "EMAIL" } : {}
2556
2602
  };
2557
2603
  }
2558
2604
  };