@c1m-public/channel-client 0.19.0 → 0.21.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.
@@ -1,2 +1,288 @@
1
- export * from "./address-model.js";
2
- export * from "./quote-model.js";
1
+ import { z } from 'zod';
2
+
3
+ // src/moving-lead-model/address-model.ts
4
+ var UK_POSTCODE_RE = /^[a-z]{1,2}\d[a-z\d]?\s*\d[a-z]{2}$/i;
5
+ function readConfirmedAddressLine(address) {
6
+ return address.normalized?.address_line1 || address.address?.line_1 || address.display;
7
+ }
8
+ function readConfirmedAddressMeta(address) {
9
+ return [
10
+ address.normalized?.address_line2 || address.address?.line_2,
11
+ address.normalized?.city || address.address?.post_town,
12
+ address.normalized?.postal_code || address.address?.postcode
13
+ ].filter(Boolean).join(", ");
14
+ }
15
+ function readManualInitialAddress(address, fallbackRaw) {
16
+ const raw = address?.rawQuery || fallbackRaw;
17
+ const rawLooksLikePostcode = UK_POSTCODE_RE.test(raw.trim());
18
+ return {
19
+ line1: address?.normalized?.address_line1 || address?.address?.line_1 || (rawLooksLikePostcode ? "" : raw),
20
+ line2: address?.normalized?.address_line2 || [address?.address?.line_2, address?.address?.line_3].filter(Boolean).join(", "),
21
+ city: address?.normalized?.city || address?.address?.post_town || "",
22
+ postcode: address?.normalized?.postal_code || address?.address?.postcode || (rawLooksLikePostcode ? raw.toUpperCase() : "")
23
+ };
24
+ }
25
+ function buildManualMovingAddress(fields) {
26
+ const line1 = fields.line1.trim();
27
+ const line2 = fields.line2.trim();
28
+ const city = fields.city.trim();
29
+ const postcode = fields.postcode.trim();
30
+ const display = [line1, line2, city, postcode].filter(Boolean).join(", ");
31
+ return {
32
+ display,
33
+ rawQuery: display,
34
+ address: null,
35
+ normalized: {
36
+ address_line1: line1,
37
+ address_line2: line2,
38
+ city,
39
+ state_region: "",
40
+ postal_code: postcode,
41
+ country_code: "GB"
42
+ }
43
+ };
44
+ }
45
+ function readAddressDisplay(address, fallback) {
46
+ return address?.display || address?.rawQuery || fallback;
47
+ }
48
+ function hasStructuredMovingAddress(address) {
49
+ const line1 = address?.normalized?.address_line1 || address?.address?.line_1 || "";
50
+ const city = address?.normalized?.city || address?.address?.post_town || "";
51
+ return line1.trim().length > 0 && city.trim().length > 0;
52
+ }
53
+ function hasCoordinates(address) {
54
+ return Boolean(
55
+ address?.address && typeof address.address.latitude === "number" && typeof address.address.longitude === "number"
56
+ );
57
+ }
58
+ function formatDuration(minutes) {
59
+ if (minutes < 60) return `${minutes} min`;
60
+ const hours = Math.floor(minutes / 60);
61
+ const remainder = minutes % 60;
62
+ return remainder ? `${hours} hr ${remainder} min` : `${hours} hr`;
63
+ }
64
+ var optionalNullableString = z.string().nullable();
65
+ var contactPreferenceSchema = z.enum(["email", "phone", "any"]);
66
+ var timePreferenceSchema = z.enum(["morning", "afternoon", "evening", "any"]);
67
+ var movingAddressLocationSchema = z.object({
68
+ display: z.string(),
69
+ raw_query: optionalNullableString,
70
+ postcode: optionalNullableString,
71
+ latitude: z.number().nullable(),
72
+ longitude: z.number().nullable(),
73
+ udprn: z.number().nullable(),
74
+ normalized: z.object({
75
+ address_line1: z.string(),
76
+ address_line2: z.string(),
77
+ city: z.string(),
78
+ state_region: z.string(),
79
+ postal_code: z.string(),
80
+ country_code: z.string(),
81
+ latitude: z.number().nullable(),
82
+ longitude: z.number().nullable(),
83
+ udprn: z.number().nullable()
84
+ }).nullable(),
85
+ directory_region: z.object({
86
+ major_region_code: z.string(),
87
+ major_region_label: z.string(),
88
+ confidence: z.number(),
89
+ match_source: z.string()
90
+ }).nullable()
91
+ });
92
+ var movingLeadPayloadSchema = z.object({
93
+ lead_type: z.literal("moving"),
94
+ brand: z.string().min(1),
95
+ source: z.string().min(1),
96
+ intent: z.literal("moving_quote"),
97
+ name: z.string(),
98
+ email: z.string(),
99
+ phone: z.string(),
100
+ preferred_contact: contactPreferenceSchema,
101
+ preferred_time: timePreferenceSchema,
102
+ message: z.string(),
103
+ meta: z.object({
104
+ from_address: z.string(),
105
+ to_address: z.string(),
106
+ from_postcode: optionalNullableString,
107
+ to_postcode: optionalNullableString,
108
+ move_date: z.string(),
109
+ flexible_date: z.boolean(),
110
+ property_size: z.string().min(1),
111
+ floor_from: z.number().int().min(0),
112
+ floor_to: z.number().int().min(0),
113
+ has_lift_from: z.boolean(),
114
+ has_lift_to: z.boolean(),
115
+ packing_required: z.boolean(),
116
+ storage_required: z.boolean(),
117
+ dismantling_required: z.boolean(),
118
+ special_items: z.array(z.string()),
119
+ additional_notes: z.string(),
120
+ distance_miles: z.number().nullable(),
121
+ duration_minutes: z.number().nullable(),
122
+ pickup_region: optionalNullableString,
123
+ delivery_region: optionalNullableString,
124
+ pickup_location: movingAddressLocationSchema,
125
+ delivery_location: movingAddressLocationSchema
126
+ })
127
+ });
128
+ var movingBrokerCapturePayloadSchema = z.object({
129
+ source_type: z.literal("web"),
130
+ name: z.string(),
131
+ email: z.string(),
132
+ phone: z.string(),
133
+ message: z.string(),
134
+ region: z.string(),
135
+ tags: z.array(z.string()).min(1),
136
+ meta: movingLeadPayloadSchema
137
+ });
138
+ var movingLeadSubmissionSchema = movingLeadPayloadSchema.superRefine((payload, ctx) => {
139
+ if (!payload.meta.from_address.trim()) {
140
+ ctx.addIssue({ code: "custom", path: ["meta", "from_address"], message: "Pickup address is required." });
141
+ }
142
+ if (!payload.meta.to_address.trim()) {
143
+ ctx.addIssue({ code: "custom", path: ["meta", "to_address"], message: "Delivery address is required." });
144
+ }
145
+ if (!payload.name.trim()) {
146
+ ctx.addIssue({ code: "custom", path: ["name"], message: "Name is required." });
147
+ }
148
+ if (!payload.email.trim() && !payload.phone.trim()) {
149
+ ctx.addIssue({ code: "custom", path: ["email"], message: "Email or phone is required." });
150
+ }
151
+ if (payload.email.trim() && !z.email().safeParse(payload.email).success) {
152
+ ctx.addIssue({ code: "custom", path: ["email"], message: "Email must be valid." });
153
+ }
154
+ });
155
+ function readAddressPostcode(address) {
156
+ return address?.normalized?.postal_code || address?.address?.postcode || null;
157
+ }
158
+ function readAddressLatitude(address) {
159
+ return address?.normalized?.latitude ?? address?.address?.latitude ?? null;
160
+ }
161
+ function readAddressLongitude(address) {
162
+ return address?.normalized?.longitude ?? address?.address?.longitude ?? null;
163
+ }
164
+ function readAddressUdprn(address) {
165
+ return address?.normalized?.udprn ?? address?.address?.udprn ?? null;
166
+ }
167
+ function buildAddressLocation(address, fallback) {
168
+ const normalized = address?.normalized;
169
+ return {
170
+ display: readAddressDisplay(address, fallback),
171
+ raw_query: address?.rawQuery || fallback || null,
172
+ postcode: readAddressPostcode(address),
173
+ latitude: readAddressLatitude(address),
174
+ longitude: readAddressLongitude(address),
175
+ udprn: readAddressUdprn(address),
176
+ normalized: normalized ? {
177
+ address_line1: normalized.address_line1,
178
+ address_line2: normalized.address_line2,
179
+ city: normalized.city,
180
+ state_region: normalized.state_region,
181
+ postal_code: normalized.postal_code,
182
+ country_code: normalized.country_code,
183
+ latitude: normalized.latitude ?? null,
184
+ longitude: normalized.longitude ?? null,
185
+ udprn: normalized.udprn ?? null
186
+ } : null,
187
+ directory_region: address?.directory_region ?? null
188
+ };
189
+ }
190
+ function buildMovingLeadPayload(input) {
191
+ const from = readAddressDisplay(input.fromAddress, input.fromRaw);
192
+ const to = readAddressDisplay(input.toAddress, input.toRaw);
193
+ return movingLeadPayloadSchema.parse({
194
+ lead_type: "moving",
195
+ brand: input.brand,
196
+ source: input.source,
197
+ intent: "moving_quote",
198
+ name: input.name,
199
+ email: input.email,
200
+ phone: input.phone,
201
+ preferred_contact: input.preferredContact,
202
+ preferred_time: input.preferredTime,
203
+ message: input.additionalNotes || `Moving request from ${from || "pickup"} to ${to || "delivery"}.`,
204
+ meta: {
205
+ from_address: from,
206
+ to_address: to,
207
+ from_postcode: input.fromAddress?.normalized?.postal_code ?? null,
208
+ to_postcode: input.toAddress?.normalized?.postal_code ?? null,
209
+ move_date: input.moveDate,
210
+ flexible_date: input.flexibleDate,
211
+ property_size: input.propertySize,
212
+ floor_from: Number(input.floorFrom),
213
+ floor_to: Number(input.floorTo),
214
+ has_lift_from: input.hasLiftFrom,
215
+ has_lift_to: input.hasLiftTo,
216
+ packing_required: input.packingRequired,
217
+ storage_required: input.storageRequired,
218
+ dismantling_required: input.dismantlingRequired,
219
+ special_items: input.specialItems.split(",").map((item) => item.trim()).filter(Boolean),
220
+ additional_notes: input.additionalNotes,
221
+ distance_miles: input.distance?.distanceMiles ?? null,
222
+ duration_minutes: input.distance?.durationMinutes ?? null,
223
+ pickup_region: input.fromAddress?.directory_region?.major_region_code ?? null,
224
+ delivery_region: input.toAddress?.directory_region?.major_region_code ?? null,
225
+ pickup_location: buildAddressLocation(input.fromAddress, input.fromRaw),
226
+ delivery_location: buildAddressLocation(input.toAddress, input.toRaw)
227
+ }
228
+ });
229
+ }
230
+ function buildMovingBrokerCapturePayload(payload) {
231
+ const region = payload.meta.pickup_region || payload.meta.from_postcode || payload.meta.pickup_location.postcode || "";
232
+ return movingBrokerCapturePayloadSchema.parse({
233
+ source_type: "web",
234
+ name: payload.name,
235
+ email: payload.email,
236
+ phone: payload.phone,
237
+ message: payload.message,
238
+ region,
239
+ tags: ["moving", "direct", "main_site", payload.brand],
240
+ meta: payload
241
+ });
242
+ }
243
+ async function readQuoteSubmitError(response) {
244
+ const fallback = response.status >= 500 ? "Something went wrong on our end. Please try again shortly." : "We could not submit this request. Please check the details and try again.";
245
+ try {
246
+ const body = await response.json();
247
+ const message = typeof body.message === "string" ? body.message.trim() : "";
248
+ if (message && message.toLowerCase() !== "invalid input") {
249
+ return message;
250
+ }
251
+ } catch {
252
+ }
253
+ return fallback;
254
+ }
255
+ function isMovingLeadReady(payload, context = {}) {
256
+ if (!movingLeadSubmissionSchema.safeParse(payload).success) return false;
257
+ if ("fromAddress" in context && !hasStructuredMovingAddress(context.fromAddress ?? null)) return false;
258
+ if ("toAddress" in context && !hasStructuredMovingAddress(context.toAddress ?? null)) return false;
259
+ return true;
260
+ }
261
+ function getMovingLeadValidationItems(payload, context = {}) {
262
+ const result = movingLeadSubmissionSchema.safeParse(payload);
263
+ const issuePaths = new Set(
264
+ result.success ? [] : result.error.issues.map((issue) => issue.path.join("."))
265
+ );
266
+ const fromAddressValid = issuePaths.has("meta.from_address") ? false : "fromAddress" in context ? hasStructuredMovingAddress(context.fromAddress ?? null) : true;
267
+ const toAddressValid = issuePaths.has("meta.to_address") ? false : "toAddress" in context ? hasStructuredMovingAddress(context.toAddress ?? null) : true;
268
+ return [
269
+ {
270
+ label: "Pickup address",
271
+ valid: fromAddressValid
272
+ },
273
+ {
274
+ label: "Delivery address",
275
+ valid: toAddressValid
276
+ },
277
+ {
278
+ label: "Name",
279
+ valid: !issuePaths.has("name")
280
+ },
281
+ {
282
+ label: "Email or phone",
283
+ valid: !issuePaths.has("email")
284
+ }
285
+ ];
286
+ }
287
+
288
+ export { buildManualMovingAddress, buildMovingBrokerCapturePayload, buildMovingLeadPayload, formatDuration, getMovingLeadValidationItems, hasCoordinates, hasStructuredMovingAddress, isMovingLeadReady, movingBrokerCapturePayloadSchema, movingLeadPayloadSchema, movingLeadSubmissionSchema, readAddressDisplay, readConfirmedAddressLine, readConfirmedAddressMeta, readManualInitialAddress, readQuoteSubmitError };