add global loading state, update to use real data for clients table

This commit is contained in:
Casey 2025-11-04 08:33:14 -06:00
parent 2cfe7ed8e6
commit 464c62d1e5
12 changed files with 1075 additions and 194 deletions

View file

@ -1,16 +1,17 @@
import { da } from "vuetify/locale";
import DataUtils from "./utils";
const ZIPPOPOTAMUS_BASE_URL = "https://api.zippopotam.us/us";
const FRAPPE_PROXY_METHOD = "custom_ui.api.proxy.request";
const FRAPPE_UPSERT_CLIENT_METHOD = "custom_ui.api.db.upsert_client";
class Api {
static async request(url, method = "GET", data = {}) {
static async request(frappeMethod, args = {}) {
try {
const response = await frappe.call({
method: "custom_ui.api.proxy.request",
method: frappeMethod,
args: {
url,
method,
data: JSON.stringify(data),
...args,
},
});
console.log("DEBUG: API - Request Response: ", response);
@ -22,20 +23,72 @@ class Api {
}
}
static async getClientDetails() {
// const data = [];
// const addresses = await this.getDocsList("Address");
// for (const addr of addresses) {
// const clientDetail = {};
// const fullAddress = await this.getDetailedDoc("Address", addr["name"] || addr["Name"]);
// const customer = await this.getDetailedCustomer(fullAddress["links"][0]["link_name"]);
// clientDetail.customer = customer;
// clientDetail.address = fullAddress;
// data.push(clientDetail);
// }
// console.log("DEBUG: API - Fetched Client Details: ", data);
const data = DataUtils.dummyClientData;
console.log("DEBUG: API - getClientDetails result: ", data);
static async getClientDetails(forTable = true) {
const data = [];
const addresses = await this.getDocsList("Address", ["*"]);
for (const addr of addresses) {
const clientDetail = {};
const customer = await this.getDetailedDoc(
"Customer",
addr["custom_customer_to_bill"],
);
const quotations = await this.getDocsList("Quotation", [], {
custom_installation_address: addr["name"],
});
const quoteDetails =
quotations.length > 0
? await this.getDetailedDoc("Quotation", quotations[0]["name"])
: null;
const jobs = await this.getDocsList("Project", [], {
project_template: "SNW Install",
custom_installation_address: addr["name"],
});
const jobDetails =
jobs.length > 0 ? await this.getDetailedDoc("Project", jobs[0]["name"]) : null;
clientDetail.customer = customer;
clientDetail.address = addr;
clientDetail.estimate = quoteDetails;
clientDetail.job = jobDetails;
const totalPaid = quoteDetails
? quoteDetails.payment_schedule
? quoteDetails.payment_schedule.reduce(
(sum, payment) => sum + (payment.paid_amount || 0),
0,
)
: 0
: 0;
const tableRow = {
fullName: `${customer.customer_name} - ${addr.address_line1}, ${addr.city} ${addr.state}`,
appointmentStatus: "not started",
estimateStatus: quoteDetails
? quoteDetails.custom_response == "Accepted"
? "completed"
: "in progress"
: "not started",
paymentStatus: quoteDetails
? totalPaid < quoteDetails.grand_total
? "in progress"
: "completed"
: "not started",
jobStatus: jobDetails
? jobDetails.status === "Completed"
? "completed"
: "in progress"
: "not started",
};
if (forTable) {
data.push(tableRow);
} else {
data.push(clientDetail);
}
}
// const data = DataUtils.dummyClientData;
console.log("DEBUG: API - Fetched Client Details: ", data);
return data;
}
@ -77,10 +130,16 @@ class Api {
*
* @param {String} doctype
* @param {string[]} fields
* @param {Object} filters
* @returns {Promise<Object[]>}
*/
static async getDocsList(doctype, fields = []) {
const docs = await frappe.db.get_list(doctype, { fields });
static async getDocsList(doctype, fields = [], filters = {}, page = 0, pageLength = 600) {
const docs = await frappe.db.get_list(doctype, {
fields,
filters,
start: page * pageLength,
limit: pageLength,
});
console.log(`DEBUG: API - Fetched ${doctype} list: `, docs);
return docs;
}
@ -90,14 +149,48 @@ class Api {
*
* @param {String} doctype
* @param {String} name
* @param {Object} filters
* @returns {Promise<Object>}
*/
static async getDetailedDoc(doctype, name) {
const doc = await frappe.db.get_doc(doctype, name);
static async getDetailedDoc(doctype, name, filters = {}) {
const doc = await frappe.db.get_doc(doctype, name, filters);
console.log(`DEBUG: API - Fetched Detailed ${doctype}: `, doc);
return doc;
}
static async getDocCount(doctype, filters = {}) {
const count = await frappe.db.count(doctype, filters);
console.log(`DEBUG: API - Counted ${doctype}: `, count);
return count;
}
static async createDoc(doctype, data) {
const doc = await frappe.db.insert({
...data,
doctype,
});
console.log(`DEBUG: API - Created ${doctype}: `, doc);
return doc;
}
static async getCustomerNames() {
const customers = await this.getDocsList("Customer", ["name"]);
const customerNames = customers.map((customer) => customer.name);
console.log("DEBUG: API - Fetched Customer Names: ", customerNames);
return customerNames;
}
// Create methods
static async createClient(clientData) {
const payload = DataUtils.toSnakeCaseObject(clientData);
const result = await this.request(FRAPPE_UPSERT_CLIENT_METHOD, { data: payload });
console.log("DEBUG: API - Created/Updated Client: ", result);
return result;
}
// External API calls
/**
* Fetch a list of places (city/state) by zipcode using Zippopotamus API.
*
@ -106,22 +199,13 @@ class Api {
*/
static async getCityStateByZip(zipcode) {
const url = `${ZIPPOPOTAMUS_BASE_URL}/${zipcode}`;
const response = await this.request(url);
const response = await this.request(FRAPPE_PROXY_METHOD, { url, method: "GET" });
const { places } = response || {};
if (!places || places.length === 0) {
throw new Error(`No location data found for zip code ${zipcode}`);
}
return places;
}
/**
* Fetch a list of Customer names.
* @returns {Promise<String[]>}
*/
static async getCustomerNames() {
const customers = await this.getDocsList("Customer", ["name"]);
return customers.map((customer) => customer.name);
}
}
export default Api;