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

Integration of the frontend with the email notification service. #1079

Closed
Tracked by #1080
jaybuidl opened this issue Aug 1, 2023 · 0 comments · Fixed by #1210
Closed
Tracked by #1080

Integration of the frontend with the email notification service. #1079

jaybuidl opened this issue Aug 1, 2023 · 0 comments · Fixed by #1210

Comments

@jaybuidl
Copy link
Member

jaybuidl commented Aug 1, 2023

Save the user's contact preference (email only for now).

Using this Netlify function with inputs

event {
  body {
     message {
        email
        telegram
        ...
     }
     signature
     address
  }
}
import { createClient } from '@supabase/supabase-js'
import { ethers } from 'ethers'

// Configuration
const SUPABASE_KEY = process.env.SUPABASE_CLIENT_API_KEY
const SUPABASE_URL = process.env.SUPABASE_URL
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY)

export const handler = async function(event: any, context: any) {
  try {
    // Assuming the event.body contains the data we need
    const { message, signature, address } = JSON.parse(event.body)

    // Recover the address from the signature
    const recoveredAddress = ethers.utils.verifyMessage(message, signature)

    // If the recovered address does not match the provided address, return an error
    if (recoveredAddress !== address) {
      throw new Error("Signature verification failed")
    }

    // Allowed columns to update
    const allowedColumns = ['discord', 'telegram', 'twitter', 'matrix', 'push', 'email'];

    // If the message is empty, delete the user record
    if (message === '{}') {
      let { data, error } = await supabase
        .from('users')
        .delete()
        .match({ address: recoveredAddress })

      if (error) throw error

      return {
        statusCode: 200,
        body: JSON.stringify({ message: "Record deleted successfully." })
      }
    }

    // Parse the signed message
    const parsedMessage = JSON.parse(message)

    // Prepare the record data based on the allowed columns
    let recordData: { [key: string]: any } = {};
    for (const key in parsedMessage) {
      if (allowedColumns.includes(key)) {
        recordData[key] = parsedMessage[key];
      }
    }

    // Assuming you have a 'users' table with 'address' and allowedColumns fields
    let { data, error } = await supabase
      .from('user-settings')
      .upsert(recordData)
      .match({ address: recoveredAddress })

    if (error) throw error

    return {
      statusCode: 200,
      body: JSON.stringify({ message: "Record updated successfully." })
    }
  } catch (err) {
    return {
      statusCode: 500,
      body: JSON.stringify({ message: `Error: ${err.message}` })
    }
  }
}
@jaybuidl jaybuidl changed the title Backend: integration with the email notification + contact setting service Integration of the frontend with the email notification service. Aug 1, 2023
@jaybuidl jaybuidl linked a pull request Sep 18, 2023 that will close this issue
@jaybuidl jaybuidl added this to the testnet-2 milestone Oct 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant