@htlkg/data 0.0.14 → 0.0.16

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.
Files changed (37) hide show
  1. package/README.md +72 -0
  2. package/dist/client/index.d.ts +123 -30
  3. package/dist/client/index.js +75 -1
  4. package/dist/client/index.js.map +1 -1
  5. package/dist/hooks/index.d.ts +76 -2
  6. package/dist/hooks/index.js +224 -6
  7. package/dist/hooks/index.js.map +1 -1
  8. package/dist/index.d.ts +6 -4
  9. package/dist/index.js +550 -7
  10. package/dist/index.js.map +1 -1
  11. package/dist/mutations/index.d.ts +149 -5
  12. package/dist/mutations/index.js +397 -0
  13. package/dist/mutations/index.js.map +1 -1
  14. package/dist/productInstances-CzT3NZKU.d.ts +98 -0
  15. package/dist/queries/index.d.ts +54 -2
  16. package/dist/queries/index.js +60 -1
  17. package/dist/queries/index.js.map +1 -1
  18. package/dist/server/index.d.ts +47 -0
  19. package/dist/server/index.js +59 -0
  20. package/dist/server/index.js.map +1 -0
  21. package/package.json +5 -1
  22. package/src/client/index.ts +82 -3
  23. package/src/client/proxy.ts +170 -0
  24. package/src/hooks/index.ts +1 -0
  25. package/src/hooks/useProductInstances.ts +174 -0
  26. package/src/index.ts +11 -0
  27. package/src/mutations/accounts.ts +102 -1
  28. package/src/mutations/brands.ts +102 -1
  29. package/src/mutations/index.ts +23 -0
  30. package/src/mutations/productInstances/index.ts +14 -0
  31. package/src/mutations/productInstances/productInstances.integration.test.ts +621 -0
  32. package/src/mutations/productInstances/productInstances.test.ts +680 -0
  33. package/src/mutations/productInstances/productInstances.ts +280 -0
  34. package/src/mutations/systemSettings.ts +130 -0
  35. package/src/mutations/users.ts +102 -1
  36. package/src/queries/index.ts +9 -0
  37. package/src/queries/systemSettings.ts +115 -0
@@ -5,6 +5,10 @@
5
5
  */
6
6
 
7
7
  import type { Account } from "@htlkg/core/types";
8
+ import {
9
+ checkRestoreEligibility,
10
+ DEFAULT_SOFT_DELETE_RETENTION_DAYS,
11
+ } from "../queries/systemSettings";
8
12
 
9
13
  /**
10
14
  * Input type for creating an account
@@ -96,7 +100,104 @@ export async function updateAccount<TClient = any>(
96
100
  }
97
101
 
98
102
  /**
99
- * Delete an account
103
+ * Soft delete an account (sets status to "deleted" instead of removing)
104
+ *
105
+ * @example
106
+ * ```typescript
107
+ * import { softDeleteAccount } from '@htlkg/data/mutations';
108
+ * import { generateClient } from '@htlkg/data/client';
109
+ *
110
+ * const client = generateClient<Schema>();
111
+ * await softDeleteAccount(client, 'account-123', 'admin@example.com');
112
+ * ```
113
+ */
114
+ export async function softDeleteAccount<TClient = any>(
115
+ client: TClient,
116
+ id: string,
117
+ deletedBy: string,
118
+ ): Promise<boolean> {
119
+ try {
120
+ const { errors } = await (client as any).models.Account.update({
121
+ id,
122
+ status: "deleted",
123
+ deletedAt: new Date().toISOString(),
124
+ deletedBy,
125
+ });
126
+
127
+ if (errors) {
128
+ console.error("[softDeleteAccount] GraphQL errors:", errors);
129
+ return false;
130
+ }
131
+
132
+ return true;
133
+ } catch (error) {
134
+ console.error("[softDeleteAccount] Error soft-deleting account:", error);
135
+ throw error;
136
+ }
137
+ }
138
+
139
+ /**
140
+ * Restore a soft-deleted account (sets status back to "active")
141
+ * Checks retention period before allowing restoration
142
+ *
143
+ * @example
144
+ * ```typescript
145
+ * import { restoreAccount } from '@htlkg/data/mutations';
146
+ * import { generateClient } from '@htlkg/data/client';
147
+ *
148
+ * const client = generateClient<Schema>();
149
+ * await restoreAccount(client, 'account-123');
150
+ * // Or with custom retention days:
151
+ * await restoreAccount(client, 'account-123', 60);
152
+ * ```
153
+ */
154
+ export async function restoreAccount<TClient = any>(
155
+ client: TClient,
156
+ id: string,
157
+ retentionDays: number = DEFAULT_SOFT_DELETE_RETENTION_DAYS,
158
+ ): Promise<{ success: boolean; error?: string }> {
159
+ try {
160
+ // First, get the account to check its deletedAt timestamp
161
+ const { data: account, errors: getErrors } = await (
162
+ client as any
163
+ ).models.Account.get({ id });
164
+
165
+ if (getErrors || !account) {
166
+ console.error("[restoreAccount] Error fetching account:", getErrors);
167
+ return { success: false, error: "Account not found" };
168
+ }
169
+
170
+ // Check if restoration is allowed based on retention period
171
+ const eligibility = checkRestoreEligibility(account.deletedAt, retentionDays);
172
+
173
+ if (!eligibility.canRestore) {
174
+ const errorMsg = `Cannot restore account. Retention period of ${retentionDays} days has expired. Item was deleted ${eligibility.daysExpired} days ago.`;
175
+ console.error("[restoreAccount]", errorMsg);
176
+ return { success: false, error: errorMsg };
177
+ }
178
+
179
+ const { errors } = await (client as any).models.Account.update({
180
+ id,
181
+ status: "active",
182
+ deletedAt: null,
183
+ deletedBy: null,
184
+ });
185
+
186
+ if (errors) {
187
+ console.error("[restoreAccount] GraphQL errors:", errors);
188
+ return { success: false, error: "Failed to restore account" };
189
+ }
190
+
191
+ return { success: true };
192
+ } catch (error) {
193
+ console.error("[restoreAccount] Error restoring account:", error);
194
+ throw error;
195
+ }
196
+ }
197
+
198
+ /**
199
+ * Hard delete an account (permanently removes from database)
200
+ * Use with caution - prefer softDeleteAccount for recoverable deletion
100
201
  *
101
202
  * @example
102
203
  * ```typescript
@@ -5,6 +5,10 @@
5
5
  */
6
6
 
7
7
  import type { Brand } from "@htlkg/core/types";
8
+ import {
9
+ checkRestoreEligibility,
10
+ DEFAULT_SOFT_DELETE_RETENTION_DAYS,
11
+ } from "../queries/systemSettings";
8
12
 
9
13
  /**
10
14
  * Input type for creating a brand
@@ -102,7 +106,104 @@ export async function updateBrand<TClient = any>(
102
106
  }
103
107
 
104
108
  /**
105
- * Delete a brand
109
+ * Soft delete a brand (sets status to "deleted" instead of removing)
110
+ *
111
+ * @example
112
+ * ```typescript
113
+ * import { softDeleteBrand } from '@htlkg/data/mutations';
114
+ * import { generateClient } from '@htlkg/data/client';
115
+ *
116
+ * const client = generateClient<Schema>();
117
+ * await softDeleteBrand(client, 'brand-123', 'admin@example.com');
118
+ * ```
119
+ */
120
+ export async function softDeleteBrand<TClient = any>(
121
+ client: TClient,
122
+ id: string,
123
+ deletedBy: string,
124
+ ): Promise<boolean> {
125
+ try {
126
+ const { errors } = await (client as any).models.Brand.update({
127
+ id,
128
+ status: "deleted",
129
+ deletedAt: new Date().toISOString(),
130
+ deletedBy,
131
+ });
132
+
133
+ if (errors) {
134
+ console.error("[softDeleteBrand] GraphQL errors:", errors);
135
+ return false;
136
+ }
137
+
138
+ return true;
139
+ } catch (error) {
140
+ console.error("[softDeleteBrand] Error soft-deleting brand:", error);
141
+ throw error;
142
+ }
143
+ }
144
+
145
+ /**
146
+ * Restore a soft-deleted brand (sets status back to "active")
147
+ * Checks retention period before allowing restoration
148
+ *
149
+ * @example
150
+ * ```typescript
151
+ * import { restoreBrand } from '@htlkg/data/mutations';
152
+ * import { generateClient } from '@htlkg/data/client';
153
+ *
154
+ * const client = generateClient<Schema>();
155
+ * await restoreBrand(client, 'brand-123');
156
+ * // Or with custom retention days:
157
+ * await restoreBrand(client, 'brand-123', 60);
158
+ * ```
159
+ */
160
+ export async function restoreBrand<TClient = any>(
161
+ client: TClient,
162
+ id: string,
163
+ retentionDays: number = DEFAULT_SOFT_DELETE_RETENTION_DAYS,
164
+ ): Promise<{ success: boolean; error?: string }> {
165
+ try {
166
+ // First, get the brand to check its deletedAt timestamp
167
+ const { data: brand, errors: getErrors } = await (
168
+ client as any
169
+ ).models.Brand.get({ id });
170
+
171
+ if (getErrors || !brand) {
172
+ console.error("[restoreBrand] Error fetching brand:", getErrors);
173
+ return { success: false, error: "Brand not found" };
174
+ }
175
+
176
+ // Check if restoration is allowed based on retention period
177
+ const eligibility = checkRestoreEligibility(brand.deletedAt, retentionDays);
178
+
179
+ if (!eligibility.canRestore) {
180
+ const errorMsg = `Cannot restore brand. Retention period of ${retentionDays} days has expired. Item was deleted ${eligibility.daysExpired} days ago.`;
181
+ console.error("[restoreBrand]", errorMsg);
182
+ return { success: false, error: errorMsg };
183
+ }
184
+
185
+ const { errors } = await (client as any).models.Brand.update({
186
+ id,
187
+ status: "active",
188
+ deletedAt: null,
189
+ deletedBy: null,
190
+ });
191
+
192
+ if (errors) {
193
+ console.error("[restoreBrand] GraphQL errors:", errors);
194
+ return { success: false, error: "Failed to restore brand" };
195
+ }
196
+
197
+ return { success: true };
198
+ } catch (error) {
199
+ console.error("[restoreBrand] Error restoring brand:", error);
200
+ throw error;
201
+ }
202
+ }
203
+
204
+ /**
205
+ * Hard delete a brand (permanently removes from database)
206
+ * Use with caution - prefer softDeleteBrand for recoverable deletion
106
207
  *
107
208
  * @example
108
209
  * ```typescript
@@ -9,6 +9,8 @@ export {
9
9
  createBrand,
10
10
  updateBrand,
11
11
  deleteBrand,
12
+ softDeleteBrand,
13
+ restoreBrand,
12
14
  type CreateBrandInput,
13
15
  type UpdateBrandInput,
14
16
  } from "./brands";
@@ -18,6 +20,8 @@ export {
18
20
  createAccount,
19
21
  updateAccount,
20
22
  deleteAccount,
23
+ softDeleteAccount,
24
+ restoreAccount,
21
25
  type CreateAccountInput,
22
26
  type UpdateAccountInput,
23
27
  } from "./accounts";
@@ -27,6 +31,25 @@ export {
27
31
  createUser,
28
32
  updateUser,
29
33
  deleteUser,
34
+ softDeleteUser,
35
+ restoreUser,
30
36
  type CreateUserInput,
31
37
  type UpdateUserInput,
32
38
  } from "./users";
39
+
40
+ // ProductInstance mutations
41
+ export {
42
+ createProductInstance,
43
+ updateProductInstance,
44
+ deleteProductInstance,
45
+ toggleProductInstanceEnabled,
46
+ type CreateProductInstanceInput,
47
+ type UpdateProductInstanceInput,
48
+ } from "./productInstances";
49
+
50
+ // SystemSettings mutations
51
+ export {
52
+ updateSystemSettings,
53
+ initializeSystemSettings,
54
+ type UpdateSystemSettingsInput,
55
+ } from "./systemSettings";
@@ -0,0 +1,14 @@
1
+ /**
2
+ * ProductInstance Mutations Index
3
+ *
4
+ * Re-exports all product instance mutation functions and types.
5
+ */
6
+
7
+ export {
8
+ createProductInstance,
9
+ updateProductInstance,
10
+ deleteProductInstance,
11
+ toggleProductInstanceEnabled,
12
+ type CreateProductInstanceInput,
13
+ type UpdateProductInstanceInput,
14
+ } from "./productInstances";