Files
Training-VehicleService-May26/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/Notification.cpp
T
joelthomastrenser 67ac7f6625 Implement Service Refactoring
<UserStory> 1954: Implement Service Refactoring </UserStory>

UserStory #1954

<Changes>
1. Refactored notification handling to persist notifications directly in the datastore instead of maintaining notification collections within User objects.
2. Removed recipient User pointer dependencies from Notification and retained recipient user identification through recipientUserId.
3. Implemented generic observer persistence support in DataStore with shared helper methods for loading and saving observer subscriptions.
4. Added datastore-backed observer management for ServiceManagementService, PaymentManagementService, and InventoryManagementService.
5. Updated attach() and detach() operations to load, modify, and persist observer subscriptions using shared memory mappings.
6. Refactored sendNotification() implementations to create and persist Notification records directly to the datastore for subscribed observers.
7. Updated UserManagementService notification retrieval and deletion logic to operate on datastore notification records filtered by recipient user ID.
8. Removed notification ownership and observer-specific notification APIs from User and Observer classes.
9. Added configurable shared memory growth factor support and updated mapping expansion logic to use centralized configuration values.
10. Removed obsolete NotificationManagementService implementation and updated project configuration references.
11. Added DataStoreLockGuard integration for observer and notification persistence operations to ensure synchronized datastore access.
</Changes>

<Test>
N/A
</Test>

<Review>
Sreeja Reghukumar, please review
</Review>
2026-06-15 15:16:55 +05:30

259 lines
6.8 KiB
C++

/*
File: Notification.cpp
Description: Implements the Notification class which represents system notifications in the Vehicle Service Management System.
Provides constructors, accessors, and mutators for notification details such as ID, recipientID, title, message, and timestamp.
Author: Trenser
Date: 19-May-2026
*/
#include <sstream>
#include "SerializedRecords.h"
#include "Notification.h"
#include "StringHelper.h"
#include "Factory.h"
int Notification::m_uid = 0;
/*
Function: Notification
Description: Default constructor that initializes a new notification with a unique ID and null recipient.
Parameters:
- None
Returns:
- A new Notification object.
*/
Notification::Notification()
: m_id("NOT" + std::to_string(++m_uid)),
m_state(util::State::ACTIVE) {}
/*
Function: Notification
Description: Parameterized constructor that initializes a new notification with a unique ID and specified details.
Parameters:
- recipientUserId: ID of the recipient user.
- recipient: Pointer to the User object representing the recipient.
- title: Title of the notification.
- message: Message content of the notification.
- createdAt: Timestamp of when the notification was created.
Returns:
- A new Notification object.
*/
Notification::Notification(const std::string& recipientUserId, const std::string& title, const std::string& message, const util::Timestamp& createdAt)
: m_id("NOT" + std::to_string(++m_uid)),
m_recipientUserId(recipientUserId),
m_title(title),
m_message(message),
m_state(util::State::ACTIVE),
m_createdAt(createdAt) {
}
/*
Function: Notification (parameterized constructor with ID)
Description: Initializes a notification with an existing ID, recipient details,
title, message, and creation timestamp. Updates UID tracking based on ID.
Parameters:
- id: const std::string&, unique notification ID
- recipientUserId: const std::string&, ID of the recipient user
- title: const std::string&, notification title
- message: const std::string&, notification message
- createdAt: const util::Timestamp&, timestamp of creation
Returns:
- A new Notification object
*/
Notification::Notification(const std::string& id, const std::string& recipientUserId, const std::string& title, const std::string& message, const util::Timestamp& createdAt, const util::State& state)
: m_id(id),
m_recipientUserId(recipientUserId),
m_title(title),
m_message(message),
m_createdAt(createdAt),
m_state(state)
{
int idNumber = util::extractNumber(m_id);
if (idNumber > m_uid)
{
m_uid = idNumber;
}
}
/*
Function: getId
Description: Retrieves the unique ID of the notification.
Returns:
- const std::string& representing the notification ID.
*/
const std::string& Notification::getId() const
{
return m_id;
}
/*
Function: getRecipientUserId
Description: Retrieves the recipient user ID associated with the notification.
Returns:
- const std::string& representing the recipient user ID.
*/
const std::string& Notification::getRecipientUserId() const
{
return m_recipientUserId;
}
/*
Function: getTitle
Description: Retrieves the title of the notification.
Returns:
- const std::string& representing the notification title.
*/
const std::string& Notification::getTitle() const
{
return m_title;
}
/*
Function: getMessage
Description: Retrieves the message content of the notification.
Returns:
- const std::string& representing the notification message.
*/
const std::string& Notification::getMessage() const
{
return m_message;
}
/*
Function: getCreatedAt
Description: Retrieves the timestamp of when the notification was created.
Returns:
- const util::Timestamp& representing the creation timestamp.
*/
const util::Timestamp& Notification::getCreatedAt() const
{
return m_createdAt;
}
/*
Function: getState
Description: Retrieves the Notification state
Returns:
- const util::Timestamp& representing the creation timestamp.
*/
util::State Notification::getState() const
{
return m_state;
}
/*
Function: setId
Description: Sets the unique ID of the notification.
Parameters:
- id: New notification ID string.
Returns:
- void
*/
void Notification::setId(const std::string& id)
{
m_id = id;
}
/*
Function: setRecipientUserId
Description: Sets the recipient user ID for the notification.
Parameters:
- recipientUserId: New recipient user ID string.
Returns:
- void
*/
void Notification::setRecipientUserId(const std::string& recipientUserId)
{
m_recipientUserId = recipientUserId;
}
/*
Function: setTitle
Description: Sets the title of the notification.
Parameters:
- title: New notification title string.
Returns:
- void
*/
void Notification::setTitle(const std::string& title)
{
m_title = title;
}
/*
Function: setMessage
Description: Sets the message content of the notification.
Parameters:
- message: New notification message string.
Returns:
- void
*/
void Notification::setMessage(const std::string& message)
{
m_message = message;
}
/*
Function: setCreatedAt
Description: Sets the timestamp of when the notification was created.
Parameters:
- createdAt: New timestamp value.
Returns:
- void
*/
void Notification::setCreatedAt(const util::Timestamp& createdAt)
{
m_createdAt = createdAt;
}
/*
Function: setState
Description: Sets the Notification state.
Parameters:
- state: Notification state value.
Returns:
- void
*/
void Notification::setState(util::State state)
{
m_state = state;
}
/*
Function: serialize
Description: Serializes the Notification object into a SerializedNotification record.
Parameters:
- None
Returns:
- SerializedNotification: Serialized representation of the notification
*/
SerializedNotification Notification::serialize() const
{
SerializedNotification serialized = {};
strcpy_s(serialized.id, sizeof(serialized.id), m_id.c_str());
strcpy_s(serialized.recipientUserId, sizeof(serialized.recipientUserId), m_recipientUserId.c_str());
strcpy_s(serialized.title, sizeof(serialized.title), m_title.c_str());
strcpy_s(serialized.message, sizeof(serialized.message), m_message.c_str());
serialized.createdAt = m_createdAt;
serialized.state = m_state;
return serialized;
}
/*
Function: deserialize
Description: Deserializes a SerializedNotification record into a Notification object.
Parameters:
- serializedNotification: const SerializedNotification&, serialized notification record
Returns:
- Notification*: Pointer to the deserialized Notification object
*/
Notification* Notification::deserialize(const SerializedNotification& serializedNotification)
{
return Factory::getObject<Notification>(
serializedNotification.id,
serializedNotification.recipientUserId,
serializedNotification.title,
serializedNotification.message,
serializedNotification.createdAt,
serializedNotification.state);
}