Skip to content

Commit

Permalink
fix(scripts): improve inactive account uids script
Browse files Browse the repository at this point in the history
Because:
 - there were a few small issues with the inactive account uids script

This commit:
 - stops opening an output file in dry run mode
 - fixes an import so that the tsc'd js won't error
 - eliminates the accounts with Stripe subscriptions in the initial
   query
  • Loading branch information
chenba committed Oct 31, 2024
1 parent 855dd3e commit 0b70ccc
Showing 1 changed file with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ import { createDB } from '../../lib/db';
import { setupFirestore } from '../../lib/firestore-db';
import { CurrencyHelper } from '../../lib/payments/currencies';
import { createStripeHelper, StripeHelper } from '../../lib/payments/stripe';
import * as oauthDb from '../../lib/oauth/db';
import oauthDb from '../../lib/oauth/db';
import {
Account,
AccountCustomers,
Email,
SecurityEvent,
SessionToken,
} from 'fxa-shared/db/models/auth';
import { EVENT_NAMES } from 'fxa-shared/db/models/auth/security-event';
import { getAccountCustomerByUid } from 'fxa-shared/db/models/auth';
import { PlayBilling } from '../../lib/payments/iap/google-play';
import { PlaySubscriptions } from '../../lib/payments/iap/google-play/subscriptions';
import { AppleIAP } from '../../lib/payments/iap/apple-app-store/apple-iap';
Expand Down Expand Up @@ -108,8 +108,6 @@ const init = async () => {
).valueOf();
const filepath = program.outputPath || createFilepath(endDate);

const fd = fs.openSync(filepath, 'a');

const config = appConfig.getProperties();
const log = initLog({
...config.log,
Expand Down Expand Up @@ -161,19 +159,25 @@ const init = async () => {
])
.as('securityEventUids');

const accountCustomerUids = AccountCustomers.query()
.distinct('uid')
.as('accountCustomerUids');

const accountWhereAndOrderBy = () =>
Account.query()
.leftJoin(emailUids, 'emailUids.uid', 'accounts.uid')
.leftJoin(sessionTokenUids, 'sessionTokenUids.uid', 'accounts.uid')
.leftJoin(securityEventUids, 'securityEventUids.uid', 'accounts.uid')
.leftJoin(accountCustomerUids, 'accountCustomerUids.uid', 'accounts.uid')
.where('accounts.emailVerified', 1)
.where('accounts.createdAt', '>=', startDateTimestamp)
.where('accounts.createdAt', '<', endDateTimestamp)
.where((builder) => {
builder
.whereNull('emailUids.uid')
.whereNull('sessionTokenUids.uid')
.whereNull('securityEventUids.uid');
.whereNull('securityEventUids.uid')
.whereNull('accountCustomerUids.uid');
})
.orderBy('accounts.createdAt', 'asc')
.orderBy('accounts.uid', 'asc');
Expand Down Expand Up @@ -202,22 +206,17 @@ const init = async () => {
const accessTokens = await oauthDb.getAccessTokensByUid(accountRecord.uid);
return accessTokens.length > 0;
};
const isStripeCustomer = async (accountRecord: Account) =>
!!(await getAccountCustomerByUid(accountRecord.uid));
const hasIapSubscription = async (accountRecord: Account) =>
(await playSubscriptions.getSubscriptions(accountRecord.uid)).length > 0 ||
(await appStoreSubscriptions.getSubscriptions(accountRecord.uid)).length >
0;
const hasSubscription = async (accountRecord: Account) =>
(await isStripeCustomer(accountRecord)) ||
(await hasIapSubscription(accountRecord));

const isActive = async (accountRecord: Account) => {
return (
(await hasActiveSessionToken(accountRecord)) ||
(await hasActiveRefreshToken(accountRecord)) ||
(await hasAccessToken(accountRecord)) ||
(await hasSubscription(accountRecord))
(await hasIapSubscription(accountRecord))
);
};

Expand All @@ -231,6 +230,8 @@ const init = async () => {
return 0;
}

const fd = fs.openSync(filepath, 'a');

let hasMaxResultsCount = true;
let totalRowsReturned = 0;
let totalInactiveAccounts = 0;
Expand Down

0 comments on commit 0b70ccc

Please sign in to comment.