@htlkg/data 0.0.22 → 0.0.24

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@htlkg/data",
3
- "version": "0.0.22",
3
+ "version": "0.0.24",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -0,0 +1,2 @@
1
+ export * from "./useContacts";
2
+ export * from "./usePaginatedContacts";
@@ -0,0 +1,176 @@
1
+ /**
2
+ * useContacts Hook
3
+ *
4
+ * Vue composable for fetching and managing contact data with reactive state.
5
+ * Provides loading states, error handling, search, pagination, and refetch capabilities.
6
+ */
7
+
8
+ import type { Ref, ComputedRef } from "vue";
9
+ import type { Contact } from "@htlkg/core/types";
10
+ import { createDataHook, type BaseHookOptions } from "../createDataHook";
11
+
12
+ export interface UseContactsOptions extends BaseHookOptions {
13
+ /** Filter by brand ID */
14
+ brandId?: string;
15
+ /** Search query (searches email, firstName, lastName) */
16
+ search?: string;
17
+ /** Filter by GDPR consent */
18
+ gdprConsent?: boolean;
19
+ /** Filter by marketing opt-in */
20
+ marketingOptIn?: boolean;
21
+ /** Filter by tags (contact must have at least one of these tags) */
22
+ tags?: string[];
23
+ /** Pagination token for fetching next page */
24
+ nextToken?: string;
25
+ }
26
+
27
+ export interface UseContactsReturn {
28
+ /** Reactive array of contacts */
29
+ contacts: Ref<Contact[]>;
30
+ /** Computed array of contacts with GDPR consent */
31
+ consentedContacts: ComputedRef<Contact[]>;
32
+ /** Computed array of contacts opted-in for marketing */
33
+ marketingContacts: ComputedRef<Contact[]>;
34
+ /** Loading state */
35
+ loading: Ref<boolean>;
36
+ /** Error state */
37
+ error: Ref<Error | null>;
38
+ /** Refetch contacts */
39
+ refetch: () => Promise<void>;
40
+ }
41
+
42
+ /**
43
+ * Build filter from hook options
44
+ */
45
+ function buildFilter(options: UseContactsOptions): any {
46
+ const conditions: any[] = [];
47
+
48
+ // Filter by brand
49
+ if (options.brandId) {
50
+ conditions.push({ brandId: { eq: options.brandId } });
51
+ }
52
+
53
+ // Search across email, firstName, lastName
54
+ if (options.search) {
55
+ conditions.push({
56
+ or: [
57
+ { email: { contains: options.search } },
58
+ { firstName: { contains: options.search } },
59
+ { lastName: { contains: options.search } },
60
+ ],
61
+ });
62
+ }
63
+
64
+ // Filter by GDPR consent
65
+ if (options.gdprConsent !== undefined) {
66
+ conditions.push({ gdprConsent: { eq: options.gdprConsent } });
67
+ }
68
+
69
+ // Filter by marketing opt-in
70
+ if (options.marketingOptIn !== undefined) {
71
+ conditions.push({ marketingOptIn: { eq: options.marketingOptIn } });
72
+ }
73
+
74
+ // Filter by tags (contact must have at least one of these tags)
75
+ if (options.tags && options.tags.length > 0) {
76
+ const tagConditions = options.tags.map((tag) => ({
77
+ tags: { contains: tag },
78
+ }));
79
+ conditions.push({ or: tagConditions });
80
+ }
81
+
82
+ // Include any additional custom filter
83
+ if (options.filter) {
84
+ conditions.push(options.filter);
85
+ }
86
+
87
+ if (conditions.length === 0) {
88
+ return undefined;
89
+ }
90
+
91
+ if (conditions.length === 1) {
92
+ return conditions[0];
93
+ }
94
+
95
+ return { and: conditions };
96
+ }
97
+
98
+ /**
99
+ * Internal hook created by factory
100
+ */
101
+ const useContactsInternal = createDataHook<
102
+ Contact,
103
+ UseContactsOptions,
104
+ { consentedContacts: Contact[]; marketingContacts: Contact[] }
105
+ >({
106
+ model: "Contact",
107
+ dataPropertyName: "contacts",
108
+ buildFilter,
109
+ computedProperties: {
110
+ consentedContacts: (contacts) =>
111
+ contacts.filter((c) => c.gdprConsent === true),
112
+ marketingContacts: (contacts) =>
113
+ contacts.filter((c) => c.marketingOptIn === true),
114
+ },
115
+ });
116
+
117
+ /**
118
+ * Composable for fetching and managing contacts
119
+ *
120
+ * @example
121
+ * ```typescript
122
+ * import { useContacts } from '@htlkg/data/hooks';
123
+ *
124
+ * const { contacts, loading, error, refetch } = useContacts({
125
+ * brandId: 'brand-123',
126
+ * limit: 25
127
+ * });
128
+ * ```
129
+ *
130
+ * @example With search
131
+ * ```typescript
132
+ * const { contacts, loading } = useContacts({
133
+ * brandId: 'brand-123',
134
+ * search: 'john',
135
+ * limit: 25
136
+ * });
137
+ * ```
138
+ *
139
+ * @example With GDPR and marketing filters
140
+ * ```typescript
141
+ * const { contacts, marketingContacts } = useContacts({
142
+ * brandId: 'brand-123',
143
+ * gdprConsent: true,
144
+ * marketingOptIn: true
145
+ * });
146
+ * ```
147
+ *
148
+ * @example With computed properties
149
+ * ```typescript
150
+ * const { contacts, consentedContacts, marketingContacts } = useContacts({
151
+ * brandId: 'brand-123'
152
+ * });
153
+ *
154
+ * // consentedContacts - contacts with GDPR consent
155
+ * // marketingContacts - contacts opted-in for marketing
156
+ * ```
157
+ *
158
+ * @example With tag filtering
159
+ * ```typescript
160
+ * const { contacts } = useContacts({
161
+ * brandId: 'brand-123',
162
+ * tags: ['vip', 'returning']
163
+ * });
164
+ * ```
165
+ */
166
+ export function useContacts(options: UseContactsOptions = {}): UseContactsReturn {
167
+ const result = useContactsInternal(options);
168
+ return {
169
+ contacts: result.contacts as Ref<Contact[]>,
170
+ consentedContacts: result.consentedContacts as ComputedRef<Contact[]>,
171
+ marketingContacts: result.marketingContacts as ComputedRef<Contact[]>,
172
+ loading: result.loading,
173
+ error: result.error,
174
+ refetch: result.refetch,
175
+ };
176
+ }
@@ -0,0 +1,261 @@
1
+ /**
2
+ * Paginated Contacts Hooks
3
+ *
4
+ * Vue composables for fetching contacts with server-side pagination.
5
+ * Provides separate hooks for active and deleted contacts to enable
6
+ * efficient tab-based filtering in large datasets.
7
+ */
8
+
9
+ import type { Ref, ComputedRef } from "vue";
10
+ import type { Contact } from "@htlkg/core/types";
11
+ import {
12
+ createPaginatedDataHook,
13
+ ACTIVE_FILTER,
14
+ DELETED_FILTER,
15
+ type PaginatedHookOptions,
16
+ type PaginationState,
17
+ } from "../createPaginatedDataHook";
18
+
19
+ /** Extended Contact type with computed fields */
20
+ export type ContactWithRelations = Contact & {
21
+ brandName?: string;
22
+ name?: string;
23
+ deletedAt?: string;
24
+ deletedBy?: string;
25
+ };
26
+
27
+ export interface UsePaginatedContactsOptions extends PaginatedHookOptions {
28
+ /** Filter by brand ID */
29
+ brandId?: string;
30
+ /** Search query (searches email, firstName, lastName) */
31
+ search?: string;
32
+ /** Filter by GDPR consent */
33
+ gdprConsent?: boolean;
34
+ /** Filter by marketing opt-in */
35
+ marketingOptIn?: boolean;
36
+ /** Filter by tags (contact must have at least one of these tags) */
37
+ tags?: string[];
38
+ }
39
+
40
+ export interface UsePaginatedContactsReturn {
41
+ /** Reactive array of contacts */
42
+ contacts: Ref<ContactWithRelations[]>;
43
+ /** Loading state (true during any fetch) */
44
+ loading: Ref<boolean>;
45
+ /** Loading state for initial fetch */
46
+ initialLoading: Ref<boolean>;
47
+ /** Loading state for loadMore */
48
+ loadingMore: Ref<boolean>;
49
+ /** Error state */
50
+ error: Ref<Error | null>;
51
+ /** Pagination state */
52
+ pagination: Ref<PaginationState>;
53
+ /** Whether there are more items to load */
54
+ hasMore: ComputedRef<boolean>;
55
+ /** Load next page of data */
56
+ loadMore: () => Promise<void>;
57
+ /** Refetch data (resets to first page) */
58
+ refetch: () => Promise<void>;
59
+ /** Reset data and pagination state */
60
+ reset: () => void;
61
+ /** Update search filter and refetch from server (searches ALL data) */
62
+ setSearchFilter: (filter: any) => Promise<void>;
63
+ /** Current search filter */
64
+ searchFilter: Ref<any>;
65
+ }
66
+
67
+ /**
68
+ * Build additional filter from hook options
69
+ */
70
+ function buildFilter(options: UsePaginatedContactsOptions): any {
71
+ const conditions: any[] = [];
72
+
73
+ if (options.brandId) {
74
+ conditions.push({ brandId: { eq: options.brandId } });
75
+ }
76
+
77
+ // Search across email, firstName, lastName
78
+ if (options.search) {
79
+ conditions.push({
80
+ or: [
81
+ { email: { contains: options.search } },
82
+ { firstName: { contains: options.search } },
83
+ { lastName: { contains: options.search } },
84
+ ],
85
+ });
86
+ }
87
+
88
+ // Filter by GDPR consent
89
+ if (options.gdprConsent !== undefined) {
90
+ conditions.push({ gdprConsent: { eq: options.gdprConsent } });
91
+ }
92
+
93
+ // Filter by marketing opt-in
94
+ if (options.marketingOptIn !== undefined) {
95
+ conditions.push({ marketingOptIn: { eq: options.marketingOptIn } });
96
+ }
97
+
98
+ // Filter by tags (contact must have at least one of these tags)
99
+ if (options.tags && options.tags.length > 0) {
100
+ const tagConditions = options.tags.map((tag) => ({
101
+ tags: { contains: tag },
102
+ }));
103
+ conditions.push({ or: tagConditions });
104
+ }
105
+
106
+ if (options.filter && Object.keys(options.filter).length > 0) {
107
+ conditions.push(options.filter);
108
+ }
109
+
110
+ if (conditions.length === 0) {
111
+ return undefined;
112
+ }
113
+ if (conditions.length === 1) {
114
+ return conditions[0];
115
+ }
116
+ return { and: conditions };
117
+ }
118
+
119
+ /**
120
+ * Transform contact data
121
+ */
122
+ function transformContact(contact: any): ContactWithRelations {
123
+ return {
124
+ ...contact,
125
+ name: `${contact.firstName || ""} ${contact.lastName || ""}`.trim() || contact.email,
126
+ brandName: contact.brand?.name || "",
127
+ };
128
+ }
129
+
130
+ /**
131
+ * Selection set for contact queries
132
+ */
133
+ const CONTACT_SELECTION_SET = [
134
+ "id",
135
+ "email",
136
+ "phone",
137
+ "firstName",
138
+ "lastName",
139
+ "locale",
140
+ "brandId",
141
+ "gdprConsent",
142
+ "gdprConsentDate",
143
+ "marketingOptIn",
144
+ "preferences",
145
+ "tags",
146
+ "totalVisits",
147
+ "lastVisitDate",
148
+ "firstVisitDate",
149
+ "source",
150
+ "status",
151
+ "createdAt",
152
+ "updatedAt",
153
+ "deletedAt",
154
+ "deletedBy",
155
+ "brand.name",
156
+ ];
157
+
158
+ /**
159
+ * Internal hook for active contacts
160
+ */
161
+ const useActiveContactsInternal = createPaginatedDataHook<ContactWithRelations, UsePaginatedContactsOptions>({
162
+ model: "Contact",
163
+ dataPropertyName: "contacts",
164
+ defaultPageSize: 25,
165
+ selectionSet: CONTACT_SELECTION_SET,
166
+ transform: transformContact,
167
+ buildFilter,
168
+ baseFilter: ACTIVE_FILTER,
169
+ });
170
+
171
+ /**
172
+ * Internal hook for deleted contacts
173
+ */
174
+ const useDeletedContactsInternal = createPaginatedDataHook<ContactWithRelations, UsePaginatedContactsOptions>({
175
+ model: "Contact",
176
+ dataPropertyName: "contacts",
177
+ defaultPageSize: 25,
178
+ selectionSet: CONTACT_SELECTION_SET,
179
+ transform: transformContact,
180
+ buildFilter,
181
+ baseFilter: DELETED_FILTER,
182
+ });
183
+
184
+ /**
185
+ * Composable for fetching active (non-deleted) contacts with pagination
186
+ *
187
+ * @example
188
+ * ```typescript
189
+ * import { useActiveContacts } from '@htlkg/data/hooks';
190
+ *
191
+ * const { contacts, loading, hasMore, loadMore, refetch, setSearchFilter } = useActiveContacts({
192
+ * pageSize: 25,
193
+ * });
194
+ *
195
+ * // Load more when user scrolls or clicks "Load More"
196
+ * async function onLoadMore() {
197
+ * await loadMore();
198
+ * }
199
+ *
200
+ * // Server-side search (searches ALL contacts in database)
201
+ * async function onSearch(filter: any) {
202
+ * await setSearchFilter(filter);
203
+ * }
204
+ * ```
205
+ *
206
+ * @example With filters
207
+ * ```typescript
208
+ * const { contacts, loading } = useActiveContacts({
209
+ * brandId: 'brand-123',
210
+ * gdprConsent: true,
211
+ * pageSize: 50
212
+ * });
213
+ * ```
214
+ */
215
+ export function useActiveContacts(options: UsePaginatedContactsOptions = {}): UsePaginatedContactsReturn {
216
+ const result = useActiveContactsInternal(options);
217
+ return {
218
+ contacts: result.contacts as Ref<ContactWithRelations[]>,
219
+ loading: result.loading,
220
+ initialLoading: result.initialLoading,
221
+ loadingMore: result.loadingMore,
222
+ error: result.error,
223
+ pagination: result.pagination,
224
+ hasMore: result.hasMore,
225
+ loadMore: result.loadMore,
226
+ refetch: result.refetch,
227
+ reset: result.reset,
228
+ setSearchFilter: result.setSearchFilter,
229
+ searchFilter: result.searchFilter,
230
+ };
231
+ }
232
+
233
+ /**
234
+ * Composable for fetching deleted contacts with pagination
235
+ *
236
+ * @example
237
+ * ```typescript
238
+ * import { useDeletedContacts } from '@htlkg/data/hooks';
239
+ *
240
+ * const { contacts, loading, hasMore, loadMore, refetch } = useDeletedContacts({
241
+ * pageSize: 25,
242
+ * });
243
+ * ```
244
+ */
245
+ export function useDeletedContacts(options: UsePaginatedContactsOptions = {}): UsePaginatedContactsReturn {
246
+ const result = useDeletedContactsInternal(options);
247
+ return {
248
+ contacts: result.contacts as Ref<ContactWithRelations[]>,
249
+ loading: result.loading,
250
+ initialLoading: result.initialLoading,
251
+ loadingMore: result.loadingMore,
252
+ error: result.error,
253
+ pagination: result.pagination,
254
+ hasMore: result.hasMore,
255
+ loadMore: result.loadMore,
256
+ refetch: result.refetch,
257
+ reset: result.reset,
258
+ setSearchFilter: result.setSearchFilter,
259
+ searchFilter: result.searchFilter,
260
+ };
261
+ }
@@ -107,6 +107,11 @@ export {
107
107
  // ============================================
108
108
  export {
109
109
  useContacts,
110
+ useActiveContacts,
111
+ useDeletedContacts,
110
112
  type UseContactsOptions,
111
113
  type UseContactsReturn,
112
- } from "./useContacts";
114
+ type ContactWithRelations,
115
+ type UsePaginatedContactsOptions,
116
+ type UsePaginatedContactsReturn,
117
+ } from "./contacts";