update method search

This commit is contained in:
wayanrivan
2025-05-06 17:45:25 +07:00
parent 4d0d6fd039
commit f0ed1cebe6
4 changed files with 162 additions and 61 deletions

View File

@ -236,58 +236,64 @@ const TransactionProvider = ({ children }: { children: React.ReactNode }) => {
const today = new Date();
const firstDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
// Default values
let startdate = firstDayOfMonth.toISOString().split('T')[0];
let enddate = today.toISOString().split('T')[0];
let transactioncode = '';
// let transactionstatus = '';
let type = ''
if (Array.isArray(filter)) {
// console.log(filter);
filter.forEach((f: any) => {
if (f.id === 'transaction_date' && f.value?.from && f.value?.to) {
startdate = f.value.from;
enddate = f.value.to;
}
if (f.id === 'code' && f.value) {
transactioncode = f.value;
}
// if (f.id === 'status' && f.value) {
// transactionstatus = f.value;
// }
if (f.id === 'kind' && f.value) {
type = f.value;
}
});
}
let type = '';
// ✅ Declare formattedFilter early
const formattedFilter: any = {
"Transactions.transaction_date": {
from: `${startdate} 00:00:00`,
to: `${enddate} 23:59:59`
}
};
if (transactioncode) {
formattedFilter["$or"] = [
{ "Transactions.code": { like: `%${transactioncode}%` } },
{ "Transactions.msisdn": { like: `${transactioncode}` } },
{ "origin_customer.fullname": { like: `${transactioncode}` } }
];
if (Array.isArray(filter)) {
let searchType = '';
let codeValue = '';
// Ambil code dan searchtype dulu
filter.forEach((f: any) => {
if (f.id === 'code' && f.value) {
codeValue = f.value;
}
if (f.id === 'searchtype' && f.value) {
searchType = f.value;
}
if (f.id === 'transaction_date' && f.value?.from && f.value?.to) {
startdate = f.value.from;
enddate = f.value.to;
formattedFilter["Transactions.transaction_date"] = {
from: `${startdate} 00:00:00`,
to: `${enddate} 23:59:59`
};
}
if (f.id === 'kind' && f.value) {
type = f.value;
}
});
// Terapkan berdasarkan searchtype + code
if (searchType && codeValue) {
if (searchType === "msisdn") {
formattedFilter["origin_customer.msisdn"] = { like: `%${codeValue}%` };
} else if (searchType === "fullname") {
formattedFilter["origin_customer.fullname"] = { like: `%${codeValue}%` };
} else if (searchType === "trxid") {
formattedFilter["Transactions.code"] = { like: `%${codeValue}%` };
}
}
}
// if (transactionstatus) {
// formattedFilter["Transactions.status"] = transactionstatus;
// }
if (type) {
formattedFilter["Transactions.kind"] = type;
}
const response = await GetData(`${API_URL}/transaction/history`, {
limit,
page: page + 1,