-
Notifications
You must be signed in to change notification settings - Fork 4
[PB-4945]: feat/command to sync users with tiers in drive #310
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
Open
xabg2
wants to merge
15
commits into
master
Choose a base branch
from
feature/script-to-sync-users-and-ther-drive-tiers
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f387e90
feat: command to sync users with tiers in drive
xabg2 d1e1f22
fix: usefor loop as map does not wait for async functions
xabg2 719e7b4
feat: pass the tierID when interacting with workspaces
xabg2 9a07a61
Merge branch 'feat/update-the-user-tier-in-drive' into feature/script…
xabg2 c8a6e66
feat: handle business user-tier relationship
xabg2 697e47c
fix: use the correct foreignTierId from Drive
xabg2 e5c689f
feat: add payments header
xabg2 4329782
fix: improve the function name to use a friendly name
xabg2 5a8b149
feat: fetch the tier for an specific user
xabg2 f24a50a
fix: remove hardcoded id
xabg2 0b21fd1
fix: pass the userID as a param
xabg2 4700083
Merge branch 'master' into feature/script-to-sync-users-and-ther-driv…
xabg2 4033644
tests: get users tiers relationship
xabg2 095bc46
fix: pass custom headers if needed for some functions
xabg2 98648eb
tests: add test for b2b user tier mapper
xabg2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| import axios from 'axios'; | ||
| import { MongoClient } from 'mongodb'; | ||
| import Stripe from 'stripe'; | ||
|
|
||
| import envVariablesConfig from '../config'; | ||
| import { PaymentService } from '../services/payment.service'; | ||
| import { UsersService } from '../services/users.service'; | ||
| import { UsersRepository } from '../core/users/UsersRepository'; | ||
| import { MongoDBUsersRepository } from '../core/users/MongoDBUsersRepository'; | ||
| import { StorageService } from '../services/storage.service'; | ||
| import { | ||
| DisplayBillingRepository, | ||
| MongoDBDisplayBillingRepository, | ||
| } from '../core/users/MongoDBDisplayBillingRepository'; | ||
| import { CouponsRepository } from '../core/coupons/CouponsRepository'; | ||
| import { UsersCouponsRepository } from '../core/coupons/UsersCouponsRepository'; | ||
| import { MongoDBCouponsRepository } from '../core/coupons/MongoDBCouponsRepository'; | ||
| import { MongoDBUsersCouponsRepository } from '../core/coupons/MongoDBUsersCouponsRepository'; | ||
| import { ProductsRepository } from '../core/users/ProductsRepository'; | ||
| import { MongoDBProductsRepository } from '../core/users/MongoDBProductsRepository'; | ||
| import { Bit2MeService } from '../services/bit2me.service'; | ||
| import { MongoDBUsersTiersRepository, UsersTiersRepository } from '../core/users/MongoDBUsersTiersRepository'; | ||
|
|
||
| const [, , subType, userId] = process.argv; | ||
|
|
||
| const isBusiness = subType?.toLowerCase() === 'business'; | ||
|
|
||
| export async function updateBusinessUsers(usersTiersRepository: UsersTiersRepository, usersService: UsersService) { | ||
| const userIdsAndForeignTierId = await usersTiersRepository.getUserTierMappings(true, userId); | ||
| const errors: Array<{ userUuid: string; error: string }> = []; | ||
|
|
||
| for (const { userUuid, foreignTierId } of userIdsAndForeignTierId) { | ||
| try { | ||
| console.log(`Processing user: ${userUuid} with business foreign tier id: ${foreignTierId}`); | ||
| await usersService.updateWorkspace({ | ||
| ownerId: userUuid, | ||
| tierId: foreignTierId, | ||
| customHeaders: { | ||
| 'x-internxt-payments-header': process.env.X_INTERNXT_PAYMENTS_HEADER as string, | ||
| }, | ||
| }); | ||
| console.log(`Successfully updated user: ${userUuid}`); | ||
| } catch (error) { | ||
| const errorMessage = error instanceof Error ? error.message : String(error); | ||
| console.error(`✗ Failed to update user ${userUuid}: ${errorMessage}`); | ||
| errors.push({ userUuid, error: errorMessage }); | ||
| } | ||
| } | ||
|
|
||
| if (errors.length > 0) { | ||
| console.error(`\n=== SUMMARY: ${errors.length} user(s) failed ===`); | ||
| errors.forEach(({ userUuid, error }) => { | ||
| console.error(` - ${userUuid}: ${error}`); | ||
| }); | ||
| } | ||
|
|
||
| return { total: userIdsAndForeignTierId.length, failed: errors.length, errors }; | ||
| } | ||
|
|
||
| export async function updateIndividualUsers( | ||
| usersTiersRepository: UsersTiersRepository, | ||
| storageService: StorageService, | ||
| ) { | ||
| const userIdsAndForeignTierId = await usersTiersRepository.getUserTierMappings(false, userId); | ||
| const errors: Array<{ userUuid: string; error: string }> = []; | ||
|
|
||
| for (const { userUuid, foreignTierId } of userIdsAndForeignTierId) { | ||
| try { | ||
| console.log(`Processing user: ${userUuid} with individual foreign tier id: ${foreignTierId}`); | ||
| await storageService.updateUserStorageAndTier(userUuid, undefined, foreignTierId, { | ||
| 'x-internxt-payments-header': process.env.X_INTERNXT_PAYMENTS_HEADER as string, | ||
| }); | ||
| console.log(`Successfully updated user: ${userUuid}`); | ||
| } catch (error) { | ||
| const errorMessage = error instanceof Error ? error.message : String(error); | ||
| console.error(`✗ Failed to update user ${userUuid}: ${errorMessage}`); | ||
| errors.push({ userUuid, error: errorMessage }); | ||
| } | ||
| } | ||
|
|
||
| if (errors.length > 0) { | ||
| console.error(`\n=== SUMMARY: ${errors.length} user(s) failed ===`); | ||
| errors.forEach(({ userUuid, error }) => { | ||
| console.error(` - ${userUuid}: ${error}`); | ||
| }); | ||
| } | ||
|
|
||
| return { total: userIdsAndForeignTierId.length, failed: errors.length, errors }; | ||
| } | ||
|
|
||
| async function main() { | ||
| const mongoClient = await new MongoClient(envVariablesConfig.MONGO_URI).connect(); | ||
| try { | ||
| const stripe = new Stripe(envVariablesConfig.STRIPE_SECRET_KEY, { apiVersion: '2025-02-24.acacia' }); | ||
| const usersRepository: UsersRepository = new MongoDBUsersRepository(mongoClient); | ||
| const storageService = new StorageService(envVariablesConfig, axios); | ||
| const displayBillingRepository: DisplayBillingRepository = new MongoDBDisplayBillingRepository(mongoClient); | ||
| const couponsRepository: CouponsRepository = new MongoDBCouponsRepository(mongoClient); | ||
| const usersCouponsRepository: UsersCouponsRepository = new MongoDBUsersCouponsRepository(mongoClient); | ||
| const productsRepository: ProductsRepository = new MongoDBProductsRepository(mongoClient); | ||
| const usersTiersRepository: UsersTiersRepository = new MongoDBUsersTiersRepository(mongoClient); | ||
| const bit2MeService = new Bit2MeService( | ||
| envVariablesConfig, | ||
| axios, | ||
| envVariablesConfig.CRYPTO_PAYMENTS_PROCESSOR_SECRET_KEY, | ||
| envVariablesConfig.CRYPTO_PAYMENTS_PROCESSOR_API_KEY, | ||
| envVariablesConfig.CRYPTO_PAYMENTS_PROCESSOR_API_URL, | ||
| ); | ||
| const paymentService = new PaymentService(stripe, productsRepository, bit2MeService); | ||
| const usersService = new UsersService( | ||
| usersRepository, | ||
| paymentService, | ||
| displayBillingRepository, | ||
| couponsRepository, | ||
| usersCouponsRepository, | ||
| envVariablesConfig, | ||
| axios, | ||
| ); | ||
|
|
||
| let result; | ||
| if (isBusiness) { | ||
| result = await updateBusinessUsers(usersTiersRepository, usersService); | ||
| } else { | ||
| result = await updateIndividualUsers(usersTiersRepository, storageService); | ||
| } | ||
|
|
||
| console.log(`\n✓ Sync completed: ${result.total - result.failed}/${result.total} users updated successfully`); | ||
|
|
||
| if (result.failed > 0) { | ||
| process.exit(1); | ||
| } | ||
| } catch (error) { | ||
| throw error; | ||
| } finally { | ||
| await mongoClient.close(); | ||
| } | ||
| } | ||
|
|
||
| main() | ||
| .then(() => { | ||
| console.log('Users and tiers synced'); | ||
| }) | ||
| .catch((err) => { | ||
| console.error('Error while syncing users: ', err.message); | ||
| process.exit(1); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -217,25 +217,33 @@ export class UsersService { | |
| tierId, | ||
| maxSpaceBytes, | ||
| seats, | ||
| customHeaders, | ||
| }: { | ||
| ownerId: string; | ||
| tierId: string; | ||
| maxSpaceBytes: number; | ||
| seats: number; | ||
| maxSpaceBytes?: number; | ||
| seats?: number; | ||
| customHeaders?: Record<string, string>; | ||
| }): Promise<void> { | ||
| const jwt = signToken('5m', this.config.DRIVE_NEW_GATEWAY_SECRET); | ||
| let totalMaxSpaceBytes: number | undefined = undefined; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be:
?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The space can be undefined because we can update the customer only by using the |
||
|
|
||
| if (maxSpaceBytes && seats) { | ||
| totalMaxSpaceBytes = maxSpaceBytes * seats; | ||
| } | ||
| const requestConfig: AxiosRequestConfig = { | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| Authorization: `Bearer ${jwt}`, | ||
| ...customHeaders, | ||
| }, | ||
| }; | ||
|
|
||
| return this.axios.patch( | ||
| `${this.config.DRIVE_NEW_GATEWAY_URL}/gateway/workspaces`, | ||
| { | ||
| ownerId, | ||
| maxSpaceBytes: maxSpaceBytes * seats, | ||
| maxSpaceBytes: totalMaxSpaceBytes, | ||
| numberOfSeats: seats, | ||
| tierId, | ||
| }, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.