B2B-82: Add private message functions (#2)
* B2B-82: Add private message functions * refactor * create enum for medipost action types --------- Co-authored-by: Helena <helena@Helenas-MacBook-Pro.local>
This commit is contained in:
@@ -1,10 +1,14 @@
|
|||||||
import { GetPublicMessageListResponse } from "@/lib/types/medipost";
|
import {
|
||||||
|
GetMessageListResponse,
|
||||||
|
MedipostAction,
|
||||||
|
Message,
|
||||||
|
} from "@/lib/types/medipost";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { xml2json } from "xml-js";
|
import { xml2json } from "xml-js";
|
||||||
|
|
||||||
const BASE_URL = process.env.MEDIPOST_URL!;
|
const BASE_URL = process.env.MEDIPOST_URL!;
|
||||||
const USER = process.env.MEDIPOST_USER;
|
const USER = process.env.MEDIPOST_USER!;
|
||||||
const PASSWORD = process.env.MEDIPOST_PASSWORD;
|
const PASSWORD = process.env.MEDIPOST_PASSWORD!;
|
||||||
|
|
||||||
export async function getMessages() {
|
export async function getMessages() {
|
||||||
try {
|
try {
|
||||||
@@ -21,11 +25,9 @@ export async function getMessages() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function getLatestPublicMessageListItem() {
|
export async function getLatestPublicMessageListItem() {
|
||||||
const { data } = await axios.get<
|
const { data } = await axios.get<GetMessageListResponse>(BASE_URL, {
|
||||||
GetPublicMessageListResponse & { code?: number; text?: string }
|
|
||||||
>(BASE_URL, {
|
|
||||||
params: {
|
params: {
|
||||||
Action: "GetPublicMessageList",
|
Action: MedipostAction.GetPublicMessageList,
|
||||||
User: USER,
|
User: USER,
|
||||||
Password: PASSWORD,
|
Password: PASSWORD,
|
||||||
Sender: "syndev",
|
Sender: "syndev",
|
||||||
@@ -35,22 +37,16 @@ export async function getLatestPublicMessageListItem() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (data.code && data.code !== 0) {
|
if (data.code && data.code !== 0) {
|
||||||
throw new Error("Failed to get message list");
|
throw new Error("Failed to get public message list");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data?.messages?.length) {
|
return getLatestMessage(data?.messages);
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return data.messages.reduce((prev, current) =>
|
|
||||||
Number(prev.messageId) > Number(current.messageId) ? prev : current
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getPublicMessage(messageId: string) {
|
export async function getPublicMessage(messageId: string) {
|
||||||
const { data } = await axios.get(BASE_URL, {
|
const { data } = await axios.get(BASE_URL, {
|
||||||
params: {
|
params: {
|
||||||
Action: "GetPublicMessage",
|
Action: MedipostAction.GetPublicMessage,
|
||||||
User: USER,
|
User: USER,
|
||||||
Password: PASSWORD,
|
Password: PASSWORD,
|
||||||
MessageId: messageId,
|
MessageId: messageId,
|
||||||
@@ -60,6 +56,10 @@ export async function getPublicMessage(messageId: string) {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (data.code && data.code !== 0) {
|
||||||
|
throw new Error(`Failed to get public message (id: ${messageId})`);
|
||||||
|
}
|
||||||
|
|
||||||
const parsed = JSON.parse(
|
const parsed = JSON.parse(
|
||||||
xml2json(data, {
|
xml2json(data, {
|
||||||
compact: true,
|
compact: true,
|
||||||
@@ -67,9 +67,115 @@ export async function getPublicMessage(messageId: string) {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
return parsed;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function sendPrivateMessage(messageXml: string, receiver: string) {
|
||||||
|
const body = new FormData();
|
||||||
|
body.append("Action", MedipostAction.SendPrivateMessage);
|
||||||
|
body.append("User", USER);
|
||||||
|
body.append("Password", PASSWORD);
|
||||||
|
body.append("Receiver", receiver);
|
||||||
|
body.append("MessageType", "Tellimus");
|
||||||
|
body.append(
|
||||||
|
"Message",
|
||||||
|
new Blob([messageXml], {
|
||||||
|
type: "text/xml; charset=UTF-8",
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
const { data } = await axios.post(BASE_URL, body);
|
||||||
|
|
||||||
if (data.code && data.code !== 0) {
|
if (data.code && data.code !== 0) {
|
||||||
throw new Error(`Failed to get message (id: ${messageId})`);
|
throw new Error(`Failed to send private message`);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getLatestPrivateMessageListItem() {
|
||||||
|
const { data } = await axios.get<GetMessageListResponse>(BASE_URL, {
|
||||||
|
params: {
|
||||||
|
Action: MedipostAction.GetPrivateMessageList,
|
||||||
|
User: USER,
|
||||||
|
Password: PASSWORD,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (data.code && data.code !== 0) {
|
||||||
|
throw new Error("Failed to get private message list");
|
||||||
|
}
|
||||||
|
|
||||||
|
return getLatestMessage(data?.messages);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getPrivateMessage(messageId: string) {
|
||||||
|
const { data } = await axios.get(BASE_URL, {
|
||||||
|
params: {
|
||||||
|
Action: MedipostAction.GetPrivateMessage,
|
||||||
|
User: USER,
|
||||||
|
Password: PASSWORD,
|
||||||
|
MessageId: messageId,
|
||||||
|
},
|
||||||
|
headers: {
|
||||||
|
Accept: "application/xml",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (data.code && data.code !== 0) {
|
||||||
|
throw new Error(`Failed to get private message (id: ${messageId})`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const parsed = JSON.parse(
|
||||||
|
xml2json(data, {
|
||||||
|
compact: true,
|
||||||
|
spaces: 2,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
return parsed;
|
return parsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function deletePrivateMessage(messageId: string) {
|
||||||
|
const { data } = await axios.get(BASE_URL, {
|
||||||
|
params: {
|
||||||
|
Action: MedipostAction.DeletePrivateMessage,
|
||||||
|
User: USER,
|
||||||
|
Password: PASSWORD,
|
||||||
|
MessageId: messageId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (data.code && data.code !== 0) {
|
||||||
|
throw new Error(`Failed to delete private message (id: ${messageId})`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function readPrivateMessageResponse() {
|
||||||
|
try {
|
||||||
|
const privateMessage = await getLatestPrivateMessageListItem();
|
||||||
|
|
||||||
|
if (!privateMessage) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const privateMessageContent = await getPrivateMessage(
|
||||||
|
privateMessage.messageId
|
||||||
|
);
|
||||||
|
|
||||||
|
if (privateMessageContent) {
|
||||||
|
// to be implemented: map and save, only delete if successful
|
||||||
|
await deletePrivateMessage(privateMessage.messageId);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getLatestMessage(messages?: Message[]) {
|
||||||
|
if (!messages?.length) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return messages.reduce((prev, current) =>
|
||||||
|
Number(prev.messageId) > Number(current.messageId) ? prev : current
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,11 +1,22 @@
|
|||||||
export type PublicMessage = {
|
export type Message = {
|
||||||
messageId: string;
|
messageId: string;
|
||||||
messageType: string;
|
messageType: string;
|
||||||
receivedDateTime: string;
|
receivedDateTime: string;
|
||||||
sender: string;
|
sender: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type GetPublicMessageListResponse = {
|
export type GetMessageListResponse = {
|
||||||
checkedDateTime: string;
|
checkedDateTime: string;
|
||||||
messages: PublicMessage[];
|
messages: Message[];
|
||||||
|
code?: number;
|
||||||
|
text?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export enum MedipostAction {
|
||||||
|
GetPublicMessageList = "GetPublicMessageList",
|
||||||
|
GetPublicMessage = "GetPublicMessage",
|
||||||
|
SendPrivateMessage = "SendPrivateMessage",
|
||||||
|
GetPrivateMessageList = "GetPrivateMessageList",
|
||||||
|
GetPrivateMessage = "GetPrivateMessage",
|
||||||
|
DeletePrivateMessage = "DeletePrivateMessage",
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user