update filter
This commit is contained in:
@ -164,7 +164,7 @@ const ApprovalTransactionProvider = ({ children }: { children: React.ReactNode }
|
|||||||
case 'C': return 'COMPLETE';
|
case 'C': return 'COMPLETE';
|
||||||
case 'F': return 'FAILED';
|
case 'F': return 'FAILED';
|
||||||
case 'O': return 'ON PROCESS';
|
case 'O': return 'ON PROCESS';
|
||||||
case "P" : return 'PENDING';
|
case "P": return 'PENDING';
|
||||||
case 'R': return 'ROLLBACK';
|
case 'R': return 'ROLLBACK';
|
||||||
case 'S': return 'REVERSAL';
|
case 'S': return 'REVERSAL';
|
||||||
default: return 'UNKNOWN';
|
default: return 'UNKNOWN';
|
||||||
@ -276,40 +276,16 @@ const ApprovalTransactionProvider = ({ children }: { children: React.ReactNode }
|
|||||||
let enddate = today.toISOString().split('T')[0];
|
let enddate = today.toISOString().split('T')[0];
|
||||||
let transactioncode = '';
|
let transactioncode = '';
|
||||||
let approvalstatus = '';
|
let approvalstatus = '';
|
||||||
|
let searchType = '';
|
||||||
|
let codeValue = '';
|
||||||
|
|
||||||
if (Array.isArray(filter)) {
|
if (Array.isArray(filter)) {
|
||||||
filter.forEach((f: any) => {
|
filter.forEach((f: any) => {
|
||||||
// console.log(f.id);
|
|
||||||
if (f.id === 'transaction_date' && f.value?.from && f.value?.to) {
|
if (f.id === 'transaction_date' && f.value?.from && f.value?.to) {
|
||||||
startdate = f.value.from;
|
startdate = f.value.from;
|
||||||
enddate = f.value.to;
|
enddate = f.value.to;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (f.id === 'code' && f.value) {
|
|
||||||
transactioncode = f.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (f.id === 'status_approve' && f.value) {
|
|
||||||
approvalstatus = f.value;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// ✅ Declare formattedFilter early
|
|
||||||
const formattedFilter: any = {
|
|
||||||
"Transactions.transaction_date": {
|
|
||||||
from: `${startdate} 00:00:00`,
|
|
||||||
to: `${enddate} 23:59:59`
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if (Array.isArray(filter)) {
|
|
||||||
let searchType = '';
|
|
||||||
let codeValue = '';
|
|
||||||
|
|
||||||
// Ambil code dan searchtype dulu
|
|
||||||
filter.forEach((f: any) => {
|
|
||||||
if (f.id === 'code' && f.value) {
|
if (f.id === 'code' && f.value) {
|
||||||
codeValue = f.value;
|
codeValue = f.value;
|
||||||
}
|
}
|
||||||
@ -318,29 +294,35 @@ const ApprovalTransactionProvider = ({ children }: { children: React.ReactNode }
|
|||||||
searchType = f.value;
|
searchType = f.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (f.id === 'transaction_date' && f.value?.from && f.value?.to) {
|
if (f.id === 'status_approve' && f.value) {
|
||||||
startdate = f.value.from;
|
approvalstatus = f.value;
|
||||||
enddate = f.value.to;
|
|
||||||
formattedFilter["Transactions.transaction_date"] = {
|
|
||||||
from: `${startdate} 00:00:00`,
|
|
||||||
to: `${enddate} 23:59:59`
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Terapkan berdasarkan searchtype + code
|
// 💡 Build formattedFilter only after values are finalized
|
||||||
if (searchType && codeValue) {
|
const formattedFilter: any = {};
|
||||||
if (searchType === "msisdn") {
|
|
||||||
formattedFilter["Transactions.origin_msisdn"] = { like: `%${codeValue}%` };
|
// ✅ Only add transaction_date filter if searchType is NOT filled
|
||||||
} else if (searchType === "fullname") {
|
if (!searchType) {
|
||||||
formattedFilter["Transactions.origin_name"] = { like: `%${codeValue}%` };
|
formattedFilter["Transactions.transaction_date"] = {
|
||||||
} else if (searchType === "trxid") {
|
from: `${startdate} 00:00:00`,
|
||||||
formattedFilter["Transactions.code"] = { like: `%${codeValue}%` };
|
to: `${enddate} 23:59:59`
|
||||||
}
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// ✅ Apply searchType filters if both searchType and codeValue exist
|
||||||
|
if (searchType && codeValue) {
|
||||||
|
if (searchType === "msisdn") {
|
||||||
|
formattedFilter["Transactions.origin_msisdn"] = { like: `%${codeValue}%` };
|
||||||
|
} else if (searchType === "fullname") {
|
||||||
|
formattedFilter["Transactions.origin_name"] = { like: `%${codeValue}%` };
|
||||||
|
} else if (searchType === "trxid") {
|
||||||
|
formattedFilter["Transactions.code"] = { like: `%${codeValue}%` };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle approval status filter
|
// ✅ Handle approval status
|
||||||
formattedFilter["Transactions.status_approve"] = approvalstatus
|
formattedFilter["Transactions.status_approve"] = approvalstatus
|
||||||
? approvalstatus
|
? approvalstatus
|
||||||
: { in: ["W", "Y", "N"] };
|
: { in: ["W", "Y", "N"] };
|
||||||
@ -365,7 +347,7 @@ const ApprovalTransactionProvider = ({ children }: { children: React.ReactNode }
|
|||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching transaction', error);
|
console.error('Error fetching transaction', error);
|
||||||
return { data: [], totalCount: 0 }; // Fail-safe fallback
|
return { data: [], totalCount: 0 };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -300,24 +300,15 @@ const TransactionProvider = ({ children }: { children: React.ReactNode }) => {
|
|||||||
const today = new Date();
|
const today = new Date();
|
||||||
const firstDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
|
const firstDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
|
||||||
|
|
||||||
let startdate = firstDayOfMonth.toISOString().split('T')[0];
|
let startdate: string | undefined;
|
||||||
let enddate = today.toISOString().split('T')[0];
|
let enddate: string | undefined;
|
||||||
let transactioncode = '';
|
let transactioncode = '';
|
||||||
let type = '';
|
let type = '';
|
||||||
|
let searchType = '';
|
||||||
// ✅ Declare formattedFilter early
|
let codeValue = '';
|
||||||
const formattedFilter: any = {
|
|
||||||
"Transactions.transaction_date": {
|
|
||||||
from: `${startdate} 00:00:00`,
|
|
||||||
to: `${enddate} 23:59:59`
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if (Array.isArray(filter)) {
|
if (Array.isArray(filter)) {
|
||||||
let searchType = '';
|
// Ambil semua value dari filter terlebih dahulu
|
||||||
let codeValue = '';
|
|
||||||
|
|
||||||
// Ambil code dan searchtype dulu
|
|
||||||
filter.forEach((f: any) => {
|
filter.forEach((f: any) => {
|
||||||
if (f.id === 'code' && f.value) {
|
if (f.id === 'code' && f.value) {
|
||||||
codeValue = f.value;
|
codeValue = f.value;
|
||||||
@ -327,21 +318,40 @@ const TransactionProvider = ({ children }: { children: React.ReactNode }) => {
|
|||||||
searchType = f.value;
|
searchType = f.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (f.id === 'transaction_date' && f.value?.from && f.value?.to) {
|
if (f.id === 'transaction_date' && f.value?.from && f.value?.to) {
|
||||||
// startdate = f.value.from;
|
startdate = f.value.from;
|
||||||
// enddate = f.value.to;
|
enddate = f.value.to;
|
||||||
// formattedFilter["Transactions.transaction_date"] = {
|
}
|
||||||
// from: `${startdate} 00:00:00`,
|
|
||||||
// to: `${enddate} 23:59:59`
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
|
|
||||||
if (f.id === 'kind' && f.value) {
|
if (f.id === 'kind' && f.value) {
|
||||||
type = f.value;
|
type = f.value;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Terapkan berdasarkan searchtype + code
|
// Kalau tidak ada searchType, baru pakai tanggal
|
||||||
|
if (!searchType) {
|
||||||
|
if (!startdate || !enddate) {
|
||||||
|
startdate = firstDayOfMonth.toISOString().split('T')[0];
|
||||||
|
enddate = today.toISOString().split('T')[0];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Kosongkan supaya tidak digunakan di bawah
|
||||||
|
startdate = undefined;
|
||||||
|
enddate = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build formattedFilter setelah semua nilai diketahui
|
||||||
|
const formattedFilter: any = {};
|
||||||
|
|
||||||
|
// Tambahkan filter tanggal HANYA jika tidak ada searchType
|
||||||
|
if (startdate && enddate) {
|
||||||
|
formattedFilter["Transactions.transaction_date"] = {
|
||||||
|
from: `${startdate} 00:00:00`,
|
||||||
|
to: `${enddate} 23:59:59`
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tambahkan filter berdasarkan searchType
|
||||||
if (searchType && codeValue) {
|
if (searchType && codeValue) {
|
||||||
if (searchType === "msisdn") {
|
if (searchType === "msisdn") {
|
||||||
formattedFilter["origin_customer.msisdn"] = { like: `%${codeValue}%` };
|
formattedFilter["origin_customer.msisdn"] = { like: `%${codeValue}%` };
|
||||||
@ -351,29 +361,31 @@ const TransactionProvider = ({ children }: { children: React.ReactNode }) => {
|
|||||||
formattedFilter["Transactions.code"] = { like: `%${codeValue}%` };
|
formattedFilter["Transactions.code"] = { like: `%${codeValue}%` };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tambahkan filter jenis transaksi jika ada
|
||||||
|
if (type) {
|
||||||
|
formattedFilter["Transactions.kind"] = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await GetData(`${API_URL}/transaction/history`, {
|
||||||
|
limit,
|
||||||
|
page: page + 1,
|
||||||
|
with_deleted: false,
|
||||||
|
order_field: "Transactions.created_at",
|
||||||
|
order_direction: 'DESC',
|
||||||
|
filter: JSON.stringify(formattedFilter)
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response || !response.data) {
|
||||||
|
console.warn('No data received:', response);
|
||||||
|
return { data: [], totalCount: 0 };
|
||||||
|
}
|
||||||
|
|
||||||
|
setTransaction(response.data.list);
|
||||||
|
return { data: response.data.list, totalCount: response.data.total_count };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return { data: [], totalCount: 0 };
|
||||||
if (type) {
|
|
||||||
formattedFilter["Transactions.kind"] = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = await GetData(`${API_URL}/transaction/history`, {
|
|
||||||
limit,
|
|
||||||
page: page + 1,
|
|
||||||
with_deleted: false,
|
|
||||||
order_field: "Transactions.created_at",
|
|
||||||
order_direction: 'DESC',
|
|
||||||
filter: JSON.stringify(formattedFilter)
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response || !response.data) {
|
|
||||||
console.warn('No data received:', response);
|
|
||||||
return { data: [], totalCount: 0 };
|
|
||||||
}
|
|
||||||
|
|
||||||
setTransaction(response.data.list);
|
|
||||||
return { data: response.data.list, totalCount: response.data.total_count };
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching transaction', error);
|
console.error('Error fetching transaction', error);
|
||||||
@ -382,6 +394,7 @@ const TransactionProvider = ({ children }: { children: React.ReactNode }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ManageTransactionContext.Provider
|
<ManageTransactionContext.Provider
|
||||||
value={{
|
value={{
|
||||||
|
|||||||
Reference in New Issue
Block a user