B2B-48: Added public message get functions and generated local supabase conf (#1)
* B2B-48: Added public message get functions and local supabase conf * only get the message list with the highest id * handle case when no public list messages found * change function name to reflect behavior more clearly --------- Co-authored-by: Helena <helena@Helenas-MacBook-Pro.local>
This commit is contained in:
75
lib/services/medipost.service.ts
Normal file
75
lib/services/medipost.service.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import { GetPublicMessageListResponse } from "@/lib/types/medipost";
|
||||
import axios from "axios";
|
||||
import { xml2json } from "xml-js";
|
||||
|
||||
const BASE_URL = process.env.MEDIPOST_URL!;
|
||||
const USER = process.env.MEDIPOST_USER;
|
||||
const PASSWORD = process.env.MEDIPOST_PASSWORD;
|
||||
|
||||
export async function getMessages() {
|
||||
try {
|
||||
const publicMessage = await getLatestPublicMessageListItem();
|
||||
|
||||
if (!publicMessage) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return getPublicMessage(publicMessage.messageId);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getLatestPublicMessageListItem() {
|
||||
const { data } = await axios.get<
|
||||
GetPublicMessageListResponse & { code?: number; text?: string }
|
||||
>(BASE_URL, {
|
||||
params: {
|
||||
Action: "GetPublicMessageList",
|
||||
User: USER,
|
||||
Password: PASSWORD,
|
||||
Sender: "syndev",
|
||||
// LastChecked (date+time) can be used here to get only messages since the last check - add when cron is created
|
||||
// MessageType check only for messages of certain type
|
||||
},
|
||||
});
|
||||
|
||||
if (data.code && data.code !== 0) {
|
||||
throw new Error("Failed to get message list");
|
||||
}
|
||||
|
||||
if (!data?.messages?.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return data.messages.reduce((prev, current) =>
|
||||
Number(prev.messageId) > Number(current.messageId) ? prev : current
|
||||
);
|
||||
}
|
||||
|
||||
export async function getPublicMessage(messageId: string) {
|
||||
const { data } = await axios.get(BASE_URL, {
|
||||
params: {
|
||||
Action: "GetPublicMessage",
|
||||
User: USER,
|
||||
Password: PASSWORD,
|
||||
MessageId: messageId,
|
||||
},
|
||||
headers: {
|
||||
Accept: "application/xml",
|
||||
},
|
||||
});
|
||||
|
||||
const parsed = JSON.parse(
|
||||
xml2json(data, {
|
||||
compact: true,
|
||||
spaces: 2,
|
||||
})
|
||||
);
|
||||
|
||||
if (data.code && data.code !== 0) {
|
||||
throw new Error(`Failed to get message (id: ${messageId})`);
|
||||
}
|
||||
|
||||
return parsed;
|
||||
}
|
||||
11
lib/types/medipost.ts
Normal file
11
lib/types/medipost.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
export type PublicMessage = {
|
||||
messageId: string;
|
||||
messageType: string;
|
||||
receivedDateTime: string;
|
||||
sender: string;
|
||||
};
|
||||
|
||||
export type GetPublicMessageListResponse = {
|
||||
checkedDateTime: string;
|
||||
messages: PublicMessage[];
|
||||
};
|
||||
Reference in New Issue
Block a user