@yoryoboy/bi-mcp 1.1.0 → 1.2.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.
Files changed (76) hide show
  1. package/README.md +126 -9
  2. package/dist/.tsbuildinfo +1 -1
  3. package/dist/index.js +30 -0
  4. package/dist/index.js.map +2 -2
  5. package/dist/mcp-use.json +2 -2
  6. package/dist/scripts/_helpers.js +44 -0
  7. package/dist/scripts/_helpers.js.map +7 -0
  8. package/dist/scripts/admin-profile-delete.js +72 -0
  9. package/dist/scripts/admin-profile-delete.js.map +7 -0
  10. package/dist/scripts/admin-profile-list.js +24 -0
  11. package/dist/scripts/admin-profile-list.js.map +7 -0
  12. package/dist/scripts/admin-profile-upsert.js +25 -0
  13. package/dist/scripts/admin-profile-upsert.js.map +7 -0
  14. package/dist/scripts/admin-vtex-list.js +28 -0
  15. package/dist/scripts/admin-vtex-list.js.map +7 -0
  16. package/dist/scripts/admin-vtex-upsert.js +73 -0
  17. package/dist/scripts/admin-vtex-upsert.js.map +7 -0
  18. package/dist/scripts/admin-vtex-validate.js +55 -0
  19. package/dist/scripts/admin-vtex-validate.js.map +7 -0
  20. package/dist/scripts/run-migrations.js +50 -0
  21. package/dist/scripts/run-migrations.js.map +7 -0
  22. package/dist/scripts/test-db-connection.js +19 -0
  23. package/dist/scripts/test-db-connection.js.map +7 -0
  24. package/dist/src/config/profile-store.js +86 -0
  25. package/dist/src/config/profile-store.js.map +7 -0
  26. package/dist/src/config/vtex-crypto.js +43 -0
  27. package/dist/src/config/vtex-crypto.js.map +7 -0
  28. package/dist/src/config/vtex-profile-store.js +132 -0
  29. package/dist/src/config/vtex-profile-store.js.map +7 -0
  30. package/dist/src/config/vtex.js +27 -21
  31. package/dist/src/config/vtex.js.map +2 -2
  32. package/dist/src/db/client.js +58 -0
  33. package/dist/src/db/client.js.map +7 -0
  34. package/dist/src/services/vtex/vtex-api.js +24 -8
  35. package/dist/src/services/vtex/vtex-api.js.map +2 -2
  36. package/dist/src/services/vtex/vtex-catalog.js +5 -3
  37. package/dist/src/services/vtex/vtex-catalog.js.map +2 -2
  38. package/dist/src/services/vtex/vtex-logistics.js +18 -9
  39. package/dist/src/services/vtex/vtex-logistics.js.map +2 -2
  40. package/dist/src/services/vtex/vtex-orders.js +13 -7
  41. package/dist/src/services/vtex/vtex-orders.js.map +2 -2
  42. package/dist/src/services/vtex/vtex-pricing.js +5 -3
  43. package/dist/src/services/vtex/vtex-pricing.js.map +2 -2
  44. package/dist/src/tools/config/check-database-connection.js +59 -0
  45. package/dist/src/tools/config/check-database-connection.js.map +7 -0
  46. package/dist/src/tools/config/index.js +3 -0
  47. package/dist/src/tools/config/index.js.map +7 -0
  48. package/dist/src/tools/config/list-profiles.js +26 -0
  49. package/dist/src/tools/config/list-profiles.js.map +7 -0
  50. package/dist/src/tools/index.js +1 -0
  51. package/dist/src/tools/index.js.map +2 -2
  52. package/dist/src/tools/vtex/computed-price.js +12 -1
  53. package/dist/src/tools/vtex/computed-price.js.map +2 -2
  54. package/dist/src/tools/vtex/index.js +1 -0
  55. package/dist/src/tools/vtex/index.js.map +2 -2
  56. package/dist/src/tools/vtex/inventory-check.js +15 -2
  57. package/dist/src/tools/vtex/inventory-check.js.map +2 -2
  58. package/dist/src/tools/vtex/order-details.js +16 -2
  59. package/dist/src/tools/vtex/order-details.js.map +2 -2
  60. package/dist/src/tools/vtex/orders-summary.js +11 -1
  61. package/dist/src/tools/vtex/orders-summary.js.map +2 -2
  62. package/dist/src/tools/vtex/product-offers.js +15 -2
  63. package/dist/src/tools/vtex/product-offers.js.map +2 -2
  64. package/dist/src/tools/vtex/profile-resolution.js +57 -0
  65. package/dist/src/tools/vtex/profile-resolution.js.map +7 -0
  66. package/dist/src/tools/vtex/sku-offers.js +16 -2
  67. package/dist/src/tools/vtex/sku-offers.js.map +2 -2
  68. package/dist/src/tools/vtex/sku-price.js +12 -2
  69. package/dist/src/tools/vtex/sku-price.js.map +2 -2
  70. package/dist/src/tools/vtex/update-inventory.js +12 -1
  71. package/dist/src/tools/vtex/update-inventory.js.map +2 -2
  72. package/dist/src/tools/vtex/update-lead-time.js +12 -1
  73. package/dist/src/tools/vtex/update-lead-time.js.map +2 -2
  74. package/dist/src/tools/vtex/warehouse-inventory.js +12 -1
  75. package/dist/src/tools/vtex/warehouse-inventory.js.map +2 -2
  76. package/package.json +12 -2
@@ -0,0 +1,55 @@
1
+ import process from "node:process";
2
+ import { loadLocalEnv, parseCliArgs, requireArg } from "./_helpers.js";
3
+ async function main() {
4
+ loadLocalEnv();
5
+ const args = parseCliArgs(process.argv.slice(2));
6
+ const profileId = requireArg(args, "profile-id");
7
+ const [{ getProfile }, { getVtexConnectionRow, markVtexConnectionValidation }, { decryptSecret }, { buildVtexUrls }, { validateVtexConfig }] = await Promise.all([
8
+ import("../src/config/profile-store.js"),
9
+ import("../src/config/vtex-profile-store.js"),
10
+ import("../src/config/vtex-crypto.js"),
11
+ import("../src/config/vtex.js"),
12
+ import("../src/services/vtex/vtex-api.js")
13
+ ]);
14
+ const profile = await getProfile(profileId);
15
+ if (!profile) {
16
+ throw new Error(`Profile not found: "${profileId}"`);
17
+ }
18
+ const connection = await getVtexConnectionRow(profileId);
19
+ if (!connection) {
20
+ throw new Error(`VTEX connection not found for profileId "${profileId}"`);
21
+ }
22
+ const urls = buildVtexUrls(connection.accountName);
23
+ try {
24
+ await validateVtexConfig({
25
+ accountName: connection.accountName,
26
+ apiKey: decryptSecret(connection.apiKeyEncrypted),
27
+ apiToken: decryptSecret(connection.apiTokenEncrypted),
28
+ baseUrl: urls.baseUrl,
29
+ pricingBaseUrl: urls.pricingBaseUrl
30
+ });
31
+ await markVtexConnectionValidation(profileId, "valid", null);
32
+ console.log("VTEX connection validation OK");
33
+ console.log(
34
+ JSON.stringify(
35
+ {
36
+ profile_id: profileId,
37
+ status: "valid",
38
+ validated_at: (/* @__PURE__ */ new Date()).toISOString()
39
+ },
40
+ null,
41
+ 2
42
+ )
43
+ );
44
+ } catch (error) {
45
+ const message = error instanceof Error ? error.message : "VTEX validation failed";
46
+ await markVtexConnectionValidation(profileId, "invalid", message);
47
+ throw new Error(message);
48
+ }
49
+ }
50
+ main().catch((error) => {
51
+ console.error("VTEX connection validation failed");
52
+ console.error(error);
53
+ process.exitCode = 1;
54
+ });
55
+ //# sourceMappingURL=admin-vtex-validate.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../scripts/admin-vtex-validate.ts"],
4
+ "sourcesContent": ["import process from \"node:process\";\n\nimport { loadLocalEnv, parseCliArgs, requireArg } from \"./_helpers.js\";\n\nasync function main() {\n loadLocalEnv();\n\n const args = parseCliArgs(process.argv.slice(2));\n const profileId = requireArg(args, \"profile-id\");\n\n const [{ getProfile }, { getVtexConnectionRow, markVtexConnectionValidation }, { decryptSecret }, { buildVtexUrls }, { validateVtexConfig }] =\n await Promise.all([\n import(\"../src/config/profile-store.js\"),\n import(\"../src/config/vtex-profile-store.js\"),\n import(\"../src/config/vtex-crypto.js\"),\n import(\"../src/config/vtex.js\"),\n import(\"../src/services/vtex/vtex-api.js\"),\n ]);\n\n const profile = await getProfile(profileId);\n if (!profile) {\n throw new Error(`Profile not found: \"${profileId}\"`);\n }\n const connection = await getVtexConnectionRow(profileId);\n\n if (!connection) {\n throw new Error(`VTEX connection not found for profileId \"${profileId}\"`);\n }\n\n const urls = buildVtexUrls(connection.accountName);\n\n try {\n await validateVtexConfig({\n accountName: connection.accountName,\n apiKey: decryptSecret(connection.apiKeyEncrypted),\n apiToken: decryptSecret(connection.apiTokenEncrypted),\n baseUrl: urls.baseUrl,\n pricingBaseUrl: urls.pricingBaseUrl,\n });\n\n await markVtexConnectionValidation(profileId, \"valid\", null);\n\n console.log(\"VTEX connection validation OK\");\n console.log(\n JSON.stringify(\n {\n profile_id: profileId,\n status: \"valid\",\n validated_at: new Date().toISOString(),\n },\n null,\n 2\n )\n );\n } catch (error) {\n const message = error instanceof Error ? error.message : \"VTEX validation failed\";\n await markVtexConnectionValidation(profileId, \"invalid\", message);\n throw new Error(message);\n }\n}\n\nmain().catch((error: unknown) => {\n console.error(\"VTEX connection validation failed\");\n console.error(error);\n process.exitCode = 1;\n});\n"],
5
+ "mappings": "AAAA,OAAO,aAAa;AAEpB,SAAS,cAAc,cAAc,kBAAkB;AAEvD,eAAe,OAAO;AACpB,eAAa;AAEb,QAAM,OAAO,aAAa,QAAQ,KAAK,MAAM,CAAC,CAAC;AAC/C,QAAM,YAAY,WAAW,MAAM,YAAY;AAE/C,QAAM,CAAC,EAAE,WAAW,GAAG,EAAE,sBAAsB,6BAA6B,GAAG,EAAE,cAAc,GAAG,EAAE,cAAc,GAAG,EAAE,mBAAmB,CAAC,IACzI,MAAM,QAAQ,IAAI;AAAA,IAChB,OAAO,gCAAgC;AAAA,IACvC,OAAO,qCAAqC;AAAA,IAC5C,OAAO,8BAA8B;AAAA,IACrC,OAAO,uBAAuB;AAAA,IAC9B,OAAO,kCAAkC;AAAA,EAC3C,CAAC;AAEH,QAAM,UAAU,MAAM,WAAW,SAAS;AAC1C,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,MAAM,uBAAuB,SAAS,GAAG;AAAA,EACrD;AACA,QAAM,aAAa,MAAM,qBAAqB,SAAS;AAEvD,MAAI,CAAC,YAAY;AACf,UAAM,IAAI,MAAM,4CAA4C,SAAS,GAAG;AAAA,EAC1E;AAEA,QAAM,OAAO,cAAc,WAAW,WAAW;AAEjD,MAAI;AACF,UAAM,mBAAmB;AAAA,MACvB,aAAa,WAAW;AAAA,MACxB,QAAQ,cAAc,WAAW,eAAe;AAAA,MAChD,UAAU,cAAc,WAAW,iBAAiB;AAAA,MACpD,SAAS,KAAK;AAAA,MACd,gBAAgB,KAAK;AAAA,IACvB,CAAC;AAED,UAAM,6BAA6B,WAAW,SAAS,IAAI;AAE3D,YAAQ,IAAI,+BAA+B;AAC3C,YAAQ;AAAA,MACN,KAAK;AAAA,QACH;AAAA,UACE,YAAY;AAAA,UACZ,QAAQ;AAAA,UACR,eAAc,oBAAI,KAAK,GAAE,YAAY;AAAA,QACvC;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAAA,IACF;AAAA,EACF,SAAS,OAAO;AACd,UAAM,UAAU,iBAAiB,QAAQ,MAAM,UAAU;AACzD,UAAM,6BAA6B,WAAW,WAAW,OAAO;AAChE,UAAM,IAAI,MAAM,OAAO;AAAA,EACzB;AACF;AAEA,KAAK,EAAE,MAAM,CAAC,UAAmB;AAC/B,UAAQ,MAAM,mCAAmC;AACjD,UAAQ,MAAM,KAAK;AACnB,UAAQ,WAAW;AACrB,CAAC;",
6
+ "names": []
7
+ }
@@ -0,0 +1,50 @@
1
+ import { readdir, readFile } from "node:fs/promises";
2
+ import path from "node:path";
3
+ import process from "node:process";
4
+ import { loadLocalEnv } from "./_helpers";
5
+ async function main() {
6
+ loadLocalEnv();
7
+ const { getDb, closeDb } = await import("../src/db/client.js");
8
+ const db = getDb();
9
+ const migrationsDir = path.resolve("db/migrations");
10
+ const files = (await readdir(migrationsDir)).filter((file) => file.endsWith(".sql")).sort((left, right) => left.localeCompare(right));
11
+ await db.query(`
12
+ create table if not exists schema_migrations (
13
+ filename text primary key,
14
+ applied_at timestamptz not null default now()
15
+ )
16
+ `);
17
+ const appliedResult = await db.query(
18
+ "select filename from schema_migrations"
19
+ );
20
+ const applied = new Set(appliedResult.rows.map((row) => row.filename));
21
+ const pending = files.filter((file) => !applied.has(file));
22
+ if (pending.length === 0) {
23
+ console.log("No pending migrations");
24
+ await closeDb();
25
+ return;
26
+ }
27
+ for (const file of pending) {
28
+ const sql = await readFile(path.join(migrationsDir, file), "utf8");
29
+ const client = await db.connect();
30
+ try {
31
+ await client.query("begin");
32
+ await client.query(sql);
33
+ await client.query("insert into schema_migrations (filename) values ($1)", [file]);
34
+ await client.query("commit");
35
+ console.log(`Applied migration: ${file}`);
36
+ } catch (error) {
37
+ await client.query("rollback");
38
+ throw error;
39
+ } finally {
40
+ client.release();
41
+ }
42
+ }
43
+ await closeDb();
44
+ }
45
+ main().catch((error) => {
46
+ console.error("Migration run failed");
47
+ console.error(error);
48
+ process.exitCode = 1;
49
+ });
50
+ //# sourceMappingURL=run-migrations.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../scripts/run-migrations.ts"],
4
+ "sourcesContent": ["import { readdir, readFile } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport process from \"node:process\";\n\nimport { loadLocalEnv } from \"./_helpers\";\n\nasync function main() {\n loadLocalEnv();\n\n const { getDb, closeDb } = await import(\"../src/db/client.js\");\n const db = getDb();\n const migrationsDir = path.resolve(\"db/migrations\");\n const files = (await readdir(migrationsDir))\n .filter((file) => file.endsWith(\".sql\"))\n .sort((left, right) => left.localeCompare(right));\n\n await db.query(`\n create table if not exists schema_migrations (\n filename text primary key,\n applied_at timestamptz not null default now()\n )\n `);\n\n const appliedResult = await db.query<{ filename: string }>(\n \"select filename from schema_migrations\"\n );\n const applied = new Set(appliedResult.rows.map((row) => row.filename));\n\n const pending = files.filter((file) => !applied.has(file));\n\n if (pending.length === 0) {\n console.log(\"No pending migrations\");\n await closeDb();\n return;\n }\n\n for (const file of pending) {\n const sql = await readFile(path.join(migrationsDir, file), \"utf8\");\n const client = await db.connect();\n\n try {\n await client.query(\"begin\");\n await client.query(sql);\n await client.query(\"insert into schema_migrations (filename) values ($1)\", [file]);\n await client.query(\"commit\");\n console.log(`Applied migration: ${file}`);\n } catch (error) {\n await client.query(\"rollback\");\n throw error;\n } finally {\n client.release();\n }\n }\n\n await closeDb();\n}\n\nmain().catch((error: unknown) => {\n console.error(\"Migration run failed\");\n console.error(error);\n process.exitCode = 1;\n});\n"],
5
+ "mappings": "AAAA,SAAS,SAAS,gBAAgB;AAClC,OAAO,UAAU;AACjB,OAAO,aAAa;AAEpB,SAAS,oBAAoB;AAE7B,eAAe,OAAO;AACpB,eAAa;AAEb,QAAM,EAAE,OAAO,QAAQ,IAAI,MAAM,OAAO,qBAAqB;AAC7D,QAAM,KAAK,MAAM;AACjB,QAAM,gBAAgB,KAAK,QAAQ,eAAe;AAClD,QAAM,SAAS,MAAM,QAAQ,aAAa,GACvC,OAAO,CAAC,SAAS,KAAK,SAAS,MAAM,CAAC,EACtC,KAAK,CAAC,MAAM,UAAU,KAAK,cAAc,KAAK,CAAC;AAElD,QAAM,GAAG,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA,GAKd;AAED,QAAM,gBAAgB,MAAM,GAAG;AAAA,IAC7B;AAAA,EACF;AACA,QAAM,UAAU,IAAI,IAAI,cAAc,KAAK,IAAI,CAAC,QAAQ,IAAI,QAAQ,CAAC;AAErE,QAAM,UAAU,MAAM,OAAO,CAAC,SAAS,CAAC,QAAQ,IAAI,IAAI,CAAC;AAEzD,MAAI,QAAQ,WAAW,GAAG;AACxB,YAAQ,IAAI,uBAAuB;AACnC,UAAM,QAAQ;AACd;AAAA,EACF;AAEA,aAAW,QAAQ,SAAS;AAC1B,UAAM,MAAM,MAAM,SAAS,KAAK,KAAK,eAAe,IAAI,GAAG,MAAM;AACjE,UAAM,SAAS,MAAM,GAAG,QAAQ;AAEhC,QAAI;AACF,YAAM,OAAO,MAAM,OAAO;AAC1B,YAAM,OAAO,MAAM,GAAG;AACtB,YAAM,OAAO,MAAM,wDAAwD,CAAC,IAAI,CAAC;AACjF,YAAM,OAAO,MAAM,QAAQ;AAC3B,cAAQ,IAAI,sBAAsB,IAAI,EAAE;AAAA,IAC1C,SAAS,OAAO;AACd,YAAM,OAAO,MAAM,UAAU;AAC7B,YAAM;AAAA,IACR,UAAE;AACA,aAAO,QAAQ;AAAA,IACjB;AAAA,EACF;AAEA,QAAM,QAAQ;AAChB;AAEA,KAAK,EAAE,MAAM,CAAC,UAAmB;AAC/B,UAAQ,MAAM,sBAAsB;AACpC,UAAQ,MAAM,KAAK;AACnB,UAAQ,WAAW;AACrB,CAAC;",
6
+ "names": []
7
+ }
@@ -0,0 +1,19 @@
1
+ import process from "node:process";
2
+ async function main() {
3
+ process.loadEnvFile?.(".env");
4
+ const { testDatabaseConnection } = await import("../src/db/client.js");
5
+ const result = await testDatabaseConnection();
6
+ console.log("Database connection OK");
7
+ console.log(`database: ${result.current_database}`);
8
+ console.log(`user: ${result.current_user}`);
9
+ console.log(`time: ${result.now.toISOString()}`);
10
+ }
11
+ main().catch((error) => {
12
+ console.error("Database connection failed");
13
+ console.error(error);
14
+ process.exitCode = 1;
15
+ }).finally(async () => {
16
+ const { closeDb } = await import("../src/db/client.js");
17
+ await closeDb();
18
+ });
19
+ //# sourceMappingURL=test-db-connection.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../scripts/test-db-connection.ts"],
4
+ "sourcesContent": ["import process from \"node:process\";\n\nasync function main() {\n // Local development helper: load values from .env when the runtime supports it.\n process.loadEnvFile?.(\".env\");\n\n const { testDatabaseConnection } = await import(\"../src/db/client.js\");\n const result = await testDatabaseConnection();\n\n console.log(\"Database connection OK\");\n console.log(`database: ${result.current_database}`);\n console.log(`user: ${result.current_user}`);\n console.log(`time: ${result.now.toISOString()}`);\n}\n\nmain()\n .catch((error: unknown) => {\n console.error(\"Database connection failed\");\n console.error(error);\n process.exitCode = 1;\n })\n .finally(async () => {\n const { closeDb } = await import(\"../src/db/client.js\");\n await closeDb();\n });\n"],
5
+ "mappings": "AAAA,OAAO,aAAa;AAEpB,eAAe,OAAO;AAEpB,UAAQ,cAAc,MAAM;AAE5B,QAAM,EAAE,uBAAuB,IAAI,MAAM,OAAO,qBAAqB;AACrE,QAAM,SAAS,MAAM,uBAAuB;AAE5C,UAAQ,IAAI,wBAAwB;AACpC,UAAQ,IAAI,aAAa,OAAO,gBAAgB,EAAE;AAClD,UAAQ,IAAI,SAAS,OAAO,YAAY,EAAE;AAC1C,UAAQ,IAAI,SAAS,OAAO,IAAI,YAAY,CAAC,EAAE;AACjD;AAEA,KAAK,EACF,MAAM,CAAC,UAAmB;AACzB,UAAQ,MAAM,4BAA4B;AAC1C,UAAQ,MAAM,KAAK;AACnB,UAAQ,WAAW;AACrB,CAAC,EACA,QAAQ,YAAY;AACnB,QAAM,EAAE,QAAQ,IAAI,MAAM,OAAO,qBAAqB;AACtD,QAAM,QAAQ;AAChB,CAAC;",
6
+ "names": []
7
+ }
@@ -0,0 +1,86 @@
1
+ import { getDb } from "../db/client.js";
2
+ function mapProfileRow(row) {
3
+ return {
4
+ id: row.id,
5
+ name: row.name,
6
+ isActive: row.is_active,
7
+ createdAt: row.created_at,
8
+ updatedAt: row.updated_at
9
+ };
10
+ }
11
+ async function getProfile(profileId) {
12
+ const result = await getDb().query(
13
+ `
14
+ select id, name, is_active, created_at, updated_at
15
+ from profiles
16
+ where id = $1
17
+ `,
18
+ [profileId]
19
+ );
20
+ const row = result.rows[0];
21
+ return row ? mapProfileRow(row) : null;
22
+ }
23
+ async function upsertProfile(input) {
24
+ const result = await getDb().query(
25
+ `
26
+ insert into profiles (id, name, is_active)
27
+ values ($1, $2, $3)
28
+ on conflict (id) do update
29
+ set
30
+ name = excluded.name,
31
+ is_active = excluded.is_active,
32
+ updated_at = now()
33
+ returning id, name, is_active, created_at, updated_at
34
+ `,
35
+ [input.id, input.name, input.isActive]
36
+ );
37
+ return mapProfileRow(result.rows[0]);
38
+ }
39
+ async function listProfiles() {
40
+ const result = await getDb().query(
41
+ `
42
+ select id, name, is_active, created_at, updated_at
43
+ from profiles
44
+ order by id asc
45
+ `
46
+ );
47
+ return result.rows.map(mapProfileRow);
48
+ }
49
+ async function deleteProfile(profileId) {
50
+ const result = await getDb().query(
51
+ `
52
+ delete from profiles
53
+ where id = $1
54
+ returning id, name, is_active, created_at, updated_at
55
+ `,
56
+ [profileId]
57
+ );
58
+ const row = result.rows[0];
59
+ if (!row) {
60
+ return null;
61
+ }
62
+ return {
63
+ id: row.id,
64
+ name: row.name,
65
+ isActive: row.is_active,
66
+ deletedAt: /* @__PURE__ */ new Date()
67
+ };
68
+ }
69
+ async function assertActiveProfile(profileId) {
70
+ const profile = await getProfile(profileId);
71
+ if (!profile) {
72
+ throw new Error(`Profile not found: "${profileId}"`);
73
+ }
74
+ if (!profile.isActive) {
75
+ throw new Error(`Profile "${profileId}" is disabled`);
76
+ }
77
+ return profile;
78
+ }
79
+ export {
80
+ assertActiveProfile,
81
+ deleteProfile,
82
+ getProfile,
83
+ listProfiles,
84
+ upsertProfile
85
+ };
86
+ //# sourceMappingURL=profile-store.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/config/profile-store.ts"],
4
+ "sourcesContent": ["import { getDb } from \"../db/client.js\";\n\nexport interface ProfileRecord {\n id: string;\n name: string;\n isActive: boolean;\n createdAt: Date;\n updatedAt: Date;\n}\n\nexport interface DeletedProfileRecord {\n id: string;\n name: string;\n isActive: boolean;\n deletedAt: Date;\n}\n\ninterface ProfileRow {\n id: string;\n name: string;\n is_active: boolean;\n created_at: Date;\n updated_at: Date;\n}\n\nfunction mapProfileRow(row: ProfileRow): ProfileRecord {\n return {\n id: row.id,\n name: row.name,\n isActive: row.is_active,\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n}\n\nexport async function getProfile(profileId: string): Promise<ProfileRecord | null> {\n const result = await getDb().query<ProfileRow>(\n `\n select id, name, is_active, created_at, updated_at\n from profiles\n where id = $1\n `,\n [profileId]\n );\n\n const row = result.rows[0];\n return row ? mapProfileRow(row) : null;\n}\n\nexport async function upsertProfile(input: {\n id: string;\n name: string;\n isActive: boolean;\n}): Promise<ProfileRecord> {\n const result = await getDb().query<ProfileRow>(\n `\n insert into profiles (id, name, is_active)\n values ($1, $2, $3)\n on conflict (id) do update\n set\n name = excluded.name,\n is_active = excluded.is_active,\n updated_at = now()\n returning id, name, is_active, created_at, updated_at\n `,\n [input.id, input.name, input.isActive]\n );\n\n return mapProfileRow(result.rows[0]);\n}\n\nexport async function listProfiles(): Promise<ProfileRecord[]> {\n const result = await getDb().query<ProfileRow>(\n `\n select id, name, is_active, created_at, updated_at\n from profiles\n order by id asc\n `\n );\n\n return result.rows.map(mapProfileRow);\n}\n\nexport async function deleteProfile(profileId: string): Promise<DeletedProfileRecord | null> {\n const result = await getDb().query<ProfileRow>(\n `\n delete from profiles\n where id = $1\n returning id, name, is_active, created_at, updated_at\n `,\n [profileId]\n );\n\n const row = result.rows[0];\n if (!row) {\n return null;\n }\n\n return {\n id: row.id,\n name: row.name,\n isActive: row.is_active,\n deletedAt: new Date(),\n };\n}\n\nexport async function assertActiveProfile(profileId: string): Promise<ProfileRecord> {\n const profile = await getProfile(profileId);\n\n if (!profile) {\n throw new Error(`Profile not found: \"${profileId}\"`);\n }\n\n if (!profile.isActive) {\n throw new Error(`Profile \"${profileId}\" is disabled`);\n }\n\n return profile;\n}\n"],
5
+ "mappings": "AAAA,SAAS,aAAa;AAyBtB,SAAS,cAAc,KAAgC;AACrD,SAAO;AAAA,IACL,IAAI,IAAI;AAAA,IACR,MAAM,IAAI;AAAA,IACV,UAAU,IAAI;AAAA,IACd,WAAW,IAAI;AAAA,IACf,WAAW,IAAI;AAAA,EACjB;AACF;AAEA,eAAsB,WAAW,WAAkD;AACjF,QAAM,SAAS,MAAM,MAAM,EAAE;AAAA,IAC3B;AAAA;AAAA;AAAA;AAAA;AAAA,IAKA,CAAC,SAAS;AAAA,EACZ;AAEA,QAAM,MAAM,OAAO,KAAK,CAAC;AACzB,SAAO,MAAM,cAAc,GAAG,IAAI;AACpC;AAEA,eAAsB,cAAc,OAIT;AACzB,QAAM,SAAS,MAAM,MAAM,EAAE;AAAA,IAC3B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAUA,CAAC,MAAM,IAAI,MAAM,MAAM,MAAM,QAAQ;AAAA,EACvC;AAEA,SAAO,cAAc,OAAO,KAAK,CAAC,CAAC;AACrC;AAEA,eAAsB,eAAyC;AAC7D,QAAM,SAAS,MAAM,MAAM,EAAE;AAAA,IAC3B;AAAA;AAAA;AAAA;AAAA;AAAA,EAKF;AAEA,SAAO,OAAO,KAAK,IAAI,aAAa;AACtC;AAEA,eAAsB,cAAc,WAAyD;AAC3F,QAAM,SAAS,MAAM,MAAM,EAAE;AAAA,IAC3B;AAAA;AAAA;AAAA;AAAA;AAAA,IAKA,CAAC,SAAS;AAAA,EACZ;AAEA,QAAM,MAAM,OAAO,KAAK,CAAC;AACzB,MAAI,CAAC,KAAK;AACR,WAAO;AAAA,EACT;AAEA,SAAO;AAAA,IACL,IAAI,IAAI;AAAA,IACR,MAAM,IAAI;AAAA,IACV,UAAU,IAAI;AAAA,IACd,WAAW,oBAAI,KAAK;AAAA,EACtB;AACF;AAEA,eAAsB,oBAAoB,WAA2C;AACnF,QAAM,UAAU,MAAM,WAAW,SAAS;AAE1C,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,MAAM,uBAAuB,SAAS,GAAG;AAAA,EACrD;AAEA,MAAI,CAAC,QAAQ,UAAU;AACrB,UAAM,IAAI,MAAM,YAAY,SAAS,eAAe;AAAA,EACtD;AAEA,SAAO;AACT;",
6
+ "names": []
7
+ }
@@ -0,0 +1,43 @@
1
+ import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
2
+ const ALGORITHM = "aes-256-gcm";
3
+ const IV_LENGTH = 12;
4
+ function getEncryptionKey() {
5
+ const rawValue = process.env.DB_ENCRYPTION_KEY?.trim();
6
+ if (!rawValue) {
7
+ throw new Error(
8
+ "Missing DB_ENCRYPTION_KEY. Configure it before encrypting or decrypting persisted secrets."
9
+ );
10
+ }
11
+ const buffer = Buffer.from(rawValue, "hex");
12
+ if (buffer.length !== 32) {
13
+ throw new Error(
14
+ "Invalid DB_ENCRYPTION_KEY. Expected a 32-byte key encoded as 64 hex characters."
15
+ );
16
+ }
17
+ return buffer;
18
+ }
19
+ function encryptSecret(plainText) {
20
+ const iv = randomBytes(IV_LENGTH);
21
+ const cipher = createCipheriv(ALGORITHM, getEncryptionKey(), iv);
22
+ const encrypted = Buffer.concat([cipher.update(plainText, "utf8"), cipher.final()]);
23
+ const authTag = cipher.getAuthTag();
24
+ return `${iv.toString("hex")}:${authTag.toString("hex")}:${encrypted.toString("hex")}`;
25
+ }
26
+ function decryptSecret(payload) {
27
+ const [ivHex, authTagHex, encryptedHex] = payload.split(":");
28
+ if (!ivHex || !authTagHex || !encryptedHex) {
29
+ throw new Error("Invalid encrypted secret payload");
30
+ }
31
+ const decipher = createDecipheriv(ALGORITHM, getEncryptionKey(), Buffer.from(ivHex, "hex"));
32
+ decipher.setAuthTag(Buffer.from(authTagHex, "hex"));
33
+ const decrypted = Buffer.concat([
34
+ decipher.update(Buffer.from(encryptedHex, "hex")),
35
+ decipher.final()
36
+ ]);
37
+ return decrypted.toString("utf8");
38
+ }
39
+ export {
40
+ decryptSecret,
41
+ encryptSecret
42
+ };
43
+ //# sourceMappingURL=vtex-crypto.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/config/vtex-crypto.ts"],
4
+ "sourcesContent": ["import { createCipheriv, createDecipheriv, randomBytes } from \"node:crypto\";\n\nconst ALGORITHM = \"aes-256-gcm\";\nconst IV_LENGTH = 12;\n\nfunction getEncryptionKey(): Buffer {\n const rawValue = process.env.DB_ENCRYPTION_KEY?.trim();\n if (!rawValue) {\n throw new Error(\n \"Missing DB_ENCRYPTION_KEY. Configure it before encrypting or decrypting persisted secrets.\"\n );\n }\n\n const buffer = Buffer.from(rawValue, \"hex\");\n if (buffer.length !== 32) {\n throw new Error(\n \"Invalid DB_ENCRYPTION_KEY. Expected a 32-byte key encoded as 64 hex characters.\"\n );\n }\n\n return buffer;\n}\n\nexport function encryptSecret(plainText: string): string {\n const iv = randomBytes(IV_LENGTH);\n const cipher = createCipheriv(ALGORITHM, getEncryptionKey(), iv);\n const encrypted = Buffer.concat([cipher.update(plainText, \"utf8\"), cipher.final()]);\n const authTag = cipher.getAuthTag();\n\n return `${iv.toString(\"hex\")}:${authTag.toString(\"hex\")}:${encrypted.toString(\"hex\")}`;\n}\n\nexport function decryptSecret(payload: string): string {\n const [ivHex, authTagHex, encryptedHex] = payload.split(\":\");\n if (!ivHex || !authTagHex || !encryptedHex) {\n throw new Error(\"Invalid encrypted secret payload\");\n }\n\n const decipher = createDecipheriv(ALGORITHM, getEncryptionKey(), Buffer.from(ivHex, \"hex\"));\n decipher.setAuthTag(Buffer.from(authTagHex, \"hex\"));\n\n const decrypted = Buffer.concat([\n decipher.update(Buffer.from(encryptedHex, \"hex\")),\n decipher.final(),\n ]);\n\n return decrypted.toString(\"utf8\");\n}\n"],
5
+ "mappings": "AAAA,SAAS,gBAAgB,kBAAkB,mBAAmB;AAE9D,MAAM,YAAY;AAClB,MAAM,YAAY;AAElB,SAAS,mBAA2B;AAClC,QAAM,WAAW,QAAQ,IAAI,mBAAmB,KAAK;AACrD,MAAI,CAAC,UAAU;AACb,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AAEA,QAAM,SAAS,OAAO,KAAK,UAAU,KAAK;AAC1C,MAAI,OAAO,WAAW,IAAI;AACxB,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;AAEO,SAAS,cAAc,WAA2B;AACvD,QAAM,KAAK,YAAY,SAAS;AAChC,QAAM,SAAS,eAAe,WAAW,iBAAiB,GAAG,EAAE;AAC/D,QAAM,YAAY,OAAO,OAAO,CAAC,OAAO,OAAO,WAAW,MAAM,GAAG,OAAO,MAAM,CAAC,CAAC;AAClF,QAAM,UAAU,OAAO,WAAW;AAElC,SAAO,GAAG,GAAG,SAAS,KAAK,CAAC,IAAI,QAAQ,SAAS,KAAK,CAAC,IAAI,UAAU,SAAS,KAAK,CAAC;AACtF;AAEO,SAAS,cAAc,SAAyB;AACrD,QAAM,CAAC,OAAO,YAAY,YAAY,IAAI,QAAQ,MAAM,GAAG;AAC3D,MAAI,CAAC,SAAS,CAAC,cAAc,CAAC,cAAc;AAC1C,UAAM,IAAI,MAAM,kCAAkC;AAAA,EACpD;AAEA,QAAM,WAAW,iBAAiB,WAAW,iBAAiB,GAAG,OAAO,KAAK,OAAO,KAAK,CAAC;AAC1F,WAAS,WAAW,OAAO,KAAK,YAAY,KAAK,CAAC;AAElD,QAAM,YAAY,OAAO,OAAO;AAAA,IAC9B,SAAS,OAAO,OAAO,KAAK,cAAc,KAAK,CAAC;AAAA,IAChD,SAAS,MAAM;AAAA,EACjB,CAAC;AAED,SAAO,UAAU,SAAS,MAAM;AAClC;",
6
+ "names": []
7
+ }
@@ -0,0 +1,132 @@
1
+ import { getDb } from "../db/client.js";
2
+ function mapVtexConnectionRow(row) {
3
+ return {
4
+ profileId: row.profile_id,
5
+ profileName: row.profile_name,
6
+ accountName: row.account_name,
7
+ apiKeyEncrypted: row.api_key_encrypted,
8
+ apiTokenEncrypted: row.api_token_encrypted,
9
+ isActive: row.is_active,
10
+ status: row.status,
11
+ lastValidatedAt: row.last_validated_at,
12
+ lastError: row.last_error,
13
+ createdAt: row.created_at,
14
+ updatedAt: row.updated_at
15
+ };
16
+ }
17
+ async function getVtexConnectionRow(profileId) {
18
+ const result = await getDb().query(
19
+ `
20
+ select
21
+ pvc.profile_id,
22
+ p.name as profile_name,
23
+ pvc.account_name,
24
+ pvc.api_key_encrypted,
25
+ pvc.api_token_encrypted,
26
+ pvc.is_active,
27
+ pvc.status,
28
+ pvc.last_validated_at,
29
+ pvc.last_error,
30
+ pvc.created_at,
31
+ pvc.updated_at
32
+ from profile_vtex_connections pvc
33
+ inner join profiles p on p.id = pvc.profile_id
34
+ where pvc.profile_id = $1
35
+ `,
36
+ [profileId]
37
+ );
38
+ const row = result.rows[0];
39
+ return row ? mapVtexConnectionRow(row) : null;
40
+ }
41
+ async function upsertVtexConnection(input) {
42
+ const result = await getDb().query(
43
+ `
44
+ insert into profile_vtex_connections (
45
+ profile_id,
46
+ account_name,
47
+ api_key_encrypted,
48
+ api_token_encrypted,
49
+ is_active,
50
+ status,
51
+ last_validated_at,
52
+ last_error
53
+ )
54
+ values ($1, $2, $3, $4, $5, $6, $7, $8)
55
+ on conflict (profile_id) do update
56
+ set
57
+ account_name = excluded.account_name,
58
+ api_key_encrypted = excluded.api_key_encrypted,
59
+ api_token_encrypted = excluded.api_token_encrypted,
60
+ is_active = excluded.is_active,
61
+ status = excluded.status,
62
+ last_validated_at = excluded.last_validated_at,
63
+ last_error = excluded.last_error,
64
+ updated_at = now()
65
+ returning
66
+ profile_id,
67
+ account_name,
68
+ api_key_encrypted,
69
+ api_token_encrypted,
70
+ is_active,
71
+ status,
72
+ last_validated_at,
73
+ last_error,
74
+ created_at,
75
+ updated_at
76
+ `,
77
+ [
78
+ input.profileId,
79
+ input.accountName,
80
+ input.apiKeyEncrypted,
81
+ input.apiTokenEncrypted,
82
+ input.isActive,
83
+ input.status,
84
+ input.lastValidatedAt ?? null,
85
+ input.lastError ?? null
86
+ ]
87
+ );
88
+ return mapVtexConnectionRow(result.rows[0]);
89
+ }
90
+ async function markVtexConnectionValidation(profileId, status, lastError) {
91
+ await getDb().query(
92
+ `
93
+ update profile_vtex_connections
94
+ set
95
+ status = $2,
96
+ last_validated_at = now(),
97
+ last_error = $3,
98
+ updated_at = now()
99
+ where profile_id = $1
100
+ `,
101
+ [profileId, status, lastError ?? null]
102
+ );
103
+ }
104
+ async function listVtexConnections() {
105
+ const result = await getDb().query(
106
+ `
107
+ select
108
+ pvc.profile_id,
109
+ p.name as profile_name,
110
+ pvc.account_name,
111
+ pvc.api_key_encrypted,
112
+ pvc.api_token_encrypted,
113
+ pvc.is_active,
114
+ pvc.status,
115
+ pvc.last_validated_at,
116
+ pvc.last_error,
117
+ pvc.created_at,
118
+ pvc.updated_at
119
+ from profile_vtex_connections pvc
120
+ inner join profiles p on p.id = pvc.profile_id
121
+ order by pvc.profile_id asc
122
+ `
123
+ );
124
+ return result.rows.map(mapVtexConnectionRow);
125
+ }
126
+ export {
127
+ getVtexConnectionRow,
128
+ listVtexConnections,
129
+ markVtexConnectionValidation,
130
+ upsertVtexConnection
131
+ };
132
+ //# sourceMappingURL=vtex-profile-store.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/config/vtex-profile-store.ts"],
4
+ "sourcesContent": ["import { getDb } from \"../db/client.js\";\n\nexport type VtexConnectionStatus = \"pending\" | \"valid\" | \"invalid\" | \"disabled\";\n\nexport interface VtexConnectionRecord {\n profileId: string;\n profileName?: string;\n accountName: string;\n apiKeyEncrypted: string;\n apiTokenEncrypted: string;\n isActive: boolean;\n status: VtexConnectionStatus;\n lastValidatedAt: Date | null;\n lastError: string | null;\n createdAt: Date;\n updatedAt: Date;\n}\n\ninterface VtexConnectionRow {\n profile_id: string;\n profile_name?: string;\n account_name: string;\n api_key_encrypted: string;\n api_token_encrypted: string;\n is_active: boolean;\n status: VtexConnectionStatus;\n last_validated_at: Date | null;\n last_error: string | null;\n created_at: Date;\n updated_at: Date;\n}\n\nfunction mapVtexConnectionRow(row: VtexConnectionRow): VtexConnectionRecord {\n return {\n profileId: row.profile_id,\n profileName: row.profile_name,\n accountName: row.account_name,\n apiKeyEncrypted: row.api_key_encrypted,\n apiTokenEncrypted: row.api_token_encrypted,\n isActive: row.is_active,\n status: row.status,\n lastValidatedAt: row.last_validated_at,\n lastError: row.last_error,\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n}\n\nexport async function getVtexConnectionRow(\n profileId: string\n): Promise<VtexConnectionRecord | null> {\n const result = await getDb().query<VtexConnectionRow>(\n `\n select\n pvc.profile_id,\n p.name as profile_name,\n pvc.account_name,\n pvc.api_key_encrypted,\n pvc.api_token_encrypted,\n pvc.is_active,\n pvc.status,\n pvc.last_validated_at,\n pvc.last_error,\n pvc.created_at,\n pvc.updated_at\n from profile_vtex_connections pvc\n inner join profiles p on p.id = pvc.profile_id\n where pvc.profile_id = $1\n `,\n [profileId]\n );\n\n const row = result.rows[0];\n return row ? mapVtexConnectionRow(row) : null;\n}\n\nexport async function upsertVtexConnection(input: {\n profileId: string;\n accountName: string;\n apiKeyEncrypted: string;\n apiTokenEncrypted: string;\n isActive: boolean;\n status: VtexConnectionStatus;\n lastValidatedAt?: Date | null;\n lastError?: string | null;\n}): Promise<VtexConnectionRecord> {\n const result = await getDb().query<VtexConnectionRow>(\n `\n insert into profile_vtex_connections (\n profile_id,\n account_name,\n api_key_encrypted,\n api_token_encrypted,\n is_active,\n status,\n last_validated_at,\n last_error\n )\n values ($1, $2, $3, $4, $5, $6, $7, $8)\n on conflict (profile_id) do update\n set\n account_name = excluded.account_name,\n api_key_encrypted = excluded.api_key_encrypted,\n api_token_encrypted = excluded.api_token_encrypted,\n is_active = excluded.is_active,\n status = excluded.status,\n last_validated_at = excluded.last_validated_at,\n last_error = excluded.last_error,\n updated_at = now()\n returning\n profile_id,\n account_name,\n api_key_encrypted,\n api_token_encrypted,\n is_active,\n status,\n last_validated_at,\n last_error,\n created_at,\n updated_at\n `,\n [\n input.profileId,\n input.accountName,\n input.apiKeyEncrypted,\n input.apiTokenEncrypted,\n input.isActive,\n input.status,\n input.lastValidatedAt ?? null,\n input.lastError ?? null,\n ]\n );\n\n return mapVtexConnectionRow(result.rows[0]);\n}\n\nexport async function markVtexConnectionValidation(\n profileId: string,\n status: VtexConnectionStatus,\n lastError?: string | null\n): Promise<void> {\n await getDb().query(\n `\n update profile_vtex_connections\n set\n status = $2,\n last_validated_at = now(),\n last_error = $3,\n updated_at = now()\n where profile_id = $1\n `,\n [profileId, status, lastError ?? null]\n );\n}\n\nexport async function listVtexConnections(): Promise<VtexConnectionRecord[]> {\n const result = await getDb().query<VtexConnectionRow>(\n `\n select\n pvc.profile_id,\n p.name as profile_name,\n pvc.account_name,\n pvc.api_key_encrypted,\n pvc.api_token_encrypted,\n pvc.is_active,\n pvc.status,\n pvc.last_validated_at,\n pvc.last_error,\n pvc.created_at,\n pvc.updated_at\n from profile_vtex_connections pvc\n inner join profiles p on p.id = pvc.profile_id\n order by pvc.profile_id asc\n `\n );\n\n return result.rows.map(mapVtexConnectionRow);\n}\n"],
5
+ "mappings": "AAAA,SAAS,aAAa;AAgCtB,SAAS,qBAAqB,KAA8C;AAC1E,SAAO;AAAA,IACL,WAAW,IAAI;AAAA,IACf,aAAa,IAAI;AAAA,IACjB,aAAa,IAAI;AAAA,IACjB,iBAAiB,IAAI;AAAA,IACrB,mBAAmB,IAAI;AAAA,IACvB,UAAU,IAAI;AAAA,IACd,QAAQ,IAAI;AAAA,IACZ,iBAAiB,IAAI;AAAA,IACrB,WAAW,IAAI;AAAA,IACf,WAAW,IAAI;AAAA,IACf,WAAW,IAAI;AAAA,EACjB;AACF;AAEA,eAAsB,qBACpB,WACsC;AACtC,QAAM,SAAS,MAAM,MAAM,EAAE;AAAA,IAC3B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAiBA,CAAC,SAAS;AAAA,EACZ;AAEA,QAAM,MAAM,OAAO,KAAK,CAAC;AACzB,SAAO,MAAM,qBAAqB,GAAG,IAAI;AAC3C;AAEA,eAAsB,qBAAqB,OAST;AAChC,QAAM,SAAS,MAAM,MAAM,EAAE;AAAA,IAC3B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAkCA;AAAA,MACE,MAAM;AAAA,MACN,MAAM;AAAA,MACN,MAAM;AAAA,MACN,MAAM;AAAA,MACN,MAAM;AAAA,MACN,MAAM;AAAA,MACN,MAAM,mBAAmB;AAAA,MACzB,MAAM,aAAa;AAAA,IACrB;AAAA,EACF;AAEA,SAAO,qBAAqB,OAAO,KAAK,CAAC,CAAC;AAC5C;AAEA,eAAsB,6BACpB,WACA,QACA,WACe;AACf,QAAM,MAAM,EAAE;AAAA,IACZ;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IASA,CAAC,WAAW,QAAQ,aAAa,IAAI;AAAA,EACvC;AACF;AAEA,eAAsB,sBAAuD;AAC3E,QAAM,SAAS,MAAM,MAAM,EAAE;AAAA,IAC3B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAiBF;AAEA,SAAO,OAAO,KAAK,IAAI,oBAAoB;AAC7C;",
6
+ "names": []
7
+ }
@@ -1,26 +1,32 @@
1
- const REQUIRED_ENV_VARS = [
2
- "VTEX_ACCOUNT_NAME",
3
- "VTEX_API_KEY",
4
- "VTEX_API_TOKEN"
5
- ];
6
- function requireEnv(name) {
7
- const value = process.env[name]?.trim();
8
- if (!value) {
9
- throw new Error(`Missing required environment variable: ${name}`);
1
+ import { assertActiveProfile } from "./profile-store.js";
2
+ import { decryptSecret } from "./vtex-crypto.js";
3
+ import { getVtexConnectionRow } from "./vtex-profile-store.js";
4
+ function buildVtexUrls(accountName) {
5
+ return {
6
+ baseUrl: `https://${accountName}.vtexcommercestable.com.br`,
7
+ pricingBaseUrl: `https://api.vtex.com/${accountName}`
8
+ };
9
+ }
10
+ async function getVtexConfigForProfile(profileId) {
11
+ await assertActiveProfile(profileId);
12
+ const connection = await getVtexConnectionRow(profileId);
13
+ if (!connection) {
14
+ throw new Error(`VTEX connection not found for profileId "${profileId}"`);
15
+ }
16
+ if (!connection.isActive || connection.status === "disabled") {
17
+ throw new Error(`VTEX connection for profileId "${profileId}" is disabled`);
10
18
  }
11
- return value;
19
+ const urls = buildVtexUrls(connection.accountName);
20
+ return {
21
+ accountName: connection.accountName,
22
+ apiKey: decryptSecret(connection.apiKeyEncrypted),
23
+ apiToken: decryptSecret(connection.apiTokenEncrypted),
24
+ baseUrl: urls.baseUrl,
25
+ pricingBaseUrl: urls.pricingBaseUrl
26
+ };
12
27
  }
13
- const accountName = requireEnv("VTEX_ACCOUNT_NAME");
14
- const apiKey = requireEnv("VTEX_API_KEY");
15
- const apiToken = requireEnv("VTEX_API_TOKEN");
16
- const vtexConfig = {
17
- accountName,
18
- apiKey,
19
- apiToken,
20
- baseUrl: `https://${accountName}.vtexcommercestable.com.br`,
21
- pricingBaseUrl: `https://api.vtex.com/${accountName}`
22
- };
23
28
  export {
24
- vtexConfig
29
+ buildVtexUrls,
30
+ getVtexConfigForProfile
25
31
  };
26
32
  //# sourceMappingURL=vtex.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/config/vtex.ts"],
4
- "sourcesContent": ["const REQUIRED_ENV_VARS = [\n \"VTEX_ACCOUNT_NAME\",\n \"VTEX_API_KEY\",\n \"VTEX_API_TOKEN\",\n] as const;\n\ntype RequiredEnvVar = (typeof REQUIRED_ENV_VARS)[number];\n\nfunction requireEnv(name: RequiredEnvVar): string {\n const value = process.env[name]?.trim();\n if (!value) {\n throw new Error(`Missing required environment variable: ${name}`);\n }\n return value;\n}\n\nconst accountName = requireEnv(\"VTEX_ACCOUNT_NAME\");\nconst apiKey = requireEnv(\"VTEX_API_KEY\");\nconst apiToken = requireEnv(\"VTEX_API_TOKEN\");\n\nexport interface VtexConfig {\n accountName: string;\n apiKey: string;\n apiToken: string;\n baseUrl: string;\n pricingBaseUrl: string;\n}\n\nexport const vtexConfig: VtexConfig = {\n accountName,\n apiKey,\n apiToken,\n baseUrl: `https://${accountName}.vtexcommercestable.com.br`,\n pricingBaseUrl: `https://api.vtex.com/${accountName}`,\n};\n"],
5
- "mappings": "AAAA,MAAM,oBAAoB;AAAA,EACxB;AAAA,EACA;AAAA,EACA;AACF;AAIA,SAAS,WAAW,MAA8B;AAChD,QAAM,QAAQ,QAAQ,IAAI,IAAI,GAAG,KAAK;AACtC,MAAI,CAAC,OAAO;AACV,UAAM,IAAI,MAAM,0CAA0C,IAAI,EAAE;AAAA,EAClE;AACA,SAAO;AACT;AAEA,MAAM,cAAc,WAAW,mBAAmB;AAClD,MAAM,SAAS,WAAW,cAAc;AACxC,MAAM,WAAW,WAAW,gBAAgB;AAUrC,MAAM,aAAyB;AAAA,EACpC;AAAA,EACA;AAAA,EACA;AAAA,EACA,SAAS,WAAW,WAAW;AAAA,EAC/B,gBAAgB,wBAAwB,WAAW;AACrD;",
4
+ "sourcesContent": ["import { assertActiveProfile } from \"./profile-store.js\";\nimport { decryptSecret } from \"./vtex-crypto.js\";\nimport { getVtexConnectionRow } from \"./vtex-profile-store.js\";\n\nexport interface VtexConfig {\n accountName: string;\n apiKey: string;\n apiToken: string;\n baseUrl: string;\n pricingBaseUrl: string;\n}\n\nexport function buildVtexUrls(accountName: string) {\n return {\n baseUrl: `https://${accountName}.vtexcommercestable.com.br`,\n pricingBaseUrl: `https://api.vtex.com/${accountName}`,\n };\n}\n\nexport async function getVtexConfigForProfile(profileId: string): Promise<VtexConfig> {\n await assertActiveProfile(profileId);\n\n const connection = await getVtexConnectionRow(profileId);\n if (!connection) {\n throw new Error(`VTEX connection not found for profileId \"${profileId}\"`);\n }\n\n if (!connection.isActive || connection.status === \"disabled\") {\n throw new Error(`VTEX connection for profileId \"${profileId}\" is disabled`);\n }\n\n const urls = buildVtexUrls(connection.accountName);\n\n return {\n accountName: connection.accountName,\n apiKey: decryptSecret(connection.apiKeyEncrypted),\n apiToken: decryptSecret(connection.apiTokenEncrypted),\n baseUrl: urls.baseUrl,\n pricingBaseUrl: urls.pricingBaseUrl,\n };\n}\n"],
5
+ "mappings": "AAAA,SAAS,2BAA2B;AACpC,SAAS,qBAAqB;AAC9B,SAAS,4BAA4B;AAU9B,SAAS,cAAc,aAAqB;AACjD,SAAO;AAAA,IACL,SAAS,WAAW,WAAW;AAAA,IAC/B,gBAAgB,wBAAwB,WAAW;AAAA,EACrD;AACF;AAEA,eAAsB,wBAAwB,WAAwC;AACpF,QAAM,oBAAoB,SAAS;AAEnC,QAAM,aAAa,MAAM,qBAAqB,SAAS;AACvD,MAAI,CAAC,YAAY;AACf,UAAM,IAAI,MAAM,4CAA4C,SAAS,GAAG;AAAA,EAC1E;AAEA,MAAI,CAAC,WAAW,YAAY,WAAW,WAAW,YAAY;AAC5D,UAAM,IAAI,MAAM,kCAAkC,SAAS,eAAe;AAAA,EAC5E;AAEA,QAAM,OAAO,cAAc,WAAW,WAAW;AAEjD,SAAO;AAAA,IACL,aAAa,WAAW;AAAA,IACxB,QAAQ,cAAc,WAAW,eAAe;AAAA,IAChD,UAAU,cAAc,WAAW,iBAAiB;AAAA,IACpD,SAAS,KAAK;AAAA,IACd,gBAAgB,KAAK;AAAA,EACvB;AACF;",
6
6
  "names": []
7
7
  }
@@ -0,0 +1,58 @@
1
+ import { Pool } from "pg";
2
+ function requireDatabaseUrl() {
3
+ const value = process.env.DATABASE_URL?.trim();
4
+ if (!value) {
5
+ throw new Error(
6
+ "Missing DATABASE_URL. Configure it in your environment before using the database client."
7
+ );
8
+ }
9
+ return value;
10
+ }
11
+ let pool = null;
12
+ function getDb() {
13
+ if (!pool) {
14
+ pool = new Pool({
15
+ connectionString: requireDatabaseUrl()
16
+ });
17
+ }
18
+ return pool;
19
+ }
20
+ function getDatabaseUrl() {
21
+ const value = process.env.DATABASE_URL?.trim();
22
+ return value ? value : void 0;
23
+ }
24
+ async function testDatabaseConnection() {
25
+ const result = await getDb().query(
26
+ `select current_database(), current_user, now()`
27
+ );
28
+ return result.rows[0];
29
+ }
30
+ async function checkTableExists(tableName) {
31
+ const result = await getDb().query(
32
+ `
33
+ select exists (
34
+ select 1
35
+ from information_schema.tables
36
+ where table_schema = 'public'
37
+ and table_name = $1
38
+ ) as exists
39
+ `,
40
+ [tableName]
41
+ );
42
+ return Boolean(result.rows[0]?.exists);
43
+ }
44
+ async function closeDb() {
45
+ if (!pool) {
46
+ return;
47
+ }
48
+ await pool.end();
49
+ pool = null;
50
+ }
51
+ export {
52
+ checkTableExists,
53
+ closeDb,
54
+ getDatabaseUrl,
55
+ getDb,
56
+ testDatabaseConnection
57
+ };
58
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/db/client.ts"],
4
+ "sourcesContent": ["import { Pool } from \"pg\";\n\nfunction requireDatabaseUrl(): string {\n const value = process.env.DATABASE_URL?.trim();\n if (!value) {\n throw new Error(\n \"Missing DATABASE_URL. Configure it in your environment before using the database client.\"\n );\n }\n\n return value;\n}\n\nlet pool: Pool | null = null;\n\nexport function getDb(): Pool {\n if (!pool) {\n pool = new Pool({\n connectionString: requireDatabaseUrl(),\n });\n }\n\n return pool;\n}\n\nexport function getDatabaseUrl(): string | undefined {\n const value = process.env.DATABASE_URL?.trim();\n return value ? value : undefined;\n}\n\nexport async function testDatabaseConnection() {\n const result = await getDb().query<{\n current_database: string;\n current_user: string;\n now: Date;\n }>(\n `select current_database(), current_user, now()`\n );\n\n return result.rows[0];\n}\n\nexport async function checkTableExists(tableName: string): Promise<boolean> {\n const result = await getDb().query<{ exists: boolean }>(\n `\n select exists (\n select 1\n from information_schema.tables\n where table_schema = 'public'\n and table_name = $1\n ) as exists\n `,\n [tableName]\n );\n\n return Boolean(result.rows[0]?.exists);\n}\n\nexport async function closeDb() {\n if (!pool) {\n return;\n }\n\n await pool.end();\n pool = null;\n}\n"],
5
+ "mappings": "AAAA,SAAS,YAAY;AAErB,SAAS,qBAA6B;AACpC,QAAM,QAAQ,QAAQ,IAAI,cAAc,KAAK;AAC7C,MAAI,CAAC,OAAO;AACV,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;AAEA,IAAI,OAAoB;AAEjB,SAAS,QAAc;AAC5B,MAAI,CAAC,MAAM;AACT,WAAO,IAAI,KAAK;AAAA,MACd,kBAAkB,mBAAmB;AAAA,IACvC,CAAC;AAAA,EACH;AAEA,SAAO;AACT;AAEO,SAAS,iBAAqC;AACnD,QAAM,QAAQ,QAAQ,IAAI,cAAc,KAAK;AAC7C,SAAO,QAAQ,QAAQ;AACzB;AAEA,eAAsB,yBAAyB;AAC7C,QAAM,SAAS,MAAM,MAAM,EAAE;AAAA,IAK3B;AAAA,EACF;AAEA,SAAO,OAAO,KAAK,CAAC;AACtB;AAEA,eAAsB,iBAAiB,WAAqC;AAC1E,QAAM,SAAS,MAAM,MAAM,EAAE;AAAA,IAC3B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAQA,CAAC,SAAS;AAAA,EACZ;AAEA,SAAO,QAAQ,OAAO,KAAK,CAAC,GAAG,MAAM;AACvC;AAEA,eAAsB,UAAU;AAC9B,MAAI,CAAC,MAAM;AACT;AAAA,EACF;AAEA,QAAM,KAAK,IAAI;AACf,SAAO;AACT;",
6
+ "names": []
7
+ }