@open-mercato/checkout 0.10.1-develop.8166.1.30b4691fc6 → 0.10.1-develop.8181.1.91f97e988f
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/dist/modules/checkout/__integration__/TC-CHKT-044-encryption-map-backfill.spec.js +116 -0
- package/dist/modules/checkout/__integration__/TC-CHKT-044-encryption-map-backfill.spec.js.map +7 -0
- package/dist/modules/checkout/migrations/Migration20261109120000.js +32 -0
- package/dist/modules/checkout/migrations/Migration20261109120000.js.map +7 -0
- package/package.json +7 -7
- package/src/modules/checkout/__integration__/TC-CHKT-044-encryption-map-backfill.spec.ts +142 -0
- package/src/modules/checkout/migrations/Migration20261109120000.ts +43 -0
- package/src/modules/checkout/migrations/__tests__/Migration20261109120000.test.ts +63 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
[build:checkout] found
|
|
1
|
+
[build:checkout] found 172 entry points
|
|
2
2
|
[build:checkout] built successfully
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { config as loadEnv } from "dotenv";
|
|
4
|
+
import { Client } from "pg";
|
|
5
|
+
import { expect, test } from "@playwright/test";
|
|
6
|
+
import { Migration20261109120000 } from "../migrations/Migration20261109120000.js";
|
|
7
|
+
const currentDir = path.dirname(fileURLToPath(import.meta.url));
|
|
8
|
+
const repoRoot = path.resolve(currentDir, "..", "..", "..", "..", "..", "..");
|
|
9
|
+
const appDir = path.join(repoRoot, "apps", "mercato");
|
|
10
|
+
const runtimeAppRoot = process.env.OM_TEST_APP_ROOT?.trim() ? path.resolve(process.env.OM_TEST_APP_ROOT.trim()) : appDir;
|
|
11
|
+
if (!process.env.OM_TEST_APP_ROOT?.trim()) {
|
|
12
|
+
loadEnv({ path: path.resolve(runtimeAppRoot, ".env") });
|
|
13
|
+
}
|
|
14
|
+
async function withDatabase(run) {
|
|
15
|
+
const connectionString = process.env.DATABASE_URL;
|
|
16
|
+
if (!connectionString) throw new Error("[internal] DATABASE_URL is required for TC-CHKT-044");
|
|
17
|
+
const client = new Client({ connectionString });
|
|
18
|
+
await client.connect();
|
|
19
|
+
try {
|
|
20
|
+
return await run(client);
|
|
21
|
+
} finally {
|
|
22
|
+
await client.end();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
async function collectMigrationSql() {
|
|
26
|
+
const migration = Object.create(Migration20261109120000.prototype);
|
|
27
|
+
const statements = [];
|
|
28
|
+
Object.defineProperty(migration, "addSql", {
|
|
29
|
+
value: (sql) => statements.push(sql)
|
|
30
|
+
});
|
|
31
|
+
await migration.up();
|
|
32
|
+
return statements;
|
|
33
|
+
}
|
|
34
|
+
async function runMigrationAgainst(client, tableName) {
|
|
35
|
+
for (const statement of await collectMigrationSql()) {
|
|
36
|
+
await client.query(statement.replaceAll('"encryption_maps"', `"${tableName}"`));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
test.describe("TC-CHKT-044: checkout encryption map backfill migration", () => {
|
|
40
|
+
test("backfills missing maps, appends missing fields while preserving custom ones, skips disabled tenants, and is idempotent", async () => {
|
|
41
|
+
await withDatabase(async (client) => {
|
|
42
|
+
const tableName = "encryption_maps_checkout_backfill_test";
|
|
43
|
+
await client.query(`create temporary table ${tableName} (like encryption_maps including defaults)`);
|
|
44
|
+
const tenantWithEncryption = "00000000-0000-0000-0000-00000000a001";
|
|
45
|
+
const tenantWithoutEncryption = "00000000-0000-0000-0000-00000000a002";
|
|
46
|
+
await client.query(
|
|
47
|
+
`insert into ${tableName}
|
|
48
|
+
(id, entity_id, tenant_id, organization_id, fields_json, is_active, created_at, updated_at, deleted_at)
|
|
49
|
+
values
|
|
50
|
+
('10000000-0000-0000-0000-000000000001', 'auth:user', $1, null,
|
|
51
|
+
'[{"field":"email"}]'::jsonb, true, now(), now(), null),
|
|
52
|
+
('10000000-0000-0000-0000-000000000002', 'checkout:checkout_transaction', $1, null,
|
|
53
|
+
'[{"field":"customer_data"},{"field":"admin_custom_field"}]'::jsonb, true, now(), now(), null)`,
|
|
54
|
+
[tenantWithEncryption]
|
|
55
|
+
);
|
|
56
|
+
await client.query(
|
|
57
|
+
`insert into ${tableName}
|
|
58
|
+
(id, entity_id, tenant_id, organization_id, fields_json, is_active, created_at, updated_at, deleted_at)
|
|
59
|
+
values
|
|
60
|
+
('10000000-0000-0000-0000-000000000003', 'auth:user', $1, null,
|
|
61
|
+
'[{"field":"email"}]'::jsonb, false, now(), now(), null)`,
|
|
62
|
+
[tenantWithoutEncryption]
|
|
63
|
+
);
|
|
64
|
+
await runMigrationAgainst(client, tableName);
|
|
65
|
+
const linkTemplate = await client.query(
|
|
66
|
+
`select fields_json from ${tableName} where entity_id = 'checkout:checkout_link_template' and tenant_id = $1 and deleted_at is null`,
|
|
67
|
+
[tenantWithEncryption]
|
|
68
|
+
);
|
|
69
|
+
expect(linkTemplate.rows).toHaveLength(1);
|
|
70
|
+
expect(linkTemplate.rows[0].fields_json).toEqual([{ field: "gateway_settings" }]);
|
|
71
|
+
const link = await client.query(
|
|
72
|
+
`select fields_json from ${tableName} where entity_id = 'checkout:checkout_link' and tenant_id = $1 and deleted_at is null`,
|
|
73
|
+
[tenantWithEncryption]
|
|
74
|
+
);
|
|
75
|
+
expect(link.rows).toHaveLength(1);
|
|
76
|
+
expect(link.rows[0].fields_json).toEqual([{ field: "gateway_settings" }]);
|
|
77
|
+
const transaction = await client.query(
|
|
78
|
+
`select fields_json from ${tableName} where entity_id = 'checkout:checkout_transaction' and tenant_id = $1 and deleted_at is null`,
|
|
79
|
+
[tenantWithEncryption]
|
|
80
|
+
);
|
|
81
|
+
expect(transaction.rows).toHaveLength(1);
|
|
82
|
+
const transactionFields = transaction.rows[0].fields_json.map((rule) => rule.field);
|
|
83
|
+
expect(transactionFields).toEqual(
|
|
84
|
+
expect.arrayContaining([
|
|
85
|
+
"customer_data",
|
|
86
|
+
"admin_custom_field",
|
|
87
|
+
"first_name",
|
|
88
|
+
"last_name",
|
|
89
|
+
"email",
|
|
90
|
+
"phone",
|
|
91
|
+
"accepted_legal_consents",
|
|
92
|
+
"ip_address"
|
|
93
|
+
])
|
|
94
|
+
);
|
|
95
|
+
expect(transactionFields).toHaveLength(8);
|
|
96
|
+
const disabledTenantRows = await client.query(
|
|
97
|
+
`select entity_id from ${tableName} where tenant_id = $1 and entity_id like 'checkout:%'`,
|
|
98
|
+
[tenantWithoutEncryption]
|
|
99
|
+
);
|
|
100
|
+
expect(disabledTenantRows.rows).toHaveLength(0);
|
|
101
|
+
await runMigrationAgainst(client, tableName);
|
|
102
|
+
const linkTemplateAfterRerun = await client.query(
|
|
103
|
+
`select fields_json from ${tableName} where entity_id = 'checkout:checkout_link_template' and tenant_id = $1 and deleted_at is null`,
|
|
104
|
+
[tenantWithEncryption]
|
|
105
|
+
);
|
|
106
|
+
expect(linkTemplateAfterRerun.rows).toHaveLength(1);
|
|
107
|
+
const transactionAfterRerun = await client.query(
|
|
108
|
+
`select fields_json from ${tableName} where entity_id = 'checkout:checkout_transaction' and tenant_id = $1 and deleted_at is null`,
|
|
109
|
+
[tenantWithEncryption]
|
|
110
|
+
);
|
|
111
|
+
expect(transactionAfterRerun.rows).toHaveLength(1);
|
|
112
|
+
expect(transactionAfterRerun.rows[0].fields_json).toHaveLength(8);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
//# sourceMappingURL=TC-CHKT-044-encryption-map-backfill.spec.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/modules/checkout/__integration__/TC-CHKT-044-encryption-map-backfill.spec.ts"],
|
|
4
|
+
"sourcesContent": ["import path from 'node:path'\nimport { fileURLToPath } from 'node:url'\nimport { config as loadEnv } from 'dotenv'\nimport { Client } from 'pg'\nimport { expect, test } from '@playwright/test'\nimport { Migration20261109120000 } from '../migrations/Migration20261109120000'\n\nconst currentDir = path.dirname(fileURLToPath(import.meta.url))\nconst repoRoot = path.resolve(currentDir, '..', '..', '..', '..', '..', '..')\nconst appDir = path.join(repoRoot, 'apps', 'mercato')\nconst runtimeAppRoot = process.env.OM_TEST_APP_ROOT?.trim()\n ? path.resolve(process.env.OM_TEST_APP_ROOT.trim())\n : appDir\n\nif (!process.env.OM_TEST_APP_ROOT?.trim()) {\n loadEnv({ path: path.resolve(runtimeAppRoot, '.env') })\n}\n\ntype PgClient = InstanceType<typeof Client>\n\nasync function withDatabase<T>(run: (client: PgClient) => Promise<T>): Promise<T> {\n const connectionString = process.env.DATABASE_URL\n if (!connectionString) throw new Error('[internal] DATABASE_URL is required for TC-CHKT-044')\n const client = new Client({ connectionString })\n await client.connect()\n try {\n return await run(client)\n } finally {\n await client.end()\n }\n}\n\nasync function collectMigrationSql(): Promise<string[]> {\n const migration = Object.create(Migration20261109120000.prototype) as Migration20261109120000\n const statements: string[] = []\n Object.defineProperty(migration, 'addSql', {\n value: (sql: string) => statements.push(sql),\n })\n await migration.up()\n return statements\n}\n\nasync function runMigrationAgainst(client: PgClient, tableName: string): Promise<void> {\n for (const statement of await collectMigrationSql()) {\n await client.query(statement.replaceAll('\"encryption_maps\"', `\"${tableName}\"`))\n }\n}\n\ntest.describe('TC-CHKT-044: checkout encryption map backfill migration', () => {\n test('backfills missing maps, appends missing fields while preserving custom ones, skips disabled tenants, and is idempotent', async () => {\n await withDatabase(async (client) => {\n const tableName = 'encryption_maps_checkout_backfill_test'\n await client.query(`create temporary table ${tableName} (like encryption_maps including defaults)`)\n\n const tenantWithEncryption = '00000000-0000-0000-0000-00000000a001'\n const tenantWithoutEncryption = '00000000-0000-0000-0000-00000000a002'\n\n // Tenant with active encryption already has a map for an unrelated entity (marks the scope as\n // \"has active maps\", which is what the backfill's src subquery selects on), and a\n // checkout_transaction map an admin already customized (missing declared fields, plus one of\n // their own that must survive untouched).\n await client.query(\n `insert into ${tableName}\n (id, entity_id, tenant_id, organization_id, fields_json, is_active, created_at, updated_at, deleted_at)\n values\n ('10000000-0000-0000-0000-000000000001', 'auth:user', $1, null,\n '[{\"field\":\"email\"}]'::jsonb, true, now(), now(), null),\n ('10000000-0000-0000-0000-000000000002', 'checkout:checkout_transaction', $1, null,\n '[{\"field\":\"customer_data\"},{\"field\":\"admin_custom_field\"}]'::jsonb, true, now(), now(), null)`,\n [tenantWithEncryption],\n )\n\n // Tenant without any active map (encryption disabled) must stay untouched.\n await client.query(\n `insert into ${tableName}\n (id, entity_id, tenant_id, organization_id, fields_json, is_active, created_at, updated_at, deleted_at)\n values\n ('10000000-0000-0000-0000-000000000003', 'auth:user', $1, null,\n '[{\"field\":\"email\"}]'::jsonb, false, now(), now(), null)`,\n [tenantWithoutEncryption],\n )\n\n await runMigrationAgainst(client, tableName)\n\n const linkTemplate = await client.query(\n `select fields_json from ${tableName} where entity_id = 'checkout:checkout_link_template' and tenant_id = $1 and deleted_at is null`,\n [tenantWithEncryption],\n )\n expect(linkTemplate.rows).toHaveLength(1)\n expect(linkTemplate.rows[0].fields_json).toEqual([{ field: 'gateway_settings' }])\n\n const link = await client.query(\n `select fields_json from ${tableName} where entity_id = 'checkout:checkout_link' and tenant_id = $1 and deleted_at is null`,\n [tenantWithEncryption],\n )\n expect(link.rows).toHaveLength(1)\n expect(link.rows[0].fields_json).toEqual([{ field: 'gateway_settings' }])\n\n const transaction = await client.query(\n `select fields_json from ${tableName} where entity_id = 'checkout:checkout_transaction' and tenant_id = $1 and deleted_at is null`,\n [tenantWithEncryption],\n )\n expect(transaction.rows).toHaveLength(1)\n const transactionFields = (transaction.rows[0].fields_json as Array<{ field: string }>).map((rule) => rule.field)\n expect(transactionFields).toEqual(\n expect.arrayContaining([\n 'customer_data',\n 'admin_custom_field',\n 'first_name',\n 'last_name',\n 'email',\n 'phone',\n 'accepted_legal_consents',\n 'ip_address',\n ]),\n )\n expect(transactionFields).toHaveLength(8)\n\n const disabledTenantRows = await client.query(\n `select entity_id from ${tableName} where tenant_id = $1 and entity_id like 'checkout:%'`,\n [tenantWithoutEncryption],\n )\n expect(disabledTenantRows.rows).toHaveLength(0)\n\n // Re-running must be a no-op: no duplicate rows, no duplicate field entries.\n await runMigrationAgainst(client, tableName)\n\n const linkTemplateAfterRerun = await client.query(\n `select fields_json from ${tableName} where entity_id = 'checkout:checkout_link_template' and tenant_id = $1 and deleted_at is null`,\n [tenantWithEncryption],\n )\n expect(linkTemplateAfterRerun.rows).toHaveLength(1)\n\n const transactionAfterRerun = await client.query(\n `select fields_json from ${tableName} where entity_id = 'checkout:checkout_transaction' and tenant_id = $1 and deleted_at is null`,\n [tenantWithEncryption],\n )\n expect(transactionAfterRerun.rows).toHaveLength(1)\n expect((transactionAfterRerun.rows[0].fields_json as Array<{ field: string }>)).toHaveLength(8)\n })\n })\n})\n"],
|
|
5
|
+
"mappings": "AAAA,OAAO,UAAU;AACjB,SAAS,qBAAqB;AAC9B,SAAS,UAAU,eAAe;AAClC,SAAS,cAAc;AACvB,SAAS,QAAQ,YAAY;AAC7B,SAAS,+BAA+B;AAExC,MAAM,aAAa,KAAK,QAAQ,cAAc,YAAY,GAAG,CAAC;AAC9D,MAAM,WAAW,KAAK,QAAQ,YAAY,MAAM,MAAM,MAAM,MAAM,MAAM,IAAI;AAC5E,MAAM,SAAS,KAAK,KAAK,UAAU,QAAQ,SAAS;AACpD,MAAM,iBAAiB,QAAQ,IAAI,kBAAkB,KAAK,IACtD,KAAK,QAAQ,QAAQ,IAAI,iBAAiB,KAAK,CAAC,IAChD;AAEJ,IAAI,CAAC,QAAQ,IAAI,kBAAkB,KAAK,GAAG;AACzC,UAAQ,EAAE,MAAM,KAAK,QAAQ,gBAAgB,MAAM,EAAE,CAAC;AACxD;AAIA,eAAe,aAAgB,KAAmD;AAChF,QAAM,mBAAmB,QAAQ,IAAI;AACrC,MAAI,CAAC,iBAAkB,OAAM,IAAI,MAAM,qDAAqD;AAC5F,QAAM,SA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
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Migration } from "@mikro-orm/migrations";
|
|
2
|
+
import { buildEncryptionMapBackfillSql } from "@open-mercato/shared/lib/encryption/migration-backfill";
|
|
3
|
+
class Migration20261109120000 extends Migration {
|
|
4
|
+
async up() {
|
|
5
|
+
this.addSql(buildEncryptionMapBackfillSql({
|
|
6
|
+
entityId: "checkout:checkout_link_template",
|
|
7
|
+
fields: [{ field: "gateway_settings" }]
|
|
8
|
+
}));
|
|
9
|
+
this.addSql(buildEncryptionMapBackfillSql({
|
|
10
|
+
entityId: "checkout:checkout_link",
|
|
11
|
+
fields: [{ field: "gateway_settings" }]
|
|
12
|
+
}));
|
|
13
|
+
this.addSql(buildEncryptionMapBackfillSql({
|
|
14
|
+
entityId: "checkout:checkout_transaction",
|
|
15
|
+
fields: [
|
|
16
|
+
{ field: "customer_data" },
|
|
17
|
+
{ field: "first_name" },
|
|
18
|
+
{ field: "last_name" },
|
|
19
|
+
{ field: "email" },
|
|
20
|
+
{ field: "phone" },
|
|
21
|
+
{ field: "accepted_legal_consents" },
|
|
22
|
+
{ field: "ip_address" }
|
|
23
|
+
]
|
|
24
|
+
}));
|
|
25
|
+
}
|
|
26
|
+
async down() {
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export {
|
|
30
|
+
Migration20261109120000
|
|
31
|
+
};
|
|
32
|
+
//# sourceMappingURL=Migration20261109120000.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/modules/checkout/migrations/Migration20261109120000.ts"],
|
|
4
|
+
"sourcesContent": ["import { Migration } from '@mikro-orm/migrations';\nimport { buildEncryptionMapBackfillSql } from '@open-mercato/shared/lib/encryption/migration-backfill';\n\n// Backfill the checkout module's encryption maps for every pre-existing (tenant, org) scope that\n// already has active encryption maps. `defaultEncryptionMaps` in `../encryption.ts` only reaches\n// tenants created after this change (`auth:setup`) \u2014 a tenant that predates the checkout module has\n// no map for any of its three entities, `encryptEntityPayload` no-ops, and checkout_transaction PII\n// (customer_data, first/last name, email, phone, accepted_legal_consents, ip_address) plus the\n// checkout_link/checkout_link_template gateway_settings are written as PLAINTEXT. Entity ids and\n// fields are written literally (not imported from encryption.ts) so this migration keeps meaning the\n// same thing even if the module's declared maps change later. `down()` stays empty: deleting by\n// entity_id would also remove maps seeded at tenant creation or edited by admins in the UI and\n// silently switch encryption off for that entity.\nexport class Migration20261109120000 extends Migration {\n\n override async up(): Promise<void> {\n this.addSql(buildEncryptionMapBackfillSql({\n entityId: 'checkout:checkout_link_template',\n fields: [{ field: 'gateway_settings' }],\n }));\n\n this.addSql(buildEncryptionMapBackfillSql({\n entityId: 'checkout:checkout_link',\n fields: [{ field: 'gateway_settings' }],\n }));\n\n this.addSql(buildEncryptionMapBackfillSql({\n entityId: 'checkout:checkout_transaction',\n fields: [\n { field: 'customer_data' },\n { field: 'first_name' },\n { field: 'last_name' },\n { field: 'email' },\n { field: 'phone' },\n { field: 'accepted_legal_consents' },\n { field: 'ip_address' },\n ],\n }));\n }\n\n override async down(): Promise<void> {}\n\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,iBAAiB;AAC1B,SAAS,qCAAqC;AAYvC,MAAM,gCAAgC,UAAU;AAAA,EAErD,MAAe,KAAoB;AACjC,SAAK,OAAO,8BAA8B;AAAA,MACxC,UAAU;AAAA,MACV,QAAQ,CAAC,EAAE,OAAO,mBAAmB,CAAC;AAAA,IACxC,CAAC,CAAC;AAEF,SAAK,OAAO,8BAA8B;AAAA,MACxC,UAAU;AAAA,MACV,QAAQ,CAAC,EAAE,OAAO,mBAAmB,CAAC;AAAA,IACxC,CAAC,CAAC;AAEF,SAAK,OAAO,8BAA8B;AAAA,MACxC,UAAU;AAAA,MACV,QAAQ;AAAA,QACN,EAAE,OAAO,gBAAgB;AAAA,QACzB,EAAE,OAAO,aAAa;AAAA,QACtB,EAAE,OAAO,YAAY;AAAA,QACrB,EAAE,OAAO,QAAQ;AAAA,QACjB,EAAE,OAAO,QAAQ;AAAA,QACjB,EAAE,OAAO,0BAA0B;AAAA,QACnC,EAAE,OAAO,aAAa;AAAA,MACxB;AAAA,IACF,CAAC,CAAC;AAAA,EACJ;AAAA,EAEA,MAAe,OAAsB;AAAA,EAAC;AAExC;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-mercato/checkout",
|
|
3
|
-
"version": "0.10.1-develop.
|
|
3
|
+
"version": "0.10.1-develop.8181.1.91f97e988f",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -62,21 +62,21 @@
|
|
|
62
62
|
}
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
|
-
"@open-mercato/core": "0.10.1-develop.
|
|
66
|
-
"@open-mercato/ui": "0.10.1-develop.
|
|
65
|
+
"@open-mercato/core": "0.10.1-develop.8181.1.91f97e988f",
|
|
66
|
+
"@open-mercato/ui": "0.10.1-develop.8181.1.91f97e988f",
|
|
67
67
|
"bcryptjs": "^3.0.3"
|
|
68
68
|
},
|
|
69
69
|
"peerDependencies": {
|
|
70
70
|
"@mikro-orm/postgresql": "^7.0.14",
|
|
71
|
-
"@open-mercato/ai-assistant": "0.10.1-develop.
|
|
72
|
-
"@open-mercato/shared": "0.10.1-develop.
|
|
71
|
+
"@open-mercato/ai-assistant": "0.10.1-develop.8181.1.91f97e988f",
|
|
72
|
+
"@open-mercato/shared": "0.10.1-develop.8181.1.91f97e988f",
|
|
73
73
|
"react": "^19.0.0",
|
|
74
74
|
"react-dom": "^19.0.0",
|
|
75
75
|
"react-is": ">=18.0.0"
|
|
76
76
|
},
|
|
77
77
|
"devDependencies": {
|
|
78
|
-
"@open-mercato/ai-assistant": "0.10.1-develop.
|
|
79
|
-
"@open-mercato/shared": "0.10.1-develop.
|
|
78
|
+
"@open-mercato/ai-assistant": "0.10.1-develop.8181.1.91f97e988f",
|
|
79
|
+
"@open-mercato/shared": "0.10.1-develop.8181.1.91f97e988f",
|
|
80
80
|
"@types/jest": "^30.0.0",
|
|
81
81
|
"@types/react": "^19.2.18",
|
|
82
82
|
"@types/react-dom": "^19.2.5",
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { fileURLToPath } from 'node:url'
|
|
3
|
+
import { config as loadEnv } from 'dotenv'
|
|
4
|
+
import { Client } from 'pg'
|
|
5
|
+
import { expect, test } from '@playwright/test'
|
|
6
|
+
import { Migration20261109120000 } from '../migrations/Migration20261109120000'
|
|
7
|
+
|
|
8
|
+
const currentDir = path.dirname(fileURLToPath(import.meta.url))
|
|
9
|
+
const repoRoot = path.resolve(currentDir, '..', '..', '..', '..', '..', '..')
|
|
10
|
+
const appDir = path.join(repoRoot, 'apps', 'mercato')
|
|
11
|
+
const runtimeAppRoot = process.env.OM_TEST_APP_ROOT?.trim()
|
|
12
|
+
? path.resolve(process.env.OM_TEST_APP_ROOT.trim())
|
|
13
|
+
: appDir
|
|
14
|
+
|
|
15
|
+
if (!process.env.OM_TEST_APP_ROOT?.trim()) {
|
|
16
|
+
loadEnv({ path: path.resolve(runtimeAppRoot, '.env') })
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type PgClient = InstanceType<typeof Client>
|
|
20
|
+
|
|
21
|
+
async function withDatabase<T>(run: (client: PgClient) => Promise<T>): Promise<T> {
|
|
22
|
+
const connectionString = process.env.DATABASE_URL
|
|
23
|
+
if (!connectionString) throw new Error('[internal] DATABASE_URL is required for TC-CHKT-044')
|
|
24
|
+
const client = new Client({ connectionString })
|
|
25
|
+
await client.connect()
|
|
26
|
+
try {
|
|
27
|
+
return await run(client)
|
|
28
|
+
} finally {
|
|
29
|
+
await client.end()
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function collectMigrationSql(): Promise<string[]> {
|
|
34
|
+
const migration = Object.create(Migration20261109120000.prototype) as Migration20261109120000
|
|
35
|
+
const statements: string[] = []
|
|
36
|
+
Object.defineProperty(migration, 'addSql', {
|
|
37
|
+
value: (sql: string) => statements.push(sql),
|
|
38
|
+
})
|
|
39
|
+
await migration.up()
|
|
40
|
+
return statements
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function runMigrationAgainst(client: PgClient, tableName: string): Promise<void> {
|
|
44
|
+
for (const statement of await collectMigrationSql()) {
|
|
45
|
+
await client.query(statement.replaceAll('"encryption_maps"', `"${tableName}"`))
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
test.describe('TC-CHKT-044: checkout encryption map backfill migration', () => {
|
|
50
|
+
test('backfills missing maps, appends missing fields while preserving custom ones, skips disabled tenants, and is idempotent', async () => {
|
|
51
|
+
await withDatabase(async (client) => {
|
|
52
|
+
const tableName = 'encryption_maps_checkout_backfill_test'
|
|
53
|
+
await client.query(`create temporary table ${tableName} (like encryption_maps including defaults)`)
|
|
54
|
+
|
|
55
|
+
const tenantWithEncryption = '00000000-0000-0000-0000-00000000a001'
|
|
56
|
+
const tenantWithoutEncryption = '00000000-0000-0000-0000-00000000a002'
|
|
57
|
+
|
|
58
|
+
// Tenant with active encryption already has a map for an unrelated entity (marks the scope as
|
|
59
|
+
// "has active maps", which is what the backfill's src subquery selects on), and a
|
|
60
|
+
// checkout_transaction map an admin already customized (missing declared fields, plus one of
|
|
61
|
+
// their own that must survive untouched).
|
|
62
|
+
await client.query(
|
|
63
|
+
`insert into ${tableName}
|
|
64
|
+
(id, entity_id, tenant_id, organization_id, fields_json, is_active, created_at, updated_at, deleted_at)
|
|
65
|
+
values
|
|
66
|
+
('10000000-0000-0000-0000-000000000001', 'auth:user', $1, null,
|
|
67
|
+
'[{"field":"email"}]'::jsonb, true, now(), now(), null),
|
|
68
|
+
('10000000-0000-0000-0000-000000000002', 'checkout:checkout_transaction', $1, null,
|
|
69
|
+
'[{"field":"customer_data"},{"field":"admin_custom_field"}]'::jsonb, true, now(), now(), null)`,
|
|
70
|
+
[tenantWithEncryption],
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
// Tenant without any active map (encryption disabled) must stay untouched.
|
|
74
|
+
await client.query(
|
|
75
|
+
`insert into ${tableName}
|
|
76
|
+
(id, entity_id, tenant_id, organization_id, fields_json, is_active, created_at, updated_at, deleted_at)
|
|
77
|
+
values
|
|
78
|
+
('10000000-0000-0000-0000-000000000003', 'auth:user', $1, null,
|
|
79
|
+
'[{"field":"email"}]'::jsonb, false, now(), now(), null)`,
|
|
80
|
+
[tenantWithoutEncryption],
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
await runMigrationAgainst(client, tableName)
|
|
84
|
+
|
|
85
|
+
const linkTemplate = await client.query(
|
|
86
|
+
`select fields_json from ${tableName} where entity_id = 'checkout:checkout_link_template' and tenant_id = $1 and deleted_at is null`,
|
|
87
|
+
[tenantWithEncryption],
|
|
88
|
+
)
|
|
89
|
+
expect(linkTemplate.rows).toHaveLength(1)
|
|
90
|
+
expect(linkTemplate.rows[0].fields_json).toEqual([{ field: 'gateway_settings' }])
|
|
91
|
+
|
|
92
|
+
const link = await client.query(
|
|
93
|
+
`select fields_json from ${tableName} where entity_id = 'checkout:checkout_link' and tenant_id = $1 and deleted_at is null`,
|
|
94
|
+
[tenantWithEncryption],
|
|
95
|
+
)
|
|
96
|
+
expect(link.rows).toHaveLength(1)
|
|
97
|
+
expect(link.rows[0].fields_json).toEqual([{ field: 'gateway_settings' }])
|
|
98
|
+
|
|
99
|
+
const transaction = await client.query(
|
|
100
|
+
`select fields_json from ${tableName} where entity_id = 'checkout:checkout_transaction' and tenant_id = $1 and deleted_at is null`,
|
|
101
|
+
[tenantWithEncryption],
|
|
102
|
+
)
|
|
103
|
+
expect(transaction.rows).toHaveLength(1)
|
|
104
|
+
const transactionFields = (transaction.rows[0].fields_json as Array<{ field: string }>).map((rule) => rule.field)
|
|
105
|
+
expect(transactionFields).toEqual(
|
|
106
|
+
expect.arrayContaining([
|
|
107
|
+
'customer_data',
|
|
108
|
+
'admin_custom_field',
|
|
109
|
+
'first_name',
|
|
110
|
+
'last_name',
|
|
111
|
+
'email',
|
|
112
|
+
'phone',
|
|
113
|
+
'accepted_legal_consents',
|
|
114
|
+
'ip_address',
|
|
115
|
+
]),
|
|
116
|
+
)
|
|
117
|
+
expect(transactionFields).toHaveLength(8)
|
|
118
|
+
|
|
119
|
+
const disabledTenantRows = await client.query(
|
|
120
|
+
`select entity_id from ${tableName} where tenant_id = $1 and entity_id like 'checkout:%'`,
|
|
121
|
+
[tenantWithoutEncryption],
|
|
122
|
+
)
|
|
123
|
+
expect(disabledTenantRows.rows).toHaveLength(0)
|
|
124
|
+
|
|
125
|
+
// Re-running must be a no-op: no duplicate rows, no duplicate field entries.
|
|
126
|
+
await runMigrationAgainst(client, tableName)
|
|
127
|
+
|
|
128
|
+
const linkTemplateAfterRerun = await client.query(
|
|
129
|
+
`select fields_json from ${tableName} where entity_id = 'checkout:checkout_link_template' and tenant_id = $1 and deleted_at is null`,
|
|
130
|
+
[tenantWithEncryption],
|
|
131
|
+
)
|
|
132
|
+
expect(linkTemplateAfterRerun.rows).toHaveLength(1)
|
|
133
|
+
|
|
134
|
+
const transactionAfterRerun = await client.query(
|
|
135
|
+
`select fields_json from ${tableName} where entity_id = 'checkout:checkout_transaction' and tenant_id = $1 and deleted_at is null`,
|
|
136
|
+
[tenantWithEncryption],
|
|
137
|
+
)
|
|
138
|
+
expect(transactionAfterRerun.rows).toHaveLength(1)
|
|
139
|
+
expect((transactionAfterRerun.rows[0].fields_json as Array<{ field: string }>)).toHaveLength(8)
|
|
140
|
+
})
|
|
141
|
+
})
|
|
142
|
+
})
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Migration } from '@mikro-orm/migrations';
|
|
2
|
+
import { buildEncryptionMapBackfillSql } from '@open-mercato/shared/lib/encryption/migration-backfill';
|
|
3
|
+
|
|
4
|
+
// Backfill the checkout module's encryption maps for every pre-existing (tenant, org) scope that
|
|
5
|
+
// already has active encryption maps. `defaultEncryptionMaps` in `../encryption.ts` only reaches
|
|
6
|
+
// tenants created after this change (`auth:setup`) — a tenant that predates the checkout module has
|
|
7
|
+
// no map for any of its three entities, `encryptEntityPayload` no-ops, and checkout_transaction PII
|
|
8
|
+
// (customer_data, first/last name, email, phone, accepted_legal_consents, ip_address) plus the
|
|
9
|
+
// checkout_link/checkout_link_template gateway_settings are written as PLAINTEXT. Entity ids and
|
|
10
|
+
// fields are written literally (not imported from encryption.ts) so this migration keeps meaning the
|
|
11
|
+
// same thing even if the module's declared maps change later. `down()` stays empty: deleting by
|
|
12
|
+
// entity_id would also remove maps seeded at tenant creation or edited by admins in the UI and
|
|
13
|
+
// silently switch encryption off for that entity.
|
|
14
|
+
export class Migration20261109120000 extends Migration {
|
|
15
|
+
|
|
16
|
+
override async up(): Promise<void> {
|
|
17
|
+
this.addSql(buildEncryptionMapBackfillSql({
|
|
18
|
+
entityId: 'checkout:checkout_link_template',
|
|
19
|
+
fields: [{ field: 'gateway_settings' }],
|
|
20
|
+
}));
|
|
21
|
+
|
|
22
|
+
this.addSql(buildEncryptionMapBackfillSql({
|
|
23
|
+
entityId: 'checkout:checkout_link',
|
|
24
|
+
fields: [{ field: 'gateway_settings' }],
|
|
25
|
+
}));
|
|
26
|
+
|
|
27
|
+
this.addSql(buildEncryptionMapBackfillSql({
|
|
28
|
+
entityId: 'checkout:checkout_transaction',
|
|
29
|
+
fields: [
|
|
30
|
+
{ field: 'customer_data' },
|
|
31
|
+
{ field: 'first_name' },
|
|
32
|
+
{ field: 'last_name' },
|
|
33
|
+
{ field: 'email' },
|
|
34
|
+
{ field: 'phone' },
|
|
35
|
+
{ field: 'accepted_legal_consents' },
|
|
36
|
+
{ field: 'ip_address' },
|
|
37
|
+
],
|
|
38
|
+
}));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
override async down(): Promise<void> {}
|
|
42
|
+
|
|
43
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { Migration20261109120000 } from '../Migration20261109120000'
|
|
2
|
+
|
|
3
|
+
function createMigration(): Migration20261109120000 {
|
|
4
|
+
return new Migration20261109120000(undefined as any, undefined as any)
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
// Mirrors exactly what the migration itself hardcodes. Written literally (not
|
|
8
|
+
// imported from `../../encryption`) so this test keeps asserting the
|
|
9
|
+
// migration's own frozen contract even if the module's declared maps change
|
|
10
|
+
// later — see the migration's file-level comment for why.
|
|
11
|
+
const EXPECTED_MAPS = [
|
|
12
|
+
{
|
|
13
|
+
entityId: 'checkout:checkout_link_template',
|
|
14
|
+
fields: ['gateway_settings'],
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
entityId: 'checkout:checkout_link',
|
|
18
|
+
fields: ['gateway_settings'],
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
entityId: 'checkout:checkout_transaction',
|
|
22
|
+
fields: [
|
|
23
|
+
'customer_data',
|
|
24
|
+
'first_name',
|
|
25
|
+
'last_name',
|
|
26
|
+
'email',
|
|
27
|
+
'phone',
|
|
28
|
+
'accepted_legal_consents',
|
|
29
|
+
'ip_address',
|
|
30
|
+
],
|
|
31
|
+
},
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
describe('Migration20261109120000 (checkout encryption map backfill)', () => {
|
|
35
|
+
it('backfills every entity/field hardcoded in the migration', async () => {
|
|
36
|
+
const migration = createMigration()
|
|
37
|
+
await migration.up()
|
|
38
|
+
const sql = migration.getQueries().join('\n')
|
|
39
|
+
|
|
40
|
+
for (const map of EXPECTED_MAPS) {
|
|
41
|
+
expect(sql).toContain(`'${map.entityId}'`)
|
|
42
|
+
for (const field of map.fields) {
|
|
43
|
+
expect(sql).toContain(`"field":"${field}"`)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('guards the insert against pre-existing rows and a missing encryption_maps table', async () => {
|
|
49
|
+
const migration = createMigration()
|
|
50
|
+
await migration.up()
|
|
51
|
+
const sql = migration.getQueries().join('\n')
|
|
52
|
+
|
|
53
|
+
expect(sql).toContain("to_regclass('encryption_maps') is null")
|
|
54
|
+
expect(sql).toContain('not exists')
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it('leaves down() empty so existing admin-edited or tenant-seeded maps are never deleted', async () => {
|
|
58
|
+
const migration = createMigration()
|
|
59
|
+
await migration.down()
|
|
60
|
+
|
|
61
|
+
expect(migration.getQueries()).toEqual([])
|
|
62
|
+
})
|
|
63
|
+
})
|