-
Notifications
You must be signed in to change notification settings - Fork 5
feat/business-account #224
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8b08f88
Draft journal entry prior to payment & handle errors
brh28 73f26b3
Clean up draft records
brh28 ceb3338
refactored to account for ERPNext integration
islandbitcoin f94aa26
fix linting errors
islandbitcoin c9efc1c
update to use doctype: Account Upgrade Request
islandbitcoin a5f0511
Merge remote-tracking branch 'origin' into feat/business-account
islandbitcoin a231176
MVP version changes
islandbitcoin 4e1dd99
update ErpNext
islandbitcoin 96ee680
updates based on PR feedback
islandbitcoin 448d4b0
update to AccountLevel enum based on PR feedback
islandbitcoin 7b80f31
update leveltoerpstring
islandbitcoin a7c847d
Feat/business account (#244)
brh28 51f1112
review changes
islandbitcoin 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
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 |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import { InvalidAccountStatusError } from "@domain/errors" | ||
| import { AccountLevel, checkedToAccountLevel } from "@domain/accounts" | ||
|
|
||
| import { AccountsRepository, UsersRepository } from "@services/mongoose" | ||
| import { IdentityRepository } from "@services/kratos" | ||
| import ErpNext from "@services/frappe/ErpNext" | ||
|
|
||
| import { updateAccountLevel } from "./update-account-level" | ||
|
|
||
| type BusinessUpgradeRequestInput = { | ||
| accountId: AccountId | ||
| level: number | ||
| fullName: string | ||
| phoneNumber?: string | ||
| email?: string | ||
| businessName?: string | ||
| businessAddress?: string | ||
| terminalRequested?: boolean | ||
| bankName?: string | ||
| bankBranch?: string | ||
| accountType?: string | ||
| currency?: string | ||
| accountNumber?: number | ||
| idDocument?: string | ||
| } | ||
|
|
||
| // Composable validation helpers | ||
| type Validator<T> = (value: T) => true | ApplicationError | ||
| type CheckedValidator<T, R> = (value: T) => R | ApplicationError | ||
|
|
||
| const validate = <T>(value: T, validators: Validator<T>[]): T | ApplicationError => { | ||
| for (const validator of validators) { | ||
| const result = validator(value) | ||
| if (result instanceof Error) return result | ||
| } | ||
| return value | ||
| } | ||
|
|
||
| const validateAndTransform = <T, R>( | ||
| value: T, | ||
| transform: CheckedValidator<T, R>, | ||
| validators: Validator<R>[], | ||
| ): R | ApplicationError => { | ||
| const transformed = transform(value) | ||
| if (transformed instanceof Error) return transformed | ||
| return validate(transformed, validators) | ||
| } | ||
|
|
||
| const isGreaterThan = | ||
| (threshold: number, errorMsg: string): Validator<number> => | ||
| (value) => | ||
| value > threshold ? true : new InvalidAccountStatusError(errorMsg) | ||
|
|
||
| const isNotEqual = | ||
| (compareTo: number, errorMsg: string): Validator<number> => | ||
| (value) => | ||
| value !== compareTo ? true : new InvalidAccountStatusError(errorMsg) | ||
|
|
||
| export const businessAccountUpgradeRequest = async ( | ||
| input: BusinessUpgradeRequestInput, | ||
| ): Promise<true | ApplicationError> => { | ||
| const { accountId, level, fullName } = input | ||
|
|
||
| const accountsRepo = AccountsRepository() | ||
| const usersRepo = UsersRepository() | ||
|
|
||
| const account = await accountsRepo.findById(accountId) | ||
| if (account instanceof Error) return account | ||
|
|
||
| const checkedLevel = validateAndTransform(level, checkedToAccountLevel, [ | ||
| isGreaterThan(account.level - 1, "Cannot request account level downgrade"), | ||
| isNotEqual(account.level, "Account is already at requested level"), | ||
| ]) | ||
| if (checkedLevel instanceof Error) return checkedLevel | ||
|
|
||
| const user = await usersRepo.findById(account.kratosUserId) | ||
| if (user instanceof Error) return user | ||
|
|
||
| const identity = await IdentityRepository().getIdentity(account.kratosUserId) | ||
| if (identity instanceof Error) return identity | ||
|
|
||
| const storedPhone = (user.phone as string) || "" | ||
| const storedEmail = (identity.email as string) || "" | ||
|
|
||
| // Validate phone number if provided and account has existing phone | ||
| if (input.phoneNumber && storedPhone && input.phoneNumber !== storedPhone) { | ||
| return new InvalidAccountStatusError("Phone number does not match account records") | ||
| } | ||
|
|
||
| // Validate email if provided and account has existing email | ||
| if (input.email && storedEmail && input.email !== storedEmail) { | ||
| return new InvalidAccountStatusError("Email does not match account records") | ||
| } | ||
|
|
||
| const requestResult = await ErpNext.createUpgradeRequest({ | ||
| username: (account.username as string) || account.id, | ||
| currentLevel: account.level, | ||
| requestedLevel: checkedLevel, | ||
| fullName, | ||
| phoneNumber: storedPhone, | ||
| email: storedEmail || undefined, | ||
| businessName: input.businessName, | ||
| businessAddress: input.businessAddress, | ||
| terminalRequested: input.terminalRequested, | ||
| bankName: input.bankName, | ||
| bankBranch: input.bankBranch, | ||
| accountType: input.accountType, | ||
| currency: input.currency, | ||
| accountNumber: input.accountNumber, | ||
| idDocument: input.idDocument, | ||
| }) | ||
|
|
||
| if (requestResult instanceof Error) return requestResult | ||
|
|
||
| // Pro accounts auto-upgrade immediately (no manual approval needed) | ||
| if (checkedLevel === AccountLevel.Pro) { | ||
| const upgradeResult = await updateAccountLevel({ | ||
| id: accountId, | ||
| level: checkedLevel, | ||
| }) | ||
| if (upgradeResult instanceof Error) return upgradeResult | ||
| } | ||
|
|
||
| return true | ||
| } |
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
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
83 changes: 83 additions & 0 deletions
83
src/graphql/public/root/mutation/business-account-upgrade-request.ts
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,83 @@ | ||
| import { Accounts } from "@app" | ||
| import { GT } from "@graphql/index" | ||
| import { mapAndParseErrorForGqlResponse } from "@graphql/error-map" | ||
| import AccountLevel from "@graphql/shared/types/scalar/account-level" | ||
| import SuccessPayload from "@graphql/shared/types/payload/success-payload" | ||
|
|
||
| const BusinessAccountUpgradeRequestInput = GT.Input({ | ||
| name: "BusinessAccountUpgradeRequestInput", | ||
| fields: () => ({ | ||
| level: { type: GT.NonNull(AccountLevel) }, | ||
| fullName: { type: GT.NonNull(GT.String) }, | ||
| phoneNumber: { type: GT.String }, | ||
| email: { type: GT.String }, | ||
| businessName: { type: GT.String }, | ||
| businessAddress: { type: GT.String }, | ||
| terminalRequested: { type: GT.Boolean }, | ||
| bankName: { type: GT.String }, | ||
| bankBranch: { type: GT.String }, | ||
| accountType: { type: GT.String }, | ||
| currency: { type: GT.String }, | ||
| accountNumber: { type: GT.Int }, | ||
| idDocument: { type: GT.String }, | ||
| }), | ||
| }) | ||
|
|
||
| const BusinessAccountUpgradeRequestMutation = GT.Field({ | ||
| extensions: { | ||
| complexity: 150, | ||
| }, | ||
| type: GT.NonNull(SuccessPayload), | ||
| args: { | ||
| input: { type: GT.NonNull(BusinessAccountUpgradeRequestInput) }, | ||
| }, | ||
| resolve: async (_, args, { domainAccount }: { domainAccount: Account }) => { | ||
| const { | ||
| level, | ||
| fullName, | ||
| phoneNumber, | ||
| email, | ||
| businessName, | ||
| businessAddress, | ||
| terminalRequested, | ||
| bankName, | ||
| bankBranch, | ||
| accountType, | ||
| currency, | ||
| accountNumber, | ||
| idDocument, | ||
| } = args.input | ||
|
|
||
| if (level instanceof Error) { | ||
| return { errors: [{ message: level.message }], success: false } | ||
| } | ||
|
|
||
| const result = await Accounts.businessAccountUpgradeRequest({ | ||
| accountId: domainAccount.id, | ||
| level, | ||
| fullName, | ||
| phoneNumber: phoneNumber || undefined, | ||
| email: email || undefined, | ||
| businessName: businessName || undefined, | ||
| businessAddress: businessAddress || undefined, | ||
| terminalRequested: terminalRequested || undefined, | ||
| bankName: bankName || undefined, | ||
| bankBranch: bankBranch || undefined, | ||
| accountType: accountType || undefined, | ||
| currency: currency || undefined, | ||
| accountNumber: accountNumber || undefined, | ||
| idDocument: idDocument || undefined, | ||
| }) | ||
|
|
||
| if (result instanceof Error) { | ||
| return { errors: [mapAndParseErrorForGqlResponse(result)], success: false } | ||
| } | ||
|
|
||
| return { | ||
| errors: [], | ||
| success: true, | ||
| } | ||
| }, | ||
| }) | ||
|
|
||
| export default BusinessAccountUpgradeRequestMutation |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be updated to include all required fields