Service Library - networking

This document provides a complete reference of the custom code library for the networking 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>").

ensureNoDuplicateOrBlocked.js

// Throws if: sender or receiver are the same, already connected, active request exists between these users
module.exports = async function ensureNoDuplicateOrBlocked(senderId, receiverId) {
  if (senderId === receiverId) throw new Error('Cannot connect to yourself.');
  // Check for existing connection
  const existing = await this.sequelize.models.connection.findOne({ where: { isActive: true, [this.sequelize.Op.or]: [ { userId1: senderId, userId2: receiverId }, { userId1: receiverId, userId2: senderId } ] } });
  if (existing) throw new Error('Users are already connected.');
  // Check for pending requests either direction
  const req = await this.sequelize.models.connectionRequest.findOne({ where: { isActive: true, status: 0, [this.sequelize.Op.or]: [ { senderUserId: senderId, receiverUserId: receiverId }, { senderUserId: receiverId, receiverUserId: senderId } ] } });
  if (req) throw new Error('Active connection request already exists.');
  return true;
}

ensureUpdateAllowed.js

// Throws if sessionUserId is not receiver or status is not 'pending'.
module.exports = function ensureUpdateAllowed(sessionUserId, requestObject) {
  if (!requestObject) throw new Error('Request not found.');
  if (requestObject.status !== 0) throw new Error('Request already responded to.');
  if (requestObject.receiverUserId !== sessionUserId) throw new Error('Only the receiver can accept/reject this request.');
  return true;
};

ensureDeleteAllowed.js

// Only sender or receiver may delete request.
module.exports = function ensureDeleteAllowed(sessionUserId, requestObject) {
  if (!requestObject) throw new Error('Request not found.');
  if (requestObject.senderUserId !== sessionUserId && requestObject.receiverUserId !== sessionUserId) {
    throw new Error('Unauthorized to delete this request.');
  }
  return true;
};

ensureDisconnectAllowed.js

// User must be part of the connection (sessionUserId == userId1 or userId2)
module.exports = function ensureDisconnectAllowed(sessionUserId, otherUserId) {
  if (!sessionUserId || !otherUserId) throw new Error('Both users required.');
  if (sessionUserId === otherUserId) throw new Error('Cannot disconnect from yourself.');
  return true;
};

Edge Functions

Edge functions are custom HTTP endpoint handlers that run outside the standard Business API pipeline. Each edge function is paired with an Edge Controller that defines its REST endpoint.

helloWorld.js

Edge Controller:

module.exports = async (request) => { return { status: 200, message: "Hello from the networking edge function!", date: new Date().toISOString() }; }

sendMail.js

Edge Controller:

module.exports = async (request) => { throw new Error('Not Implemented: Use notification microservice for emails.'); }

Edge Controllers Summary

Function Name Method Path Login Required
helloWorld GET /helloworld No
sendMail GET /sendmail Yes

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