@htlkg/data 0.0.20 → 0.0.21

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.
@@ -0,0 +1,237 @@
1
+ /**
2
+ * Contact Query Functions
3
+ *
4
+ * Provides query functions for fetching contact data from the GraphQL API.
5
+ */
6
+
7
+ import type { Contact } from "@htlkg/core/types";
8
+
9
+ /**
10
+ * Get a single contact by ID
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * import { getContact } from '@htlkg/data/queries';
15
+ * import { generateClient } from '@htlkg/data/client';
16
+ *
17
+ * const client = generateClient<Schema>();
18
+ * const contact = await getContact(client, 'contact-123');
19
+ * ```
20
+ */
21
+ export async function getContact<TClient = any>(
22
+ client: TClient,
23
+ id: string,
24
+ ): Promise<Contact | null> {
25
+ try {
26
+ const { data, errors } = await (client as any).models.Contact.get({ id });
27
+
28
+ if (errors) {
29
+ console.error("[getContact] GraphQL errors:", errors);
30
+ return null;
31
+ }
32
+
33
+ return data as Contact;
34
+ } catch (error) {
35
+ console.error("[getContact] Error fetching contact:", error);
36
+ throw error;
37
+ }
38
+ }
39
+
40
+ /**
41
+ * List all contacts with optional filtering
42
+ *
43
+ * @example
44
+ * ```typescript
45
+ * import { listContacts } from '@htlkg/data/queries';
46
+ * import { generateClient } from '@htlkg/data/client';
47
+ *
48
+ * const client = generateClient<Schema>();
49
+ * const contacts = await listContacts(client, {
50
+ * filter: { gdprConsent: { eq: true } },
51
+ * limit: 50
52
+ * });
53
+ * ```
54
+ */
55
+ export async function listContacts<TClient = any>(
56
+ client: TClient,
57
+ options?: {
58
+ filter?: any;
59
+ limit?: number;
60
+ nextToken?: string;
61
+ },
62
+ ): Promise<{ items: Contact[]; nextToken?: string }> {
63
+ try {
64
+ const { data, errors, nextToken } = await (
65
+ client as any
66
+ ).models.Contact.list(options);
67
+
68
+ if (errors) {
69
+ console.error("[listContacts] GraphQL errors:", errors);
70
+ return { items: [], nextToken: undefined };
71
+ }
72
+
73
+ return {
74
+ items: (data || []) as Contact[],
75
+ nextToken,
76
+ };
77
+ } catch (error) {
78
+ console.error("[listContacts] Error fetching contacts:", error);
79
+ throw error;
80
+ }
81
+ }
82
+
83
+ /**
84
+ * List contacts by brand ID
85
+ *
86
+ * @example
87
+ * ```typescript
88
+ * import { listContactsByBrand } from '@htlkg/data/queries';
89
+ * import { generateClient } from '@htlkg/data/client';
90
+ *
91
+ * const client = generateClient<Schema>();
92
+ * const contacts = await listContactsByBrand(client, 'brand-123');
93
+ * ```
94
+ */
95
+ export async function listContactsByBrand<TClient = any>(
96
+ client: TClient,
97
+ brandId: string,
98
+ options?: {
99
+ limit?: number;
100
+ nextToken?: string;
101
+ },
102
+ ): Promise<{ items: Contact[]; nextToken?: string }> {
103
+ return listContacts(client, {
104
+ filter: { brandId: { eq: brandId } },
105
+ ...options,
106
+ });
107
+ }
108
+
109
+ /**
110
+ * Get a contact by email within a brand
111
+ *
112
+ * @example
113
+ * ```typescript
114
+ * import { getContactByEmail } from '@htlkg/data/queries';
115
+ * import { generateClient } from '@htlkg/data/client';
116
+ *
117
+ * const client = generateClient<Schema>();
118
+ * const contact = await getContactByEmail(client, 'guest@example.com', 'brand-123');
119
+ * ```
120
+ */
121
+ export async function getContactByEmail<TClient = any>(
122
+ client: TClient,
123
+ email: string,
124
+ brandId: string,
125
+ ): Promise<Contact | null> {
126
+ try {
127
+ const { data, errors } = await (client as any).models.Contact.list({
128
+ filter: {
129
+ and: [{ email: { eq: email } }, { brandId: { eq: brandId } }],
130
+ },
131
+ limit: 1,
132
+ });
133
+
134
+ if (errors) {
135
+ console.error("[getContactByEmail] GraphQL errors:", errors);
136
+ return null;
137
+ }
138
+
139
+ return data?.[0] as Contact | null;
140
+ } catch (error) {
141
+ console.error("[getContactByEmail] Error fetching contact:", error);
142
+ throw error;
143
+ }
144
+ }
145
+
146
+ /**
147
+ * Get a contact by phone within a brand
148
+ *
149
+ * @example
150
+ * ```typescript
151
+ * import { getContactByPhone } from '@htlkg/data/queries';
152
+ * import { generateClient } from '@htlkg/data/client';
153
+ *
154
+ * const client = generateClient<Schema>();
155
+ * const contact = await getContactByPhone(client, '+1234567890', 'brand-123');
156
+ * ```
157
+ */
158
+ export async function getContactByPhone<TClient = any>(
159
+ client: TClient,
160
+ phone: string,
161
+ brandId: string,
162
+ ): Promise<Contact | null> {
163
+ try {
164
+ const { data, errors } = await (client as any).models.Contact.list({
165
+ filter: {
166
+ and: [{ phone: { eq: phone } }, { brandId: { eq: brandId } }],
167
+ },
168
+ limit: 1,
169
+ });
170
+
171
+ if (errors) {
172
+ console.error("[getContactByPhone] GraphQL errors:", errors);
173
+ return null;
174
+ }
175
+
176
+ return data?.[0] as Contact | null;
177
+ } catch (error) {
178
+ console.error("[getContactByPhone] Error fetching contact:", error);
179
+ throw error;
180
+ }
181
+ }
182
+
183
+ /**
184
+ * Search contacts by query string within a brand
185
+ * Searches across email, firstName, and lastName fields
186
+ *
187
+ * @example
188
+ * ```typescript
189
+ * import { searchContacts } from '@htlkg/data/queries';
190
+ * import { generateClient } from '@htlkg/data/client';
191
+ *
192
+ * const client = generateClient<Schema>();
193
+ * const contacts = await searchContacts(client, 'john', 'brand-123');
194
+ * ```
195
+ */
196
+ export async function searchContacts<TClient = any>(
197
+ client: TClient,
198
+ query: string,
199
+ brandId: string,
200
+ options?: {
201
+ limit?: number;
202
+ nextToken?: string;
203
+ },
204
+ ): Promise<{ items: Contact[]; nextToken?: string }> {
205
+ try {
206
+ const { data, errors, nextToken } = await (
207
+ client as any
208
+ ).models.Contact.list({
209
+ filter: {
210
+ and: [
211
+ { brandId: { eq: brandId } },
212
+ {
213
+ or: [
214
+ { email: { contains: query } },
215
+ { firstName: { contains: query } },
216
+ { lastName: { contains: query } },
217
+ ],
218
+ },
219
+ ],
220
+ },
221
+ ...options,
222
+ });
223
+
224
+ if (errors) {
225
+ console.error("[searchContacts] GraphQL errors:", errors);
226
+ return { items: [], nextToken: undefined };
227
+ }
228
+
229
+ return {
230
+ items: (data || []) as Contact[],
231
+ nextToken,
232
+ };
233
+ } catch (error) {
234
+ console.error("[searchContacts] Error searching contacts:", error);
235
+ throw error;
236
+ }
237
+ }
@@ -63,3 +63,13 @@ export {
63
63
  getReservationByConfirmation,
64
64
  } from "./reservations";
65
65
  export type { Reservation } from "./reservations";
66
+
67
+ // Contact queries
68
+ export {
69
+ getContact,
70
+ listContacts,
71
+ listContactsByBrand,
72
+ getContactByEmail,
73
+ getContactByPhone,
74
+ searchContacts,
75
+ } from "./contacts";