Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IOCOM-1091] Add a message endpoint to create a new message #349

Merged
merged 5 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions assets/html/homeDashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
<body>
<include>assets/html/wallet2_config.html</include>
<include>assets/html/mail.html</include>
<include>assets/html/messages.html</include>
</body>
</html>
45 changes: 45 additions & 0 deletions assets/html/messages.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Messages</title>
<script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
<script>
function createNewMessage(){
const element = document.getElementById("newMessageResult");
$.ajax({
type: 'POST',
url: "/api/v1/message",
contentType: "application/json"
}).done(function(data, textStatus, jqXHR) {
element.innerHTML = `${jqXHR.status} ${jqXHR.statusText}<br/>${JSON.stringify(data)}`
}).fail(function(jqXHR, textStatus, errorThrown) {
element.innerHTML = `${jqXHR.status} ${jqXHR.statusText}<br/>${jqXHR.responseText}<br/>${JSON.stringify(errorThrown)}`
});
}

</script>
</head>

<body>
<table style="padding:16px;border: 1px solid black;margin-top:16px">
<tr>
<td>
<b>Messages</b>
</td>
</tr>
<tr class="form_item">
<td>
<button onclick="createNewMessage()">Create new message</button>
</td>
</tr>
<tr class="form_item">
<td>
<div id="newMessageResult"></div>
</td>
</tr>
</table>
</body>

</html>
14 changes: 7 additions & 7 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ const defaultConfig: IoDevServerConfig = {
{
count: 0,
template: {
unpaidValidPayments: 360,
unpaidExpiredPayments: 5,
paidPayments: 12,
failedPayments: 7,
unrelatedPayments: 20,
unpaidValidPayments: 3,
unpaidExpiredPayments: 2,
paidPayments: 4,
failedPayments: 1,
unrelatedPayments: 5,
isCancelled: false,
attachmentCount: 2,
f24Count: 19
attachmentCount: 5,
f24Count: 9
}
}
],
Expand Down
7 changes: 7 additions & 0 deletions src/features/messages/persistence/messagesDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ let inboxMessages: CreatedMessageWithContentAndAttachments[] = [];
// eslint-disable-next-line functional/no-let
let archivedMessages: CreatedMessageWithContentAndAttachments[] = [];

const addNewMessage = (message: CreatedMessageWithContentAndAttachments) =>
pipe({ ...message, is_read: false, is_archived: false }, enrichedMessage => {
inboxMessages = [enrichedMessage, ...inboxMessages];
return enrichedMessage;
});

/**
* Move the message with ID to the archived collection.
* Return false if message ID is not in the Inbox.
Expand Down Expand Up @@ -153,6 +159,7 @@ function setReadMessage(id: string) {
}

export default {
addNewMessage,
archive,
dropAll,
findAllArchived,
Expand Down
6 changes: 6 additions & 0 deletions src/features/messages/routers/messagesRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,9 @@ addHandler(
)
)
);

addHandler(messageRouter, "post", addApiV1Prefix("/message"), (_, res) =>
pipe(MessagesService.createMessage(), MessagesDB.addNewMessage, newMessage =>
res.status(201).json(newMessage)
)
);
27 changes: 26 additions & 1 deletion src/features/messages/services/messagesService.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ulid } from "ulid";
import { identity, pipe } from "fp-ts/lib/function";
import * as S from "fp-ts/lib/string";
import * as B from "fp-ts/lib/boolean";
Expand All @@ -22,6 +23,7 @@ import {
TagEnum as TagEnumPN
} from "../../../../generated/definitions/backend/MessageCategoryPN";
import { rptIdFromServiceAndPaymentData } from "../../../utils/payment";
import { ioDevServerConfig } from "../../../config";

export const getMessageCategory = (
message: CreatedMessageWithContent
Expand Down Expand Up @@ -176,7 +178,29 @@ const computeGetMessagesQueryIndexes = (
}));
};

export const handleAttachment = (
const createMessage = () =>
pipe(
ServicesDB.getLocalServices(),
localServices =>
pipe(
localServices.length,
length => Math.min(Math.floor(Math.random() * length), length - 1),
randomServiceIndex => localServices[randomServiceIndex]
),
localService =>
({
id: ulid(),
fiscal_code: ioDevServerConfig.profile.attrs.fiscal_code,
created_at: new Date(),
content: {
subject: `Created on ${new Date().toTimeString()}`,
markdown: `Message content that was created on ${new Date().toTimeString()}\n\nJust some more content to make sure that it has a viable length`
},
sender_service_id: localService.service_id
} as CreatedMessageWithContentAndAttachments)
);

const handleAttachment = (
attachment: ThirdPartyAttachment,
attachmentPollingData: Map<string, [Date, Date]>,
config: IoDevServerConfig,
Expand Down Expand Up @@ -305,6 +329,7 @@ const generateExpirationDate = (

export default {
computeGetMessagesQueryIndexes,
createMessage,
getMessageCategory,
getPublicMessages,
handleAttachment
Expand Down
Loading