208 lines
6.1 KiB
JavaScript
208 lines
6.1 KiB
JavaScript
import axios from 'axios';
|
|
|
|
export const TransactionService = {
|
|
_getAuthHeader: () => {
|
|
const bmToken =
|
|
(typeof document !== 'undefined'
|
|
? document.cookie
|
|
.split('; ')
|
|
.find((row) => row.startsWith('bm-token='))
|
|
?.split('=')[1]
|
|
: null) || (typeof localStorage !== 'undefined' ? localStorage.getItem('bm-token') : null);
|
|
|
|
return bmToken && bmToken !== 'undefined' && bmToken !== 'null' ? { Authorization: `Bearer ${bmToken}` } : {};
|
|
},
|
|
/**
|
|
* Get a single transaction by ID
|
|
* @param {string} transactionId - The transaction ID
|
|
* @returns {Promise<Object>} - Transaction data
|
|
*/
|
|
getTransaction: async (transactionId) => {
|
|
const config = {
|
|
method: 'get',
|
|
url: '/api/transactions/admin/get-one/' + transactionId,
|
|
withCredentials: true
|
|
};
|
|
try {
|
|
const response = await axios(config);
|
|
return response.data.data;
|
|
} catch (err) {
|
|
console.log(err);
|
|
throw err;
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Get all transactions
|
|
* @returns {Promise<Array>} - List of all transactions
|
|
*/
|
|
getAllTransactions: async () => {
|
|
const config = {
|
|
method: 'get',
|
|
url: '/api/transactions/admin/get-all',
|
|
withCredentials: true
|
|
};
|
|
try {
|
|
const response = await axios(config);
|
|
return response.data.data;
|
|
} catch (err) {
|
|
console.log(err);
|
|
throw err;
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Get transactions related to a specific belonging
|
|
* @param {string} belongingId - The belonging ID
|
|
* @returns {Promise<Array>} - List of transactions for the belonging
|
|
*/
|
|
getTransactionsByBelongingId: async (belongingId) => {
|
|
const config = {
|
|
method: 'get',
|
|
url: `/api/transactions/admin/get-all-by-belonging/${belongingId}`,
|
|
withCredentials: true
|
|
};
|
|
try {
|
|
const response = await axios(config);
|
|
console.log(response.data);
|
|
return response.data.data;
|
|
} catch (err) {
|
|
console.log(err);
|
|
throw err;
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Get stock of a belonging ID
|
|
* @param {string} belongingId - The belonging ID
|
|
* @returns {Promise<Array>} - List of transactions for the belonging
|
|
*/
|
|
getStockByBelongingId: async (belongingId) => {
|
|
const config = {
|
|
method: 'get',
|
|
url: `/api/transactions/admin/get-stock-by-belonging/${belongingId}`,
|
|
withCredentials: true
|
|
};
|
|
try {
|
|
const response = await axios(config);
|
|
console.log(response.data);
|
|
return response.data.data;
|
|
} catch (err) {
|
|
console.log(err);
|
|
throw err;
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Create a new transaction
|
|
* @param {Object} data - Transaction data
|
|
* @returns {Promise<string>} - Success message or error
|
|
*/
|
|
createTransaction: async (data) => {
|
|
const config = {
|
|
method: 'post',
|
|
url: '/api/transactions/admin/create',
|
|
withCredentials: true,
|
|
data
|
|
};
|
|
try {
|
|
const response = await axios(config);
|
|
return 'success';
|
|
} catch (err) {
|
|
console.log(err);
|
|
return err.response?.data?.message || 'Error creating transaction';
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Update an existing transaction
|
|
* @param {string} transactionId - The transaction ID
|
|
* @param {Object} data - Updated transaction data
|
|
* @returns {Promise<string>} - Success message or error
|
|
*/
|
|
updateTransaction: async (transactionId, data) => {
|
|
const config = {
|
|
method: 'put',
|
|
url: '/api/transactions/admin/update/' + transactionId,
|
|
withCredentials: true,
|
|
data
|
|
};
|
|
try {
|
|
const response = await axios(config);
|
|
return 'success';
|
|
} catch (err) {
|
|
console.log(err);
|
|
return err.response?.data?.message || 'Error updating transaction';
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Delete a transaction
|
|
* @param {string} transactionId - The transaction ID
|
|
* @returns {Promise<string>} - Success message or error
|
|
*/
|
|
deleteTransaction: async (transactionId) => {
|
|
const config = {
|
|
method: 'delete',
|
|
url: '/api/transactions/admin/delete/' + transactionId,
|
|
withCredentials: true
|
|
};
|
|
try {
|
|
const response = await axios(config);
|
|
return 'success';
|
|
} catch (err) {
|
|
console.log(err);
|
|
return err.response?.data?.message || 'Error deleting transaction';
|
|
}
|
|
},
|
|
|
|
searchTransactions: async (query) => {
|
|
const config = {
|
|
method: 'get',
|
|
url: '/api/transactions/admin/search',
|
|
withCredentials: true,
|
|
params: query,
|
|
headers: TransactionService._getAuthHeader()
|
|
};
|
|
try {
|
|
const response = await axios(config);
|
|
return response.data;
|
|
} catch (err) {
|
|
console.log(err);
|
|
throw err;
|
|
}
|
|
},
|
|
|
|
searchTransactionsMongo: async (query) => {
|
|
const config = {
|
|
method: 'get',
|
|
url: '/api/transactions/admin/search-mongo',
|
|
withCredentials: true,
|
|
params: query,
|
|
headers: TransactionService._getAuthHeader()
|
|
};
|
|
try {
|
|
const response = await axios(config);
|
|
return response.data;
|
|
} catch (err) {
|
|
console.log(err);
|
|
throw err;
|
|
}
|
|
},
|
|
|
|
indexTransactions: async () => {
|
|
const config = {
|
|
method: 'post',
|
|
url: '/api/transactions/admin/index-all',
|
|
withCredentials: true,
|
|
headers: TransactionService._getAuthHeader()
|
|
};
|
|
try {
|
|
const response = await axios(config);
|
|
return response.data;
|
|
} catch (err) {
|
|
console.log(err);
|
|
}
|
|
}
|
|
};
|