Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(scripts): improve inactive account uids script #17951

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there non-distinct uids?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I just got copy-paste happy. Thank you for catching that.

.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