forked from NdoleStudio/httpsms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessages.ts
More file actions
95 lines (85 loc) · 2.54 KB
/
Copy pathmessages.ts
File metadata and controls
95 lines (85 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { defineStore } from 'pinia'
import type { EntitiesMessage, EntitiesBulkMessage } from '~~/shared/types/api'
import type { SearchMessagesRequest } from '~~/shared/types/message'
import { getApiErrorMessage } from '~/utils/api-error'
export type SIM = 'SIM1' | 'SIM2' | 'DEFAULT'
export interface SendMessageRequest {
from: string
to: string
content: string
sim: SIM
request_id?: string
}
export const useMessagesStore = defineStore('messages', () => {
const { apiFetch } = useApi()
const notificationsStore = useNotificationsStore()
async function sendMessage(request: SendMessageRequest) {
try {
const response = await apiFetch<{ message: string }>(
'/v1/messages/send',
{
method: 'POST',
body: request,
},
)
notificationsStore.addNotification({
message: response.message,
type: 'success',
})
} catch (e: unknown) {
notificationsStore.addNotification({
message: getApiErrorMessage(e, 'Error while sending message'),
type: 'error',
})
}
const threadsStore = useThreadsStore()
await threadsStore.loadThreads()
}
async function deleteMessage(messageId: string) {
await apiFetch(`/v1/messages/${messageId}`, { method: 'DELETE' })
notificationsStore.addNotification({
message: 'The message has been deleted successfully',
type: 'success',
})
}
async function searchMessages(
payload: SearchMessagesRequest,
): Promise<EntitiesMessage[]> {
const token = payload.token
const params = { ...payload }
delete params.token
const response = await apiFetch<{ data: EntitiesMessage[] }>(
'/v1/messages/search',
{
params,
headers: token ? { token } : undefined,
},
)
return response.data
}
async function sendBulkMessages(document: File): Promise<void> {
const formData = new FormData()
formData.append('document', document)
const response = await apiFetch<{ message?: string }>('/v1/bulk-messages', {
method: 'POST',
body: formData,
})
notificationsStore.addNotification({
message: response.message ?? 'Bulk messages sent successfully',
type: 'success',
})
}
async function fetchBulkMessageOrders(): Promise<EntitiesBulkMessage[]> {
const response = await apiFetch<{ data: EntitiesBulkMessage[] }>(
'/v1/bulk-messages',
)
return response.data ?? []
}
return {
sendMessage,
deleteMessage,
searchMessages,
sendBulkMessages,
fetchBulkMessageOrders,
}
})