You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Global-sales/src/actions/ConversationActions.js

127 lines
4.4 KiB
JavaScript

import { groupBy, pick } from '@/utils/utils';
import { fetchJSON, postJSON } from '@/utils/request'
import { parseRenderMessageList } from '@/lib/msgUtils';
import { API_HOST } from '@/config';
import { isEmpty } from '@/utils/commons';
import dayjs from 'dayjs';
export const fetchTemplates = async () => {
const data = await fetchJSON(`${API_HOST}/listtemplates`);
const canUseTemplates = (data?.result?.items || [])
.filter((_t) => _t.status !== 'REJECTED')
.map((ele) => ({ ...ele, components_origin: ele.components, components: groupBy(ele.components, (_c) => _c.type.toLowerCase()) }));
return canUseTemplates;
};
/**
*
* @param {object} params { opisn }
*/
export const fetchConversationsList = async (params) => {
const { errcode, result: data } = await fetchJSON(`${API_HOST}/getconversations`, params);
if (errcode !== 0) return [];
const list = (data || []).map((ele) => ({ ...ele, customer_name: `${ele.whatsapp_name || ''}`.trim(), whatsapp_name: `${ele.whatsapp_name || ''}`.trim() }));
return list;
};
/**
*
* @param {object} params { opisn, whatsappid, colisn }
* * opisn, colisn : 用于查询
* * whatsappid: 用于创建会话
*/
export const fetchOrderConversationsList = async (params) => {
const { errcode, result: data } = await fetchJSON(`${API_HOST}/getorderconversation`, params);
if (errcode !== 0) return [];
const list = data.map((ele) => ({ ...ele, customer_name: `${ele.whatsapp_name || ''}`.trim(), whatsapp_name: `${ele.whatsapp_name || ''}`.trim() }));
return list;
};
export const MESSAGE_PAGE_SIZE = 50;
/**
*
* @param {object} params { opisn, whatsappid, lasttime, pagesize }
*/
export const fetchMessages = async (params) => {
const defaultParams = {
opisn: '',
whatsappid: '',
lasttime: '',
pagesize: MESSAGE_PAGE_SIZE,
};
const { errcode, result } = await fetchJSON(`${API_HOST}/getcusmessages`, {...defaultParams, ...params});
return errcode !== 0 ? [] : parseRenderMessageList((result || []).reverse());
}
/**
*
* @param {object} body { opisn, conversationid }
*/
export const fetchConversationItemClose = async (body) => {
const { result } = await fetchJSON(`${API_HOST}/closeconversation`, body);
return result;
};
/**
* @param {object} body { phone_number, name }
*/
export const postNewConversationItem = async (body) => {
const { errcode, result } = await postJSON(`${API_HOST}/newconversation`, body);
return errcode !== 0 ? {} : result;
};
/**
* @param {object} params { opisn, whatsappid }
*/
export const fetchCleanUnreadMsgCount = async (params) => {
const { errcode, result } = await fetchJSON(`${API_HOST}/clean_unread_msg_count`, params);
return errcode !== 0 ? {} : result;
};
/**
* ------------------------------------------------------------------------------------------------
* 历史记录
*/
/**
* @param {object} params { search, from_date, end_date, whatsapp_id, opisn, coli_id, msg_type }
* @todo msg_type
*/
export const fetchConversationsSearch = async (params) => {
const { errcode, result: data } = await fetchJSON(`${API_HOST}/conversation_search`, params);
const list =
errcode !== 0
? []
: (data || []).map((ele) => ({
...ele,
customer_name: `${ele.whatsapp_name || ''}`.trim(),
whatsapp_name: `${ele.whatsapp_name || ''}`.trim(),
opi_sn: ele.OPI_SN || ele.opi_sn || 0,
OPI_Name: `${ele.OPI_Name || ele.opi_name || ''}`.trim(),
dateText: dayjs((ele.last_received_time || ele.last_send_time)).format('MM-DD HH:mm'),
matchMsgList: parseRenderMessageList((ele.msglist_AsJOSN || [])), // .reverse()),
}));
return list;
};
/**
*
* @param {object} params { opisn, whatsappid, lasttime, pagesize, pagedir }
*/
export const fetchMessagesHistory = async (params) => {
const defaultParams = {
opisn: '',
whatsappid: '',
lasttime: '2024-01-01T00:00:00',
pagesize: MESSAGE_PAGE_SIZE,
pagedir: 'next',
};
const _params = pick(params, Object.keys(defaultParams));
if (isEmpty(_params.opisn) || isEmpty(_params.whatsappid)) {
return [];
}
const { errcode, result } = await fetchJSON(`${API_HOST}/get_item_messages`, {...defaultParams, ..._params});
const data = errcode !== 0 ? [] : result; // _params.pagedir === 'next' ? result.reverse() : result;
return parseRenderMessageList(data);
}