bid-tool/auto-bid-admin/src/apis/bid.ts

121 lines
2.9 KiB
TypeScript

import { generateNestParams, handleError, handleSuccess } from '.';
import axios from '../lib/axios';
import { IBid, IWebBid } from '../system/type';
import { removeFalsyValues } from '../utils';
export const getBids = async (params: Record<string, string | number>) => {
return await axios({
url: 'bids',
params: generateNestParams(params),
withCredentials: true,
method: 'GET',
});
};
export const createBid = async (bid: Omit<IBid, 'id' | 'created_at' | 'updated_at' | 'is_system_account'>) => {
const newData = removeFalsyValues(bid);
try {
const { data } = await axios({
url: 'bids',
withCredentials: true,
method: 'POST',
data: newData,
});
handleSuccess(data);
return data;
} catch (error) {
handleError(error);
}
};
export const updateBid = async (bid: Partial<IBid>) => {
const { plus_price, max_price, quantity } = removeFalsyValues(bid, ['plus_price']);
try {
const { data } = await axios({
url: 'bids/' + bid.id,
withCredentials: true,
method: 'PUT',
data: { plus_price, max_price, quantity },
});
handleSuccess(data);
return data;
} catch (error) {
handleError(error);
}
};
export const toggleBid = async (bid: Partial<IBid>) => {
try {
const { data } = await axios({
url: 'bids/toggle/' + bid.id,
withCredentials: true,
method: 'POST',
});
handleSuccess(data);
return data;
} catch (error) {
handleError(error);
}
};
export const deleteBid = async (bid: IBid) => {
try {
const { data } = await axios({
url: 'bids/' + bid.id,
withCredentials: true,
method: 'DELETE',
});
handleSuccess(data);
return data;
} catch (error) {
handleError(error);
}
};
export const deletesBid = async (bids: IBid[]) => {
const ids = bids.reduce((prev, cur) => {
prev.push(cur.id);
return prev;
}, [] as number[]);
try {
const { data } = await axios({
url: 'bids/deletes',
withCredentials: true,
method: 'POST',
data: {
ids,
},
});
handleSuccess(data);
return data;
} catch (error) {
handleError(error);
}
};
export const getImagesWorking = async (values: (IBid | IWebBid) & { type: string }) => {
try {
const { data } = await axios({
url: `bids/images-working/${values.type.toLocaleLowerCase().replace('_', '-')}/${values.id}`,
withCredentials: true,
method: 'GET',
});
return data;
} catch (error) {
handleError(error);
}
};