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

feat(wallet/backend): add script for managed user and customer creation #1675

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
133 changes: 107 additions & 26 deletions packages/wallet/backend/src/card/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,120 @@ export interface ILinksResponse {
}

export interface ICreateCustomerRequest {
emailAddress: string
walletAddress: string
account: {
productCode: string
card: {
productCode: string
}
}
card: {
productCode: string
}
user: {
firstName: string
lastName: string
mobileNumber?: string
nationalIdentifier?: string
}
identification: {
documents: Array<{
type: string
file: string // Base64-encoded file content
}>
}
address: {
addressLine1: string
addressLine2?: string
city: string
region?: string
postalCode: string
countryCode: string
citizen: {
name: string
surname: string
birthPlace?: string | null
}
}

export interface ICitizen {
name: string
surname: string
birthDate?: string | null
birthPlace?: string | null
gender?: 'Female' | 'Male' | 'Unspecified' | 'Unknown' | null
title?: string | null
language?: string | null
}

export interface ILegalEntity {
longName: string
shortName: string
sector?:
| 'Public'
| 'Private'
| 'Corporate'
| 'Others'
| 'NoInformation'
| 'UnrelatedPersonsLegalEntities'
| null
industrialClassificationProvider?: string | null
industrialClassificationValue?: string | null
type?: string | null
vat?: string | null
hqCustomerId?: number | null
contactPerson?: string | null
agentCode?: string | null
agentName?: string | null
}

export interface IAddress {
sourceId?: string | null
type: 'PermanentResidence' | 'Work' | 'Other' | 'TemporaryResidence'
countryCode: string
line1: string
line2?: string | null
line3?: string | null
city: string
postOffice?: string | null
zipCode: string
status?: 'Inactive' | 'Active' | null
id?: string | null
customerId?: string | null
customerSourceId?: string | null
}

export interface ICommunication {
sourceId?: string | null
type: 'Email' | 'Mobile'
value?: string | null
id?: string | null
status?: 'Inactive' | 'Active' | null
customerId?: string | null
customerSourceId?: string | null
}

export interface IAccount {
sourceId?: string | null
type?: 'CHARGE' | 'LOAN' | 'DEBIT' | 'PREPAID' | null
productCode?: string | null
accountNumber?: string | null
feeProfile?: string | null
accountProfile?: string | null
id?: string | null
customerId?: string | null
customerSourceId?: string | null
status?: 'ACTIVE' | 'LOCKED' | 'BLOCKED' | null
statusReasonCode?:
| 'TemporaryBlockForDelinquency'
| 'TemporaryBlockOnIssuerRequest'
| 'TemporaryBlockForDepo'
| 'TemporaryBlockForAmlKyc'
| 'IssuerRequestGeneral'
| 'UserRequest'
| 'PremanentBlockChargeOff'
| 'IssuerRequestBureauInquiry'
| 'IssuerRequestCustomerDeceased'
| 'IssuerRequestStornoFromCollectionStraight'
| 'IssuerRequestStornoFromCollectionDepo'
| 'IssuerRequestStornoFromCollectionDepoPaid'
| 'IssuerRequestHandoverToAttorney'
| 'IssuerRequestLegalAction'
| 'IssuerRequestAmlKyc'
| null
currency?: string | null
cards?: ICardResponse[] | null
}

export interface ICreateCustomerResponse {
customerId: string
accountId: string
cardId: string
sourceId?: string | null
taxNumber?: string | null
code: string
type: 'Citizen' | 'LegalEntity'
citizen?: ICitizen | null
legalEntity?: ILegalEntity | null
id?: string | null
addresses?: IAddress[] | null
communications?: ICommunication[] | null
accounts?: IAccount[] | null
}

export interface ICardResponse {
Expand Down
85 changes: 85 additions & 0 deletions packages/wallet/backend/src/cardUsers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { Knex } from 'knex'
import { createContainer } from '@/createContainer'
import { env } from '@/config/env'
import { ICreateCustomerRequest, ICreateCustomerResponse } from '@/card/types'
import { GateHubClient } from './gatehub/client'

interface UserData {
email: string
walletAddress: string
firstName: string
lastName: string
}

const usersData: UserData[] = [
{
email: 'user1@example.com',
walletAddress: '123456789',
firstName: 'Alice',
lastName: 'Smith'
}
]

async function cardManagement() {
// Initialize container and dependencies
const container = await createContainer(env)
const knex = container.resolve<Knex>('knex')
const gateHubClient = container.resolve<GateHubClient>('gateHubClient')

try {
// TODO: Create whitelist table and everything related
// TODO: Add users to whitelist

// Fetch card application products
const cardProducts = await gateHubClient.fetchCardApplicationProducts()
if (!cardProducts || cardProducts.length === 0) {
throw new Error('No card application products found.')
}

// Use the "code" field from the fetched products
// Should only be one product
const productCodes = cardProducts.map((product) => product.code)

for (const userData of usersData) {
const { email, walletAddress, firstName, lastName } = userData

const managedUser = await gateHubClient.createManagedUser(email)

console.log(`Created managed user for ${email}: ${managedUser.id}`)

// TODO: Get wallet address from GateHub

// Create customer using product codes fetched earlier
const createCustomerRequestBody: ICreateCustomerRequest = {
walletAddress,
account: {
productCode: productCodes[0],
card: {
productCode: productCodes[0]
}
},
citizen: {
name: firstName,
surname: lastName
}
}

const customer: ICreateCustomerResponse =
await gateHubClient.createCustomer(createCustomerRequestBody)

console.log(`Created customer for ${email}: ${customer.id}`)

// TODO: Map managed user to customer
}
} catch (error: any) {

Check failure on line 74 in packages/wallet/backend/src/cardUsers.ts

View workflow job for this annotation

GitHub Actions / ESLint and Prettier checks

Unexpected any. Specify a different type
console.log(`An error occurred: ${error.message}`)
} finally {
await knex.destroy()
await container.dispose()
}
}

cardManagement().catch((error) => {
console.error(`Script failed: ${error.message}`)
process.exit(1)
})
2 changes: 1 addition & 1 deletion packages/wallet/backend/src/gatehub/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ export class GateHubClient {
async createCustomer(
requestBody: ICreateCustomerRequest
): Promise<ICreateCustomerResponse> {
const url = `${this.apiUrl}/v1/customers`
const url = `${this.apiUrl}/v1/customers/managed`
return this.request<ICreateCustomerResponse>(
'POST',
url,
Expand Down
Loading