@pradip1995/segment-jewelry-account-pages 0.1.4 → 0.1.6
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.
package/package.json
CHANGED
|
@@ -8,7 +8,9 @@ import {
|
|
|
8
8
|
normalizeIndianPhone,
|
|
9
9
|
} from "@pradip1995/commerce-core/util/phone"
|
|
10
10
|
import {
|
|
11
|
+
requestContactChange,
|
|
11
12
|
sendRegistrationOtp,
|
|
13
|
+
verifyContactChange,
|
|
12
14
|
verifyRegistrationOtp,
|
|
13
15
|
} from "@pradip1995/segment-login-template/auth-server"
|
|
14
16
|
import type { OtpVerificationComponent } from "@pradip1995/segment-login-template/otp-component"
|
|
@@ -17,6 +19,18 @@ import {
|
|
|
17
19
|
isNextRedirect,
|
|
18
20
|
safeErrorMessage,
|
|
19
21
|
} from "@pradip1995/segment-primitives/is-next-redirect"
|
|
22
|
+
import {
|
|
23
|
+
EMAIL_OTP_ERRORS,
|
|
24
|
+
PHONE_OTP_ERRORS,
|
|
25
|
+
isValidEmail,
|
|
26
|
+
normalizeEmail,
|
|
27
|
+
phoneDigits,
|
|
28
|
+
resolveEmailOtpFlow,
|
|
29
|
+
resolvePhoneOtpFlow,
|
|
30
|
+
verifiedFieldAction,
|
|
31
|
+
type ContactOtpFlow,
|
|
32
|
+
type VerifiedFieldAction,
|
|
33
|
+
} from "./profile-contact-field"
|
|
20
34
|
|
|
21
35
|
function getInitials(name: string): string {
|
|
22
36
|
const parts = name.trim().split(/\s+/)
|
|
@@ -26,21 +40,77 @@ function getInitials(name: string): string {
|
|
|
26
40
|
return name.slice(0, 2).toUpperCase() || "RA"
|
|
27
41
|
}
|
|
28
42
|
|
|
29
|
-
function phoneDigits(value: string): string {
|
|
30
|
-
return value.replace(/\D/g, "")
|
|
31
|
-
}
|
|
32
|
-
|
|
33
43
|
function readVerified(
|
|
34
44
|
customer: HttpTypes.StoreCustomer,
|
|
35
45
|
field: "email_verified" | "phone_verified"
|
|
36
46
|
): boolean {
|
|
37
|
-
const direct = (customer as Record<string, unknown>)[field]
|
|
47
|
+
const direct = (customer as unknown as Record<string, unknown>)[field]
|
|
38
48
|
if (direct === true || direct === "true") return true
|
|
39
49
|
const meta = customer.metadata as Record<string, unknown> | undefined
|
|
40
50
|
const fromMeta = meta?.[field]
|
|
41
51
|
return fromMeta === true || fromMeta === "true"
|
|
42
52
|
}
|
|
43
53
|
|
|
54
|
+
const COMPACT_BTN_STYLE = {
|
|
55
|
+
minHeight: "2.85rem",
|
|
56
|
+
padding: "0 1.15rem",
|
|
57
|
+
borderRadius: "8px",
|
|
58
|
+
flexShrink: 0,
|
|
59
|
+
} as const
|
|
60
|
+
|
|
61
|
+
function ContactActions({
|
|
62
|
+
action,
|
|
63
|
+
verifyPending,
|
|
64
|
+
verifyLabel,
|
|
65
|
+
verifyAriaLabel,
|
|
66
|
+
editAriaLabel,
|
|
67
|
+
onVerify,
|
|
68
|
+
onEdit,
|
|
69
|
+
}: {
|
|
70
|
+
action: VerifiedFieldAction
|
|
71
|
+
verifyPending: boolean
|
|
72
|
+
verifyLabel: string
|
|
73
|
+
verifyAriaLabel: string
|
|
74
|
+
editAriaLabel: string
|
|
75
|
+
onVerify: () => void
|
|
76
|
+
onEdit: () => void
|
|
77
|
+
}) {
|
|
78
|
+
if (action === "edit") {
|
|
79
|
+
return (
|
|
80
|
+
<>
|
|
81
|
+
<span className="modern-verified-pill">✓ Verified</span>
|
|
82
|
+
<button
|
|
83
|
+
type="button"
|
|
84
|
+
onClick={onEdit}
|
|
85
|
+
className="modern-btn modern-btn--outline"
|
|
86
|
+
style={COMPACT_BTN_STYLE}
|
|
87
|
+
aria-label={editAriaLabel}
|
|
88
|
+
>
|
|
89
|
+
Edit
|
|
90
|
+
</button>
|
|
91
|
+
</>
|
|
92
|
+
)
|
|
93
|
+
}
|
|
94
|
+
if (action === "verified") {
|
|
95
|
+
return <span className="modern-verified-pill">✓ Verified</span>
|
|
96
|
+
}
|
|
97
|
+
if (action === "verify") {
|
|
98
|
+
return (
|
|
99
|
+
<button
|
|
100
|
+
type="button"
|
|
101
|
+
onClick={onVerify}
|
|
102
|
+
disabled={verifyPending}
|
|
103
|
+
className="modern-btn modern-btn--primary"
|
|
104
|
+
style={COMPACT_BTN_STYLE}
|
|
105
|
+
aria-label={verifyAriaLabel}
|
|
106
|
+
>
|
|
107
|
+
{verifyPending ? "Sending…" : verifyLabel}
|
|
108
|
+
</button>
|
|
109
|
+
)
|
|
110
|
+
}
|
|
111
|
+
return null
|
|
112
|
+
}
|
|
113
|
+
|
|
44
114
|
export default function ModernAccountProfile({
|
|
45
115
|
customer,
|
|
46
116
|
countryCode,
|
|
@@ -53,17 +123,25 @@ export default function ModernAccountProfile({
|
|
|
53
123
|
const [message, setMessage] = useState<string | null>(null)
|
|
54
124
|
const [phone, setPhone] = useState(customer.phone || "")
|
|
55
125
|
const [phoneError, setPhoneError] = useState<string | null>(null)
|
|
126
|
+
const [editingPhone, setEditingPhone] = useState(false)
|
|
127
|
+
const [email, setEmail] = useState(customer.email || "")
|
|
128
|
+
const [emailError, setEmailError] = useState<string | null>(null)
|
|
129
|
+
const [editingEmail, setEditingEmail] = useState(false)
|
|
56
130
|
const [otpOpen, setOtpOpen] = useState(false)
|
|
57
131
|
const [otp, setOtp] = useState("")
|
|
58
132
|
const [otpToken, setOtpToken] = useState<string | null>(null)
|
|
59
133
|
const [otpType, setOtpType] = useState<"email_verification" | "phone_verification">(
|
|
60
134
|
"email_verification"
|
|
61
135
|
)
|
|
136
|
+
const [otpFlow, setOtpFlow] = useState<ContactOtpFlow>("verify_existing")
|
|
62
137
|
const [otpError, setOtpError] = useState<string | null>(null)
|
|
63
138
|
const [otpPending, setOtpPending] = useState(false)
|
|
64
139
|
const [phoneVerified, setPhoneVerified] = useState(() =>
|
|
65
140
|
readVerified(customer, "phone_verified")
|
|
66
141
|
)
|
|
142
|
+
const [emailVerified, setEmailVerified] = useState(() =>
|
|
143
|
+
readVerified(customer, "email_verified")
|
|
144
|
+
)
|
|
67
145
|
|
|
68
146
|
async function saveProfile(
|
|
69
147
|
_prev: { ok: boolean; error: string | null },
|
|
@@ -97,12 +175,24 @@ export default function ModernAccountProfile({
|
|
|
97
175
|
})
|
|
98
176
|
|
|
99
177
|
const phoneReady = isValidIndianPhone(phone)
|
|
100
|
-
const
|
|
178
|
+
const emailReady = isValidEmail(email)
|
|
179
|
+
const phoneLocked = phoneVerified && !editingPhone
|
|
180
|
+
const emailLocked = emailVerified && !editingEmail
|
|
181
|
+
const phoneAction = verifiedFieldAction({
|
|
182
|
+
verified: phoneVerified,
|
|
183
|
+
editing: editingPhone,
|
|
184
|
+
ready: phoneReady,
|
|
185
|
+
})
|
|
186
|
+
const emailAction = verifiedFieldAction({
|
|
187
|
+
verified: emailVerified,
|
|
188
|
+
editing: editingEmail,
|
|
189
|
+
ready: emailReady,
|
|
190
|
+
})
|
|
101
191
|
|
|
102
192
|
async function ensurePhoneSavedForVerification(): Promise<boolean> {
|
|
103
193
|
setPhoneError(null)
|
|
104
194
|
if (!phoneReady) {
|
|
105
|
-
setPhoneError(
|
|
195
|
+
setPhoneError(PHONE_OTP_ERRORS.empty)
|
|
106
196
|
return false
|
|
107
197
|
}
|
|
108
198
|
|
|
@@ -128,13 +218,47 @@ export default function ModernAccountProfile({
|
|
|
128
218
|
setOtpPending(true)
|
|
129
219
|
setOtpError(null)
|
|
130
220
|
setPhoneError(null)
|
|
221
|
+
setEmailError(null)
|
|
131
222
|
try {
|
|
132
223
|
if (type === "phone_verification") {
|
|
133
|
-
const
|
|
134
|
-
|
|
224
|
+
const resolved = resolvePhoneOtpFlow({
|
|
225
|
+
currentPhone: customer.phone,
|
|
226
|
+
draftE164: normalizeIndianPhone(phone),
|
|
227
|
+
})
|
|
228
|
+
if (!resolved.ok) {
|
|
229
|
+
setPhoneError(PHONE_OTP_ERRORS[resolved.reason])
|
|
230
|
+
return
|
|
231
|
+
}
|
|
232
|
+
setOtpFlow(resolved.flow)
|
|
233
|
+
let res
|
|
234
|
+
if (resolved.flow === "change_contact") {
|
|
235
|
+
res = await requestContactChange({ phone: resolved.phone })
|
|
236
|
+
} else {
|
|
237
|
+
const saved = await ensurePhoneSavedForVerification()
|
|
238
|
+
if (!saved) return
|
|
239
|
+
res = await sendRegistrationOtp(customer.id, type)
|
|
240
|
+
}
|
|
241
|
+
if (!res.success) throw new Error(res.error || "Failed to send code")
|
|
242
|
+
setOtpToken(res.token ?? null)
|
|
243
|
+
setOtp("")
|
|
244
|
+
setOtpOpen(true)
|
|
245
|
+
return
|
|
135
246
|
}
|
|
136
|
-
|
|
137
|
-
|
|
247
|
+
|
|
248
|
+
const resolved = resolveEmailOtpFlow({
|
|
249
|
+
currentEmail: customer.email,
|
|
250
|
+
draftEmail: email,
|
|
251
|
+
})
|
|
252
|
+
if (!resolved.ok) {
|
|
253
|
+
setEmailError(EMAIL_OTP_ERRORS[resolved.reason])
|
|
254
|
+
return
|
|
255
|
+
}
|
|
256
|
+
setOtpFlow(resolved.flow)
|
|
257
|
+
const res =
|
|
258
|
+
resolved.flow === "change_contact"
|
|
259
|
+
? await requestContactChange({ email: resolved.email })
|
|
260
|
+
: await sendRegistrationOtp(customer.id, type)
|
|
261
|
+
if (!res?.success) throw new Error(res?.error || "Failed to send code")
|
|
138
262
|
setOtpToken(res.token ?? null)
|
|
139
263
|
setOtp("")
|
|
140
264
|
setOtpOpen(true)
|
|
@@ -143,6 +267,7 @@ export default function ModernAccountProfile({
|
|
|
143
267
|
const msg = safeErrorMessage(err, "Failed to send code")
|
|
144
268
|
setOtpError(msg)
|
|
145
269
|
if (type === "phone_verification") setPhoneError(msg)
|
|
270
|
+
if (type === "email_verification") setEmailError(msg)
|
|
146
271
|
} finally {
|
|
147
272
|
setOtpPending(false)
|
|
148
273
|
}
|
|
@@ -153,15 +278,30 @@ export default function ModernAccountProfile({
|
|
|
153
278
|
setOtpPending(true)
|
|
154
279
|
setOtpError(null)
|
|
155
280
|
try {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
281
|
+
if (otpFlow === "change_contact") {
|
|
282
|
+
const res = await verifyContactChange({
|
|
283
|
+
otpToken: otpToken || "",
|
|
284
|
+
code: otp,
|
|
285
|
+
})
|
|
286
|
+
if (!res.success) throw new Error(res.error || "Invalid code")
|
|
287
|
+
} else {
|
|
288
|
+
const res = await verifyRegistrationOtp({
|
|
289
|
+
otpToken: otpToken || "",
|
|
290
|
+
code: otp,
|
|
291
|
+
countryCode,
|
|
292
|
+
skipRedirect: true,
|
|
293
|
+
})
|
|
294
|
+
if (!res.success) throw new Error(res.error || "Invalid code")
|
|
295
|
+
}
|
|
296
|
+
if (otpType === "phone_verification") {
|
|
297
|
+
setPhoneVerified(true)
|
|
298
|
+
setEditingPhone(false)
|
|
299
|
+
}
|
|
300
|
+
if (otpType === "email_verification") {
|
|
301
|
+
setEmailVerified(true)
|
|
302
|
+
setEditingEmail(false)
|
|
303
|
+
}
|
|
163
304
|
setOtpOpen(false)
|
|
164
|
-
if (otpType === "phone_verification") setPhoneVerified(true)
|
|
165
305
|
setMessage("Verification successful.")
|
|
166
306
|
window.location.reload()
|
|
167
307
|
} catch (err) {
|
|
@@ -177,13 +317,26 @@ export default function ModernAccountProfile({
|
|
|
177
317
|
const next = normalizeIndianPhone(value)
|
|
178
318
|
if (next && phoneDigits(next) !== phoneDigits(customer.phone || "")) {
|
|
179
319
|
setPhoneVerified(false)
|
|
320
|
+
} else {
|
|
321
|
+
setPhoneVerified(readVerified(customer, "phone_verified"))
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function handleEmailChange(value: string) {
|
|
326
|
+
setEmail(value)
|
|
327
|
+
setEmailError(null)
|
|
328
|
+
if (normalizeEmail(value) !== normalizeEmail(customer.email)) {
|
|
329
|
+
setEmailVerified(false)
|
|
330
|
+
} else {
|
|
331
|
+
setEmailVerified(readVerified(customer, "email_verified"))
|
|
180
332
|
}
|
|
181
333
|
}
|
|
182
334
|
|
|
183
335
|
const fullName = [customer.first_name, customer.last_name].filter(Boolean).join(" ")
|
|
184
336
|
const initials = getInitials(fullName || customer.email)
|
|
185
|
-
const emailVerified = readVerified(customer, "email_verified")
|
|
186
337
|
const mobileVerified = phoneVerified && Boolean(customer.phone || phoneReady)
|
|
338
|
+
const otpTarget =
|
|
339
|
+
otpType === "email_verification" ? email || "email" : phone || "phone"
|
|
187
340
|
|
|
188
341
|
return (
|
|
189
342
|
<div className="modern-account-page">
|
|
@@ -235,7 +388,7 @@ export default function ModernAccountProfile({
|
|
|
235
388
|
<label className="modern-field__label" htmlFor="account-profile-phone">
|
|
236
389
|
MOBILE NUMBER*
|
|
237
390
|
</label>
|
|
238
|
-
<div
|
|
391
|
+
<div className="modern-field-row">
|
|
239
392
|
<input
|
|
240
393
|
id="account-profile-phone"
|
|
241
394
|
name="phone"
|
|
@@ -244,59 +397,72 @@ export default function ModernAccountProfile({
|
|
|
244
397
|
onChange={(e) => handlePhoneChange(e.target.value)}
|
|
245
398
|
placeholder="+91 9876543210"
|
|
246
399
|
className="modern-field__input"
|
|
247
|
-
style={{ flex: 1 }}
|
|
248
400
|
autoComplete="tel"
|
|
401
|
+
readOnly={phoneLocked}
|
|
402
|
+
aria-readonly={phoneLocked || undefined}
|
|
403
|
+
/>
|
|
404
|
+
<ContactActions
|
|
405
|
+
action={phoneAction}
|
|
406
|
+
verifyPending={otpPending && otpType === "phone_verification"}
|
|
407
|
+
verifyLabel="Verify"
|
|
408
|
+
verifyAriaLabel="Verify mobile number"
|
|
409
|
+
editAriaLabel="Edit mobile number"
|
|
410
|
+
onVerify={() => sendVerifyOtp("phone_verification")}
|
|
411
|
+
onEdit={() => setEditingPhone(true)}
|
|
249
412
|
/>
|
|
250
|
-
{showVerify ? (
|
|
251
|
-
<button
|
|
252
|
-
type="button"
|
|
253
|
-
onClick={() => sendVerifyOtp("phone_verification")}
|
|
254
|
-
disabled={otpPending}
|
|
255
|
-
className="modern-btn modern-btn--primary"
|
|
256
|
-
style={{ minHeight: "2.85rem", padding: "0 1.15rem", borderRadius: "8px", flexShrink: 0 }}
|
|
257
|
-
aria-label="Verify mobile number"
|
|
258
|
-
>
|
|
259
|
-
{otpPending && otpType === "phone_verification" ? "Sending…" : "Verify"}
|
|
260
|
-
</button>
|
|
261
|
-
) : phoneVerified ? (
|
|
262
|
-
<span className="modern-verified-pill">✓ Verified</span>
|
|
263
|
-
) : null}
|
|
264
413
|
</div>
|
|
265
414
|
{phoneError ? (
|
|
266
|
-
<p role="alert"
|
|
415
|
+
<p role="alert" className="modern-field-hint modern-field-hint--error">
|
|
267
416
|
{phoneError}
|
|
268
417
|
</p>
|
|
269
|
-
) :
|
|
270
|
-
<p
|
|
271
|
-
|
|
272
|
-
|
|
418
|
+
) : phoneAction === "edit" ? (
|
|
419
|
+
<p className="modern-field-hint">Tap Edit to change this number, then verify it again.</p>
|
|
420
|
+
) : phoneAction === "verify" ? (
|
|
421
|
+
<p className="modern-field-hint">Tap Verify to get an OTP on this number.</p>
|
|
422
|
+
) : editingPhone ? (
|
|
423
|
+
<p className="modern-field-hint">Enter a new number, then tap Verify.</p>
|
|
273
424
|
) : null}
|
|
274
425
|
</div>
|
|
275
426
|
</div>
|
|
276
427
|
|
|
277
428
|
<div className="modern-field modern-field--full">
|
|
278
|
-
<label className="modern-field__label">
|
|
279
|
-
|
|
429
|
+
<label className="modern-field__label" htmlFor="account-profile-email">
|
|
430
|
+
EMAIL
|
|
431
|
+
</label>
|
|
432
|
+
<div className="modern-field-row">
|
|
280
433
|
<input
|
|
281
|
-
|
|
282
|
-
|
|
434
|
+
id="account-profile-email"
|
|
435
|
+
name="email"
|
|
436
|
+
type="email"
|
|
437
|
+
value={email}
|
|
438
|
+
onChange={(e) => handleEmailChange(e.target.value)}
|
|
439
|
+
readOnly={emailLocked}
|
|
440
|
+
aria-readonly={emailLocked || undefined}
|
|
441
|
+
placeholder="Enter your email"
|
|
442
|
+
autoComplete="email"
|
|
283
443
|
className="modern-field__input"
|
|
284
|
-
style={{ flex: 1 }}
|
|
285
444
|
/>
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
</button>
|
|
296
|
-
) : (
|
|
297
|
-
<span className="modern-verified-pill">✓ Verified</span>
|
|
298
|
-
)}
|
|
445
|
+
<ContactActions
|
|
446
|
+
action={emailAction}
|
|
447
|
+
verifyPending={otpPending && otpType === "email_verification"}
|
|
448
|
+
verifyLabel="Verify Email"
|
|
449
|
+
verifyAriaLabel="Verify email"
|
|
450
|
+
editAriaLabel="Edit email"
|
|
451
|
+
onVerify={() => sendVerifyOtp("email_verification")}
|
|
452
|
+
onEdit={() => setEditingEmail(true)}
|
|
453
|
+
/>
|
|
299
454
|
</div>
|
|
455
|
+
{emailError ? (
|
|
456
|
+
<p role="alert" className="modern-field-hint modern-field-hint--error">
|
|
457
|
+
{emailError}
|
|
458
|
+
</p>
|
|
459
|
+
) : emailAction === "edit" ? (
|
|
460
|
+
<p className="modern-field-hint">Tap Edit to change this email, then verify it again.</p>
|
|
461
|
+
) : emailAction === "verify" ? (
|
|
462
|
+
<p className="modern-field-hint">Tap Verify to get an OTP at this address.</p>
|
|
463
|
+
) : editingEmail ? (
|
|
464
|
+
<p className="modern-field-hint">Enter a new email, then tap Verify.</p>
|
|
465
|
+
) : null}
|
|
300
466
|
</div>
|
|
301
467
|
|
|
302
468
|
{state.error || message ? (
|
|
@@ -329,7 +495,7 @@ export default function ModernAccountProfile({
|
|
|
329
495
|
<OtpComponent
|
|
330
496
|
open={otpOpen}
|
|
331
497
|
title={otpType === "email_verification" ? "Verify Email" : "Verify Phone"}
|
|
332
|
-
description={`Enter verification code sent to
|
|
498
|
+
description={`Enter verification code sent to ${otpTarget}.`}
|
|
333
499
|
otp={otp}
|
|
334
500
|
onOtpChange={setOtp}
|
|
335
501
|
error={otpError}
|
package/src/modern-account.css
CHANGED
|
@@ -718,6 +718,28 @@
|
|
|
718
718
|
cursor: not-allowed;
|
|
719
719
|
}
|
|
720
720
|
|
|
721
|
+
.modern-field-row {
|
|
722
|
+
display: flex;
|
|
723
|
+
gap: 0.5rem;
|
|
724
|
+
align-items: center;
|
|
725
|
+
flex-wrap: wrap;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
.modern-field-row .modern-field__input {
|
|
729
|
+
flex: 1;
|
|
730
|
+
min-width: 12rem;
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
.modern-field-hint {
|
|
734
|
+
margin: 0.35rem 0 0;
|
|
735
|
+
font-size: 0.75rem;
|
|
736
|
+
color: #888888;
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
.modern-field-hint--error {
|
|
740
|
+
color: #b91c1c;
|
|
741
|
+
}
|
|
742
|
+
|
|
721
743
|
/* Buttons */
|
|
722
744
|
.modern-btn {
|
|
723
745
|
display: inline-flex;
|
|
@@ -103,7 +103,7 @@ const CASES: Array<{
|
|
|
103
103
|
want: { label: "Delivered", tone: "done" },
|
|
104
104
|
},
|
|
105
105
|
{
|
|
106
|
-
name: "
|
|
106
|
+
name: "shows exchange requested when a return is tied to an exchange",
|
|
107
107
|
order: {
|
|
108
108
|
fulfillment_status: "delivered",
|
|
109
109
|
returns: [
|
|
@@ -111,6 +111,14 @@ const CASES: Array<{
|
|
|
111
111
|
{ status: "requested", claim_id: "claim_1" },
|
|
112
112
|
],
|
|
113
113
|
},
|
|
114
|
+
want: { label: "Exchange Requested", tone: "return" },
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
name: "ignores claim returns and keeps delivered",
|
|
118
|
+
order: {
|
|
119
|
+
fulfillment_status: "delivered",
|
|
120
|
+
returns: [{ status: "requested", claim_id: "claim_1" }],
|
|
121
|
+
},
|
|
114
122
|
want: { label: "Delivered", tone: "done" },
|
|
115
123
|
},
|
|
116
124
|
{
|
|
@@ -140,6 +148,66 @@ const CASES: Array<{
|
|
|
140
148
|
order: { fulfillment_status: "fulfilled" },
|
|
141
149
|
want: { label: "Order Confirmed", tone: "done" },
|
|
142
150
|
},
|
|
151
|
+
{
|
|
152
|
+
name: "shows return requested from item return quantities when returns are omitted",
|
|
153
|
+
order: {
|
|
154
|
+
fulfillment_status: "delivered",
|
|
155
|
+
items: [{ detail: { return_requested_quantity: 1 } }],
|
|
156
|
+
},
|
|
157
|
+
want: { label: "Return Requested", tone: "return" },
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
name: "shows exchange requested from item quantities when the order has an exchange",
|
|
161
|
+
order: {
|
|
162
|
+
fulfillment_status: "delivered",
|
|
163
|
+
exchanges: [{ id: "exch_1" }],
|
|
164
|
+
items: [{ detail: { return_requested_quantity: 1 } }],
|
|
165
|
+
},
|
|
166
|
+
want: { label: "Exchange Requested", tone: "return" },
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
name: "shows exchange requested for a same-SKU replacement line",
|
|
170
|
+
order: {
|
|
171
|
+
fulfillment_status: "delivered",
|
|
172
|
+
items: [
|
|
173
|
+
{ product_id: "prod_1", detail: { return_requested_quantity: 1 } },
|
|
174
|
+
{ product_id: "prod_1" },
|
|
175
|
+
],
|
|
176
|
+
},
|
|
177
|
+
want: { label: "Exchange Requested", tone: "return" },
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
name: "shows exchange requested when the same product appears twice after an exchange",
|
|
181
|
+
order: {
|
|
182
|
+
fulfillment_status: "delivered",
|
|
183
|
+
items: [{ product_id: "prod_1" }, { product_id: "prod_1" }],
|
|
184
|
+
},
|
|
185
|
+
want: { label: "Exchange Requested", tone: "return" },
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
name: "maps an approved exchange return to exchange approved",
|
|
189
|
+
order: {
|
|
190
|
+
fulfillment_status: "delivered",
|
|
191
|
+
returns: [{ status: "open", exchange_id: "exch_1" }],
|
|
192
|
+
},
|
|
193
|
+
want: { label: "Exchange Approved", tone: "return" },
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
name: "shows returned from received item quantities",
|
|
197
|
+
order: {
|
|
198
|
+
fulfillment_status: "delivered",
|
|
199
|
+
items: [{ detail: { return_requested_quantity: 1, return_received_quantity: 1 } }],
|
|
200
|
+
},
|
|
201
|
+
want: { label: "Returned", tone: "return" },
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
name: "shows return in progress when only some quantity is received",
|
|
205
|
+
order: {
|
|
206
|
+
fulfillment_status: "delivered",
|
|
207
|
+
items: [{ detail: { return_requested_quantity: 2, return_received_quantity: 1 } }],
|
|
208
|
+
},
|
|
209
|
+
want: { label: "Return In Progress", tone: "return" },
|
|
210
|
+
},
|
|
143
211
|
{
|
|
144
212
|
name: "shows processing as the default",
|
|
145
213
|
order: { status: "pending" },
|
|
@@ -21,6 +21,18 @@ export type OrderHistoryStatusInput = {
|
|
|
21
21
|
fulfillment_status?: string | null
|
|
22
22
|
payment_status?: string | null
|
|
23
23
|
returns?: OrderHistoryReturnLike[] | null
|
|
24
|
+
exchanges?: Array<{
|
|
25
|
+
id?: string | null
|
|
26
|
+
canceled_at?: string | Date | null
|
|
27
|
+
}> | null
|
|
28
|
+
items?: Array<{
|
|
29
|
+
product_id?: string | null
|
|
30
|
+
created_at?: string | Date | null
|
|
31
|
+
detail?: {
|
|
32
|
+
return_requested_quantity?: number | null
|
|
33
|
+
return_received_quantity?: number | null
|
|
34
|
+
} | null
|
|
35
|
+
}> | null
|
|
24
36
|
}
|
|
25
37
|
|
|
26
38
|
type ReturnLifecycle =
|
|
@@ -48,6 +60,14 @@ const RETURN_LABEL: Record<ReturnLifecycle, string> = {
|
|
|
48
60
|
refunded: "Refunded",
|
|
49
61
|
}
|
|
50
62
|
|
|
63
|
+
const EXCHANGE_LABEL: Record<ReturnLifecycle, string> = {
|
|
64
|
+
requested: "Exchange Requested",
|
|
65
|
+
approved: "Exchange Approved",
|
|
66
|
+
in_progress: "Exchange In Progress",
|
|
67
|
+
returned: "Exchanged",
|
|
68
|
+
refunded: "Refunded",
|
|
69
|
+
}
|
|
70
|
+
|
|
51
71
|
function normalize(value?: string | null) {
|
|
52
72
|
return (value || "").toLowerCase().trim()
|
|
53
73
|
}
|
|
@@ -56,6 +76,44 @@ function isStandaloneReturn(ret: OrderHistoryReturnLike) {
|
|
|
56
76
|
return !ret.exchange_id && !ret.claim_id
|
|
57
77
|
}
|
|
58
78
|
|
|
79
|
+
function isOpenExchangeReturn(ret: OrderHistoryReturnLike) {
|
|
80
|
+
return Boolean(ret.exchange_id) && !CLOSED_RETURN.has(normalize(ret.status))
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function itemCreatedAt(item: { created_at?: string | Date | null }): number {
|
|
84
|
+
if (!item.created_at) return 0
|
|
85
|
+
const t = new Date(item.created_at).getTime()
|
|
86
|
+
return Number.isFinite(t) ? t : 0
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function orderHasActiveExchange(order: OrderHistoryStatusInput): boolean {
|
|
90
|
+
if ((order.exchanges || []).some((row) => row?.id && !row.canceled_at)) {
|
|
91
|
+
return true
|
|
92
|
+
}
|
|
93
|
+
if ((order.returns || []).some(isOpenExchangeReturn)) return true
|
|
94
|
+
|
|
95
|
+
const items = order.items || []
|
|
96
|
+
const byProduct = new Map<string, number>()
|
|
97
|
+
for (const item of items) {
|
|
98
|
+
if (!item.product_id) continue
|
|
99
|
+
byProduct.set(item.product_id, (byProduct.get(item.product_id) || 0) + 1)
|
|
100
|
+
}
|
|
101
|
+
if ([...byProduct.values()].some((count) => count >= 2)) return true
|
|
102
|
+
|
|
103
|
+
for (const item of items) {
|
|
104
|
+
const createdAt = itemCreatedAt(item)
|
|
105
|
+
if (!createdAt) continue
|
|
106
|
+
const hasEarlierItem = items.some((other) => {
|
|
107
|
+
if (other === item) return false
|
|
108
|
+
const otherAt = itemCreatedAt(other)
|
|
109
|
+
return otherAt > 0 && createdAt > otherAt + 1000
|
|
110
|
+
})
|
|
111
|
+
if (hasEarlierItem) return true
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return false
|
|
115
|
+
}
|
|
116
|
+
|
|
59
117
|
function lifecycleFromReturnStatus(status: string): ReturnLifecycle | null {
|
|
60
118
|
switch (status) {
|
|
61
119
|
case "requested":
|
|
@@ -84,29 +142,60 @@ function pickHigher(
|
|
|
84
142
|
return RETURN_RANK[next] > RETURN_RANK[current] ? next : current
|
|
85
143
|
}
|
|
86
144
|
|
|
87
|
-
function
|
|
145
|
+
function requestLifecycle(
|
|
146
|
+
order: OrderHistoryStatusInput,
|
|
147
|
+
mode: "return" | "exchange"
|
|
148
|
+
): ReturnLifecycle | null {
|
|
88
149
|
const payment = normalize(order.payment_status)
|
|
89
150
|
let best: ReturnLifecycle | null = null
|
|
151
|
+
const exchange = orderHasActiveExchange(order)
|
|
90
152
|
|
|
91
|
-
if (payment === "refunded" || payment === "partially_refunded") {
|
|
153
|
+
if (mode === "return" && (payment === "refunded" || payment === "partially_refunded")) {
|
|
92
154
|
best = "refunded"
|
|
93
155
|
}
|
|
94
156
|
|
|
95
157
|
for (const ret of order.returns || []) {
|
|
96
|
-
if (!isStandaloneReturn(ret)) continue
|
|
97
158
|
const status = normalize(ret.status)
|
|
98
159
|
if (CLOSED_RETURN.has(status)) continue
|
|
160
|
+
if (mode === "exchange" && !isOpenExchangeReturn(ret)) continue
|
|
161
|
+
if (mode === "return" && !isStandaloneReturn(ret)) continue
|
|
99
162
|
const life = status
|
|
100
163
|
? lifecycleFromReturnStatus(status) ?? "requested"
|
|
101
164
|
: "requested"
|
|
102
165
|
best = pickHigher(best, life)
|
|
103
166
|
}
|
|
104
167
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
168
|
+
let requestedQty = 0
|
|
169
|
+
let receivedQty = 0
|
|
170
|
+
for (const item of order.items || []) {
|
|
171
|
+
requestedQty += Number(item.detail?.return_requested_quantity) || 0
|
|
172
|
+
receivedQty += Number(item.detail?.return_received_quantity) || 0
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const qtyApplies =
|
|
176
|
+
(mode === "exchange" && exchange) || (mode === "return" && !exchange)
|
|
177
|
+
|
|
178
|
+
if (qtyApplies) {
|
|
179
|
+
if (receivedQty > 0 && requestedQty > receivedQty) {
|
|
180
|
+
best = pickHigher(best, "in_progress")
|
|
181
|
+
} else if (receivedQty > 0) {
|
|
182
|
+
best = pickHigher(best, "returned")
|
|
183
|
+
} else if (requestedQty > 0) {
|
|
184
|
+
best = pickHigher(best, "requested")
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (mode === "return") {
|
|
189
|
+
const fulfillment = normalize(order.fulfillment_status)
|
|
190
|
+
if (fulfillment === "returned") {
|
|
191
|
+
best = pickHigher(best, "returned")
|
|
192
|
+
} else if (fulfillment === "partially_returned") {
|
|
193
|
+
best = pickHigher(best, "in_progress")
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (mode === "exchange" && exchange) {
|
|
198
|
+
return best || "requested"
|
|
110
199
|
}
|
|
111
200
|
|
|
112
201
|
return best
|
|
@@ -120,9 +209,14 @@ export function resolveOrderHistoryStatus(
|
|
|
120
209
|
return { label: "Cancelled", tone: "cancel" }
|
|
121
210
|
}
|
|
122
211
|
|
|
123
|
-
const
|
|
124
|
-
if (
|
|
125
|
-
return { label:
|
|
212
|
+
const exchangeLife = requestLifecycle(order, "exchange")
|
|
213
|
+
if (exchangeLife) {
|
|
214
|
+
return { label: EXCHANGE_LABEL[exchangeLife], tone: "return" }
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const returnLife = requestLifecycle(order, "return")
|
|
218
|
+
if (returnLife) {
|
|
219
|
+
return { label: RETURN_LABEL[returnLife], tone: "return" }
|
|
126
220
|
}
|
|
127
221
|
|
|
128
222
|
const fulfillment = normalize(order.fulfillment_status)
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import assert from "node:assert/strict"
|
|
2
|
+
import { describe, it } from "node:test"
|
|
3
|
+
import {
|
|
4
|
+
EMAIL_OTP_ERRORS,
|
|
5
|
+
PHONE_OTP_ERRORS,
|
|
6
|
+
isValidEmail,
|
|
7
|
+
isVerifiedFieldLocked,
|
|
8
|
+
normalizeEmail,
|
|
9
|
+
phoneDigits,
|
|
10
|
+
resolveEmailOtpFlow,
|
|
11
|
+
resolvePhoneOtpFlow,
|
|
12
|
+
verifiedFieldAction,
|
|
13
|
+
type ContactOtpFlow,
|
|
14
|
+
type ResolveEmailOtpFlowResult,
|
|
15
|
+
type ResolvePhoneOtpFlowResult,
|
|
16
|
+
type VerifiedFieldAction,
|
|
17
|
+
} from "./profile-contact-field.ts"
|
|
18
|
+
|
|
19
|
+
describe("phoneDigits", () => {
|
|
20
|
+
const cases: Array<{ name: string; value: string; want: string }> = [
|
|
21
|
+
{ name: "strips spaces and plus", value: "+91 87800 50157", want: "918780050157" },
|
|
22
|
+
{ name: "keeps digits only", value: "878-005-0157", want: "8780050157" },
|
|
23
|
+
{ name: "empty stays empty", value: "", want: "" },
|
|
24
|
+
]
|
|
25
|
+
for (const tc of cases) {
|
|
26
|
+
it(tc.name, () => {
|
|
27
|
+
assert.equal(phoneDigits(tc.value), tc.want)
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
describe("isVerifiedFieldLocked", () => {
|
|
33
|
+
const cases: Array<{
|
|
34
|
+
name: string
|
|
35
|
+
verified: boolean
|
|
36
|
+
editing: boolean
|
|
37
|
+
want: boolean
|
|
38
|
+
}> = [
|
|
39
|
+
{ name: "verified and not editing is locked", verified: true, editing: false, want: true },
|
|
40
|
+
{ name: "verified while editing is unlocked", verified: true, editing: true, want: false },
|
|
41
|
+
{ name: "unverified is never locked", verified: false, editing: false, want: false },
|
|
42
|
+
{ name: "unverified stays unlocked even if editing", verified: false, editing: true, want: false },
|
|
43
|
+
]
|
|
44
|
+
for (const tc of cases) {
|
|
45
|
+
it(tc.name, () => {
|
|
46
|
+
assert.equal(
|
|
47
|
+
isVerifiedFieldLocked({ verified: tc.verified, editing: tc.editing }),
|
|
48
|
+
tc.want
|
|
49
|
+
)
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
describe("verifiedFieldAction", () => {
|
|
55
|
+
const cases: Array<{
|
|
56
|
+
name: string
|
|
57
|
+
verified: boolean
|
|
58
|
+
editing: boolean
|
|
59
|
+
ready: boolean
|
|
60
|
+
want: VerifiedFieldAction
|
|
61
|
+
}> = [
|
|
62
|
+
{
|
|
63
|
+
name: "verified contact shows edit",
|
|
64
|
+
verified: true,
|
|
65
|
+
editing: false,
|
|
66
|
+
ready: true,
|
|
67
|
+
want: "edit",
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
name: "editing a verified contact keeps verified until the value changes",
|
|
71
|
+
verified: true,
|
|
72
|
+
editing: true,
|
|
73
|
+
ready: true,
|
|
74
|
+
want: "verified",
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
name: "typed valid unverified contact shows verify",
|
|
78
|
+
verified: false,
|
|
79
|
+
editing: false,
|
|
80
|
+
ready: true,
|
|
81
|
+
want: "verify",
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
name: "empty unverified contact shows no action yet",
|
|
85
|
+
verified: false,
|
|
86
|
+
editing: false,
|
|
87
|
+
ready: false,
|
|
88
|
+
want: "none",
|
|
89
|
+
},
|
|
90
|
+
]
|
|
91
|
+
for (const tc of cases) {
|
|
92
|
+
it(tc.name, () => {
|
|
93
|
+
assert.equal(
|
|
94
|
+
verifiedFieldAction({
|
|
95
|
+
verified: tc.verified,
|
|
96
|
+
editing: tc.editing,
|
|
97
|
+
ready: tc.ready,
|
|
98
|
+
}),
|
|
99
|
+
tc.want
|
|
100
|
+
)
|
|
101
|
+
})
|
|
102
|
+
}
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
describe("normalizeEmail", () => {
|
|
106
|
+
const cases: Array<{ name: string; value: string | null | undefined; want: string }> = [
|
|
107
|
+
{ name: "trims and lowercases", value: " Jay@Empire.COM ", want: "jay@empire.com" },
|
|
108
|
+
{ name: "empty stays empty", value: "", want: "" },
|
|
109
|
+
{ name: "null becomes empty", value: null, want: "" },
|
|
110
|
+
]
|
|
111
|
+
for (const tc of cases) {
|
|
112
|
+
it(tc.name, () => {
|
|
113
|
+
assert.equal(normalizeEmail(tc.value), tc.want)
|
|
114
|
+
})
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
describe("isValidEmail", () => {
|
|
119
|
+
const cases: Array<{ name: string; value: string | null | undefined; want: boolean }> = [
|
|
120
|
+
{ name: "accepts a normal email", value: "jay@empire.com", want: true },
|
|
121
|
+
{ name: "rejects empty", value: "", want: false },
|
|
122
|
+
{ name: "rejects missing at-sign", value: "jayempire.com", want: false },
|
|
123
|
+
]
|
|
124
|
+
for (const tc of cases) {
|
|
125
|
+
it(tc.name, () => {
|
|
126
|
+
assert.equal(isValidEmail(tc.value), tc.want)
|
|
127
|
+
})
|
|
128
|
+
}
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
describe("resolveEmailOtpFlow", () => {
|
|
132
|
+
const cases: Array<{
|
|
133
|
+
name: string
|
|
134
|
+
currentEmail: string | null | undefined
|
|
135
|
+
draftEmail: string
|
|
136
|
+
want: ResolveEmailOtpFlowResult
|
|
137
|
+
}> = [
|
|
138
|
+
{
|
|
139
|
+
name: "same email verifies the existing address",
|
|
140
|
+
currentEmail: "jay@empire.com",
|
|
141
|
+
draftEmail: "Jay@Empire.com",
|
|
142
|
+
want: { ok: true, flow: "verify_existing", email: "jay@empire.com" },
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
name: "changing to a different email uses contact change",
|
|
146
|
+
currentEmail: "old@empire.com",
|
|
147
|
+
draftEmail: "new@empire.com",
|
|
148
|
+
want: { ok: true, flow: "change_contact", email: "new@empire.com" },
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
name: "empty draft is empty",
|
|
152
|
+
currentEmail: "jay@empire.com",
|
|
153
|
+
draftEmail: " ",
|
|
154
|
+
want: { ok: false, reason: "empty" },
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
name: "malformed draft is invalid",
|
|
158
|
+
currentEmail: "jay@empire.com",
|
|
159
|
+
draftEmail: "not-an-email",
|
|
160
|
+
want: { ok: false, reason: "invalid" },
|
|
161
|
+
},
|
|
162
|
+
]
|
|
163
|
+
for (const tc of cases) {
|
|
164
|
+
it(tc.name, () => {
|
|
165
|
+
assert.deepEqual(
|
|
166
|
+
resolveEmailOtpFlow({ currentEmail: tc.currentEmail, draftEmail: tc.draftEmail }),
|
|
167
|
+
tc.want
|
|
168
|
+
)
|
|
169
|
+
})
|
|
170
|
+
}
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
describe("resolvePhoneOtpFlow", () => {
|
|
174
|
+
const cases: Array<{
|
|
175
|
+
name: string
|
|
176
|
+
currentPhone: string | null | undefined
|
|
177
|
+
draftE164: string
|
|
178
|
+
want: ResolvePhoneOtpFlowResult
|
|
179
|
+
}> = [
|
|
180
|
+
{
|
|
181
|
+
name: "same number verifies the existing phone",
|
|
182
|
+
currentPhone: "+918780050157",
|
|
183
|
+
draftE164: "+918780050157",
|
|
184
|
+
want: { ok: true, flow: "verify_existing" as ContactOtpFlow, phone: "+918780050157" },
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
name: "same digits with different formatting still verifies existing",
|
|
188
|
+
currentPhone: "+91 87800 50157",
|
|
189
|
+
draftE164: "+918780050157",
|
|
190
|
+
want: { ok: true, flow: "verify_existing", phone: "+918780050157" },
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
name: "a new number uses contact change",
|
|
194
|
+
currentPhone: "+918780050157",
|
|
195
|
+
draftE164: "+919876543210",
|
|
196
|
+
want: { ok: true, flow: "change_contact", phone: "+919876543210" },
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
name: "empty draft is empty",
|
|
200
|
+
currentPhone: "+918780050157",
|
|
201
|
+
draftE164: "",
|
|
202
|
+
want: { ok: false, reason: "empty" },
|
|
203
|
+
},
|
|
204
|
+
]
|
|
205
|
+
for (const tc of cases) {
|
|
206
|
+
it(tc.name, () => {
|
|
207
|
+
assert.deepEqual(
|
|
208
|
+
resolvePhoneOtpFlow({ currentPhone: tc.currentPhone, draftE164: tc.draftE164 }),
|
|
209
|
+
tc.want
|
|
210
|
+
)
|
|
211
|
+
})
|
|
212
|
+
}
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
describe("otp error copy", () => {
|
|
216
|
+
const cases = [
|
|
217
|
+
{ name: "empty email", actual: EMAIL_OTP_ERRORS.empty, want: "Please enter an email address before verifying." },
|
|
218
|
+
{ name: "invalid email", actual: EMAIL_OTP_ERRORS.invalid, want: "Please enter a valid email address." },
|
|
219
|
+
{
|
|
220
|
+
name: "empty phone",
|
|
221
|
+
actual: PHONE_OTP_ERRORS.empty,
|
|
222
|
+
want: "Please enter a valid mobile number before verifying.",
|
|
223
|
+
},
|
|
224
|
+
]
|
|
225
|
+
for (const tc of cases) {
|
|
226
|
+
it(tc.name, () => {
|
|
227
|
+
assert.equal(tc.actual, tc.want)
|
|
228
|
+
})
|
|
229
|
+
}
|
|
230
|
+
})
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
export type VerifiedFieldAction = "verify" | "edit" | "verified" | "none"
|
|
2
|
+
|
|
3
|
+
export type ContactOtpFlow = "verify_existing" | "change_contact"
|
|
4
|
+
|
|
5
|
+
export const EMAIL_REGEX = /^\S+@\S+\.\S+$/
|
|
6
|
+
|
|
7
|
+
export const EMAIL_OTP_ERRORS = {
|
|
8
|
+
empty: "Please enter an email address before verifying.",
|
|
9
|
+
invalid: "Please enter a valid email address.",
|
|
10
|
+
} as const
|
|
11
|
+
|
|
12
|
+
export const PHONE_OTP_ERRORS = {
|
|
13
|
+
empty: "Please enter a valid mobile number before verifying.",
|
|
14
|
+
} as const
|
|
15
|
+
|
|
16
|
+
export function phoneDigits(value: string): string {
|
|
17
|
+
return value.replace(/\D/g, "")
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function isVerifiedFieldLocked(input: {
|
|
21
|
+
verified: boolean
|
|
22
|
+
editing: boolean
|
|
23
|
+
}): boolean {
|
|
24
|
+
return input.verified && !input.editing
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function verifiedFieldAction(input: {
|
|
28
|
+
verified: boolean
|
|
29
|
+
editing: boolean
|
|
30
|
+
ready: boolean
|
|
31
|
+
}): VerifiedFieldAction {
|
|
32
|
+
if (isVerifiedFieldLocked(input)) return "edit"
|
|
33
|
+
if (input.verified) return "verified"
|
|
34
|
+
if (input.ready) return "verify"
|
|
35
|
+
return "none"
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function normalizeEmail(value: string | null | undefined): string {
|
|
39
|
+
return (value || "").trim().toLowerCase()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function isValidEmail(value: string | null | undefined): boolean {
|
|
43
|
+
return EMAIL_REGEX.test(normalizeEmail(value))
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export type ResolveEmailOtpFlowResult =
|
|
47
|
+
| { ok: true; flow: ContactOtpFlow; email: string }
|
|
48
|
+
| { ok: false; reason: "empty" | "invalid" }
|
|
49
|
+
|
|
50
|
+
export function resolveEmailOtpFlow(input: {
|
|
51
|
+
currentEmail: string | null | undefined
|
|
52
|
+
draftEmail: string | null | undefined
|
|
53
|
+
}): ResolveEmailOtpFlowResult {
|
|
54
|
+
const email = normalizeEmail(input.draftEmail)
|
|
55
|
+
if (!email) return { ok: false, reason: "empty" }
|
|
56
|
+
if (!isValidEmail(email)) return { ok: false, reason: "invalid" }
|
|
57
|
+
const current = normalizeEmail(input.currentEmail)
|
|
58
|
+
if (current && email === current) {
|
|
59
|
+
return { ok: true, flow: "verify_existing", email }
|
|
60
|
+
}
|
|
61
|
+
return { ok: true, flow: "change_contact", email }
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export type ResolvePhoneOtpFlowResult =
|
|
65
|
+
| { ok: true; flow: ContactOtpFlow; phone: string }
|
|
66
|
+
| { ok: false; reason: "empty" }
|
|
67
|
+
|
|
68
|
+
export function resolvePhoneOtpFlow(input: {
|
|
69
|
+
currentPhone: string | null | undefined
|
|
70
|
+
draftE164: string
|
|
71
|
+
}): ResolvePhoneOtpFlowResult {
|
|
72
|
+
const phone = input.draftE164
|
|
73
|
+
if (!phone) return { ok: false, reason: "empty" }
|
|
74
|
+
const current = phoneDigits(input.currentPhone || "")
|
|
75
|
+
const draft = phoneDigits(phone)
|
|
76
|
+
if (current && current === draft) {
|
|
77
|
+
return { ok: true, flow: "verify_existing", phone }
|
|
78
|
+
}
|
|
79
|
+
return { ok: true, flow: "change_contact", phone }
|
|
80
|
+
}
|