@machinemetrics/mm-erp-sdk 0.1.8-beta.8 → 0.1.8

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 (57) hide show
  1. package/README.md +2 -1
  2. package/dist/{config-qat9zgOl.js → config-cB7h4yvc.js} +2 -2
  3. package/dist/{config-qat9zgOl.js.map → config-cB7h4yvc.js.map} +1 -1
  4. package/dist/{connector-factory-C2czCs9v.js → connector-factory-CKm74_WZ.js} +2 -2
  5. package/dist/{connector-factory-C2czCs9v.js.map → connector-factory-CKm74_WZ.js.map} +1 -1
  6. package/dist/{hashed-cache-manager-CzyFSt2B.js → hashed-cache-manager-Ds-HksA0.js} +34 -4
  7. package/dist/hashed-cache-manager-Ds-HksA0.js.map +1 -0
  8. package/dist/{index-B9wo8pld.js → index-DTtmv8Iq.js} +16 -5
  9. package/dist/index-DTtmv8Iq.js.map +1 -0
  10. package/dist/index.d.ts +6 -4
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/{logger-Db8CkwR6.js → logger-CBDNtsMq.js} +62 -11
  13. package/dist/{logger-Db8CkwR6.js.map → logger-CBDNtsMq.js.map} +1 -1
  14. package/dist/mm-erp-sdk.js +256 -17
  15. package/dist/mm-erp-sdk.js.map +1 -1
  16. package/dist/services/data-sync-service/configuration-manager.d.ts +5 -0
  17. package/dist/services/data-sync-service/configuration-manager.d.ts.map +1 -1
  18. package/dist/services/data-sync-service/jobs/clean-up-expired-cache.js +4 -4
  19. package/dist/services/data-sync-service/jobs/from-erp.js +4 -4
  20. package/dist/services/data-sync-service/jobs/retry-failed-labor-tickets.js +3 -3
  21. package/dist/services/data-sync-service/jobs/run-migrations.js +1 -1
  22. package/dist/services/data-sync-service/jobs/to-erp.js +3 -3
  23. package/dist/services/psql-erp-service/configuration.d.ts +8 -0
  24. package/dist/services/psql-erp-service/configuration.d.ts.map +1 -0
  25. package/dist/services/psql-erp-service/index.d.ts +14 -0
  26. package/dist/services/psql-erp-service/index.d.ts.map +1 -0
  27. package/dist/services/psql-erp-service/internal/types/psql-types.d.ts +12 -0
  28. package/dist/services/psql-erp-service/internal/types/psql-types.d.ts.map +1 -0
  29. package/dist/services/psql-erp-service/psql-helpers.d.ts +32 -0
  30. package/dist/services/psql-erp-service/psql-helpers.d.ts.map +1 -0
  31. package/dist/services/psql-erp-service/psql-service.d.ts +50 -0
  32. package/dist/services/psql-erp-service/psql-service.d.ts.map +1 -0
  33. package/dist/services/reporting-service/logger.d.ts.map +1 -1
  34. package/dist/utils/index.d.ts +1 -1
  35. package/dist/utils/index.d.ts.map +1 -1
  36. package/dist/utils/local-data-store/jobs-shared-data.d.ts +15 -1
  37. package/dist/utils/local-data-store/jobs-shared-data.d.ts.map +1 -1
  38. package/dist/utils/time-utils.d.ts +2 -1
  39. package/dist/utils/time-utils.d.ts.map +1 -1
  40. package/dist/utils/timezone.d.ts +6 -3
  41. package/dist/utils/timezone.d.ts.map +1 -1
  42. package/package.json +4 -1
  43. package/src/index.ts +31 -6
  44. package/src/services/data-sync-service/configuration-manager.ts +33 -0
  45. package/src/services/psql-erp-service/configuration.ts +7 -0
  46. package/src/services/psql-erp-service/index.ts +22 -0
  47. package/src/services/psql-erp-service/internal/types/psql-types.ts +13 -0
  48. package/src/services/psql-erp-service/psql-helpers.ts +114 -0
  49. package/src/services/psql-erp-service/psql-service.ts +247 -0
  50. package/src/services/reporting-service/logger.ts +73 -11
  51. package/src/utils/application-initializer.ts +1 -1
  52. package/src/utils/index.ts +4 -1
  53. package/src/utils/local-data-store/jobs-shared-data.ts +24 -1
  54. package/src/utils/time-utils.ts +11 -6
  55. package/src/utils/timezone.ts +9 -6
  56. package/dist/hashed-cache-manager-CzyFSt2B.js.map +0 -1
  57. package/dist/index-B9wo8pld.js.map +0 -1
@@ -0,0 +1,247 @@
1
+ import { PsqlConfiguration } from "./configuration";
2
+ import { ERPResponse } from "../../types/erp-types";
3
+ import { OdbcErrorResponse } from "./internal/types/psql-types";
4
+ import logger from "../reporting-service/logger";
5
+
6
+ type PagingParams = {
7
+ limit?: number;
8
+ offset?: number;
9
+ };
10
+
11
+ /**
12
+ * ODBC connection interface for type safety
13
+ */
14
+ interface OdbcConnection {
15
+ query(sql: string): Promise<any[]>;
16
+ close(): Promise<void>;
17
+ }
18
+
19
+ /**
20
+ * ODBC module interface
21
+ */
22
+ interface OdbcModule {
23
+ connect(connectionString: string): Promise<OdbcConnection>;
24
+ }
25
+
26
+ export class PsqlService {
27
+ private config: PsqlConfiguration;
28
+ private static odbcModule: OdbcModule | null = null;
29
+ private static odbcLoadError: Error | null = null;
30
+
31
+ constructor(config: PsqlConfiguration) {
32
+ this.config = config;
33
+ }
34
+
35
+ /**
36
+ * Dynamically load the ODBC module with lazy initialization and caching
37
+ * @throws Error with helpful message if ODBC package is not installed
38
+ */
39
+ private static async getOdbc(): Promise<OdbcModule> {
40
+ // If we've already tried and failed, throw the cached error
41
+ if (this.odbcLoadError) {
42
+ throw this.odbcLoadError;
43
+ }
44
+
45
+ // If already loaded, return cached module
46
+ if (this.odbcModule) {
47
+ return this.odbcModule;
48
+ }
49
+
50
+ try {
51
+ // Dynamic import - only loads when actually needed
52
+ // @ts-ignore - odbc is an optional dependency, may not be installed at build time
53
+ const odbcImport = await import("odbc");
54
+ // Handle both default export and named export patterns
55
+ const odbc = odbcImport.default || odbcImport;
56
+ this.odbcModule = odbc as OdbcModule;
57
+ return this.odbcModule;
58
+ } catch (error) {
59
+ const errorMessage = error instanceof Error ? error.message : String(error);
60
+ this.odbcLoadError = new Error(
61
+ `ODBC package is required for PSQL service but is not installed or failed to load.\n` +
62
+ `Install it with: npm install odbc\n` +
63
+ `Also install OS-level dependencies, e.g. on Alpine Linux:\n` +
64
+ ` apk add --no-cache unixodbc unixodbc-dev python3 make g++\n` +
65
+ `For other Linux distributions, install unixodbc and unixodbc-dev packages.\n` +
66
+ `Original error: ${errorMessage}`
67
+ );
68
+ throw this.odbcLoadError;
69
+ }
70
+ }
71
+
72
+ // REMOVED: dispose() method - not needed anymore
73
+ // REMOVED: connection property - not needed anymore
74
+ // REMOVED: openConnection() method - not needed anymore
75
+ // REMOVED: closeConnection() method - not needed anymore
76
+
77
+ /**
78
+ * Build PSQL ODBC connection string
79
+ * CRITICAL: ServerName must use IP.PORT format (e.g., 10.4.0.11.1583)
80
+ */
81
+ private buildConnectionString(): string {
82
+ const serverName = `${this.config.host}.${this.config.port}`;
83
+
84
+ return (
85
+ [
86
+ "Driver={Pervasive ODBC Interface}",
87
+ `ServerName=${serverName}`,
88
+ `DBQ=${this.config.database}`,
89
+ `UID=${this.config.username}`,
90
+ `PWD=${this.config.password}`,
91
+ "AutoDoubleQuote=0",
92
+ ].join(";") + ";"
93
+ );
94
+ }
95
+
96
+ /**
97
+ * Execute a query and return the results
98
+ * Creates a fresh connection for each query to avoid handle corruption
99
+ *
100
+ * SECURITY WARNING: This method executes the provided SQL string as-is.
101
+ * - Parameter binding is NOT implemented; the `params` argument is currently ignored.
102
+ * - Never concatenate untrusted/user-supplied input into `query`.
103
+ * - Doing so can result in SQL injection vulnerabilities and data exposure.
104
+ * If dynamic values are required, ensure they are strictly validated/escaped
105
+ * or implement proper parameterized execution before accepting untrusted input.
106
+ *
107
+ * @param query The SQL query to execute
108
+ * @param params Query parameters (currently unused for PSQL read operations)
109
+ * @param paging Optional paging parameters
110
+ * @returns The entities fetched from the database, along with paging information
111
+ */
112
+ public async executePreparedStatement(
113
+ query: string,
114
+ params: Record<string, string> = {},
115
+ paging?: PagingParams
116
+ ): Promise<ERPResponse | undefined> {
117
+ // Dynamically load ODBC module (will throw helpful error if not installed)
118
+ const odbc = await PsqlService.getOdbc();
119
+ let connection: OdbcConnection | null = null;
120
+
121
+ try {
122
+ // Create fresh connection for THIS query only
123
+ const connStr = this.buildConnectionString();
124
+ logger.debug("Creating fresh PSQL connection for query");
125
+ connection = await odbc.connect(connStr);
126
+
127
+ if (Object.keys(params).length > 0) {
128
+ logger.warn(
129
+ "PsqlService: Query parameters provided but parameter binding not yet implemented. " +
130
+ "Using direct query execution."
131
+ );
132
+ }
133
+
134
+ const records = await connection.query(query);
135
+ const allRecords = PsqlService.recordsetToRecords(records);
136
+ const rowsFetched = allRecords.length;
137
+
138
+ // Apply paging if requested
139
+ const pagedData =
140
+ paging?.offset !== undefined || paging?.limit !== undefined
141
+ ? allRecords.slice(
142
+ paging.offset || 0,
143
+ (paging.offset || 0) + (paging.limit || allRecords.length)
144
+ )
145
+ : allRecords;
146
+
147
+ return {
148
+ data: pagedData,
149
+ paging: {
150
+ count: rowsFetched,
151
+ limit: paging?.limit || 0,
152
+ offset: paging?.offset || 0,
153
+ nextPage:
154
+ paging?.limit && (paging.offset || 0) + paging.limit < rowsFetched
155
+ ? String((paging.offset || 0) + paging.limit)
156
+ : undefined,
157
+ previousPage: paging?.offset
158
+ ? String(Math.max(0, (paging.offset || 0) - (paging.limit || 10)))
159
+ : undefined,
160
+ },
161
+ };
162
+ } catch (error) {
163
+ // If this is an ODBC load error (from getOdbc), re-throw it as-is
164
+ // since it already has a helpful error message
165
+ if (error instanceof Error && error.message.includes("ODBC package is required")) {
166
+ throw error;
167
+ }
168
+
169
+ // Otherwise, handle as ODBC runtime error
170
+ const errorInfo = error as OdbcErrorResponse;
171
+ logger.error("Error fetching data from PSQL", {
172
+ error: errorInfo.message,
173
+ odbcErrors: errorInfo.odbcErrors,
174
+ query: query.substring(0, 200), // Log first 200 chars of query
175
+ });
176
+
177
+ throw this.handleOdbcError(errorInfo);
178
+ } finally {
179
+ // CRITICAL: Always close connection, even on error
180
+ if (connection) {
181
+ try {
182
+ await connection.close();
183
+ logger.debug("PSQL connection closed successfully");
184
+ } catch (err) {
185
+ // Don't throw on close errors, just log
186
+ logger.warn("Error closing PSQL connection (non-fatal)", {
187
+ error: err,
188
+ });
189
+ }
190
+ }
191
+ }
192
+ }
193
+
194
+ /**
195
+ * Transform ODBC result set to array of Record<string, string> instances.
196
+ * IMPORTANT: PSQL CHAR fields are often padded with spaces - we trim them
197
+ */
198
+ public static recordsetToRecords(recordset: any[]): Record<string, string>[] {
199
+ if (!Array.isArray(recordset)) {
200
+ return [];
201
+ }
202
+
203
+ const data: Record<string, string>[] = recordset.map((row) => {
204
+ const transformedRow: Record<string, string> = {};
205
+ Object.keys(row).forEach((key) => {
206
+ const value = row[key];
207
+ transformedRow[key] =
208
+ value !== null && value !== undefined ? String(value).trim() : "";
209
+ });
210
+ return transformedRow;
211
+ });
212
+
213
+ return data;
214
+ }
215
+
216
+ /**
217
+ * Handle ODBC errors and provide meaningful messages
218
+ */
219
+ private handleOdbcError(error: OdbcErrorResponse): Error {
220
+ const odbcError = error.odbcErrors?.[0];
221
+ const errorCode = odbcError?.state;
222
+ const message = odbcError?.message || error.message;
223
+
224
+ switch (errorCode) {
225
+ case "08S01":
226
+ return new Error(
227
+ "PSQL connection failed. Check: " +
228
+ "1) PVSW environment variable set to /usr/local/psql/etc/pvsw.ini, " +
229
+ "2) Network connectivity to ports 1583/3351, " +
230
+ "3) ODBC configuration files in /usr/local/psql/etc/ and /etc/. " +
231
+ `Original error: ${message}`
232
+ );
233
+ case "28000":
234
+ return new Error(
235
+ `PSQL authentication failed. Check username/password. Original error: ${message}`
236
+ );
237
+ case "42000":
238
+ return new Error(`PSQL SQL syntax error. Original error: ${message}`);
239
+ case "42S02":
240
+ return new Error(
241
+ `PSQL table or view not found. Check table names in query. Original error: ${message}`
242
+ );
243
+ default:
244
+ return new Error(`PSQL error (${errorCode || "unknown"}): ${message}`);
245
+ }
246
+ }
247
+ }
@@ -76,22 +76,84 @@ const logger = createLogger({
76
76
  ],
77
77
  });
78
78
 
79
+ // Helper function to create a file transport with shared configuration
80
+ const createFileTransport = (): DailyRotateFile => {
81
+ return new DailyRotateFile({
82
+ filename: path.join(logDirectory, "%DATE%.log"),
83
+ datePattern: "YYYY-MM-DD",
84
+ zippedArchive: true,
85
+ maxSize: "20m",
86
+ maxFiles: "14d",
87
+ format: logFormat,
88
+ });
89
+ };
90
+
91
+ // Rotate mitigation helper: attaches rotate handler to transport and recursively attaches to replacements
92
+ function attachRotateMitigation(
93
+ transport: DailyRotateFile,
94
+ opts: { logLevel: string; nodeEnv: string }
95
+ ) {
96
+ const { logLevel, nodeEnv } = opts;
97
+ let isRefreshing = false;
98
+ transport.on("rotate", (_old: string, _new: string) => {
99
+ if (isRefreshing) return;
100
+ isRefreshing = true;
101
+ let removalTimer: NodeJS.Timeout | null = null;
102
+
103
+ // Create replacement first to avoid any logging gap
104
+ const next = createFileTransport();
105
+ // When the new file is created, remove the old transport
106
+ next.on("new", () => {
107
+ if (removalTimer) {
108
+ clearTimeout(removalTimer);
109
+ removalTimer = null;
110
+ }
111
+ try {
112
+ logger.remove(transport);
113
+ } catch {}
114
+ isRefreshing = false;
115
+ });
116
+ attachRotateMitigation(next, opts);
117
+ logger.add(next);
118
+
119
+ // Fallback: if the "new" event doesn't fire, remove the old transport after a grace period
120
+ const REMOVAL_GRACE_MS = 30000;
121
+ removalTimer = setTimeout(() => {
122
+ try {
123
+ logger.remove(transport);
124
+ } catch {}
125
+ isRefreshing = false;
126
+ removalTimer = null;
127
+ }, REMOVAL_GRACE_MS);
128
+
129
+ // Keep console and other transports intact; do not silence or clear
130
+ logger.level = logLevel;
131
+ });
132
+ }
133
+
79
134
  // Function to reconfigure the logger once CoreConfiguration is available
80
135
  export const configureLogger = (logLevel: string, nodeEnv: string) => {
81
- // Remove existing transports
136
+ // Remove existing transports (safely): close any DailyRotateFile streams first
137
+ try {
138
+ const existingFileTransports = (logger.transports || []).filter(
139
+ (t: any) => t instanceof DailyRotateFile
140
+ );
141
+ for (const t of existingFileTransports) {
142
+ const s = (t as any).logStream;
143
+ if (s && typeof s.end === "function") {
144
+ try {
145
+ s.end();
146
+ } catch {}
147
+ }
148
+ }
149
+ } catch {}
150
+
82
151
  logger.clear();
83
152
 
84
153
  // Add file transport
85
- logger.add(
86
- new DailyRotateFile({
87
- filename: path.join(logDirectory, "%DATE%.log"),
88
- datePattern: "YYYY-MM-DD",
89
- zippedArchive: true,
90
- maxSize: "20m",
91
- maxFiles: "14d",
92
- format: logFormat,
93
- })
94
- );
154
+ const fileTransport = createFileTransport();
155
+ attachRotateMitigation(fileTransport, { logLevel, nodeEnv });
156
+ logger.add(fileTransport);
95
157
 
96
158
  // Add console transport in non-production environments
97
159
  if (nodeEnv !== "production") {
@@ -21,7 +21,7 @@ export class ApplicationInitializer {
21
21
 
22
22
  // Load and validate core configuration
23
23
  const coreConfig = CoreConfiguration.inst();
24
- logger.info("Core Configuration loaded:", coreConfig);
24
+ logger.info("Core Configuration loaded:", coreConfig.toSafeLogObject());
25
25
 
26
26
  // Perform database startup checks
27
27
  logger.info("Performing database startup checks...");
@@ -33,7 +33,10 @@ export { BatchCacheManager } from "../services/caching-service/batch-cache-manag
33
33
  export { StandardProcessDrivers } from "./standard-process-drivers/";
34
34
  export type { WriteEntitiesToMMResult } from "./standard-process-drivers/";
35
35
  export { MMBatchValidationError } from "./standard-process-drivers/";
36
- export { getCachedTimezoneOffset } from "./local-data-store/jobs-shared-data";
36
+ export {
37
+ getCachedTimezoneOffset,
38
+ getCachedTimezoneName,
39
+ } from "./local-data-store/jobs-shared-data";
37
40
 
38
41
  // Local data store
39
42
  export {
@@ -71,13 +71,17 @@ export const setInitialLoadComplete = (complete: boolean): void => {
71
71
  writeStorage(data);
72
72
  };
73
73
 
74
+ /**
75
+ * Gets the company's cached current timezone offset (e.g., -5)
76
+ * @returns The cached timezone offset or 0 if not found
77
+ */
74
78
  export const getCachedTimezoneOffset = (): number => {
75
79
  const data = readStorage();
76
80
  return (data.timezoneOffset as number) ?? 0;
77
81
  };
78
82
 
79
83
  /**
80
- * Sets the timezone offset in the cache
84
+ * Sets the company's current timezone offset in the cache
81
85
  * @param offset The timezone offset in hours
82
86
  */
83
87
  export const setTimezoneOffsetInCache = (offset: number): void => {
@@ -86,6 +90,25 @@ export const setTimezoneOffsetInCache = (offset: number): void => {
86
90
  writeStorage(data);
87
91
  };
88
92
 
93
+ /**
94
+ * Gets the cached timezone name (e.g., "America/New_York")
95
+ * @returns The cached timezone name or null if not found
96
+ */
97
+ export const getCachedTimezoneName = (): string | null => {
98
+ const data = readStorage();
99
+ return (data.timezoneName as string) ?? null;
100
+ };
101
+
102
+ /**
103
+ * Sets the timezone name in the cache
104
+ * @param timezone The timezone name (e.g., "America/New_York")
105
+ */
106
+ export const setTimezoneNameInCache = (timezone: string): void => {
107
+ const data = readStorage();
108
+ data.timezoneName = timezone;
109
+ writeStorage(data);
110
+ };
111
+
89
112
  interface CachedToken {
90
113
  token: string;
91
114
  expiration: number | null;
@@ -1,5 +1,8 @@
1
1
  import logger from "../services/reporting-service/logger";
2
- import { setTimezoneOffsetInCache } from "./local-data-store/jobs-shared-data";
2
+ import {
3
+ setTimezoneOffsetInCache,
4
+ setTimezoneNameInCache,
5
+ } from "./local-data-store/jobs-shared-data";
3
6
  import {
4
7
  convertToLocalTime,
5
8
  formatDateWithTZOffset,
@@ -91,8 +94,9 @@ interface TimezoneOffsetParams {
91
94
  }
92
95
 
93
96
  /**
94
- * Gets the timezone offset for the company and sets it in the cache
97
+ * Gets the timezone offset and timezone name for the company and sets them in the cache
95
98
  * The cached offset can be acquired from getCachedTimezoneOffset() in "./local-data-store/jobs-shared-data"
99
+ * The cached timezone name can be acquired from getCachedTimezoneName() in "./local-data-store/jobs-shared-data"
96
100
  *
97
101
  * @param params.maxRetries Maximum number of retry attempts
98
102
  * @param params.retryIntervalMs Time to wait between retries in milliseconds
@@ -106,13 +110,14 @@ export const getTimezoneOffsetAndPersist = async (
106
110
  let success = false;
107
111
  let retries = 0;
108
112
  logger.info(
109
- "Acquiring the timezone offset from MachineMetrics and storing in cache"
113
+ "Acquiring the timezone offset and timezone name from MachineMetrics and storing in cache"
110
114
  );
111
115
  while (!success && retries < params.maxRetries) {
112
116
  try {
113
- const offsetHours = await getTimezoneOffset();
114
- logger.info(`Timezone offset: ${offsetHours} hours`);
115
- setTimezoneOffsetInCache(offsetHours);
117
+ const { offset, timezone } = await getTimezoneOffset();
118
+ logger.info(`Timezone offset: ${offset} hours, timezone: ${timezone}`);
119
+ setTimezoneOffsetInCache(offset);
120
+ setTimezoneNameInCache(timezone);
116
121
  success = true;
117
122
  } catch (error) {
118
123
  logger.error("Error getting timezone offset:", error);
@@ -2,11 +2,11 @@ import { CoreConfiguration } from "../services/data-sync-service/configuration-m
2
2
  import { HTTPClientFactory } from "./http-client";
3
3
 
4
4
  /**
5
- * Gets the timezone offset in hours for the company's timezone
6
- * @returns Promise<number> The timezone offset in hours
5
+ * Gets the timezone offset in hours and timezone name for the company's timezone
6
+ * @returns Promise<{ offset: number; timezone: string }> The timezone offset in hours and timezone name
7
7
  * @throws Error if unable to fetch timezone information
8
8
  */
9
- export const getTimezoneOffset = async (): Promise<number> => {
9
+ export const getTimezoneOffset = async (): Promise<{ offset: number; timezone: string }> => {
10
10
  try {
11
11
  // Get the timezone from configuration
12
12
  const config = CoreConfiguration.inst();
@@ -37,15 +37,18 @@ export const getTimezoneOffset = async (): Promise<number> => {
37
37
  throw new Error("Unable to retrieve company timezone from API");
38
38
  }
39
39
 
40
+ const timezone = userInfo.company.timezone;
41
+
40
42
  // Calculate the timezone offset
41
43
  const date = new Date();
42
44
  const utcDate = new Date(date.toLocaleString("en-US", { timeZone: "UTC" }));
43
45
  const tzDate = new Date(
44
- date.toLocaleString("en-US", { timeZone: userInfo.company.timezone })
46
+ date.toLocaleString("en-US", { timeZone: timezone })
45
47
  );
46
48
 
47
- // Return offset in hours
48
- return (tzDate.getTime() - utcDate.getTime()) / 3600000;
49
+ // Return offset in hours and timezone name
50
+ const offset = (tzDate.getTime() - utcDate.getTime()) / 3600000;
51
+ return { offset, timezone };
49
52
  } catch (error) {
50
53
  throw new Error(
51
54
  `Failed to get timezone offset: ${error instanceof Error ? error.message : "Unknown error"}`
@@ -1 +0,0 @@
1
- {"version":3,"file":"hashed-cache-manager-CzyFSt2B.js","sources":["../src/services/data-sync-service/configuration-manager.ts","../src/services/caching-service/hashed-cache-manager.ts"],"sourcesContent":["import \"dotenv/config\";\nimport { configureLogger } from \"../reporting-service/logger\";\nimport { SQLServerConfiguration } from \"../sql-server-erp-service/configuration\";\n\nexport class CoreConfiguration {\n private static instance: CoreConfiguration;\n\n // General Configuration\n public readonly logLevel: string;\n public readonly erpSystem: string;\n public readonly nodeEnv: string;\n\n // MM API (aka \"Mapping\") Service\n public readonly mmERPSvcApiBaseUrl: string;\n public readonly mmApiBaseUrl: string;\n public readonly mmApiAuthToken: string;\n public readonly mmApiRetryAttempts: number;\n\n // Caching (optionally used for interacting with the MM API)\n public readonly cacheTTL: number;\n\n // ERP API Service\n public readonly erpApiPagingLimit: number; //Pagination limit for ERP API\n\n // Job timing Intervals\n public readonly fromErpInterval: string;\n public readonly toErpInterval: string;\n public readonly retryLaborTicketsInterval: string;\n public readonly cacheExpirationCheckInterval: string;\n\n private constructor() {\n this.logLevel = process.env.LOG_LEVEL || \"info\";\n this.erpSystem = process.env.ERP_SYSTEM || \"template\";\n this.nodeEnv = process.env.NODE_ENV || \"development\";\n\n //#region MM API (aka \"Mapping\") Service\n /**\n * MM ERP Service REST API URL (typically https://erp-api.svc.machinemetrics.com)\n */\n this.mmERPSvcApiBaseUrl = process.env.MM_MAPPING_SERVICE_URL || \"\";\n\n /**\n * MM REST API URL (typically https://api.machinemetrics.com)\n */\n console.log(\"=== CONFIG DEBUG ===\");\n console.log(\"MM_MAPPING_AUTH_SERVICE_URL env var:\", process.env.MM_MAPPING_AUTH_SERVICE_URL);\n this.mmApiBaseUrl = process.env.MM_MAPPING_AUTH_SERVICE_URL || \"\";\n console.log(\"mmApiBaseUrl set to:\", this.mmApiBaseUrl);\n console.log(\"=== END CONFIG DEBUG ===\");\n\n /**\n * Company Auth Token\n */\n this.mmApiAuthToken = process.env.MM_MAPPING_SERVICE_TOKEN || \"\";\n\n /**\n * Number of retry attempts for MM API calls\n */\n this.mmApiRetryAttempts = parseInt(process.env.RETRY_ATTEMPTS || \"0\");\n //#endregion MM API (aka \"Mapping\") Service\n\n /**\n * Default pagination limit for ERP API\n */\n this.erpApiPagingLimit = parseInt(process.env.ERP_PAGINATION_LIMIT || \"0\");\n //#endregion ERP API Service\n\n /**\n * For how to define the intervals, see Bree's documentation: https://github.com/breejs/bree\n */\n this.fromErpInterval =\n process.env.FROM_ERP_INTERVAL || process.env.POLL_INTERVAL || \"5 min\";\n this.toErpInterval = process.env.TO_ERP_INTERVAL || \"5 min\";\n this.retryLaborTicketsInterval =\n process.env.RETRY_LABOR_TICKETS_INTERVAL || \"30 min\";\n this.cacheExpirationCheckInterval =\n process.env.CACHE_EXPIRATION_CHECK_INTERVAL || \"5 min\";\n\n /**\n * Cache TTL (in seconds)\n */\n const cacheTTLDef = 7 * 24 * 60 * 60; // 7 days\n this.cacheTTL = parseInt(process.env.CACHE_TTL || cacheTTLDef.toString());\n\n // Configure the logger with our settings\n configureLogger(this.logLevel, this.nodeEnv);\n }\n\n public static inst(): CoreConfiguration {\n if (!CoreConfiguration.instance) {\n CoreConfiguration.instance = new CoreConfiguration();\n }\n return CoreConfiguration.instance;\n }\n}\n\n/**\n * Helper function to get the SQL Server Configuration for collectors that use SQL Server to interact with the ERP\n */\nexport const getSQLServerConfiguration = (): SQLServerConfiguration => {\n return {\n username: process.env.ERP_SQLSERVER_USERNAME || \"\",\n password: process.env.ERP_SQLSERVER_PASSWORD || \"\",\n database: process.env.ERP_SQLSERVER_DATABASE || \"\",\n host:\n process.env.ERP_SQLSERVER_HOST || process.env.ERP_SQLSERVER_SERVER || \"\",\n port: process.env.ERP_SQLSERVER_PORT || \"1433\",\n connectionTimeout: process.env.ERP_SQLSERVER_CONNECTION_TIMEOUT || \"30000\",\n requestTimeout: process.env.ERP_SQLSERVER_REQUEST_TIMEOUT || \"60000\",\n poolMax: process.env.ERP_SQLSERVER_MAX || \"10\",\n poolMin: process.env.ERP_SQLSERVER_MIN || \"0\",\n idleTimeoutMillis:\n process.env.ERP_SQLSERVER_IDLE_TIMEOUT_MMILLIS || \"30000\",\n encrypt: process.env.ERP_SQLSERVER_ENCRYPT === \"true\",\n trustServer: process.env.ERP_SQLSERVER_TRUST_SERVER === \"true\",\n };\n};\n\n/**\n * Parameters required to connect to an ERP system via its API.\n * Contains all the necessary settings to establish a connection and authenticate with an ERP system's API.\n */\nexport class ErpApiConnectionParams {\n constructor(\n public readonly erpApiUrl: string, // Base url of ERP\n public readonly erpApiClientId: string, // Client ID to authenticate with ERP\n public readonly erpApiClientSecret: string, // Client Secret to authenticate with ERP\n public readonly erpApiOrganizationId: string, // Organization / tenant Id\n public readonly erpAuthBaseUrl: string, // Auth base url\n public readonly retryAttempts: number = 3 // Number of retry attempts for API calls\n ) {}\n}\n\n/**\n * Helper function to get the ERP API Connection Parameters\n * Not all connectors use these, but keeping these commonly values in one place may\n * make it easier to set and understand env var names set in App.\n */\nexport const getErpApiConnectionParams = (): ErpApiConnectionParams => {\n return new ErpApiConnectionParams(\n process.env.ERP_API_URL || \"\",\n process.env.ERP_API_CLIENT_ID || \"\",\n process.env.ERP_API_CLIENT_SECRET || \"\",\n process.env.ERP_API_ORGANIZATION_ID || \"\",\n process.env.ERP_AUTH_BASE_URL || \"\",\n parseInt(process.env.ERP_API_RETRY_ATTEMPTS || \"3\")\n );\n};\n","import knex, { Knex } from \"knex\";\nimport config from \"../../knexfile\";\nimport stringify from \"json-stable-stringify\";\nimport XXH from \"xxhashjs\";\nimport { ERPObjType } from \"../../types/erp-types\";\nimport { CacheMetrics } from \"./index\";\nimport { CoreConfiguration } from \"../data-sync-service/configuration-manager\";\nimport { logger } from \"../reporting-service\";\n\ntype HashedCacheManagerOptions = {\n ttl?: number;\n tableName?: string;\n};\n\nexport class HashedCacheManager {\n private static TABLE_NAME = \"sdk_cache\";\n private db: Knex;\n private options: HashedCacheManagerOptions;\n private static readonly SEED = 0xabcd; // Arbitrary seed for hashing\n private isDestroyed: boolean = false;\n private metrics: CacheMetrics = {\n recordCounts: {},\n };\n\n constructor(options?: HashedCacheManagerOptions) {\n this.options = {\n ttl: options?.ttl || CoreConfiguration.inst().cacheTTL,\n tableName: options?.tableName || HashedCacheManager.TABLE_NAME,\n };\n this.db = knex({\n ...config.local,\n pool: {\n min: 0,\n max: 10,\n },\n });\n }\n\n /**\n * Checks if the cache manager is still valid\n * @throws Error if the cache manager has been destroyed\n */\n private checkValid(): void {\n if (this.isDestroyed) {\n throw new Error(\"Cache manager has been destroyed\");\n }\n }\n\n /**\n * Generates a stable hash of a record using JSON stringify + xxhash\n */\n public static hashRecord(record: object): string {\n try {\n const serialized = stringify(record);\n if (!serialized) {\n throw new Error(\"Failed to serialize record for hashing\");\n }\n const hash = XXH.h64(serialized, HashedCacheManager.SEED).toString(16);\n return hash;\n } catch (error) {\n if (error instanceof Error && error.message.includes(\"circular\")) {\n throw new Error(\"Failed to serialize record for hashing\");\n }\n throw error;\n }\n }\n\n /**\n * Gets a record from the cache\n * @param type The type of record\n * @param hash The hash of the record\n * @returns The record if it exists, null otherwise\n */\n private async getRecord(\n type: ERPObjType,\n hash: string\n ): Promise<{ key: string } | null> {\n this.checkValid();\n return this.db(this.options.tableName)\n .select(\"key\")\n .where({ type, key: hash })\n .first();\n }\n\n /**\n * Stores a record in the cache\n * @param type The type of record\n * @param record The record to store\n * @returns true if a new record was created, false if an existing record was updated\n */\n public async store(type: ERPObjType, record: object): Promise<boolean> {\n if (!this.isDestroyed && record) {\n try {\n const hash = HashedCacheManager.hashRecord(record);\n const now = new Date();\n\n // First check if record exists with same type and hash\n const existing = await this.db(this.options.tableName)\n .where({\n type,\n key: hash,\n })\n .first();\n\n if (existing) {\n return false; // No need to update, hash hasn't changed\n } else {\n // Insert new record with minimal data\n const result = await this.db(this.options.tableName)\n .insert({\n type,\n key: hash,\n created_at: now,\n })\n .returning(\"id\");\n return result.length > 0;\n }\n } catch (error) {\n logger.error(\"Error storing record:\", error);\n throw error;\n }\n }\n return false;\n }\n\n /**\n * Checks if a record has changed since last seen\n * @param type The type of record\n * @param record The record to check\n * @returns true if the record has changed or is new\n */\n async hasChanged(type: ERPObjType, record: object): Promise<boolean> {\n this.checkValid();\n const newHash = HashedCacheManager.hashRecord(record);\n const existing = await this.getRecord(type, newHash);\n return !existing;\n }\n\n /**\n * Checks if a record has changed and stores it if it has\n * @param type The type of record\n * @param record The record to check and store\n * @returns true if the record was changed or is new\n */\n async upsert(type: ERPObjType, record: object): Promise<boolean> {\n this.checkValid();\n const hasChanged = await this.hasChanged(type, record);\n if (hasChanged) {\n await this.store(type, record as Record<string, unknown>);\n }\n return hasChanged;\n }\n\n /**\n * Removes expired records based on TTL\n */\n async removeExpiredObjects(): Promise<void> {\n this.checkValid();\n const ttl = this.options.ttl;\n if (!ttl) return;\n\n const ttlMilliseconds = ttl * 1000;\n const expirationLimitDate = new Date(Date.now() - ttlMilliseconds);\n const expirationLimit = expirationLimitDate\n .toISOString()\n .slice(0, 19)\n .replace(\"T\", \" \");\n\n await this.db(this.options.tableName)\n .where(\"created_at\", \"<\", expirationLimit)\n .del();\n }\n\n /**\n * Gets all records of a specific type\n */\n async getRecordsByType(type: ERPObjType): Promise<string[]> {\n this.checkValid();\n const records = await this.db(this.options.tableName)\n .select(\"key\")\n .where({ type });\n\n return records.map((record) => record.key);\n }\n\n /**\n * Removes all records of a specific type\n */\n async removeRecordsByType(type: ERPObjType): Promise<void> {\n this.checkValid();\n await this.db(this.options.tableName).where({ type }).del();\n }\n\n /**\n * Removes a specific record\n */\n public async removeRecord(type: ERPObjType, record: object): Promise<void> {\n if (!this.isDestroyed) {\n try {\n const hash = HashedCacheManager.hashRecord(record);\n await this.db(this.options.tableName)\n .where({ type, key: hash }) // Use key for deletion\n .del();\n } catch (error) {\n logger.error(\"Error removing record:\", error);\n throw error;\n }\n }\n }\n\n /**\n * Clears all records from the cache\n */\n async clear(): Promise<void> {\n this.checkValid();\n await this.db(this.options.tableName).del();\n }\n\n /**\n * Cleans up database connection and marks the cache manager as destroyed\n */\n async destroy(): Promise<void> {\n if (!this.isDestroyed) {\n await this.db.destroy();\n this.isDestroyed = true;\n }\n }\n\n /**\n * Gets the current cache metrics\n * @returns The current cache metrics\n */\n async getMetrics(): Promise<CacheMetrics> {\n this.checkValid();\n\n // Get counts for each type\n const counts = (await this.db(this.options.tableName)\n .select(\"type\")\n .count(\"* as count\")\n .groupBy(\"type\")) as Array<{ type: string; count: string }>;\n\n // Update metrics\n this.metrics.recordCounts = counts.reduce(\n (acc, row) => {\n acc[row.type] = parseInt(row.count, 10);\n return acc;\n },\n {} as Record<string, number>\n );\n\n return this.metrics;\n }\n}\n"],"names":[],"mappings":";;;;;;;AAIO,MAAM,kBAAkB;AAAA,EAC7B,OAAe;AAAA;AAAA,EAGC;AAAA,EACA;AAAA,EACA;AAAA;AAAA,EAGA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AAAA,EAGA;AAAA;AAAA,EAGA;AAAA;AAAA;AAAA,EAGA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAER,cAAc;AACpB,SAAK,WAAW,QAAQ,IAAI,aAAa;AACzC,SAAK,YAAY,QAAQ,IAAI,cAAc;AAC3C,SAAK,UAAU,QAAQ,IAAI,YAAY;AAMvC,SAAK,qBAAqB,QAAQ,IAAI,0BAA0B;AAKhE,YAAQ,IAAI,sBAAsB;AAClC,YAAQ,IAAI,wCAAwC,QAAQ,IAAI,2BAA2B;AAC3F,SAAK,eAAe,QAAQ,IAAI,+BAA+B;AAC/D,YAAQ,IAAI,wBAAwB,KAAK,YAAY;AACrD,YAAQ,IAAI,0BAA0B;AAKtC,SAAK,iBAAiB,QAAQ,IAAI,4BAA4B;AAK9D,SAAK,qBAAqB,SAAS,QAAQ,IAAI,kBAAkB,GAAG;AAMpE,SAAK,oBAAoB,SAAS,QAAQ,IAAI,wBAAwB,GAAG;AAMzE,SAAK,kBACH,QAAQ,IAAI,qBAAqB,QAAQ,IAAI,iBAAiB;AAChE,SAAK,gBAAgB,QAAQ,IAAI,mBAAmB;AACpD,SAAK,4BACH,QAAQ,IAAI,gCAAgC;AAC9C,SAAK,+BACH,Q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AAC,KAAK,aAAa;AACrB,YAAM,KAAK,GAAG,QAAA;AACd,WAAK,cAAc;AAAA,IACrB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,aAAoC;AACxC,SAAK,WAAA;AAGL,UAAM,SAAU,MAAM,KAAK,GAAG,KAAK,QAAQ,SAAS,EACjD,OAAO,MAAM,EACb,MAAM,YAAY,EAClB,QAAQ,MAAM;AAGjB,SAAK,QAAQ,eAAe,OAAO;AAAA,MACjC,CAAC,KAAK,QAAQ;AACZ,YAAI,IAAI,IAAI,IAAI,SAAS,IAAI,OAAO,EAAE;AACtC,eAAO;AAAA,MACT;AAAA,MACA,CAAA;AAAA,IAAC;AAGH,WAAO,KAAK;AAAA,EACd;AACF;"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"index-B9wo8pld.js","sources":["../src/utils/local-data-store/jobs-shared-data.ts","../src/utils/local-data-store/database-lock.ts","../src/services/sqlite-service/sqlite-coordinator.ts"],"sourcesContent":["import fs from \"fs\";\nimport path from \"path\";\nimport { mkdirSync } from \"fs\";\n\n/**\n * This file contains the logic for storing and retrieving data from the job state file.\n * It is used to store data that is shared between jobs, and (more importantly) across job instances.\n */\n\nconst STORAGE_FILE = path.join(\"/tmp\", \"job-state.json\");\n\n// Ensure parent directory exists\nconst parentDir = path.dirname(STORAGE_FILE);\ntry {\n mkdirSync(parentDir, { recursive: true });\n} catch (error) {\n if ((error as NodeJS.ErrnoException).code !== \"EEXIST\") {\n throw error;\n }\n}\n\n//#region Non-exported functions\nconst ensureStorageFile = () => {\n if (!fs.existsSync(STORAGE_FILE)) {\n fs.writeFileSync(STORAGE_FILE, JSON.stringify({}), \"utf-8\");\n }\n};\n\nconst readStorage = (): Record<string, unknown> => {\n ensureStorageFile();\n try {\n return JSON.parse(fs.readFileSync(STORAGE_FILE, \"utf-8\"));\n } catch (error) {\n console.error(`Failed to read storage from ${STORAGE_FILE}:`, error);\n return {};\n }\n};\n\nconst writeStorage = (data: Record<string, unknown>): void => {\n ensureStorageFile();\n fs.writeFileSync(STORAGE_FILE, JSON.stringify(data, null, 2), \"utf-8\");\n};\n//#endregion\n\n//#region Database lock storage functions\n/**\n * Reads the database lock state from the shared storage file\n * @returns The data stored in the file\n */\nexport const readDatabaseLockState = (): Record<string, unknown> => {\n return readStorage();\n};\n\n/**\n * Writes the database lock state to the shared storage file\n * @param data The lock state data to write\n */\nexport const writeDatabaseLockState = (data: Record<string, unknown>): void => {\n writeStorage(data);\n};\n//#endregion\n\nexport const getInitialLoadComplete = (): boolean => {\n const data = readStorage();\n return (data.initialLoadComplete as boolean) ?? false;\n};\n\nexport const setInitialLoadComplete = (complete: boolean): void => {\n const data = readStorage();\n data.initialLoadComplete = complete;\n writeStorage(data);\n};\n\nexport const getCachedTimezoneOffset = (): number => {\n const data = readStorage();\n return (data.timezoneOffset as number) ?? 0;\n};\n\n/**\n * Sets the timezone offset in the cache\n * @param offset The timezone offset in hours\n */\nexport const setTimezoneOffsetInCache = (offset: number): void => {\n const data = readStorage();\n data.timezoneOffset = offset;\n writeStorage(data);\n};\n\ninterface CachedToken {\n token: string;\n expiration: number | null;\n}\n\n/**\n * Gets the cached MM API token and its expiration\n * @returns The cached token and expiration or null if not found\n */\nexport const getCachedMMToken = (): CachedToken | null => {\n const data = readStorage();\n return (data.mmApiToken as CachedToken) ?? null;\n};\n\n/**\n * Sets the MM API token and its expiration in the cache\n * @param tokenData The token and expiration to cache\n */\nexport const setCachedMMToken = (tokenData: CachedToken): void => {\n const data = readStorage();\n data.mmApiToken = tokenData;\n writeStorage(data);\n};\n","import {\n readDatabaseLockState,\n writeDatabaseLockState,\n} from \"./jobs-shared-data\";\n\ninterface DatabaseLock {\n isLocked: boolean;\n lockedBy: string;\n lockedAt: string | null;\n}\n\n/**\n * Gets the current database lock state\n * @returns The current database lock state\n */\nexport const getDatabaseLock = (): DatabaseLock => {\n const data = readDatabaseLockState();\n return (\n (data.databaseLock as DatabaseLock) ?? {\n isLocked: false,\n lockedBy: \"\",\n lockedAt: null,\n }\n );\n};\n\n/**\n * Attempts to acquire the database lock\n * @param processName Name of the process requesting the lock\n * @returns true if lock was acquired, false if database is already locked\n */\nexport const acquireDatabaseLock = (processName: string): boolean => {\n const data = readDatabaseLockState();\n const currentLock = (data.databaseLock as DatabaseLock) ?? {\n isLocked: false,\n lockedBy: \"\",\n lockedAt: null,\n };\n\n if (currentLock.isLocked) {\n return false;\n }\n\n data.databaseLock = {\n isLocked: true,\n lockedBy: processName,\n lockedAt: new Date().toISOString(),\n };\n writeDatabaseLockState(data);\n return true;\n};\n\n/**\n * Releases the database lock\n * @param processName Name of the process releasing the lock\n * @returns true if lock was released, false if process doesn't own the lock\n */\nexport const releaseDatabaseLock = (processName: string): boolean => {\n const data = readDatabaseLockState();\n const currentLock = (data.databaseLock as DatabaseLock) ?? {\n isLocked: false,\n lockedBy: \"\",\n lockedAt: null,\n };\n\n if (!currentLock.isLocked || currentLock.lockedBy !== processName) {\n return false;\n }\n\n data.databaseLock = {\n isLocked: false,\n lockedBy: \"\",\n lockedAt: null,\n };\n writeDatabaseLockState(data);\n return true;\n};\n\n/**\n * Checks if the database is available for use\n * @returns true if database is available, false if locked\n */\nexport const isDatabaseAvailable = (): boolean => {\n const lock = getDatabaseLock();\n return !lock.isLocked;\n};\n","import {\n acquireDatabaseLock,\n releaseDatabaseLock,\n getDatabaseLock,\n} from \"../../utils/local-data-store/database-lock\";\nimport { logger } from \"../reporting-service\";\n\nexport class SQLiteCoordinator {\n private static readonly LOCK_TIMEOUT_MS = 30_000; // 30 seconds\n private static readonly LOCK_RETRY_INTERVAL_MS = 1_000; // 1 second\n\n /**\n * Performs startup checks to ensure no stale locks exist\n * Should be called when the application starts\n */\n static async performStartupCheck(): Promise<void> {\n const currentLock = getDatabaseLock();\n\n if (currentLock.isLocked) {\n logger.warn(\n `Found existing lock held by ${currentLock.lockedBy}, releasing for clean startup`\n );\n releaseDatabaseLock(currentLock.lockedBy);\n }\n }\n\n /**\n * Attempts to acquire the database lock\n * @param processName Name of the process requesting the lock\n * @returns true if lock was acquired, false if database is already locked\n */\n private static async tryAcquireLock(processName: string): Promise<boolean> {\n return acquireDatabaseLock(processName);\n }\n\n /**\n * Executes a database operation with proper locking\n * @param processName Name of the process executing the operation\n * @param operation The operation to execute\n * @returns The result of the operation\n */\n static async executeWithLock<T>(\n processName: string,\n operation: () => Promise<T>\n ): Promise<T> {\n const startTime = Date.now();\n\n // Try to acquire the lock with timeout\n while (Date.now() - startTime < this.LOCK_TIMEOUT_MS) {\n if (await this.tryAcquireLock(processName)) {\n try {\n // Execute the operation\n const result = await operation();\n return result;\n } finally {\n // Always release the lock\n releaseDatabaseLock(processName);\n }\n }\n\n // Wait before retrying\n await new Promise((resolve) =>\n setTimeout(resolve, this.LOCK_RETRY_INTERVAL_MS)\n );\n }\n\n throw new Error(\n `Failed to acquire database lock after ${this.LOCK_TIMEOUT_MS}ms`\n );\n }\n\n /**\n * Checks if the database is currently available for operations\n * @returns true if the database is available, false if locked\n */\n static isAvailable(): boolean {\n const lock = getDatabaseLock();\n return !lock.isLocked;\n }\n}\n"],"names":[],"mappings":";;;AASA,MAAM,eAAe,KAAK,KAAK,QAAQ,gBAAgB;AAGvD,MAAM,YAAY,KAAK,QAAQ,YAAY;AAC3C,IAAI;AACF,YAAU,WAAW,EAAE,WAAW,KAAA,CAAM;AAC1C,SAAS,OAAO;AACd,MAAK,MAAgC,SAAS,UAAU;AACtD,UAAM;AAAA,EACR;AACF;AAGA,MAAM,oBAAoB,MAAM;AAC9B,MAAI,CAAC,GAAG,WAAW,YAAY,GAAG;AAChC,OAAG,cAAc,cAAc,KAAK,UAAU,CAAA,CAAE,GAAG,OAAO;AAAA,EAC5D;AACF;AAEA,MAAM,cAAc,MAA+B;AACjD,oBAAA;AACA,MAAI;AACF,WAAO,KAAK,MAAM,GAAG,aAAa,cAAc,OAAO,CAAC;AAAA,EAC1D,SAAS,OAAO;AACd,YAAQ,MAAM,+BAA+B,YAAY,KAAK,KAAK;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