Skip to content

Commit

Permalink
Add fetch customer data
Browse files Browse the repository at this point in the history
  • Loading branch information
rigwild committed Jul 7, 2024
1 parent 8ae2a7e commit 423576a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ The signature is valid for a few days.
const loginSignature = await nemeosCustomerClient.requestLoginSignature()
```

### Fetch customer data

Fetch the customer data associated with the wallet address.

```ts
const customerData = await nemeosCustomerClient.fetchCustomerData(loginSignature)
```

#### Register email

Register the wallet to an email address. This is used to send email notifications and reminders to customers about their loans.
Expand Down
17 changes: 17 additions & 0 deletions src/NemeosBackendClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ export class NemeosBackendClient {
}
}

public static async fetchCustomerData(borrowerAddress: string, loginSignature: NemeosLoginSignature): Promise<CustomerData> {
return NemeosBackendClient.ofetchClient(`/customerData/${borrowerAddress}`, {
headers: {
'X-Login-Signed-Message': loginSignature.message,
'X-Login-Signed-Signature': loginSignature.signature,
},
}).catch(extractHttpErrorMessageThenThrow)
}

public static async setCustomerDataEmail(borrowerAddress: string, loginSignature: NemeosLoginSignature, email: string): Promise<void> {
return NemeosBackendClient.ofetchClient(`/customerData/${borrowerAddress}/email`, {
method: 'PUT',
Expand Down Expand Up @@ -66,6 +75,14 @@ export type NemeosLoginSignature = {
signature: string
}

export type CustomerData = {
/** Borrower address */
borrower: string

email?: string
web3EmailProtectedData?: string
}

export type NftStartLoanData = {
customerBuyNftParameters: {
/** ID of the NFT */
Expand Down
17 changes: 16 additions & 1 deletion src/NemeosSDK.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as ethers from 'ethers'
import { NemeosBackendClient, NemeosLoginSignature } from './NemeosBackendClient.js'
import { NemeosBackendClient, NemeosLoginSignature, CustomerData } from './NemeosBackendClient.js'
import { assertValidHexAddress, NemeosSDKError } from './utils.js'

const NEMEOS_POOL_HUMAN_ABI = [
Expand Down Expand Up @@ -40,10 +40,23 @@ class NemeosCustomerClient {
*/
public async requestLoginSignature(): Promise<NemeosLoginSignature> {
const borrowerAddress = await this.signer.getAddress()

console.log(`[nemeos][generateLoginSignature] Requesting wallet signature for borrowerAddress=${borrowerAddress}`)
return NemeosBackendClient.generateLoginSignature(this.signer)
}

/**
* Fetch the customer data associated with the wallet address.
*
* @param loginSignature Signature to ensure that the customer is the owner of the wallet
*/
public async fetchCustomerData(loginSignature: NemeosLoginSignature): Promise<CustomerData> {
const borrowerAddress = await this.signer.getAddress()

console.log(`[nemeos][fetchCustomerData] Fetching customer data for borrowerAddress=${borrowerAddress}`)
return NemeosBackendClient.fetchCustomerData(borrowerAddress, loginSignature)
}

/**
* Register the wallet to an email address. This is used to send email notifications and reminders to customers about their loans.
*
Expand All @@ -66,6 +79,8 @@ class NemeosCustomerClient {
* Unregister the wallet from its associated email address. The customer will no longer receive email notifications and reminders.
*
* This will remove the email address from the Nemeos backend database.
*
* @param loginSignature Signature to ensure that the customer is the owner of the wallet
*/
public async unregisterEmail(loginSignature: NemeosLoginSignature): Promise<void> {
const borrowerAddress = await this.signer.getAddress()
Expand Down
10 changes: 9 additions & 1 deletion tests/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@ async function main() {

const loginSignature = await nemeosCustomerClient.requestLoginSignature()
console.log('loginSignature:', loginSignature)

await nemeosCustomerClient.unregisterEmail(loginSignature)

const customerDataBeforeEmailRegistration = await nemeosCustomerClient.fetchCustomerData(loginSignature)
console.log('customerData before email registration:', customerDataBeforeEmailRegistration)

await nemeosCustomerClient.registerEmail(loginSignature, '[email protected]')
// await nemeosCustomerClient.unregisterEmail(loginSignature)

const customerDataAfterEmailRegistration = await nemeosCustomerClient.fetchCustomerData(loginSignature)
console.log('customerData after email registration:', customerDataAfterEmailRegistration)

//
// NemeosPoolClient
Expand Down

0 comments on commit 423576a

Please sign in to comment.