285 lines
11 KiB
JavaScript
285 lines
11 KiB
JavaScript
// 1 => LOADING
|
|
// 2 => DONE
|
|
// 3 => ERROR
|
|
import * as api from "../api/patient.js"
|
|
|
|
export default {
|
|
namespaced: true,
|
|
state: {
|
|
search_packet: 0,
|
|
search_error_message: '',
|
|
start_date: moment(new Date()).format('YYYY-MM-DD'),
|
|
last_search_val: '',
|
|
last_search_type: '',
|
|
end_date: moment(new Date()).format('YYYY-MM-DD'),
|
|
search: '',
|
|
search_priceheader: '',
|
|
total_packet: 0,
|
|
selected_packet: {},
|
|
save_error_message: '',
|
|
statuses: [
|
|
{ name: 'Semua', value: 'A' },
|
|
{ name: 'Sudah Terkirim', value: 'Y' },
|
|
{ name: 'Belum Terkirim', value: 'N' }
|
|
],
|
|
selected_status: { name: 'Semua', value: 'A' },
|
|
open_alert_no_pay: false,
|
|
msg_alert_no_pay: "Loh ... Gak jadi bayar dong ?",
|
|
current_page: 1,
|
|
total_page: 0,
|
|
pay_disabled: 'Y',
|
|
autocomplete_status: 0,
|
|
companies: [{ id: 0, name: 'Semua' }],
|
|
selected_company: { id: 0, name: 'Semua' },
|
|
logs: [],
|
|
unduhan: [],
|
|
loading_download: false,
|
|
loading_send: false,
|
|
monitoring_data: [],
|
|
monitoring_status: 0,
|
|
monitoring_error_message: '',
|
|
packets: [],
|
|
transaction_log: null,
|
|
dialog_info: false,
|
|
dialog_info_message: ''
|
|
},
|
|
mutations: {
|
|
set_last_search(state, { val, type }) {
|
|
state.last_search_val = val;
|
|
state.last_search_type = type;
|
|
},
|
|
update_loading_send(state, val) {
|
|
state.loading_send = val
|
|
},
|
|
update_loading_download(state, val) {
|
|
state.loading_download = val
|
|
},
|
|
update_unduhan(state, val) {
|
|
state.unduhan = val
|
|
},
|
|
update_logs(state, val) {
|
|
state.logs = val
|
|
},
|
|
update_autocomplete_status(state, val) {
|
|
state.autocomplete_status = val
|
|
},
|
|
update_companies(state, val) {
|
|
state.companies = val
|
|
},
|
|
update_selected_company(state, val) {
|
|
state.selected_company = val
|
|
},
|
|
update_pay_disabled(state, val) {
|
|
state.pay_disabled = val
|
|
},
|
|
update_total_page(state, val) {
|
|
state.total_page = val
|
|
},
|
|
update_current_page(state, val) {
|
|
state.current_page = val
|
|
},
|
|
update_search_error_message(state, message) {
|
|
state.search_error_message = message
|
|
},
|
|
update_search_packet(state, status) {
|
|
state.search_packet = status
|
|
},
|
|
update_selected_packet(state, val) {
|
|
state.selected_packet = val
|
|
},
|
|
update_start_date(state, val) {
|
|
state.start_date = val
|
|
},
|
|
update_end_date(state, val) {
|
|
state.end_date = val
|
|
},
|
|
update_search(state, val) {
|
|
state.search = val
|
|
},
|
|
update_search_priceheader(state, val) {
|
|
state.search_priceheader = val
|
|
},
|
|
update_selected_status(state, val) {
|
|
state.selected_status = val
|
|
},
|
|
update_open_alert_no_pay(state, val) {
|
|
state.open_alert_no_pay = val
|
|
},
|
|
update_msg_alert_no_pay(state, val) {
|
|
state.msg_alert_no_pay = val
|
|
},
|
|
set_monitoring_data(state, data) {
|
|
state.monitoring_data = data;
|
|
},
|
|
set_monitoring_status(state, status) {
|
|
state.monitoring_status = status;
|
|
},
|
|
set_monitoring_error_message(state, message) {
|
|
state.monitoring_error_message = message;
|
|
},
|
|
// FIXED: Hapus duplikasi, gunakan satu mutation untuk packets
|
|
set_transaction_log(state, payload) {
|
|
state.transaction_log = payload;
|
|
},
|
|
set_dialog_info(state, status) {
|
|
state.dialog_info = status;
|
|
},
|
|
set_dialog_info_message(state, message) {
|
|
state.dialog_info_message = message;
|
|
}
|
|
},
|
|
actions: {
|
|
async search(context, prm) {
|
|
context.commit("update_search_packet", 1)
|
|
try {
|
|
prm.token = one_token()
|
|
prm.company = context.state.selected_company.id
|
|
let resp = await api.search(prm)
|
|
if (resp.status != "OK") {
|
|
context.commit("update_search_packet", 3)
|
|
context.commit("update_search_error_message", resp.message)
|
|
} else {
|
|
context.commit("update_search_packet", 2)
|
|
context.commit("update_search_error_message", "")
|
|
let data = {
|
|
records: resp.data.records,
|
|
total: resp.data.total
|
|
}
|
|
context.commit("update_packets", data)
|
|
context.commit("update_selected_packet", {})
|
|
context.commit("update_total_page", data.total)
|
|
}
|
|
} catch (e) {
|
|
context.commit("update_search_packet", 3)
|
|
context.commit("update_search_error_message", e.message)
|
|
console.log(e)
|
|
}
|
|
},
|
|
|
|
async monitoring_packet(context, prm) {
|
|
context.commit("set_monitoring_status", 1);
|
|
|
|
try {
|
|
let resp = await api.monitoring_packet({
|
|
page: prm.page,
|
|
packet_name: prm.packet_name,
|
|
packet_code: prm.packet_code,
|
|
priceheader_name: prm.priceheader_name,
|
|
priceheader_code: prm.priceheader_code,
|
|
packet_status: prm.packet_status
|
|
});
|
|
|
|
if (resp.status !== "success") {
|
|
context.commit("set_monitoring_status", 3);
|
|
context.commit("set_monitoring_error_message", resp.message);
|
|
return;
|
|
}
|
|
|
|
const wrapped = {
|
|
data: resp.data,
|
|
total: resp.total,
|
|
page: resp.page,
|
|
limit: resp.limit,
|
|
total_page: resp.total_page
|
|
};
|
|
|
|
context.commit("set_monitoring_data", wrapped);
|
|
context.commit("set_monitoring_error_message", "");
|
|
context.commit("update_total_page", resp.total_page);
|
|
context.commit("update_current_page", resp.page);
|
|
context.commit("set_monitoring_status", 2);
|
|
|
|
} catch (err) {
|
|
context.commit("set_monitoring_status", 3);
|
|
context.commit("set_monitoring_error_message", err.message);
|
|
}
|
|
},
|
|
|
|
async get_json_response({ commit, state }) {
|
|
commit('set_transaction_log', null);
|
|
commit('set_monitoring_status', 1);
|
|
|
|
if (!state.selected_packet || !state.selected_packet.packet_code) {
|
|
commit('set_monitoring_status', 2);
|
|
return;
|
|
}
|
|
|
|
const params = {
|
|
packet_code: state.selected_packet.packet_code
|
|
};
|
|
|
|
const response = await api.get_json_response(params);
|
|
|
|
if (response.status === 'success' && response.data) {
|
|
commit('set_transaction_log', response.data);
|
|
} else {
|
|
commit('set_transaction_log', null);
|
|
console.error("Failed to get JSON response:", response.message);
|
|
}
|
|
commit('set_monitoring_status', 2);
|
|
},
|
|
|
|
async send_packet(context, prm) {
|
|
context.commit("set_monitoring_status", 1);
|
|
try {
|
|
// 1. Check if packet exists in AIS
|
|
const checkResponse = await api.get_packet_by_code(prm);
|
|
|
|
let postResponse;
|
|
let actionMessage;
|
|
|
|
// 2. Decide to re-post or post based on the check
|
|
if (checkResponse.status === 'success' && checkResponse.data != null) {
|
|
// Data exists, use re_post
|
|
postResponse = await api.re_post_packet_by_code(prm);
|
|
actionMessage = "diperbarui";
|
|
} else {
|
|
// Data does not exist, use post
|
|
postResponse = await api.post_packet_by_code(prm);
|
|
actionMessage = "dikirim";
|
|
}
|
|
|
|
// 3. Handle the response of the post/re-post action
|
|
if (postResponse.status === 'success') {
|
|
context.commit("set_dialog_info_message", `Data paket berhasil ${actionMessage}.`);
|
|
|
|
// Refresh table and logs
|
|
await context.dispatch("monitoring_packet", {
|
|
// packet_name: context.state.search,
|
|
packet_status: context.state.selected_status.value,
|
|
page: context.state.current_page,
|
|
...(() => {
|
|
const params = {};
|
|
const searchVal = context.state.search.trim();
|
|
if (searchVal) {
|
|
/^[a-zA-Z0-9]+$/.test(searchVal) ? params.packet_code = searchVal : params.packet_name = searchVal;
|
|
}
|
|
const searchPriceHeaderVal = (context.state.search_priceheader || '').trim();
|
|
if (searchPriceHeaderVal) {
|
|
/^[a-zA-Z0-9]+$/.test(searchPriceHeaderVal) ? params.priceheader_code = searchPriceHeaderVal : params.priceheader_name = searchPriceHeaderVal;
|
|
}
|
|
return params;
|
|
})()
|
|
});
|
|
|
|
const updatedItem = context.state.monitoring_data.data?.find(
|
|
item => item.packet_code === prm.packet_code
|
|
);
|
|
if (updatedItem) {
|
|
context.commit("update_selected_packet", updatedItem);
|
|
await context.dispatch("get_json_response");
|
|
}
|
|
} else {
|
|
const errorMessage = postResponse.message || `Gagal ${actionMessage} data paket.`;
|
|
context.commit("set_dialog_info_message", errorMessage);
|
|
}
|
|
|
|
} catch (e) {
|
|
context.commit("set_dialog_info_message", "Terjadi kesalahan pada sistem: " + e.message);
|
|
} finally {
|
|
context.commit("set_dialog_info", true);
|
|
context.commit("set_monitoring_status", 2);
|
|
}
|
|
}
|
|
}
|
|
} |