|
| 1 | +#!/usr/bin/env node -r esbuild-register |
| 2 | + |
| 3 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 | + |
| 7 | +/** |
| 8 | + * A script to start the inactive account deletion process. It should (per |
| 9 | + * current rqeuirements), for every inactive account: |
| 10 | + * - send a pre-deletion event to relevant RPs of the account |
| 11 | + * - enqueue a cloud task to send the first notification email |
| 12 | + * |
| 13 | + * This script relies on the same set of enivronment variables as the FxA auth |
| 14 | + * server. |
| 15 | + */ |
| 16 | + |
| 17 | +import { Command } from 'commander'; |
| 18 | +import { StatsD } from 'hot-shots'; |
| 19 | +import { Container } from 'typedi'; |
| 20 | + |
| 21 | +import { parseDryRun } from '../lib/args'; |
| 22 | +import { AppConfig, AuthFirestore, AuthLogger } from '../../lib/types'; |
| 23 | +import appConfig from '../../config'; |
| 24 | +import initLog from '../../lib/log'; |
| 25 | +import initRedis from '../../lib/redis'; |
| 26 | +import Token from '../../lib/tokens'; |
| 27 | +import * as random from '../../lib/crypto/random'; |
| 28 | +import { createDB } from '../../lib/db'; |
| 29 | +import { setupFirestore } from '../../lib/firestore-db'; |
| 30 | +import { CurrencyHelper } from '../../lib/payments/currencies'; |
| 31 | +import { createStripeHelper, StripeHelper } from '../../lib/payments/stripe'; |
| 32 | +import oauthDb from '../../lib/oauth/db'; |
| 33 | +import { PlayBilling } from '../../lib/payments/iap/google-play'; |
| 34 | +import { PlaySubscriptions } from '../../lib/payments/iap/google-play/subscriptions'; |
| 35 | +import { AppleIAP } from '../../lib/payments/iap/apple-app-store/apple-iap'; |
| 36 | +import { AppStoreSubscriptions } from '../../lib/payments/iap/apple-app-store/subscriptions'; |
| 37 | + |
| 38 | +import { |
| 39 | + accountWhereAndOrderByQueryBuilder, |
| 40 | + hasAccessToken, |
| 41 | + hasActiveRefreshToken, |
| 42 | + hasActiveSessionToken, |
| 43 | + IsActiveFnBuilder, |
| 44 | + setDateToUTC, |
| 45 | +} from './lib'; |
| 46 | + |
| 47 | +const defaultResultsLImit = 500000; |
| 48 | +const defaultInactiveByDate = () => { |
| 49 | + const inactiveBy = new Date(); |
| 50 | + inactiveBy.setFullYear(inactiveBy.getFullYear() - 2); |
| 51 | + return inactiveBy; |
| 52 | +}; |
| 53 | + |
| 54 | +const init = async () => { |
| 55 | + const program = new Command(); |
| 56 | + program |
| 57 | + .description( |
| 58 | + 'Starts the inactive account deletion process by enqueuing the first email\n' + |
| 59 | + 'notification for inactive accounts. This script allows segmenting the\n' + |
| 60 | + 'accounts to search by account creation date. It also optionally accepts a\n' + |
| 61 | + 'date at or after when an account is active in order to be excluded.\n\n' + |
| 62 | + 'For example, to start the inactive deletion process on accounts created\n' + |
| 63 | + 'between 2015-01-01 and 2015-01-31 where the account is not active after\n' + |
| 64 | + '2024-10-31:\n' + |
| 65 | + ' enqueue-inactive-account-deletions.ts \\\n' + |
| 66 | + ' --start-date 2015-01-01 \\\n' + |
| 67 | + ' --end-date 2015-12-31 \\\n' + |
| 68 | + ' --active-by-date 2024-10-31' |
| 69 | + ) |
| 70 | + .option( |
| 71 | + '--dry-run [true|false]', |
| 72 | + 'Print out the argument and configuration values that will be used in the execution of the script. Defaults to true.', |
| 73 | + true |
| 74 | + ) |
| 75 | + .option( |
| 76 | + '--active-by-date [date]', |
| 77 | + 'An account is considered active if it has any activity at or after this date. Optional. Defaults to two years ago from script execution time.', |
| 78 | + Date.parse |
| 79 | + ) |
| 80 | + .option( |
| 81 | + '--start-date [date]', |
| 82 | + 'Start of date range of account creation date, inclusive. Optional. Defaults to 2012-03-12.', |
| 83 | + Date.parse, |
| 84 | + '2012-03-12' |
| 85 | + ) |
| 86 | + .option( |
| 87 | + '--end-date [date]', |
| 88 | + 'End of date range of account creation date, inclusive.', |
| 89 | + Date.parse |
| 90 | + ) |
| 91 | + .option( |
| 92 | + '--results-limit [number]', |
| 93 | + 'The number of results per accounts DB query. Defaults to 500000.', |
| 94 | + parseInt, |
| 95 | + defaultResultsLImit |
| 96 | + ); |
| 97 | + // @TODO add testing related parameters, such as UID(s), time between certain actions, etc. |
| 98 | + |
| 99 | + program.parse(process.argv); |
| 100 | + |
| 101 | + const isDryRun = parseDryRun(program.dryRun); |
| 102 | + const startDate = setDateToUTC(program.startDate); |
| 103 | + const endDate = setDateToUTC(program.endDate); |
| 104 | + const activeByDate = program.activeByDate |
| 105 | + ? setDateToUTC(program.activeByDate) |
| 106 | + : defaultInactiveByDate(); |
| 107 | + const startDateTimestamp = startDate.valueOf(); |
| 108 | + const endDateTimestamp = endDate.valueOf() + 86400000; // next day for < comparisons |
| 109 | + const activeByDateTimestamp = activeByDate.valueOf(); |
| 110 | + |
| 111 | + const config = appConfig.getProperties(); |
| 112 | + const log = initLog({ |
| 113 | + ...config.log, |
| 114 | + }); |
| 115 | + const statsd = new StatsD({ ...config.statsd }); |
| 116 | + const redis = initRedis( |
| 117 | + { ...config.redis, ...config.redis.sessionTokens }, |
| 118 | + log |
| 119 | + ); |
| 120 | + const db = createDB( |
| 121 | + config, |
| 122 | + log, |
| 123 | + Token(log, config), |
| 124 | + random.base32(config.signinUnblock.codeLength) |
| 125 | + ); |
| 126 | + const fxaDb = await db.connect(config, redis); |
| 127 | + |
| 128 | + Container.set(AppConfig, config); |
| 129 | + Container.set(AuthLogger, log); |
| 130 | + |
| 131 | + const authFirestore = setupFirestore(config); |
| 132 | + Container.set(AuthFirestore, authFirestore); |
| 133 | + const currencyHelper = new CurrencyHelper(config); |
| 134 | + Container.set(CurrencyHelper, currencyHelper); |
| 135 | + const stripeHelper = createStripeHelper(log, config, statsd); |
| 136 | + Container.set(StripeHelper, stripeHelper); |
| 137 | + const playBilling = Container.get(PlayBilling); |
| 138 | + const playSubscriptions = Container.get(PlaySubscriptions); |
| 139 | + const appleIap = Container.get(AppleIAP); |
| 140 | + const appStoreSubscriptions = Container.get(AppStoreSubscriptions); |
| 141 | + |
| 142 | + if (isDryRun) { |
| 143 | + console.log( |
| 144 | + 'Dry run mode is on. It is the default; use --dry-run=false when you are ready.' |
| 145 | + ); |
| 146 | + console.log('Per DB query results limit: ', program.resultsLimit); |
| 147 | + // @TODO add more dry-run output |
| 148 | + return 0; |
| 149 | + } |
| 150 | + |
| 151 | + const accountQueryBuilder = () => |
| 152 | + accountWhereAndOrderByQueryBuilder( |
| 153 | + startDateTimestamp, |
| 154 | + endDateTimestamp, |
| 155 | + activeByDateTimestamp |
| 156 | + ) |
| 157 | + .select('accounts.uid') |
| 158 | + .limit(program.resultsLimit); |
| 159 | + |
| 160 | + const sessionTokensFn = fxaDb.sessions.bind(fxaDb); |
| 161 | + const refreshTokensFn = oauthDb.getRefreshTokensByUid.bind(oauthDb); |
| 162 | + const accessTokensFn = oauthDb.getAccessTokensByUid.bind(oauthDb); |
| 163 | + |
| 164 | + const checkActiveSessionToken = async (uid: string) => |
| 165 | + await hasActiveSessionToken(sessionTokensFn, uid, activeByDateTimestamp); |
| 166 | + const checkRefreshToken = async (uid: string) => |
| 167 | + await hasActiveRefreshToken(refreshTokensFn, uid, activeByDateTimestamp); |
| 168 | + const checkAccessToken = async (uid: string) => |
| 169 | + await hasAccessToken(accessTokensFn, uid); |
| 170 | + |
| 171 | + const iapSubUids = new Set<string>(); |
| 172 | + const playSubscriptionsCollection = await playBilling.purchaseDbRef().get(); |
| 173 | + const appleSubscriptionsCollection = await appleIap.purchasesDbRef().get(); |
| 174 | + ((collections) => { |
| 175 | + for (const c of collections) { |
| 176 | + for (const purchaseRecordSnapshot of c.docs) { |
| 177 | + const x = purchaseRecordSnapshot.data(); |
| 178 | + if (x.userId) { |
| 179 | + iapSubUids.add(x.userId); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + })([playSubscriptionsCollection, appleSubscriptionsCollection]); |
| 184 | + |
| 185 | + const hasIapSubscription = async (uid: string) => |
| 186 | + iapSubUids.has(uid) && |
| 187 | + ((await playSubscriptions.getSubscriptions(uid)).length > 0 || |
| 188 | + (await appStoreSubscriptions.getSubscriptions(uid)).length > 0); |
| 189 | + |
| 190 | + const isActive = new IsActiveFnBuilder() |
| 191 | + .setActiveSessionTokenFn(checkActiveSessionToken) |
| 192 | + .setRefreshTokenFn(checkRefreshToken) |
| 193 | + .setAccessTokenFn(checkAccessToken) |
| 194 | + .setIapSubscriptionFn(hasIapSubscription) |
| 195 | + .build(); |
| 196 | + |
| 197 | + let hasMaxResultsCount = true; |
| 198 | + let totalRowsReturned = 0; |
| 199 | + let totalInactiveAccounts = 0; |
| 200 | + |
| 201 | + while (hasMaxResultsCount) { |
| 202 | + const accountsQuery = accountQueryBuilder(); |
| 203 | + accountsQuery.offset(totalRowsReturned); |
| 204 | + |
| 205 | + const accounts = await accountsQuery; |
| 206 | + |
| 207 | + if (!accounts.length) { |
| 208 | + hasMaxResultsCount = false; |
| 209 | + break; |
| 210 | + } |
| 211 | + |
| 212 | + for (const accountRecord of accounts) { |
| 213 | + if (!(await isActive(accountRecord.uid))) { |
| 214 | + // @TODO add concurrency and rate limiting |
| 215 | + // @TODO enqueue first email notification |
| 216 | + |
| 217 | + totalInactiveAccounts++; |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + hasMaxResultsCount = accounts.length === program.resultsLimit; |
| 222 | + totalRowsReturned += accounts.length; |
| 223 | + } |
| 224 | + |
| 225 | + console.log(`Total accounts processed: ${totalRowsReturned}`); |
| 226 | + console.log(`Number of inactive accounts: ${totalInactiveAccounts}`); |
| 227 | + |
| 228 | + return 0; |
| 229 | +}; |
| 230 | + |
| 231 | +if (require.main === module) { |
| 232 | + init() |
| 233 | + .catch((err: Error) => { |
| 234 | + console.error(err); |
| 235 | + process.exit(1); |
| 236 | + }) |
| 237 | + .then((exitCode: number) => process.exit(exitCode)); |
| 238 | +} |
0 commit comments