
# Service Library - `announcementManagement`

This document provides a complete reference of the custom code library for the `announcementManagement` service. It includes all library functions, edge functions with their REST endpoints, templates, and assets.


## Library Functions

Library functions are reusable modules available to all business APIs and other custom code within the service via `require("lib/<moduleName>")`.


### `processScheduledAnnouncements.js`

```js
const db = require("dbLayer");

module.exports = async () => {
  try {
    // Find all scheduled announcements where sendTime has passed
    const now = new Date();
    const scheduledAnnouncements = await db.getAnnouncementListByMQuery({
      status: 'scheduled',
      sendTime: { $lte: now }
    });
    
    if (!scheduledAnnouncements || scheduledAnnouncements.length === 0) {
      return { processed: 0, message: 'No scheduled announcements to process' };
    }
    
    // Update each announcement status to 'sent'
    const results = [];
    for (const announcement of scheduledAnnouncements) {
      try {
        const updated = await db.updateAnnouncementById(announcement.id, {
          status: 'sent'
        });
        results.push({ id: announcement.id, status: 'updated', title: announcement.title });
      } catch (err) {
        results.push({ id: announcement.id, status: 'error', error: err.message });
      }
    }
    
    return {
      processed: results.filter(r => r.status === 'updated').length,
      failed: results.filter(r => r.status === 'error').length,
      total: scheduledAnnouncements.length,
      details: results
    };
  } catch (error) {
    throw new Error(`Failed to process scheduled announcements: ${error.message}`);
  }
};
```














---

*This document was generated from the service library configuration and should be kept in sync with design changes.*
