808 lines
19 KiB
JavaScript
808 lines
19 KiB
JavaScript
import ApplicationService from "../../../services/ApplicationService";
|
|
|
|
export const getVMEmissionSummary = () => {
|
|
return async (dispatch) => {
|
|
try {
|
|
const response = await ApplicationService.http()
|
|
.post(
|
|
"/graphql",
|
|
{
|
|
query: `
|
|
{
|
|
vmEmissionSummary {
|
|
vmId
|
|
vmName
|
|
vmPower
|
|
vmStatus
|
|
totalEmission
|
|
createdDate
|
|
physicalMachine
|
|
project
|
|
dataCenter
|
|
co2
|
|
ch4
|
|
n2o
|
|
reportGeneratedTime
|
|
}
|
|
}
|
|
`
|
|
},
|
|
{
|
|
headers: {
|
|
Authorization: "Bearer " + localStorage.getItem("accessToken"),
|
|
},
|
|
}
|
|
);
|
|
|
|
if (response.data.errors) {
|
|
console.error("GraphQL Errors:", response.data.errors);
|
|
throw new Error(response.data.errors[0]?.message || "GraphQL Error");
|
|
}
|
|
|
|
const vmEmissionSummary = response.data.data.vmEmissionSummary;
|
|
|
|
dispatch({
|
|
type: "GET_VM_EMISSION_SUMMARY",
|
|
payload: {
|
|
vmEmissionSummary,
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.error("API Error:", error);
|
|
throw error;
|
|
}
|
|
};
|
|
};
|
|
|
|
export const getMainDataTables = (data) => {
|
|
return async (dispatch) => {
|
|
ApplicationService.http()
|
|
.post(
|
|
"/graphql",
|
|
{
|
|
query: `
|
|
{
|
|
mainDataTables(
|
|
criteria:{
|
|
year:"${data?.filterOptions?.year?.value}"
|
|
deleted:false,
|
|
city:"${data?.filterOptions.city}",
|
|
district:"${data?.filterOptions.district}",
|
|
neighborhood:"${data?.filterOptions.neighborhood}",
|
|
}
|
|
) {
|
|
year
|
|
sector {
|
|
id
|
|
tag
|
|
}
|
|
subSector{
|
|
id
|
|
tag
|
|
}
|
|
activitySubUnit{
|
|
id
|
|
tag
|
|
}
|
|
totalEmission
|
|
emissionScope{
|
|
tag
|
|
}
|
|
emissionSource{
|
|
id
|
|
tag
|
|
}
|
|
}
|
|
}
|
|
`,
|
|
},
|
|
{
|
|
headers: {
|
|
Authorization: "Bearer " + localStorage.getItem("accessToken"),
|
|
},
|
|
}
|
|
)
|
|
.then((response) => {
|
|
const mainDataTables = response.data.data.mainDataTables;
|
|
dispatch({
|
|
type: "GET_MAIN_TABLES",
|
|
payload: {
|
|
mainDataTables,
|
|
},
|
|
});
|
|
});
|
|
};
|
|
};
|
|
|
|
export const getMainDataTablesWithPaginate = (data) => {
|
|
return async (dispatch) => {
|
|
try {
|
|
const response = await ApplicationService.http()
|
|
.post(
|
|
"/graphql",
|
|
{
|
|
query: `
|
|
{
|
|
paginateMainDataTables(
|
|
pagination: { page: 0, rowsPerPage: 100 }
|
|
criteria: { deleted: false }
|
|
sortBy: [{ field: "createdDate", direction: DESC }]
|
|
) {
|
|
content {
|
|
id
|
|
sector {
|
|
id
|
|
tag
|
|
}
|
|
subSector {
|
|
id
|
|
tag
|
|
}
|
|
co2
|
|
ch4
|
|
n2o
|
|
totalEmission
|
|
createdDate
|
|
}
|
|
pageInfo {
|
|
totalElements
|
|
totalPages
|
|
pageNumber
|
|
pageSize
|
|
}
|
|
}
|
|
}
|
|
`
|
|
},
|
|
{
|
|
headers: {
|
|
Authorization: "Bearer " + localStorage.getItem("accessToken"),
|
|
},
|
|
}
|
|
);
|
|
|
|
if (response.data.errors) {
|
|
console.error("GraphQL Errors:", response.data.errors);
|
|
throw new Error(response.data.errors[0]?.message || "GraphQL Error");
|
|
}
|
|
|
|
const mainDataTablesWithPaginate = response.data.data.paginateMainDataTables;
|
|
|
|
// Log the response for debugging
|
|
console.log("API Response:", response.data);
|
|
|
|
dispatch({
|
|
type: "GET_MAIN_TABLES_WITH_PAGINATE",
|
|
payload: {
|
|
mainDataTablesWithPaginate,
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.error("API Error:", error);
|
|
throw error;
|
|
}
|
|
};
|
|
};
|
|
|
|
export const addDataInput = (data) => async (dispatch) => {
|
|
try {
|
|
const dataToJSON = JSON.stringify(data);
|
|
const deleteQuotesFromKey = dataToJSON.replace(/"([^(")"]+)":/g, "$1:");
|
|
const response = await ApplicationService.http().post(
|
|
"/graphql",
|
|
{
|
|
query: `
|
|
mutation {
|
|
createMainDataTable(input: ${deleteQuotesFromKey}) {
|
|
id
|
|
createdDate
|
|
co2
|
|
ch4
|
|
n2o
|
|
totalEmission
|
|
deleted
|
|
}
|
|
}
|
|
`,
|
|
},
|
|
{
|
|
headers: {
|
|
Authorization: "Bearer " + localStorage.getItem("accessToken"),
|
|
},
|
|
}
|
|
);
|
|
if (response.data.errors) {
|
|
const errorMessage = response.data.errors[0].extensions.ErrorCode;
|
|
throw errorMessage;
|
|
} else {
|
|
dispatch({
|
|
type: "ADD_DATA_INPUT",
|
|
payload: {
|
|
dataInput: response.data.data.createMainDataTable,
|
|
},
|
|
});
|
|
}
|
|
} catch (error) {
|
|
console.log("Network error:", error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const createSolidWasteMainDataTable = (data) => async (dispatch) => {
|
|
try {
|
|
const dataToJSON = JSON.stringify(data);
|
|
const deleteQuotesFromKey = dataToJSON.replace(/"([^(")"]+)":/g, "$1:");
|
|
|
|
const response = await ApplicationService.http().post(
|
|
"/graphql",
|
|
{
|
|
query: `
|
|
mutation {
|
|
createSolidWasteMainDataTable(input: ${deleteQuotesFromKey}) {
|
|
id
|
|
createdDate
|
|
co2
|
|
ch4
|
|
n2o
|
|
totalEmission
|
|
deleted
|
|
}
|
|
}
|
|
`,
|
|
},
|
|
{
|
|
headers: {
|
|
Authorization: "Bearer " + localStorage.getItem("accessToken"),
|
|
},
|
|
}
|
|
);
|
|
|
|
if (response.data.errors) {
|
|
const errorMessage = response.data.errors[0].extensions.ErrorCode;
|
|
throw errorMessage;
|
|
} else {
|
|
dispatch({
|
|
type: "ADD_SOLID_WASTE",
|
|
payload: {
|
|
dataInput: response.data.data.createSolidWasteMainDataTable,
|
|
},
|
|
});
|
|
}
|
|
} catch (error) {
|
|
console.log("Network error:", error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const createWasteMainDataTable = (data) => async (dispatch) => {
|
|
try {
|
|
const dataToJSON = JSON.stringify(data);
|
|
const deleteQuotesFromKey = dataToJSON.replace(/"([^(")"]+)":/g, "$1:");
|
|
const response = await ApplicationService.http().post(
|
|
"/graphql",
|
|
{
|
|
query: `
|
|
mutation {
|
|
createWasteMainDataTable(input: ${deleteQuotesFromKey}) {
|
|
id
|
|
createdDate
|
|
co2
|
|
ch4
|
|
n2o
|
|
totalEmission
|
|
deleted
|
|
}
|
|
}
|
|
`,
|
|
},
|
|
{
|
|
headers: {
|
|
Authorization: "Bearer " + localStorage.getItem("accessToken"),
|
|
},
|
|
}
|
|
);
|
|
|
|
if (response.data.errors) {
|
|
const errorMessage = response.data.errors[0].extensions.ErrorCode;
|
|
throw errorMessage;
|
|
} else {
|
|
dispatch({
|
|
type: "ADD_WASTE",
|
|
payload: {
|
|
dataInput: response.data.data.createWasteMainDataTable,
|
|
},
|
|
});
|
|
}
|
|
} catch (error) {
|
|
console.log("Network error:", error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const updateDataInput = (data) => async (dispatch) => {
|
|
try {
|
|
let deleteIdFromData = { ...data };
|
|
delete deleteIdFromData.id;
|
|
const dataToJSON = JSON.stringify(deleteIdFromData);
|
|
const deleteQuotesFromKey = dataToJSON.replace(/"([^(")"]+)":/g, "$1:");
|
|
|
|
const response = await ApplicationService.http().post(
|
|
"/graphql",
|
|
{
|
|
query: `
|
|
mutation {
|
|
updateMainDataTable(id: "${data.id}",input: ${deleteQuotesFromKey}) {
|
|
id
|
|
createdDate
|
|
year
|
|
month
|
|
city {
|
|
id
|
|
name
|
|
}
|
|
district {
|
|
id
|
|
name
|
|
city {
|
|
id
|
|
name
|
|
}
|
|
}
|
|
neighborhood {
|
|
id
|
|
name
|
|
district {
|
|
id
|
|
name
|
|
city {
|
|
id
|
|
name
|
|
}
|
|
}
|
|
}
|
|
sector {
|
|
id
|
|
tag
|
|
}
|
|
subSector {
|
|
id
|
|
tag
|
|
}
|
|
emissionSource {
|
|
id
|
|
tag
|
|
}
|
|
activitySubUnit {
|
|
id
|
|
tag
|
|
}
|
|
consuptionUnit {
|
|
id
|
|
tag
|
|
description
|
|
}
|
|
emissionScope {
|
|
id
|
|
tag
|
|
}
|
|
gpcReference
|
|
solidWasteSupplement{
|
|
domestic
|
|
frec
|
|
garden
|
|
industrial
|
|
mcfType{
|
|
id
|
|
typeName
|
|
value
|
|
}
|
|
methaneCaptureRate
|
|
paper
|
|
textile
|
|
tree
|
|
}
|
|
consuptionAmount
|
|
co2
|
|
ch4
|
|
n2o
|
|
totalEmission
|
|
deleted
|
|
scopeCheck
|
|
}
|
|
}
|
|
`,
|
|
},
|
|
{
|
|
headers: {
|
|
Authorization: "Bearer " + localStorage.getItem("accessToken"),
|
|
},
|
|
}
|
|
);
|
|
|
|
if (response.data.errors) {
|
|
const errorMessage = response.data.errors[0].extensions.ErrorCode;
|
|
throw errorMessage;
|
|
} else {
|
|
dispatch({
|
|
type: "UPDATE_DATA_INPUT",
|
|
payload: {
|
|
dataInput: response.data.data.updateMainDataTable,
|
|
},
|
|
});
|
|
}
|
|
} catch (error) {
|
|
console.log("Network error:", error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const updateSolidWasteMainDataTable = (data) => async (dispatch) => {
|
|
try {
|
|
let deleteIdFromData = { ...data };
|
|
delete deleteIdFromData.id;
|
|
const dataToJSON = JSON.stringify(deleteIdFromData);
|
|
const deleteQuotesFromKey = dataToJSON.replace(/"([^(")"]+)":/g, "$1:");
|
|
|
|
const response = await ApplicationService.http().post(
|
|
"/graphql",
|
|
{
|
|
query: `
|
|
mutation {
|
|
updateSolidWasteMainDataTable(id: "${data.id}",input: ${deleteQuotesFromKey}) {
|
|
id
|
|
createdDate
|
|
year
|
|
month
|
|
city {
|
|
id
|
|
name
|
|
}
|
|
district {
|
|
id
|
|
name
|
|
city {
|
|
id
|
|
name
|
|
}
|
|
}
|
|
neighborhood {
|
|
id
|
|
name
|
|
district {
|
|
id
|
|
name
|
|
city {
|
|
id
|
|
name
|
|
}
|
|
}
|
|
}
|
|
sector {
|
|
id
|
|
tag
|
|
}
|
|
subSector {
|
|
id
|
|
tag
|
|
}
|
|
emissionSource {
|
|
id
|
|
tag
|
|
}
|
|
activitySubUnit {
|
|
id
|
|
tag
|
|
}
|
|
consuptionUnit {
|
|
id
|
|
tag
|
|
description
|
|
}
|
|
emissionScope {
|
|
id
|
|
tag
|
|
}
|
|
gpcReference
|
|
solidWasteSupplement{
|
|
domestic
|
|
frec
|
|
garden
|
|
industrial
|
|
mcfType{
|
|
id
|
|
typeName
|
|
value
|
|
}
|
|
methaneCaptureRate
|
|
paper
|
|
textile
|
|
tree
|
|
}
|
|
consuptionAmount
|
|
co2
|
|
ch4
|
|
n2o
|
|
totalEmission
|
|
deleted
|
|
scopeCheck
|
|
}
|
|
}
|
|
`,
|
|
},
|
|
{
|
|
headers: {
|
|
Authorization: "Bearer " + localStorage.getItem("accessToken"),
|
|
},
|
|
}
|
|
);
|
|
|
|
if (response.data.errors) {
|
|
const errorMessage = response.data.errors[0].extensions.ErrorCode;
|
|
throw errorMessage;
|
|
} else {
|
|
dispatch({
|
|
type: "UPDATE_SOLID_WASTE",
|
|
payload: {
|
|
dataInput: response.data.data.updateSolidWasteMainDataTable,
|
|
},
|
|
});
|
|
}
|
|
} catch (error) {
|
|
//console.log("Network error:", error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const updateWasteMainDataTable = (data) => async (dispatch) => {
|
|
try {
|
|
let deleteIdFromData = { ...data };
|
|
delete deleteIdFromData.id;
|
|
const dataToJSON = JSON.stringify(deleteIdFromData);
|
|
const deleteQuotesFromKey = dataToJSON.replace(/"([^(")"]+)":/g, "$1:");
|
|
|
|
const response = await ApplicationService.http().post(
|
|
"/graphql",
|
|
{
|
|
query: `
|
|
mutation {
|
|
updateWasteMainDataTable(id: "${data.id}",input: ${deleteQuotesFromKey}) {
|
|
id
|
|
createdDate
|
|
year
|
|
month
|
|
city {
|
|
id
|
|
name
|
|
}
|
|
district {
|
|
id
|
|
name
|
|
city {
|
|
id
|
|
name
|
|
}
|
|
}
|
|
neighborhood {
|
|
id
|
|
name
|
|
district {
|
|
id
|
|
name
|
|
city {
|
|
id
|
|
name
|
|
}
|
|
}
|
|
}
|
|
sector {
|
|
id
|
|
tag
|
|
}
|
|
subSector {
|
|
id
|
|
tag
|
|
}
|
|
emissionSource {
|
|
id
|
|
tag
|
|
}
|
|
activitySubUnit {
|
|
id
|
|
tag
|
|
}
|
|
consuptionUnit {
|
|
id
|
|
tag
|
|
description
|
|
}
|
|
emissionScope {
|
|
id
|
|
tag
|
|
}
|
|
gpcReference
|
|
weightType
|
|
personCount
|
|
proteinAmount
|
|
burnOrOpenBurn
|
|
consuptionAmount
|
|
co2
|
|
ch4
|
|
n2o
|
|
totalEmission
|
|
deleted
|
|
scopeCheck
|
|
}
|
|
}
|
|
`,
|
|
},
|
|
{
|
|
headers: {
|
|
Authorization: "Bearer " + localStorage.getItem("accessToken"),
|
|
},
|
|
}
|
|
);
|
|
|
|
if (response.data.errors) {
|
|
const errorMessage = response.data.errors[0].extensions.ErrorCode;
|
|
throw errorMessage;
|
|
} else {
|
|
dispatch({
|
|
type: "UPDATE_WASTE",
|
|
payload: {
|
|
dataInput: response.data.data.updateWasteMainDataTable,
|
|
},
|
|
});
|
|
}
|
|
} catch (error) {
|
|
//console.log("Network error:", error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const deleteDataInput = (dataId) => {
|
|
return async (dispatch) => {
|
|
ApplicationService.http()
|
|
.post(
|
|
"/graphql",
|
|
{
|
|
query: `mutation {
|
|
deleteMainDataTable(id:"${dataId}")
|
|
}
|
|
`,
|
|
},
|
|
{
|
|
headers: {
|
|
Authorization: "Bearer " + localStorage.getItem("accessToken"),
|
|
},
|
|
}
|
|
)
|
|
.then(() => {
|
|
dispatch({
|
|
type: "DELETE_DATA_INPUT",
|
|
payload: {
|
|
id: dataId,
|
|
},
|
|
});
|
|
})
|
|
.catch((error) => {
|
|
console.log("error", error);
|
|
});
|
|
};
|
|
};
|
|
|
|
export const getMainDataTablesByVMs = (data) => {
|
|
return async (dispatch) => {
|
|
const { filterOptions } = data;
|
|
try {
|
|
const response = await ApplicationService.http()
|
|
.post(
|
|
"/graphql",
|
|
{
|
|
query: `
|
|
{
|
|
mainDataTables(
|
|
criteria: {
|
|
year: "${filterOptions?.year?.value}"
|
|
deleted: false
|
|
}
|
|
) {
|
|
year
|
|
sector { id tag }
|
|
subSector{ id tag }
|
|
activitySubUnit{ id tag }
|
|
totalEmission
|
|
emissionScope{ tag }
|
|
emissionSource{ id tag }
|
|
vm { id name status power }
|
|
}
|
|
}
|
|
`,
|
|
},
|
|
{
|
|
headers: {
|
|
Authorization: "Bearer " + localStorage.getItem("accessToken"),
|
|
},
|
|
}
|
|
);
|
|
const allMainDataTables = response.data.data.mainDataTables || [];
|
|
dispatch({
|
|
type: "GET_MAIN_TABLES",
|
|
payload: {
|
|
mainDataTables: allMainDataTables,
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.error("Error fetching main data tables:", error);
|
|
dispatch({
|
|
type: "GET_MAIN_TABLES",
|
|
payload: {
|
|
mainDataTables: [],
|
|
},
|
|
});
|
|
}
|
|
};
|
|
};
|
|
|
|
export const testMainDataTables = () => {
|
|
return async (dispatch) => {
|
|
try {
|
|
console.log('testMainDataTables: Testing basic mainDataTables query');
|
|
|
|
const response = await ApplicationService.http()
|
|
.post(
|
|
"/graphql",
|
|
{
|
|
query: `
|
|
{
|
|
mainDataTables(
|
|
criteria: {
|
|
deleted: false
|
|
}
|
|
) {
|
|
id
|
|
year
|
|
totalEmission
|
|
vm {
|
|
id
|
|
name
|
|
status
|
|
power
|
|
}
|
|
}
|
|
}
|
|
`,
|
|
},
|
|
{
|
|
headers: {
|
|
Authorization: "Bearer " + localStorage.getItem("accessToken"),
|
|
},
|
|
}
|
|
);
|
|
|
|
console.log('testMainDataTables: Response:', response.data);
|
|
|
|
if (response.data.errors) {
|
|
console.error('testMainDataTables: GraphQL Errors:', response.data.errors);
|
|
}
|
|
|
|
const allData = response.data.data.mainDataTables || [];
|
|
console.log('testMainDataTables: All mainDataTables:', allData);
|
|
console.log('testMainDataTables: Count:', allData.length);
|
|
|
|
// Check which records have VM data
|
|
const recordsWithVM = allData.filter(record => record.vm);
|
|
console.log('testMainDataTables: Records with VM:', recordsWithVM);
|
|
console.log('testMainDataTables: Records with VM count:', recordsWithVM.length);
|
|
|
|
dispatch({
|
|
type: "GET_MAIN_TABLES",
|
|
payload: {
|
|
mainDataTables: allData,
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.error("testMainDataTables: Error:", error);
|
|
}
|
|
};
|
|
};
|