Skip to main content

Vendors

This page contains guides related to Borku Africa vendors

Vendor class definition

See here: Vendor Details

List vendors

Fetch a list of vendors on the platform

  try {
const res = await database.listDocuments(
"DATABASE_ID",
"VENDORS_COLLECTION_ID"
);

const total: number = res.total; // use this to determine if there are more documents and show total number of documents to the user

const vendorList = res.documents.map((doc) => {
return Vendor.fromMap(doc);
})

console.log(vendorList);
} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error fetching vendors: ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}
info

Appwrite will always return a list of 25 items by default, unless specified using the Query.limit(limit) query. Note that only a max of 100 items can be returned at once. A pagination example is shown below.

  try {
const existingList: Vendor[] = [];
const lastItem = existingList[existingList.length - 1];

const res = await database.listDocuments(
"DATABASE_ID",
"VENDORS_COLLECTION_ID",
[Query.cursorAfter(lastItem.$id)]
);

const moreVendors: Vendor[] = res.documents.map((doc) => {
return Vendor.fromMap(doc);
})

existingList.push(...moreVendors); // update the existing list

} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error fetching vendors: ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}

Update vendor status

Change the approval status of a vendor. Approval status can be any of: pending, approved, rejected, suspended, or actionRequired

note

Note that the suspension status goes in sync with the vendor's User object. i.e, if the vendor document is suspended, the corresponding user account will also be suspended. All these are handle in the update-vendor-status cloud function

It receives the vendor's ID and approval status in the body of the request.

{
"vendorId": "vendor-id",
"status": "pending"
}

It returns a JSON string with an error or success key that holds a message. E.g.:

{
"error": "Some error message"
}
{
"success": "Vendor status updated successfully"
}

Below is the code to update the status of a vendor:

  try {
const res = await functions.createExecution('UPDATE_VENDOR_STATUS_FUNCTION_ID', JSON.stringify({
"vendorId": "VENDOR_ID",
"status": "approved" // or any other value
}));

// confirm response status code
if (res.responseStatusCode != 200) {
console.error(`Error executing function: ${res.errors}`);
// display error message to the user
}

// check the status of the function execution

if (res.status == 'processing') {
console.log('Function is still processing');
// handle processing state
return;
}

if (res.status == 'failed') {
console.error(`Function execution failed: ${res.errors}`);
// display error message to the user
return;
}

// check the output of the function execution
const body = res.responseBody;
const data = JSON.parse(body);

// check if data contains an error key
if (data.error) {
console.log(data.error);
console.error(`Function execution failed: ${data.error}`);
// display error message to the user
return;
}

// handle successful function execution
console.log('Function executed successfully:', data.success);

return;

} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error executing function: ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}