clean up api utils
This commit is contained in:
parent
b087ea673e
commit
ce708f5209
@ -92,15 +92,29 @@ def get_client(client_name):
|
||||
["custom_installation_address", "=", address.address_title],
|
||||
["custom_address", "=", address.address_title]
|
||||
], limit_page_length=100)
|
||||
|
||||
contacts = []
|
||||
onsite_meetings = []
|
||||
quotations = []
|
||||
sales_orders = []
|
||||
projects = [frappe.get_doc("Project", proj["name"]) for proj in project_names]
|
||||
projects_data = []
|
||||
sales_invoices = []
|
||||
payment_entries = []
|
||||
jobs = []
|
||||
for project in projects:
|
||||
job = []
|
||||
jobs.append(job)
|
||||
customer = frappe.get_doc("Customer", customer_name)
|
||||
# get all associated data as needed
|
||||
return {
|
||||
"address": address,
|
||||
"customer": customer,
|
||||
"projects": projects
|
||||
"contacts": contacts,
|
||||
"jobs": jobs,
|
||||
"sales_invoices": sales_invoices,
|
||||
"payment_entries": payment_entries,
|
||||
"sales_orders": sales_orders,
|
||||
"quotations": quotations,
|
||||
"onsite_meetings": onsite_meetings,
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import DataUtils from "./utils";
|
||||
import ApiUtils from "./apiUtils";
|
||||
|
||||
const ZIPPOPOTAMUS_BASE_URL = "https://api.zippopotam.us/us";
|
||||
const FRAPPE_PROXY_METHOD = "custom_ui.api.proxy.request";
|
||||
@ -12,12 +12,12 @@ const FRAPPE_GET_CLIENT_METHOD = "custom_ui.api.db.clients.get_client";
|
||||
|
||||
class Api {
|
||||
static async request(frappeMethod, args = {}) {
|
||||
args = DataUtils.toSnakeCaseObject(args);
|
||||
args = ApiUtils.toSnakeCaseObject(args);
|
||||
const request = { method: frappeMethod, args };
|
||||
console.log("DEBUG: API - Request Args: ", request);
|
||||
try {
|
||||
let response = await frappe.call(request);
|
||||
response = DataUtils.toCamelCaseObject(response);
|
||||
response = ApiUtils.toCamelCaseObject(response);
|
||||
console.log("DEBUG: API - Request Response: ", response);
|
||||
return response.message;
|
||||
} catch (error) {
|
||||
@ -32,7 +32,7 @@ class Api {
|
||||
}
|
||||
|
||||
static async getClientDetails(clientName) {
|
||||
return await this.request(FRAPPE_GET_CLIENT_DETAILS_METHOD, { clientName });
|
||||
return await this.request(FRAPPE_GET_CLIENT_METHOD, { clientName });
|
||||
}
|
||||
|
||||
static async getJobDetails() {
|
||||
|
||||
53
frontend/src/apiUtils.js
Normal file
53
frontend/src/apiUtils.js
Normal file
@ -0,0 +1,53 @@
|
||||
class ApiUtils {
|
||||
static toSnakeCaseObject(obj) {
|
||||
console.log("Converting to snake case:", obj);
|
||||
const newObj = Object.entries(obj).reduce((acc, [key, value]) => {
|
||||
const snakeKey = key.replace(/[A-Z]/g, (match) => "_" + match.toLowerCase());
|
||||
if (key === "sorting") {
|
||||
value = value
|
||||
? value.map((item) => {
|
||||
const [field, order] = item;
|
||||
const snakeField = field.replace(
|
||||
/[A-Z]/g,
|
||||
(match) => "_" + match.toLowerCase(),
|
||||
);
|
||||
return [snakeField, order];
|
||||
})
|
||||
: value;
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
value = value.map((item) => {
|
||||
return Object.prototype.toString.call(item) === "[object Object]"
|
||||
? this.toSnakeCaseObject(item)
|
||||
: item;
|
||||
});
|
||||
} else if (Object.prototype.toString.call(value) === "[object Object]") {
|
||||
value = this.toSnakeCaseObject(value);
|
||||
}
|
||||
acc[snakeKey] = value;
|
||||
return acc;
|
||||
}, {});
|
||||
return newObj;
|
||||
}
|
||||
|
||||
static toCamelCaseObject(obj) {
|
||||
const newObj = Object.entries(obj).reduce((acc, [key, value]) => {
|
||||
const camelKey = key.replace(/_([a-z])/g, (match, p1) => p1.toUpperCase());
|
||||
// check if value is an array
|
||||
if (Array.isArray(value)) {
|
||||
value = value.map((item) => {
|
||||
return Object.prototype.toString.call(item) === "[object Object]"
|
||||
? this.toCamelCaseObject(item)
|
||||
: item;
|
||||
});
|
||||
} else if (Object.prototype.toString.call(value) === "[object Object]") {
|
||||
value = this.toCamelCaseObject(value);
|
||||
}
|
||||
acc[camelKey] = value;
|
||||
return acc;
|
||||
}, {});
|
||||
return newObj;
|
||||
}
|
||||
}
|
||||
|
||||
export default ApiUtils;
|
||||
@ -1694,56 +1694,6 @@ class DataUtils {
|
||||
"WI",
|
||||
"WY",
|
||||
];
|
||||
|
||||
static toSnakeCaseObject(obj) {
|
||||
console.log("Converting to snake case:", obj);
|
||||
const newObj = Object.entries(obj).reduce((acc, [key, value]) => {
|
||||
const snakeKey = key.replace(/[A-Z]/g, (match) => "_" + match.toLowerCase());
|
||||
if (key === "sorting") {
|
||||
value = value
|
||||
? value.map((item) => {
|
||||
const [field, order] = item;
|
||||
const snakeField = field.replace(
|
||||
/[A-Z]/g,
|
||||
(match) => "_" + match.toLowerCase(),
|
||||
);
|
||||
return [snakeField, order];
|
||||
})
|
||||
: value;
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
value = value.map((item) => {
|
||||
return Object.prototype.toString.call(item) === "[object Object]"
|
||||
? this.toSnakeCaseObject(item)
|
||||
: item;
|
||||
});
|
||||
} else if (Object.prototype.toString.call(value) === "[object Object]") {
|
||||
value = this.toSnakeCaseObject(value);
|
||||
}
|
||||
acc[snakeKey] = value;
|
||||
return acc;
|
||||
}, {});
|
||||
return newObj;
|
||||
}
|
||||
|
||||
static toCamelCaseObject(obj) {
|
||||
const newObj = Object.entries(obj).reduce((acc, [key, value]) => {
|
||||
const camelKey = key.replace(/_([a-z])/g, (match, p1) => p1.toUpperCase());
|
||||
// check if value is an array
|
||||
if (Array.isArray(value)) {
|
||||
value = value.map((item) => {
|
||||
return Object.prototype.toString.call(item) === "[object Object]"
|
||||
? this.toCamelCaseObject(item)
|
||||
: item;
|
||||
});
|
||||
} else if (Object.prototype.toString.call(value) === "[object Object]") {
|
||||
value = this.toCamelCaseObject(value);
|
||||
}
|
||||
acc[camelKey] = value;
|
||||
return acc;
|
||||
}, {});
|
||||
return newObj;
|
||||
}
|
||||
}
|
||||
|
||||
export default DataUtils;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user