@teleporthq/teleport-plugin-next-workflows 0.44.4 → 0.44.5
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/__tests__/account-delete-owned-rows.test.ts +488 -0
- package/dist/cjs/account-delete-route-generator.d.ts +9 -6
- package/dist/cjs/account-delete-route-generator.d.ts.map +1 -1
- package/dist/cjs/account-delete-route-generator.js +131 -1
- package/dist/cjs/account-delete-route-generator.js.map +1 -1
- package/dist/cjs/tsconfig.tsbuildinfo +1 -1
- package/dist/esm/account-delete-route-generator.d.ts +9 -6
- package/dist/esm/account-delete-route-generator.d.ts.map +1 -1
- package/dist/esm/account-delete-route-generator.js +131 -1
- package/dist/esm/account-delete-route-generator.js.map +1 -1
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/src/account-delete-route-generator.ts +140 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@teleporthq/teleport-plugin-next-workflows",
|
|
3
|
-
"version": "0.44.
|
|
3
|
+
"version": "0.44.5",
|
|
4
4
|
"description": "A plugin for workflow code generation in Next.js projects.",
|
|
5
5
|
"author": "teleportHQ",
|
|
6
6
|
"license": "MIT",
|
|
@@ -66,8 +66,8 @@
|
|
|
66
66
|
},
|
|
67
67
|
"dependencies": {
|
|
68
68
|
"@babel/types": "^7.5.5",
|
|
69
|
-
"@teleporthq/teleport-shared": "^0.44.
|
|
69
|
+
"@teleporthq/teleport-shared": "^0.44.5",
|
|
70
70
|
"@teleporthq/teleport-types": "^0.44.0"
|
|
71
71
|
},
|
|
72
|
-
"gitHead": "
|
|
72
|
+
"gitHead": "f58fa8b4c3e69d550a3fc13f85245f61789d7d92"
|
|
73
73
|
}
|
|
@@ -6,17 +6,21 @@
|
|
|
6
6
|
* 1. resolves the current user from the NextAuth session (never trusting a
|
|
7
7
|
* client-supplied id),
|
|
8
8
|
* 2. runs a single DB transaction that detaches + anonymises business/legal
|
|
9
|
-
* records (orders, withdrawal requests) so tracking survives,
|
|
10
|
-
* user's personal data (reviews, cart, favourites, chat
|
|
11
|
-
* user
|
|
9
|
+
* records (orders, withdrawal requests, bookings) so tracking survives,
|
|
10
|
+
* deletes the user's personal data (reviews, cart, favourites, chat
|
|
11
|
+
* history), deletes the rows the user owns in the project's own tables —
|
|
12
|
+
* the ones the AI made for the site, each through the one column the
|
|
13
|
+
* site's "My …" pages read it by (teleport-shared's resolveOwnerColumn,
|
|
14
|
+
* shipped into the route as text) — and then the user row, and
|
|
12
15
|
* 3. best-effort sends the farewell email (a failed send never fails the
|
|
13
16
|
* deletion).
|
|
14
17
|
*
|
|
15
|
-
* Every table statement is guarded by a runtime `information_schema`
|
|
16
|
-
* check, so the route is a no-op for feature tables a given project
|
|
17
|
-
* provisioned.
|
|
18
|
+
* Every platform-table statement is guarded by a runtime `information_schema`
|
|
19
|
+
* column check, so the route is a no-op for feature tables a given project
|
|
20
|
+
* never provisioned.
|
|
18
21
|
*/
|
|
19
22
|
import { UIDLLocalizedEmailTemplates } from '@teleporthq/teleport-types'
|
|
23
|
+
import { OwnerColumns } from '@teleporthq/teleport-shared'
|
|
20
24
|
import { generatePgClientCode } from './pg-client-code'
|
|
21
25
|
import { generateCommonJsSessionTokenResolverCode } from './session-cookie-resolver'
|
|
22
26
|
import {
|
|
@@ -84,11 +88,22 @@ const DELETE_TABLES = [
|
|
|
84
88
|
'teleport_cart',
|
|
85
89
|
'teleport_ai_chat_messages',
|
|
86
90
|
'teleport_ai_chat_conversations',
|
|
91
|
+
// A push subscription is the capability to notify the user's device.
|
|
92
|
+
'teleport_push_subscriptions',
|
|
87
93
|
];
|
|
88
94
|
// Business / legal records that must survive: the user link is nulled and any
|
|
89
95
|
// stored personal email is replaced with a synthetic, self-describing address.
|
|
90
|
-
const ANONYMIZE_TABLES = [
|
|
96
|
+
const ANONYMIZE_TABLES = [
|
|
97
|
+
'teleport_orders',
|
|
98
|
+
'teleport_withdrawal_requests',
|
|
99
|
+
'teleport_bookings',
|
|
100
|
+
'teleport_subscriptions',
|
|
101
|
+
'teleport_abandoned_carts',
|
|
102
|
+
'teleport_voucher_redemptions',
|
|
103
|
+
];
|
|
91
104
|
const EMAIL_COLUMNS = ['billing_email', 'shipping_email', 'customer_email', 'email'];
|
|
105
|
+
// A booking also names the customer and carries their phone; both are cleared.
|
|
106
|
+
const CLEARED_COLUMNS = { teleport_bookings: ['customer_name', 'customer_phone'] };
|
|
92
107
|
|
|
93
108
|
async function tableColumns(client, table) {
|
|
94
109
|
var r = await client.query(
|
|
@@ -111,6 +126,10 @@ async function anonymizeUserRows(client, table, userId, syntheticEmail) {
|
|
|
111
126
|
sets.push(EMAIL_COLUMNS[i] + ' = $' + params.length);
|
|
112
127
|
}
|
|
113
128
|
}
|
|
129
|
+
var cleared = CLEARED_COLUMNS[table] || [];
|
|
130
|
+
for (var c = 0; c < cleared.length; c++) {
|
|
131
|
+
if (cols.indexOf(cleared[c]) !== -1) { sets.push(cleared[c] + ' = NULL'); }
|
|
132
|
+
}
|
|
114
133
|
await client.query('UPDATE ' + table + ' SET ' + sets.join(', ') + ' WHERE user_id = $1', params);
|
|
115
134
|
}
|
|
116
135
|
|
|
@@ -127,6 +146,119 @@ async function deleteRowsByEmail(client, table, column, email) {
|
|
|
127
146
|
await client.query('DELETE FROM ' + table + ' WHERE ' + column + ' = $1', [email]);
|
|
128
147
|
}
|
|
129
148
|
|
|
149
|
+
// Rows the user owns in the project's OWN tables — the ones the AI made for
|
|
150
|
+
// this site (saved items, requests, reviews …). Their link to the user is
|
|
151
|
+
// ON DELETE SET NULL, so they outlived the account with a blank owner, and the
|
|
152
|
+
// "My …" pages read "owner = me OR owner IS NULL": every other signed-in
|
|
153
|
+
// visitor saw them. Platform tables keep the explicit handling above —
|
|
154
|
+
// deleted, anonymised, or kept as the business's records.
|
|
155
|
+
//
|
|
156
|
+
// Each table is attributed through exactly ONE column, the one its "My …"
|
|
157
|
+
// pages read it by: resolveOwnerColumn below is the AI pipeline's own
|
|
158
|
+
// decision, shipped here as text, so the two sides cannot disagree. Rows go
|
|
159
|
+
// only through an "owner" column (whose the row is: author, submitter,
|
|
160
|
+
// sender); an "actor" column (who approved, reviewed, was assigned or
|
|
161
|
+
// addressed) names somebody else's row and leaves the table alone.
|
|
162
|
+
${OwnerColumns.generateOwnerColumnResolverCode()}
|
|
163
|
+
|
|
164
|
+
const PLATFORM_TABLE_PREFIX = 'teleport_';
|
|
165
|
+
const PLATFORM_TABLES = [AUTH_USERS_TABLE, 'password_reset_tokens'];
|
|
166
|
+
|
|
167
|
+
function isPlatformTable(table) {
|
|
168
|
+
return PLATFORM_TABLES.indexOf(table) !== -1 || table.indexOf(PLATFORM_TABLE_PREFIX) === 0;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function quoteIdentifier(name) {
|
|
172
|
+
return '"' + String(name).split('"').join('""') + '"';
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// The (table, column) pairs through which the project's own tables name their
|
|
176
|
+
// owner: the resolved owner column when it references the users table — or
|
|
177
|
+
// references nothing, in older projects whose link was dropped. One that
|
|
178
|
+
// references another table (account_id -> accounts) is a domain link and the
|
|
179
|
+
// table is left alone. Links are read from pg_constraint rather than
|
|
180
|
+
// information_schema: constraint names are only unique per table, so a join
|
|
181
|
+
// by name can pair the wrong tables.
|
|
182
|
+
async function ownedRowLinks(client) {
|
|
183
|
+
var columns = await client.query(
|
|
184
|
+
"SELECT c.table_name, c.column_name, c.ordinal_position FROM information_schema.columns c" +
|
|
185
|
+
" JOIN information_schema.tables t ON t.table_schema = c.table_schema AND t.table_name = c.table_name" +
|
|
186
|
+
" WHERE c.table_schema = 'public' AND t.table_type = 'BASE TABLE'" +
|
|
187
|
+
" ORDER BY c.table_name, c.ordinal_position"
|
|
188
|
+
);
|
|
189
|
+
var links = await client.query(
|
|
190
|
+
"SELECT cl.relname AS table_name, a.attname AS column_name," +
|
|
191
|
+
" rcl.relname AS referenced_table, ra.attname AS referenced_column" +
|
|
192
|
+
" FROM pg_constraint k" +
|
|
193
|
+
" JOIN pg_class cl ON cl.oid = k.conrelid" +
|
|
194
|
+
" JOIN pg_namespace n ON n.oid = cl.relnamespace" +
|
|
195
|
+
" JOIN pg_attribute a ON a.attrelid = k.conrelid AND a.attnum = k.conkey[1]" +
|
|
196
|
+
" JOIN pg_class rcl ON rcl.oid = k.confrelid" +
|
|
197
|
+
" JOIN pg_attribute ra ON ra.attrelid = k.confrelid AND ra.attnum = k.confkey[1]" +
|
|
198
|
+
" WHERE k.contype = 'f' AND n.nspname = 'public' AND array_length(k.conkey, 1) = 1"
|
|
199
|
+
);
|
|
200
|
+
var referenced = {};
|
|
201
|
+
for (var i = 0; i < links.rows.length; i++) {
|
|
202
|
+
var link = links.rows[i];
|
|
203
|
+
referenced[link.table_name + '.' + link.column_name] = link.referenced_table + '.' + link.referenced_column;
|
|
204
|
+
}
|
|
205
|
+
var tables = [];
|
|
206
|
+
var columnsOf = {};
|
|
207
|
+
for (var j = 0; j < columns.rows.length; j++) {
|
|
208
|
+
var row = columns.rows[j];
|
|
209
|
+
if (!columnsOf[row.table_name]) {
|
|
210
|
+
columnsOf[row.table_name] = [];
|
|
211
|
+
tables.push(row.table_name);
|
|
212
|
+
}
|
|
213
|
+
columnsOf[row.table_name].push({ name: row.column_name, ordinalPosition: Number(row.ordinal_position) });
|
|
214
|
+
}
|
|
215
|
+
var owned = [];
|
|
216
|
+
for (var t = 0; t < tables.length; t++) {
|
|
217
|
+
var table = tables[t];
|
|
218
|
+
if (isPlatformTable(table)) { continue; }
|
|
219
|
+
var resolved = resolveOwnerColumn(columnsOf[table]);
|
|
220
|
+
if (!resolved || resolved.kind !== 'owner') { continue; }
|
|
221
|
+
var target = referenced[table + '.' + resolved.column];
|
|
222
|
+
if (target && target !== AUTH_USERS_TABLE + '.id') { continue; }
|
|
223
|
+
owned.push({ table: table, column: resolved.column });
|
|
224
|
+
}
|
|
225
|
+
return owned;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// Each table is deleted from under its own savepoint, so one that refuses
|
|
229
|
+
// (another user's row still points at this user's, through a link without
|
|
230
|
+
// ON DELETE CASCADE) stops neither the other tables nor the account from being
|
|
231
|
+
// deleted. A refused table is retried once the rest are done — the blocker may
|
|
232
|
+
// have been this user's own row in another table; what is still refused stays
|
|
233
|
+
// as it always did, and the log names it.
|
|
234
|
+
async function deleteOwnedRows(client, userId) {
|
|
235
|
+
var pending = await ownedRowLinks(client);
|
|
236
|
+
while (pending.length > 0) {
|
|
237
|
+
var refused = [];
|
|
238
|
+
for (var i = 0; i < pending.length; i++) {
|
|
239
|
+
var link = pending[i];
|
|
240
|
+
await client.query('SAVEPOINT owned_rows');
|
|
241
|
+
try {
|
|
242
|
+
await client.query(
|
|
243
|
+
'DELETE FROM ' + quoteIdentifier(link.table) + ' WHERE ' + quoteIdentifier(link.column) + ' = $1',
|
|
244
|
+
[userId]
|
|
245
|
+
);
|
|
246
|
+
await client.query('RELEASE SAVEPOINT owned_rows');
|
|
247
|
+
} catch (err) {
|
|
248
|
+
await client.query('ROLLBACK TO SAVEPOINT owned_rows');
|
|
249
|
+
await client.query('RELEASE SAVEPOINT owned_rows');
|
|
250
|
+
link.error = err && err.message ? err.message : String(err);
|
|
251
|
+
refused.push(link);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
if (refused.length === pending.length) { break; }
|
|
255
|
+
pending = refused;
|
|
256
|
+
}
|
|
257
|
+
for (var k = 0; k < pending.length; k++) {
|
|
258
|
+
console.error('[account-delete] rows kept in ' + pending[k].table + ' (' + pending[k].column + '): ' + pending[k].error);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
130
262
|
// Invoices are a financial/tax record that must survive, but they store the
|
|
131
263
|
// customer's PII denormalised and link to the user only through order_id (no
|
|
132
264
|
// user_id). Scrub the PII in place, matching invoices whose order belongs to the
|
|
@@ -246,6 +378,7 @@ module.exports = async function handler(req, res) {
|
|
|
246
378
|
for (var d = 0; d < DELETE_TABLES.length; d++) {
|
|
247
379
|
await deleteUserRows(client, DELETE_TABLES[d], userId);
|
|
248
380
|
}
|
|
381
|
+
await deleteOwnedRows(client, userId);
|
|
249
382
|
// password_reset_tokens has no user_id — it is keyed by the email string.
|
|
250
383
|
await deleteRowsByEmail(client, 'password_reset_tokens', 'email', userEmail);
|
|
251
384
|
await client.query('DELETE FROM ' + AUTH_USERS_TABLE + ' WHERE id = $1', [userId]);
|