Skip to main content

import-users

Users can be imported in bulk using CSV

  • localhost:3000/import

User data is imported to Supabase using the client import { supabase } from "@utils/supabaseClient";

Data is entered into a text field, then the resulting array is processed by getNewUsers and passed to the addUser function

src/pages/import.js
// process the user data
const getNewUsers = () => {
// process and output the user data to pass to addUser function

const userList = newUsers.entry.split("\n");

userList.map(function (em, index) {
const account = em.split(",");
addUser(account); // pass in account and then destructure in function
});
};

As with the delete user function the auth user needs to be created using a serverless function:

src/pages/import.js
async function addUser(account) {
const mobileNumber = {
entry: account[7],
type: "mobileNumber",
};

const userDataToSend = {
province: account[0],
district: account[1],
health_facility: account[2],
name: account[3],
gender: account[4],
role: account[6],
mobile_number: mobileNumber.entry,
dob: account[5],
organisation: account[8],
};

try {
setLoading(true);

const { data, error } = await supabase.functions.invoke("add-user", {
body: JSON.stringify(mobileNumber),
});

if (error) alert(error);

if (data) {
if (data.error) {
console.log(data.error);
setErrorOnSubmit(data.error);
} else {
setResponseJson(data);
addUserProfile(data, userDataToSend);
setErrorOnSubmit(false);
setUserCreated(true);
}
}
} catch (error) {
// console.log(error);
} finally {
setLoading(false);
}
}