Orders
Create Order
try {
const info = {
"status": OrderStatus.pending,
"time": new Date().toISOString(),
"message": "Order created"
}
// usually, this is gotten from the customer details document in a JSON string format. They'd usually have 3 shipping addresses, but the first one will be used
const shippingAddress = {
"name": "John Doe",
"address": "123 Main St",
"city": "Anytown",
"state": "CA",
"zip": "12345",
"phone": "555-555-5555"
}
const order = new Order(
ID.unique(),
new Date().toISOString(),
new Date().toISOString(),
acct.$id,
'Vendor 1', // you can search for a vendor by name and add their ID here
'Product 1', // you can search for a product by name and add its ID here
1,
Platform.web,
[JSON.stringify(info)],
'USD',
100,
10,
110,
PaymentMethod.stripe,
JSON.stringify(shippingAddress),
new Date().toISOString(),
OrderStatus.pending
);
await database.createDocument(
"[DATABASE_ID]",
"[PRODUCT_COLLECTION_ID]",
order.$id,
order.toMap()
);
console.log('Order created:', order);
} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error creating order : ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}
List All Orders
try {
const res = await database.listDocuments(
"[DATABASE_ID]",
"[ORDERS_COLLECTION_ID]"
);
const orders = res.documents.map((doc) => Order.fromMap(doc));
const total = res.total; // total number of orders ...keep this for pagination and to display to user where needed
console.log('Orders fetched:', orders);
} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error fetching orders: ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}
note
A list of 25 products is returned by default. So to get the next page, we use the cursorAfter query method to get the next page of results after the last document in the previous page.
Pagination
try {
const res = await database.listDocuments(
"[DATABASE_ID]",
"[ORDERS_COLLECTION_ID]",
[Query.cursorAfter('LAST_ORDER_ID')]
);
const moreProducts = res.documents.map((doc) => Order.fromMap(doc));
console.log('Orders fetched:', moreProducts);
} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error fetching orders: ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}
List products by status
try {
const res = await database.listDocuments(
"[DATABASE_ID]",
"[ORDERS_COLLECTION_ID]",
[Query.equal('status', OrderStatus.confirmed)] // filter by status here
);
const orders = res.documents.map((doc) => Order.fromMap(doc));
console.log('Orders fetched:', orders);
} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error fetching orders: ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}
Delete An Order
try {
await database.deleteDocument(
"[DATABASE_ID]",
"[ORDERS_COLLECTION_ID]",
"[ORDER_ID]"
);
console.log('Order deleted successfully');
} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error deleting order: ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}
Edit/Update An Order
try {
await database.updateDocument(
"[DATABASE_ID]",
"[ORDERS_COLLECTION_ID]",
"[ORDER_ID]",
{
"status": OrderStatus.delivered,
}
);
console.log('Order updated successfully');
} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error updating order: ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}
Assign Order To Customer
This can be used when creating or editing an order to search for a customer by their name and assign the customer to the order
try {
const res = await database.listDocuments(
"[DATABASE_ID]",
"[CUSTOMERS_COLLECTION_ID]",
[Query.search('name', searchQuery)]
);
const customers = res.documents.map((doc) => Customer.fromMap(doc));
console.log('customers fetched:', customers);
} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error fetching customers: ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}
Assign Order To Vendor
This can be used when creating or editing an order to search for a vendor by their name and assign the vendor to the order
try {
const res = await database.listDocuments(
"[DATABASE_ID]",
"[VENDORS_COLLECTION_ID]",
[Query.search('name', searchQuery)]
);
const vendors = res.documents.map((doc) => Vendor.fromMap(doc));
console.log('vendors fetched:', vendors);
} 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
}
}