Files
nobat724_front/lib/representationAdapters.js
T

98 lines
3.1 KiB
JavaScript

import moment from "moment-jalaali";
const DEFAULT_AVATAR = "/assets/images/doctor.png";
function toJalali(date) {
if (!date) return "--";
return moment(date).format("jYYYY/jM/jD");
}
export function adaptRepresentationInfo(representationInfo) {
const data = representationInfo?.data ?? representationInfo ?? {};
return {
name: data.full_name ?? "",
src: data.avatar ?? DEFAULT_AVATAR,
detail: data.city_name ? `نماینده شهر ${data.city_name}` : "نماینده",
mobile_number: data.mobile_number ?? "",
commission_percent: data.commission_percent ?? null,
bank_account: data.bank_account ?? null,
city: { label: data.city_name ?? "" },
all_patients: null,
total_turns: null,
number_of_patients: null,
today_turns: null,
payments: null,
todays_activities: [],
list_of_doctors_appointments: [],
chart_amount_income: [],
};
}
export function adaptRepresentationDashboard(dashboard) {
const stats = dashboard?.data?.stats ?? dashboard?.stats ?? {};
const daily = Array.isArray(stats.daily) ? stats.daily : [];
return {
number_of_patients: stats.total_appointments ?? 0,
today_turns: daily.length ? daily[daily.length - 1].appointments ?? 0 : 0,
payments: stats.total_revenue_rials ?? 0,
commission_rials: stats.commission_rials ?? 0,
chart_amount_income: daily.map((d) => d.revenue ?? 0),
list_of_doctors_appointments: daily.map((d) => ({
name: "",
expertise: "",
image: DEFAULT_AVATAR,
date: toJalali(d.date),
hour: "",
number_of_turns: d.appointments ?? 0,
})),
todays_activities: [],
};
}
export function buildPatientUser(profile, extras = {}) {
// GET /user-profile is double-nested: { data: { data: {...} } } (after fetchReq → response.data)
const data = profile?.data?.data ?? profile?.data ?? profile ?? {};
return {
profile: data.avatar ?? extras.avatar ?? "/assets/images/profile-user.png",
name:
[data.label ?? data.name, data.family].filter(Boolean).join(" ").trim() ||
extras.realName ||
"",
phone: data.phone ?? data.mobile ?? extras.mobile ?? "",
total_transactions: extras.total_transactions ?? 0,
number_of_turns: extras.number_of_turns ?? 0,
national_code: data.national_code ?? "",
gender: data.gender ?? "",
date_of_birth: data.birthday ?? "",
father_name: data.fathers_name ?? "",
insurance: data.basic_insurance ?? "",
insurance_number: "",
blood_group: data.blood_type ?? "",
marital_status: data.marital_status ?? "",
home_phone: data.home_phone ?? "",
education: data.education ?? "",
job: data.job ?? "",
work_phone: data.work_phone ?? "",
turns: { current: [], done: [] },
comments: [],
messages: [],
};
}
export function adaptBankAccount(bankAccount) {
if (!bankAccount) return [];
return [
{
id: 1,
card_number: bankAccount.account_number ?? "",
shaba_number: bankAccount.iban ?? "",
bank: bankAccount.bank_name ?? "",
owner_name: bankAccount.owner_name ?? "",
status: true,
},
];
}