Got the message scheduling working

This commit is contained in:
2026-06-30 11:44:23 -04:00
parent c93a581924
commit edbae3ffdf
3 changed files with 79 additions and 41 deletions
+5 -15
View File
@@ -1,15 +1,10 @@
import { API_BASE_URL } from '../constants/api'; import { API_BASE_URL } from '../constants/api';
export const scheduleApi = { export const scheduleApi = {
scheduleMessage: async (s, authBearerToken) => { scheduleMessage: async (reqBody, authBearerToken) => {
console.log('Scheduling message'); console.log('Scheduling message');
const reqBody = {
scheduled: s.scheduedTime.toISOString(),
status: s.status,
user_id: s.userId,
};
console.log(`Request body: ${reqBody}`); console.log(`Request body: ${reqBody}`);
const response = await fetch(`${API_BASE_URL}/api/v1/schedule/message`, { const response = await fetch(`${API_BASE_URL}/api/v1/schedule/message`, {
method: 'POST', method: 'POST',
headers: { headers: {
@@ -26,19 +21,14 @@ export const scheduleApi = {
} }
}, },
prepareMessage: async (req, authBearerToken) => { prepareMessage: async (reqBody, authBearerToken) => {
console.log('Preparing Scheduled message'); console.log('Preparing Scheduled message');
const reqBody = {
scheduled_message_id: req.scheduledMessageId,
status: req.status,
};
console.log(`Request body: ${reqBody}`); console.log(`Request body: ${reqBody}`);
const response = await fetch( const response = await fetch(
`${API_BASE_URL}/api/v1/schedule/status/message/update`, `${API_BASE_URL}/api/v1/schedule/message/status/update`,
{ {
method: 'POST', method: 'PATCH',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
Authorization: authBearerToken, Authorization: authBearerToken,
+1 -6
View File
@@ -1,13 +1,8 @@
import { API_BASE_URL } from '../constants/api'; import { API_BASE_URL } from '../constants/api';
export const scheduleEventApi = { export const scheduleEventApi = {
scheduleMessageEvent: async (s, authBearerToken) => { scheduleMessageEvent: async (reqBody, authBearerToken) => {
console.log('Scheduling message Event'); console.log('Scheduling message Event');
const reqBody = {
contact_id: s.contactId,
message_id: s.messageId,
scheduled_message_id: s.scheduledMessageId,
};
console.log(`Request body: ${reqBody}`); console.log(`Request body: ${reqBody}`);
const response = await fetch( const response = await fetch(
@@ -5,6 +5,8 @@ import {
sendMessageApi, sendMessageApi,
SendInstantMessageRequest, SendInstantMessageRequest,
} from '../../api/sendMessageApi'; } from '../../api/sendMessageApi';
import { scheduleApi } from '../../api/scheduleApi.js';
import { scheduleEventApi } from '../../api/scheduleEventApi.js';
const useSendMessageWizard = () => { const useSendMessageWizard = () => {
const [state, setState] = useState({ const [state, setState] = useState({
@@ -168,6 +170,56 @@ const useSendMessageWizard = () => {
})); }));
try { try {
if (state.scheduledTime != null) {
console.log('Going to schedule message');
// TODO: Put code here to schedule message
const schMsgReq = {
scheduled: state.scheduledTime.toISOString(),
status: 'PENDING',
user_id: tokenService.getUserId()
};
console.log(schMsgReq);
const token = tokenService.bearerToken();
const schMsgResponse = await scheduleApi.scheduleMessage(schMsgReq, token);
console.log(schMsgResponse);
if (schMsgResponse) {
console.log('Message scheduled');
const scheduledMessageId = schMsgResponse.data[0].id;
for (let contact of state.selectedContacts) {
let schMsgEvtReq = {
contact_id: contact,
message_id:state.createdMessage.id,
scheduled_message_id: scheduledMessageId,
};
console.log(schMsgEvtReq);
const schMsgEvtResponse = await scheduleEventApi.scheduleMessageEvent(schMsgEvtReq, token);
console.log(schMsgEvtResponse);
}
console.log('Updating message status');
const req = {
scheduled_message_id: scheduledMessageId,
status: 'READY'
};
console.log(req);
const response = await scheduleApi.prepareMessage(req, token);
if (response) {
console.log('Message scheduled');
setState((prev) => ({
...prev,
step: 'result',
isSubmitting: false,
isScheduled: false,
scheduledTime: null,
}));
}
}
} else {
console.log('Sending message'); console.log('Sending message');
let reqBod = new SendInstantMessageRequest(); let reqBod = new SendInstantMessageRequest();
reqBod.user_id = tokenService.getUserId(); reqBod.user_id = tokenService.getUserId();
@@ -190,6 +242,7 @@ const useSendMessageWizard = () => {
scheduledTime: null, scheduledTime: null,
})); }));
} }
}
} catch (error) { } catch (error) {
if (error.name === 'AbortError') { if (error.name === 'AbortError') {
console.log('Submission was aborted'); console.log('Submission was aborted');