diff --git a/.nvmrc b/.nvmrc index 6ed5da9..32cfab6 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -v20.2.0 +v22.18.0 diff --git a/apps/bridge-frontend/Dockerfile b/apps/bridge-frontend/Dockerfile index 25f7052..9d9eb26 100644 --- a/apps/bridge-frontend/Dockerfile +++ b/apps/bridge-frontend/Dockerfile @@ -34,10 +34,7 @@ RUN DEBIAN_FRONTEND=noninteractive apt-get update && \ dumb-init RUN mkdir -p ${APP_DIR} WORKDIR ${APP_DIR} -COPY --from=installer ${APP_DIR}/node_modules/ ./node_modules/ -COPY --from=installer ${APP_DIR}/apps/bridge-frontend/ ./apps/bridge-frontend/ -COPY --from=installer ${APP_DIR}/apps/bridge-migrations/ ./apps/bridge-migrations/ -COPY --from=installer ${APP_DIR}/package.json ./package.json +COPY --from=installer ${APP_DIR} ./ RUN chown -R node:node ${APP_DIR}/ WORKDIR ${APP_DIR}/apps/bridge-frontend/ RUN chmod +x docker-entrypoint.sh diff --git a/apps/bridge-frontend/README.md b/apps/bridge-frontend/README.md index c403366..ebec248 100644 --- a/apps/bridge-frontend/README.md +++ b/apps/bridge-frontend/README.md @@ -1,36 +1,133 @@ -This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app). +# Bridge Frontend -## Getting Started +Frontend application for managing communication bridges between various messaging platforms and the CDR Link system. -First, run the development server: +## Overview + +Bridge Frontend provides a web interface for configuring and managing communication channels including Signal, WhatsApp, Facebook, and Voice integrations. It handles bot registration, webhook configuration, and channel settings. + +## Features + +- **Channel Management**: Configure Signal, WhatsApp, Facebook, and Voice channels +- **Bot Registration**: Register and manage bots for each communication platform +- **Webhook Configuration**: Set up webhooks for message routing +- **Settings Management**: Configure channel-specific settings and behaviors +- **User Authentication**: Secure access with NextAuth.js + +## Development + +### Prerequisites + +- Node.js >= 20 +- npm >= 10 +- PostgreSQL database +- Running bridge-worker service + +### Setup ```bash +# Install dependencies +npm install + +# Run database migrations +npm run migrate:latest + +# Run development server npm run dev -# or -yarn dev -# or -pnpm dev -# or -bun dev + +# Build for production +npm run build + +# Start production server +npm run start ``` -Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. +### Environment Variables -You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file. +Required environment variables: -This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font. +- `DATABASE_URL` - PostgreSQL connection string +- `DATABASE_HOST` - Database host +- `DATABASE_NAME` - Database name +- `DATABASE_USER` - Database username +- `DATABASE_PASSWORD` - Database password +- `NEXTAUTH_URL` - Application URL +- `NEXTAUTH_SECRET` - NextAuth.js secret +- `GOOGLE_CLIENT_ID` - Google OAuth client ID +- `GOOGLE_CLIENT_SECRET` - Google OAuth client secret -## Learn More +### Available Scripts -To learn more about Next.js, take a look at the following resources: +- `npm run dev` - Start development server +- `npm run build` - Build for production +- `npm run start` - Start production server +- `npm run lint` - Run ESLint +- `npm run migrate:latest` - Run all pending migrations +- `npm run migrate:down` - Rollback last migration +- `npm run migrate:up` - Run next migration +- `npm run migrate:make` - Create new migration -- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. -- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. +## Architecture -You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome! +### Database Schema -## Deploy on Vercel +The application manages the following main entities: -The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. +- **Bots**: Communication channel bot configurations +- **Webhooks**: Webhook endpoints for external integrations +- **Settings**: Channel-specific configuration settings +- **Users**: User accounts with role-based permissions -Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details. +### API Routes + +- `/api/auth` - Authentication endpoints +- `/api/[service]/bots` - Bot management for each service +- `/api/[service]/webhooks` - Webhook configuration + +### Page Structure + +- `/` - Dashboard/home page +- `/login` - Authentication page +- `/[...segment]` - Dynamic routing for CRUD operations + - `@create` - Create new entities + - `@detail` - View entity details + - `@edit` - Edit existing entities + +## Integration + +### Database Access + +Uses Kysely ORM for type-safe database queries: + +```typescript +import { db } from '@link-stack/database' + +const bots = await db + .selectFrom('bots') + .selectAll() + .execute() +``` + +### Authentication + +Integrated with NextAuth.js using database adapter: + +```typescript +import { authOptions } from '@link-stack/auth' +``` + +## Docker Support + +```bash +# Build image +docker build -t link-stack/bridge-frontend . + +# Run with docker-compose +docker-compose -f docker/compose/bridge.yml up +``` + +## Related Services + +- **bridge-worker**: Processes messages from configured channels +- **bridge-whatsapp**: WhatsApp-specific integration service +- **bridge-migrations**: Database schema management \ No newline at end of file diff --git a/apps/bridge-frontend/app/(main)/[...segment]/@create/page.tsx b/apps/bridge-frontend/app/(main)/[...segment]/@create/page.tsx index 3973926..bc5b6b8 100644 --- a/apps/bridge-frontend/app/(main)/[...segment]/@create/page.tsx +++ b/apps/bridge-frontend/app/(main)/[...segment]/@create/page.tsx @@ -1,10 +1,11 @@ import { Create } from "@link-stack/bridge-ui"; type PageProps = { - params: { segment: string[] }; + params: Promise<{ segment: string[] }>; }; -export default function Page({ params: { segment } }: PageProps) { +export default async function Page({ params }: PageProps) { + const { segment } = await params; const service = segment[0]; return ; diff --git a/apps/bridge-frontend/app/(main)/[...segment]/@detail/page.tsx b/apps/bridge-frontend/app/(main)/[...segment]/@detail/page.tsx index 0b1f49c..e857b7d 100644 --- a/apps/bridge-frontend/app/(main)/[...segment]/@detail/page.tsx +++ b/apps/bridge-frontend/app/(main)/[...segment]/@detail/page.tsx @@ -1,11 +1,12 @@ import { db } from "@link-stack/bridge-common"; import { serviceConfig, Detail } from "@link-stack/bridge-ui"; -type Props = { - params: { segment: string[] }; +type PageProps = { + params: Promise<{ segment: string[] }>; }; -export default async function Page({ params: { segment } }: Props) { +export default async function Page({ params }: PageProps) { + const { segment } = await params; const service = segment[0]; const id = segment?.[1]; diff --git a/apps/bridge-frontend/app/(main)/[...segment]/@edit/page.tsx b/apps/bridge-frontend/app/(main)/[...segment]/@edit/page.tsx index 59977eb..82c8052 100644 --- a/apps/bridge-frontend/app/(main)/[...segment]/@edit/page.tsx +++ b/apps/bridge-frontend/app/(main)/[...segment]/@edit/page.tsx @@ -2,10 +2,11 @@ import { db } from "@link-stack/bridge-common"; import { serviceConfig, Edit } from "@link-stack/bridge-ui"; type PageProps = { - params: { segment: string[] }; + params: Promise<{ segment: string[] }>; }; -export default async function Page({ params: { segment } }: PageProps) { +export default async function Page({ params }: PageProps) { + const { segment } = await params; const service = segment[0]; const id = segment?.[1]; diff --git a/apps/bridge-frontend/app/(main)/[...segment]/page.tsx b/apps/bridge-frontend/app/(main)/[...segment]/page.tsx index e248a86..7b4fc03 100644 --- a/apps/bridge-frontend/app/(main)/[...segment]/page.tsx +++ b/apps/bridge-frontend/app/(main)/[...segment]/page.tsx @@ -2,12 +2,13 @@ import { db } from "@link-stack/bridge-common"; import { serviceConfig, List } from "@link-stack/bridge-ui"; type PageProps = { - params: { + params: Promise<{ segment: string[]; - }; + }>; }; -export default async function Page({ params: { segment } }: PageProps) { +export default async function Page({ params }: PageProps) { + const { segment } = await params; const service = segment[0]; if (!service) return null; diff --git a/apps/bridge-frontend/app/layout.tsx b/apps/bridge-frontend/app/layout.tsx index 08546c3..c1a54d6 100644 --- a/apps/bridge-frontend/app/layout.tsx +++ b/apps/bridge-frontend/app/layout.tsx @@ -2,7 +2,7 @@ import type { Metadata } from "next"; import { LicenseInfo } from "@mui/x-license"; LicenseInfo.setLicenseKey( - "c787ac6613c5f2aa0494c4285fe3e9f2Tz04OTY1NyxFPTE3NDYzNDE0ODkwMDAsUz1wcm8sTE09c3Vic2NyaXB0aW9uLEtWPTI=", + "2a7dd73ee59e3e028b96b0d2adee1ad8Tz0xMTMwOTUsRT0xNzc5MDYyMzk5MDAwLFM9cHJvLExNPXN1YnNjcmlwdGlvbixQVj1pbml0aWFsLEtWPTI=", ); export const metadata: Metadata = { diff --git a/apps/bridge-frontend/package.json b/apps/bridge-frontend/package.json index b5797a6..7595d4c 100644 --- a/apps/bridge-frontend/package.json +++ b/apps/bridge-frontend/package.json @@ -1,6 +1,6 @@ { "name": "@link-stack/bridge-frontend", - "version": "2.2.0", + "version": "3.2.0b3", "type": "module", "scripts": { "dev": "next dev", @@ -13,28 +13,30 @@ "migrate:down:one": "tsx database/migrate.ts down:one" }, "dependencies": { - "@auth/kysely-adapter": "^1.5.2", + "@auth/kysely-adapter": "^1.10.0", + "@mui/icons-material": "^6", + "@mui/material": "^6", + "@mui/material-nextjs": "^6", + "@mui/x-license": "^7", "@link-stack/bridge-common": "*", "@link-stack/bridge-ui": "*", - "@link-stack/ui": "*", - "@mui/icons-material": "^5", - "@mui/material": "^5", - "@mui/material-nextjs": "^5", - "@mui/x-license": "^7.18.0", - "next": "^14.2.23", - "next-auth": "^4.24.8", - "react": "18.3.1", - "react-dom": "18.3.1", - "sharp": "^0.33.5", - "tsx": "^4.19.1" + "next": "15.5.4", + "next-auth": "^4.24.11", + "react": "19.2.0", + "react-dom": "19.2.0", + "sharp": "^0.34.4", + "tsx": "^4.20.6", + "@link-stack/ui": "*" }, "devDependencies": { "@link-stack/eslint-config": "*", "@link-stack/typescript-config": "*", - "@types/node": "^22", - "@types/pg": "^8.11.10", - "@types/react": "^18", - "@types/react-dom": "^18", + "@types/node": "^24", + "@types/pg": "^8.15.5", + "@types/react": "^19", + "@types/react-dom": "^19", + "@link-stack/eslint-config": "*", + "@link-stack/typescript-config": "*", "typescript": "^5" } } diff --git a/apps/bridge-frontend/public/robots.txt b/apps/bridge-frontend/public/robots.txt new file mode 100644 index 0000000..1f53798 --- /dev/null +++ b/apps/bridge-frontend/public/robots.txt @@ -0,0 +1,2 @@ +User-agent: * +Disallow: / diff --git a/apps/bridge-frontend/tsconfig.json b/apps/bridge-frontend/tsconfig.json index e700859..22be23b 100644 --- a/apps/bridge-frontend/tsconfig.json +++ b/apps/bridge-frontend/tsconfig.json @@ -1,6 +1,10 @@ { "compilerOptions": { - "lib": ["dom", "dom.iterable", "esnext"], + "lib": [ + "dom", + "dom.iterable", + "esnext" + ], "allowJs": true, "skipLibCheck": true, "strict": true, @@ -14,14 +18,24 @@ "jsx": "preserve", "incremental": true, "paths": { - "@/*": ["./*"] + "@/*": [ + "./*" + ] }, "plugins": [ { "name": "next" } - ] + ], + "target": "ES2017" }, - "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], - "exclude": ["node_modules"] + "include": [ + "next-env.d.ts", + "**/*.ts", + "**/*.tsx", + ".next/types/**/*.ts" + ], + "exclude": [ + "node_modules" + ] } diff --git a/apps/bridge-migrations/README.md b/apps/bridge-migrations/README.md new file mode 100644 index 0000000..a9e45e3 --- /dev/null +++ b/apps/bridge-migrations/README.md @@ -0,0 +1,158 @@ +# Bridge Migrations + +Database migration management for the CDR Link bridge system. + +## Overview + +Bridge Migrations handles database schema versioning and migrations for all bridge-related tables using Kysely migration framework. It manages the database structure for authentication, messaging channels, webhooks, and settings. + +## Features + +- **Schema Versioning**: Track and apply database schema changes +- **Up/Down Migrations**: Support for rolling forward and backward +- **Type-Safe Migrations**: TypeScript-based migration files +- **Migration History**: Track applied migrations in the database +- **Multiple Migration Strategies**: Run all, run one, or rollback migrations + +## Migration Files + +Current migrations in order: + +1. **0001-add-next-auth.ts** - NextAuth.js authentication tables +2. **0002-add-signal.ts** - Signal messenger integration +3. **0003-add-whatsapp.ts** - WhatsApp integration +4. **0004-add-voice.ts** - Voice/Twilio integration +5. **0005-add-facebook.ts** - Facebook Messenger integration +6. **0006-add-webhooks.ts** - Webhook configuration +7. **0007-add-settings.ts** - Application settings +8. **0008-add-user-role.ts** - User role management + +## Development + +### Prerequisites + +- Node.js >= 20 +- npm >= 10 +- PostgreSQL database +- Database connection credentials + +### Setup + +```bash +# Install dependencies +npm install + +# Run all pending migrations +npm run migrate:latest + +# Check migration status +npm run migrate:list +``` + +### Environment Variables + +Required environment variables: + +- `DATABASE_URL` - PostgreSQL connection string +- `DATABASE_HOST` - Database host +- `DATABASE_NAME` - Database name +- `DATABASE_USER` - Database username +- `DATABASE_PASSWORD` - Database password + +### Available Scripts + +- `npm run migrate:latest` - Run all pending migrations +- `npm run migrate:up` - Run next pending migration +- `npm run migrate:down` - Rollback last migration +- `npm run migrate:up:all` - Run all migrations (alias) +- `npm run migrate:up:one` - Run one migration +- `npm run migrate:down:all` - Rollback all migrations +- `npm run migrate:down:one` - Rollback one migration +- `npm run migrate:list` - List migration status +- `npm run migrate:make ` - Create new migration file + +## Creating New Migrations + +To create a new migration: + +```bash +npm run migrate:make add-new-feature +``` + +This creates a new timestamped migration file in the `migrations/` directory. + +Example migration structure: + +```typescript +import { Kysely } from 'kysely' + +export async function up(db: Kysely): Promise { + await db.schema + .createTable('new_table') + .addColumn('id', 'serial', (col) => col.primaryKey()) + .addColumn('name', 'varchar', (col) => col.notNull()) + .addColumn('created_at', 'timestamp', (col) => + col.defaultTo('now()').notNull() + ) + .execute() +} + +export async function down(db: Kysely): Promise { + await db.schema.dropTable('new_table').execute() +} +``` + +## Database Schema + +### Core Tables + +- **users** - User accounts with roles +- **accounts** - OAuth account connections +- **sessions** - User sessions +- **verification_tokens** - Email verification + +### Communication Tables + +- **bots** - Bot configurations for each service +- **signal_messages** - Signal message history +- **whatsapp_messages** - WhatsApp message history +- **voice_messages** - Voice/call records +- **facebook_messages** - Facebook message history + +### Configuration Tables + +- **webhooks** - External webhook endpoints +- **settings** - Application settings + +## Best Practices + +1. **Test Migrations**: Always test migrations in development first +2. **Backup Database**: Create backups before running migrations in production +3. **Review Changes**: Review migration files before applying +4. **Atomic Operations**: Keep migrations focused and atomic +5. **Rollback Plan**: Ensure down() methods properly reverse changes + +## Troubleshooting + +### Common Issues + +1. **Migration Failed**: Check error logs and database permissions +2. **Locked Migrations**: Check for concurrent migration processes +3. **Missing Tables**: Ensure all previous migrations have run +4. **Connection Issues**: Verify DATABASE_URL and network access + +### Recovery + +If migrations fail: + +1. Check migration history table +2. Manually verify database state +3. Run specific migrations as needed +4. Use rollback if necessary + +## Integration + +Migrations are used by: +- **bridge-frontend** - Requires migrated schema +- **bridge-worker** - Depends on message tables +- **bridge-whatsapp** - Uses bot configuration tables \ No newline at end of file diff --git a/apps/bridge-migrations/migrate.ts b/apps/bridge-migrations/migrate.ts index 2e6281b..731018f 100644 --- a/apps/bridge-migrations/migrate.ts +++ b/apps/bridge-migrations/migrate.ts @@ -10,6 +10,9 @@ import { CamelCasePlugin, } from "kysely"; import pkg from "pg"; +import { createLogger } from "@link-stack/logger"; + +const logger = createLogger('bridge-migrations-migrate'); const { Pool } = pkg; import * as dotenv from "dotenv"; @@ -72,17 +75,17 @@ export const migrate = async (arg: string) => { results?.forEach((it) => { if (it.status === "Success") { - console.log( + logger.info( `Migration "${it.migrationName} ${it.direction.toLowerCase()}" was executed successfully`, ); } else if (it.status === "Error") { - console.error(`Failed to execute migration "${it.migrationName}"`); + logger.error(`Failed to execute migration "${it.migrationName}"`); } }); if (error) { - console.error("Failed to migrate"); - console.error(error); + logger.error("Failed to migrate"); + logger.error(error); process.exit(1); } diff --git a/apps/bridge-migrations/package.json b/apps/bridge-migrations/package.json index c3a10b6..7aba07a 100644 --- a/apps/bridge-migrations/package.json +++ b/apps/bridge-migrations/package.json @@ -1,6 +1,6 @@ { "name": "@link-stack/bridge-migrations", - "version": "2.2.0", + "version": "3.2.0b3", "type": "module", "scripts": { "migrate:up:all": "tsx migrate.ts up:all", @@ -9,14 +9,15 @@ "migrate:down:one": "tsx migrate.ts down:one" }, "dependencies": { - "dotenv": "^16.4.5", - "kysely": "0.26.1", - "pg": "^8.13.0", - "tsx": "^4.19.1" + "@link-stack/logger": "*", + "dotenv": "^17.2.3", + "kysely": "0.27.5", + "pg": "^8.16.3", + "tsx": "^4.20.6" }, "devDependencies": { - "@types/node": "^22", - "@types/pg": "^8.11.10", + "@types/node": "^24", + "@types/pg": "^8.15.5", "@link-stack/eslint-config": "*", "@link-stack/typescript-config": "*", "typescript": "^5" diff --git a/apps/bridge-whatsapp/README.md b/apps/bridge-whatsapp/README.md new file mode 100644 index 0000000..705ced2 --- /dev/null +++ b/apps/bridge-whatsapp/README.md @@ -0,0 +1,172 @@ +# Bridge WhatsApp + +WhatsApp integration service for the CDR Link communication bridge system. + +## Overview + +Bridge WhatsApp provides a REST API for sending and receiving WhatsApp messages using the Baileys library (WhatsApp Web API). It handles bot session management, message routing, and media processing for WhatsApp communication channels. + +## Features + +- **Bot Management**: Register and manage multiple WhatsApp bot sessions +- **Message Handling**: Send and receive text messages with formatting +- **Media Support**: Handle images, documents, audio, and video files +- **QR Code Authentication**: Web-based WhatsApp authentication +- **REST API**: Simple HTTP endpoints for integration + +## Development + +### Prerequisites + +- Node.js >= 20 +- npm >= 10 +- PostgreSQL database (for bot configuration) + +### Setup + +```bash +# Install dependencies +npm install + +# Build TypeScript +npm run build + +# Run development server +npm run dev + +# Start production server +npm run start +``` + +### Environment Variables + +- `PORT` - Server port (default: 5000) +- `DATABASE_URL` - PostgreSQL connection string (optional) +- Additional WhatsApp-specific configuration as needed + +### Available Scripts + +- `npm run build` - Compile TypeScript +- `npm run dev` - Development mode with auto-reload +- `npm run start` - Start production server + +## API Endpoints + +### Bot Management + +- `POST /api/bots/:token` - Register/initialize a bot +- `GET /api/bots/:token` - Get bot status and QR code + +### Messaging + +- `POST /api/bots/:token/send` - Send a message +- `POST /api/bots/:token/receive` - Webhook for incoming messages + +### Request/Response Format + +#### Send Message + +```json +{ + "to": "1234567890@s.whatsapp.net", + "message": "Hello World", + "media": { + "url": "https://example.com/image.jpg", + "type": "image" + } +} +``` + +#### Receive Message Webhook + +```json +{ + "from": "1234567890@s.whatsapp.net", + "message": "Hello", + "timestamp": "2024-01-01T00:00:00Z", + "media": { + "url": "https://...", + "type": "image", + "mimetype": "image/jpeg" + } +} +``` + +## Architecture + +### Server Framework + +Built with Hapi.js for: + +- Route validation +- Plugin architecture +- Error handling +- Request lifecycle + +### WhatsApp Integration + +Uses @whiskeysockets/baileys: + +- WhatsApp Web protocol +- Multi-device support +- Message encryption +- Media handling + +### Session Management + +- File-based session storage +- Automatic reconnection +- QR code regeneration +- Session cleanup + +## Media Handling + +Supported media types: + +- **Images**: JPEG, PNG, GIF +- **Documents**: PDF, DOC, DOCX +- **Audio**: MP3, OGG, WAV +- **Video**: MP4, AVI + +Media is processed and uploaded before sending. + +## Error Handling + +- Connection errors trigger reconnection +- Invalid sessions regenerate QR codes +- API errors return appropriate HTTP status codes +- Comprehensive logging for debugging + +## Security + +- Token-based bot authentication +- Message validation +- Rate limiting (configurable) +- Secure session storage + +## Integration + +Designed to work with: + +- **bridge-worker**: Processes WhatsApp message jobs +- **bridge-frontend**: Manages bot configuration +- External webhooks for message routing + +## Docker Support + +```bash +# Build image +docker build -t link-stack/bridge-whatsapp . + +# Run container +docker run -p 5000:5000 link-stack/bridge-whatsapp +``` + +## Testing + +While test configuration exists (jest.config.json), tests should be implemented for: + +- API endpoint validation +- Message processing logic +- Session management +- Error scenarios diff --git a/apps/bridge-whatsapp/package.json b/apps/bridge-whatsapp/package.json index a998164..5c80ea6 100644 --- a/apps/bridge-whatsapp/package.json +++ b/apps/bridge-whatsapp/package.json @@ -1,26 +1,27 @@ { "name": "@link-stack/bridge-whatsapp", - "version": "2.2.0", + "version": "3.2.0b3", "main": "build/main/index.js", "author": "Darren Clarke ", "license": "AGPL-3.0-or-later", "dependencies": { "@adiwajshing/keyed-db": "0.2.4", - "@hapi/hapi": "^21.3.10", + "@hapi/hapi": "^21.4.3", "@hapipal/schmervice": "^3.0.0", "@hapipal/toys": "^4.0.0", - "@whiskeysockets/baileys": "^6.7.8", - "hapi-pino": "^12.1.0", - "link-preview-js": "^3.0.5" + "@link-stack/logger": "*", + "@whiskeysockets/baileys": "^6.7.20", + "hapi-pino": "^13.0.0", + "link-preview-js": "^3.1.0" }, "devDependencies": { "@link-stack/eslint-config": "*", "@link-stack/jest-config": "*", "@link-stack/typescript-config": "*", "@types/node": "*", - "dotenv-cli": "^7.4.2", - "tsx": "^4.19.1", - "typescript": "^5.6.2" + "dotenv-cli": "^10.0.0", + "tsx": "^4.20.6", + "typescript": "^5.9.3" }, "scripts": { "build": "tsc -p tsconfig.json", diff --git a/apps/bridge-whatsapp/src/index.ts b/apps/bridge-whatsapp/src/index.ts index 21814f4..5be8763 100644 --- a/apps/bridge-whatsapp/src/index.ts +++ b/apps/bridge-whatsapp/src/index.ts @@ -9,6 +9,9 @@ import { SendMessageRoute, ReceiveMessageRoute, } from "./routes.js"; +import { createLogger } from "@link-stack/logger"; + +const logger = createLogger('bridge-whatsapp-index'); const server = Hapi.server({ port: 5000 }); @@ -34,6 +37,6 @@ const main = async () => { }; main().catch((err) => { - console.error(err); + logger.error(err); process.exit(1); }); diff --git a/apps/bridge-whatsapp/src/routes.ts b/apps/bridge-whatsapp/src/routes.ts index 10c9757..1d9caf6 100644 --- a/apps/bridge-whatsapp/src/routes.ts +++ b/apps/bridge-whatsapp/src/routes.ts @@ -17,6 +17,7 @@ const getService = (request: Hapi.Request): WhatsappService => { interface MessageRequest { phoneNumber: string; message: string; + attachments?: Array<{ data: string; filename: string; mime_type: string }>; } export const SendMessageRoute = withDefaults({ @@ -26,11 +27,23 @@ export const SendMessageRoute = withDefaults({ description: "Send a message", async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) { const { id } = request.params; - console.log({ payload: request.payload }); - const { phoneNumber, message } = request.payload as MessageRequest; + const { phoneNumber, message, attachments } = + request.payload as MessageRequest; const whatsappService = getService(request); - await whatsappService.send(id, phoneNumber, message as string); - request.logger.info({ id }, "Sent a message at %s", new Date()); + await whatsappService.send( + id, + phoneNumber, + message as string, + attachments, + ); + request.logger.info( + { + id, + attachmentCount: attachments?.length || 0, + }, + "Sent a message at %s", + new Date().toISOString(), + ); return _h .response({ @@ -56,7 +69,7 @@ export const ReceiveMessageRoute = withDefaults({ const date = new Date(); const twoDaysAgo = new Date(date.getTime()); twoDaysAgo.setDate(date.getDate() - 2); - request.logger.info({ id }, "Received messages at %s", new Date()); + request.logger.info({ id }, "Received messages at %s", new Date().toISOString()); return whatsappService.receive(id, twoDaysAgo); }, diff --git a/apps/bridge-whatsapp/src/service.ts b/apps/bridge-whatsapp/src/service.ts index 4f6eafa..1708fa9 100644 --- a/apps/bridge-whatsapp/src/service.ts +++ b/apps/bridge-whatsapp/src/service.ts @@ -11,6 +11,9 @@ import makeWASocket, { useMultiFileAuthState, } from "@whiskeysockets/baileys"; import fs from "fs"; +import { createLogger } from "@link-stack/logger"; + +const logger = createLogger("bridge-whatsapp-service"); export type AuthCompleteCallback = (error?: string) => void; @@ -57,7 +60,7 @@ export default class WhatsappService extends Service { try { connection.end(null); } catch (error) { - console.log(error); + logger.error({ error }, "Connection reset error"); } } this.connections = {}; @@ -92,27 +95,27 @@ export default class WhatsappService extends Service { isNewLogin, } = update; if (qr) { - console.log("got qr code"); + logger.info("got qr code"); const botDirectory = this.getBotDirectory(botID); const qrPath = `${botDirectory}/qr.txt`; fs.writeFileSync(qrPath, qr, "utf8"); } else if (isNewLogin) { - console.log("got new login"); + logger.info("got new login"); const botDirectory = this.getBotDirectory(botID); const verifiedFile = `${botDirectory}/verified`; fs.writeFileSync(verifiedFile, ""); } else if (connectionState === "open") { - console.log("opened connection"); + logger.info("opened connection"); } else if (connectionState === "close") { - console.log("connection closed due to ", lastDisconnect?.error); + logger.info({ lastDisconnect }, "connection closed"); const disconnectStatusCode = (lastDisconnect?.error as any)?.output ?.statusCode; if (disconnectStatusCode === DisconnectReason.restartRequired) { - console.log("reconnecting after got new login"); + logger.info("reconnecting after got new login"); await this.createConnection(botID, server, options); authCompleteCallback?.(); } else if (disconnectStatusCode !== DisconnectReason.loggedOut) { - console.log("reconnecting"); + logger.info("reconnecting"); await this.sleep(pause); pause *= 2; this.createConnection(botID, server, options); @@ -121,12 +124,12 @@ export default class WhatsappService extends Service { } if (events["creds.update"]) { - console.log("creds update"); + logger.info("creds update"); await saveCreds(); } if (events["messages.upsert"]) { - console.log("messages upsert"); + logger.info("messages upsert"); const upsert = events["messages.upsert"]; const { messages } = upsert; if (messages) { @@ -143,13 +146,16 @@ export default class WhatsappService extends Service { const baseDirectory = this.getBaseDirectory(); const botIDs = fs.readdirSync(baseDirectory); - console.log({ botIDs }); + for await (const botID of botIDs) { const directory = this.getBotDirectory(botID); const verifiedFile = `${directory}/verified`; if (fs.existsSync(verifiedFile)) { const { version, isLatest } = await fetchLatestBaileysVersion(); - console.log(`using WA v${version.join(".")}, isLatest: ${isLatest}`); + logger.info( + { version: version.join("."), isLatest }, + "using WA version", + ); await this.createConnection(botID, this.server, { browser: WhatsappService.browserDescription, @@ -169,7 +175,13 @@ export default class WhatsappService extends Service { message, messageTimestamp, } = webMessageInfo; - console.log(webMessageInfo); + logger.info("Message type debug"); + for (const key in message) { + logger.info( + { key, exists: !!message[key as keyof proto.IMessage] }, + "Message field", + ); + } const isValidMessage = message && remoteJid !== "status@broadcast" && !fromMe; if (isValidMessage) { @@ -186,19 +198,22 @@ export default class WhatsappService extends Service { if (isMediaMessage) { if (audioMessage) { messageType = "audio"; - filename = id + "." + audioMessage.mimetype?.split("/").pop(); + const extension = audioMessage.mimetype?.split("/").pop() || "audio"; + filename = `${id}.${extension}`; mimeType = audioMessage.mimetype; } else if (documentMessage) { messageType = "document"; - filename = documentMessage.fileName; + filename = documentMessage.fileName || `${id}.bin`; mimeType = documentMessage.mimetype; } else if (imageMessage) { messageType = "image"; - filename = id + "." + imageMessage.mimetype?.split("/").pop(); + const extension = imageMessage.mimetype?.split("/").pop() || "jpg"; + filename = `${id}.${extension}`; mimeType = imageMessage.mimetype; } else if (videoMessage) { messageType = "video"; - filename = id + "." + videoMessage.mimetype?.split("/").pop(); + const extension = videoMessage.mimetype?.split("/").pop() || "mp4"; + filename = `${id}.${extension}`; mimeType = videoMessage.mimetype; } @@ -293,10 +308,45 @@ export default class WhatsappService extends Service { botID: string, phoneNumber: string, message: string, + attachments?: Array<{ data: string; filename: string; mime_type: string }>, ): Promise { const connection = this.connections[botID]?.socket; const recipient = `${phoneNumber.replace(/\D+/g, "")}@s.whatsapp.net`; - await connection.sendMessage(recipient, { text: message }); + + // Send text message if provided + if (message) { + await connection.sendMessage(recipient, { text: message }); + } + + // Send attachments if provided + if (attachments && attachments.length > 0) { + for (const attachment of attachments) { + const buffer = Buffer.from(attachment.data, "base64"); + + if (attachment.mime_type.startsWith("image/")) { + await connection.sendMessage(recipient, { + image: buffer, + caption: attachment.filename, + }); + } else if (attachment.mime_type.startsWith("video/")) { + await connection.sendMessage(recipient, { + video: buffer, + caption: attachment.filename, + }); + } else if (attachment.mime_type.startsWith("audio/")) { + await connection.sendMessage(recipient, { + audio: buffer, + mimetype: attachment.mime_type, + }); + } else { + await connection.sendMessage(recipient, { + document: buffer, + fileName: attachment.filename, + mimetype: attachment.mime_type, + }); + } + } + } } async receive( diff --git a/apps/bridge-worker/README.md b/apps/bridge-worker/README.md new file mode 100644 index 0000000..8e5638a --- /dev/null +++ b/apps/bridge-worker/README.md @@ -0,0 +1,147 @@ +# Bridge Worker + +Background job processor for handling asynchronous tasks in the CDR Link communication bridge system. + +## Overview + +Bridge Worker uses Graphile Worker to process queued jobs for message handling, media conversion, webhook notifications, and scheduled tasks. It manages the flow of messages between various communication channels (Signal, WhatsApp, Facebook, Voice) and the Zammad ticketing system. + +## Features + +- **Message Processing**: Handle incoming/outgoing messages for all supported channels +- **Media Conversion**: Convert audio/video files between formats +- **Webhook Notifications**: Notify external systems of events +- **Scheduled Tasks**: Cron-based job scheduling +- **Job Queue Management**: Reliable job processing with retries +- **Multi-Channel Support**: Signal, WhatsApp, Facebook, Voice (Twilio) + +## Development + +### Prerequisites + +- Node.js >= 20 +- npm >= 10 +- PostgreSQL database +- Redis (for caching) +- FFmpeg (for media conversion) + +### Setup + +```bash +# Install dependencies +npm install + +# Build TypeScript +npm run build + +# Run development server with auto-reload +npm run dev + +# Start production worker +npm run start +``` + +### Environment Variables + +Required environment variables: + +- `DATABASE_URL` - PostgreSQL connection string +- `GRAPHILE_WORKER_CONCURRENCY` - Number of concurrent jobs (default: 10) +- `GRAPHILE_WORKER_POLL_INTERVAL` - Job poll interval in ms (default: 1000) +- `ZAMMAD_URL` - Zammad instance URL +- `ZAMMAD_API_TOKEN` - Zammad API token +- `TWILIO_ACCOUNT_SID` - Twilio account SID +- `TWILIO_AUTH_TOKEN` - Twilio auth token +- `SIGNAL_CLI_URL` - Signal CLI REST API URL +- `WHATSAPP_SERVICE_URL` - WhatsApp bridge service URL +- `FACEBOOK_APP_SECRET` - Facebook app secret +- `FACEBOOK_PAGE_ACCESS_TOKEN` - Facebook page token + +### Available Scripts + +- `npm run build` - Compile TypeScript +- `npm run dev` - Development mode with watch +- `npm run start` - Start production worker + +## Task Types + +### Signal Tasks +- `receive-signal-message` - Process incoming Signal messages +- `send-signal-message` - Send outgoing Signal messages +- `fetch-signal-messages` - Fetch messages from Signal CLI + +### WhatsApp Tasks +- `receive-whatsapp-message` - Process incoming WhatsApp messages +- `send-whatsapp-message` - Send outgoing WhatsApp messages + +### Facebook Tasks +- `receive-facebook-message` - Process incoming Facebook messages +- `send-facebook-message` - Send outgoing Facebook messages + +### Voice Tasks +- `receive-voice-message` - Process incoming voice calls/messages +- `send-voice-message` - Send voice messages via Twilio +- `twilio-recording` - Handle Twilio call recordings +- `voice-line-audio-update` - Update voice line audio +- `voice-line-delete` - Delete voice line +- `voice-line-provider-update` - Update voice provider settings + +### Common Tasks +- `notify-webhooks` - Send webhook notifications + +### Leafcutter Tasks +- `import-leafcutter` - Import data to Leafcutter +- `import-label-studio` - Import Label Studio annotations + +## Architecture + +### Job Processing + +Jobs are queued in PostgreSQL using Graphile Worker: + +```typescript +await addJob('send-signal-message', { + to: '+1234567890', + message: 'Hello world' +}) +``` + +### Cron Schedule + +Scheduled tasks are configured in `crontab`: +- Periodic message fetching +- Cleanup tasks +- Health checks + +### Error Handling + +- Automatic retries with exponential backoff +- Dead letter queue for failed jobs +- Comprehensive logging with winston + +## Media Handling + +Supports conversion between formats: +- Audio: MP3, OGG, WAV, M4A +- Uses fluent-ffmpeg for processing +- Automatic format detection + +## Integration Points + +- **Zammad**: Creates/updates tickets via API +- **Signal CLI**: REST API for Signal messaging +- **WhatsApp Bridge**: HTTP API for WhatsApp +- **Twilio**: Voice and SMS capabilities +- **Facebook**: Graph API for Messenger + +## Docker Support + +```bash +# Build image +docker build -t link-stack/bridge-worker . + +# Run with docker-compose +docker-compose -f docker/compose/bridge.yml up +``` + +The worker includes cron support via built-in crontab. \ No newline at end of file diff --git a/apps/bridge-worker/graphile.config.ts b/apps/bridge-worker/graphile.config.ts index 8b550c7..9367a65 100644 --- a/apps/bridge-worker/graphile.config.ts +++ b/apps/bridge-worker/graphile.config.ts @@ -4,8 +4,12 @@ import type {} from "graphile-worker"; const preset: GraphileConfig.Preset = { worker: { connectionString: process.env.DATABASE_URL, - maxPoolSize: 10, - pollInterval: 2000, + maxPoolSize: process.env.BRIDGE_WORKER_POOL_SIZE + ? parseInt(process.env.BRIDGE_WORKER_POOL_SIZE, 10) + : 10, + pollInterval: process.env.BRIDGE_WORKER_POLL_INTERVAL + ? parseInt(process.env.BRIDGE_WORKER_POLL_INTERVAL, 10) + : 2000, fileExtensions: [".ts"], }, }; diff --git a/apps/bridge-worker/index.ts b/apps/bridge-worker/index.ts index 5a7dbd5..5c83505 100644 --- a/apps/bridge-worker/index.ts +++ b/apps/bridge-worker/index.ts @@ -1,18 +1,27 @@ import { run } from "graphile-worker"; +import { createLogger } from "@link-stack/logger"; import * as path from "path"; import { fileURLToPath } from "url"; +const logger = createLogger('bridge-worker'); const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const startWorker = async () => { - console.log("Starting worker..."); - console.log(process.env); + logger.info("Starting worker..."); + await run({ connectionString: process.env.DATABASE_URL, - concurrency: 10, noHandleSignals: false, - pollInterval: 1000, + concurrency: process.env.BRIDGE_WORKER_CONCURRENCY + ? parseInt(process.env.BRIDGE_WORKER_CONCURRENCY, 10) + : 10, + maxPoolSize: process.env.BRIDGE_WORKER_POOL_SIZE + ? parseInt(process.env.BRIDGE_WORKER_POOL_SIZE, 10) + : 10, + pollInterval: process.env.BRIDGE_WORKER_POLL_INTERVAL + ? parseInt(process.env.BRIDGE_WORKER_POLL_INTERVAL, 10) + : 1000, taskDirectory: `${__dirname}/tasks`, crontabFile: `${__dirname}/crontab`, }); @@ -23,6 +32,6 @@ const main = async () => { }; main().catch((err) => { - console.error(err); + logger.error({ error: err }, 'Worker failed to start'); process.exit(1); }); diff --git a/apps/bridge-worker/lib/common.ts b/apps/bridge-worker/lib/common.ts index 26e4aeb..b74b0b6 100644 --- a/apps/bridge-worker/lib/common.ts +++ b/apps/bridge-worker/lib/common.ts @@ -3,6 +3,9 @@ import Twilio from "twilio"; import { CallInstance } from "twilio/lib/rest/api/v2010/account/call"; import { Zammad, getOrCreateUser } from "./zammad.js"; +import { createLogger } from "@link-stack/logger"; + +const logger = createLogger('bridge-worker-common'); type SavedVoiceProvider = any; @@ -62,9 +65,8 @@ export const createZammadTicket = async ( }, }); } catch (error: any) { - console.log(Object.keys(error)); if (error.isBoom) { - console.log(error.output); + logger.error({ output: error.output }, 'Zammad ticket creation failed'); throw new Error("Failed to create zamamd ticket"); } } diff --git a/apps/bridge-worker/lib/logger.ts b/apps/bridge-worker/lib/logger.ts deleted file mode 100644 index 89d34aa..0000000 --- a/apps/bridge-worker/lib/logger.ts +++ /dev/null @@ -1,11 +0,0 @@ -//import { defState } from "@digiresilience/montar"; -//import { configureLogger } from "@digiresilience/bridge-common"; -// import config from "@digiresilience/bridge-config"; - -//export const logger = defState("workerLogger", { -// start: async () => configureLogger(config), -//}); -//export default logger; - -export const logger = {}; -export default logger; diff --git a/apps/bridge-worker/lib/media-convert.ts b/apps/bridge-worker/lib/media-convert.ts index bc6a22a..90d4b5f 100644 --- a/apps/bridge-worker/lib/media-convert.ts +++ b/apps/bridge-worker/lib/media-convert.ts @@ -1,6 +1,9 @@ import { Readable } from "stream"; import ffmpeg from "fluent-ffmpeg"; import * as R from "remeda"; +import { createLogger } from "@link-stack/logger"; + +const logger = createLogger('bridge-worker-media-convert'); const requiredCodecs = ["mp3", "webm", "wav"]; @@ -25,7 +28,7 @@ const defaultAudioConvertOpts = { **/ export const convert = ( input: Buffer, - opts?: AudioConvertOpts + opts?: AudioConvertOpts, ): Promise => { const settings = { ...defaultAudioConvertOpts, ...opts }; return new Promise((resolve, reject) => { @@ -35,12 +38,8 @@ export const convert = ( .audioCodec(settings.audioCodec) .audioBitrate(settings.bitrate) .toFormat(settings.format) - .on("error", (err, stdout, stderr) => { - console.error(err.message); - console.log("FFMPEG OUTPUT"); - console.log(stdout); - console.log("FFMPEG ERROR"); - console.log(stderr); + .on("error", (err, _stdout, _stderr) => { + logger.error({ error: err }, 'FFmpeg conversion error'); reject(err); }) .on("end", () => { @@ -62,12 +61,16 @@ export const selfCheck = (): Promise => { return new Promise((resolve) => { ffmpeg.getAvailableFormats((err, codecs) => { if (err) { - console.error("FFMPEG error:", err); + logger.error({ error: err }, 'FFMPEG error'); resolve(false); } - const preds = R.map(requiredCodecs, (codec) => (available: any) => - available[codec] && available[codec].canDemux && available[codec].canMux + const preds = R.map( + requiredCodecs, + (codec) => (available: any) => + available[codec] && + available[codec].canDemux && + available[codec].canMux, ); resolve(R.allPass(codecs, preds)); @@ -79,6 +82,6 @@ export const assertFfmpegAvailable = async (): Promise => { const r = await selfCheck(); if (!r) throw new Error( - `ffmpeg is not installed, could not be located, or does not support the required codecs: ${requiredCodecs}` + `ffmpeg is not installed, could not be located, or does not support the required codecs: ${requiredCodecs}`, ); }; diff --git a/apps/bridge-worker/package.json b/apps/bridge-worker/package.json index aa162e9..3699243 100644 --- a/apps/bridge-worker/package.json +++ b/apps/bridge-worker/package.json @@ -1,6 +1,6 @@ { "name": "@link-stack/bridge-worker", - "version": "2.2.0", + "version": "3.2.0b3", "type": "module", "main": "build/main/index.js", "author": "Darren Clarke ", @@ -13,17 +13,18 @@ "dependencies": { "@hapi/wreck": "^18.1.0", "@link-stack/bridge-common": "*", + "@link-stack/logger": "*", "@link-stack/signal-api": "*", "fluent-ffmpeg": "^2.1.3", "graphile-worker": "^0.16.6", - "remeda": "^2.14.0", - "twilio": "^5.3.2" + "remeda": "^2.32.0", + "twilio": "^5.10.2" }, "devDependencies": { - "@types/fluent-ffmpeg": "^2.1.26", - "dotenv-cli": "^7.4.2", + "@types/fluent-ffmpeg": "^2.1.27", + "dotenv-cli": "^10.0.0", "@link-stack/eslint-config": "*", "@link-stack/typescript-config": "*", - "typescript": "^5.6.2" + "typescript": "^5.9.3" } } diff --git a/apps/bridge-worker/tasks/common/notify-webhooks.ts b/apps/bridge-worker/tasks/common/notify-webhooks.ts index 0ed2c17..5744110 100644 --- a/apps/bridge-worker/tasks/common/notify-webhooks.ts +++ b/apps/bridge-worker/tasks/common/notify-webhooks.ts @@ -1,4 +1,7 @@ import { db } from "@link-stack/bridge-common"; +import { createLogger } from "@link-stack/logger"; + +const logger = createLogger('notify-webhooks'); export interface NotifyWebhooksOptions { backendId: string; @@ -9,6 +12,11 @@ const notifyWebhooksTask = async ( options: NotifyWebhooksOptions, ): Promise => { const { backendId, payload } = options; + + logger.debug({ + backendId, + payloadKeys: Object.keys(payload), + }, 'Processing webhook notification'); const webhooks = await db .selectFrom("Webhook") @@ -16,16 +24,48 @@ const notifyWebhooksTask = async ( .where("backendId", "=", backendId) .execute(); + logger.debug({ count: webhooks.length, backendId }, 'Found webhooks'); + for (const webhook of webhooks) { const { endpointUrl, httpMethod, headers } = webhook; const finalHeaders = { "Content-Type": "application/json", ...headers }; - console.log({ endpointUrl, httpMethod, headers, finalHeaders }); - const result = await fetch(endpointUrl, { + const body = JSON.stringify(payload); + + logger.debug({ + url: endpointUrl, method: httpMethod, - headers: finalHeaders, - body: JSON.stringify(payload), - }); - console.log(result); + bodyLength: body.length, + headerKeys: Object.keys(finalHeaders), + }, 'Sending webhook'); + + try { + const result = await fetch(endpointUrl, { + method: httpMethod, + headers: finalHeaders, + body, + }); + + logger.debug({ + url: endpointUrl, + status: result.status, + statusText: result.statusText, + ok: result.ok, + }, 'Webhook response'); + + if (!result.ok) { + const responseText = await result.text(); + logger.error({ + url: endpointUrl, + status: result.status, + responseSample: responseText.substring(0, 500), + }, 'Webhook error response'); + } + } catch (error) { + logger.error({ + url: endpointUrl, + error: error instanceof Error ? error.message : error, + }, 'Webhook request failed'); + } } }; diff --git a/apps/bridge-worker/tasks/facebook/send-facebook-message.ts b/apps/bridge-worker/tasks/facebook/send-facebook-message.ts index 3f28fc8..6d63cab 100644 --- a/apps/bridge-worker/tasks/facebook/send-facebook-message.ts +++ b/apps/bridge-worker/tasks/facebook/send-facebook-message.ts @@ -1,4 +1,7 @@ import { db } from "@link-stack/bridge-common"; +import { createLogger } from "@link-stack/logger"; + +const logger = createLogger('bridge-worker-send-facebook-message'); interface SendFacebookMessageTaskOptions { token: string; @@ -31,9 +34,8 @@ const sendFacebookMessageTask = async ( headers: { "Content-Type": "application/json" }, body: JSON.stringify(outgoingMessage), }); - console.log({ response }); } catch (error) { - console.error({ error }); + logger.error({ error }); throw error; } }; diff --git a/apps/bridge-worker/tasks/fetch-signal-messages.ts b/apps/bridge-worker/tasks/fetch-signal-messages.ts index 1044107..f6529e2 100644 --- a/apps/bridge-worker/tasks/fetch-signal-messages.ts +++ b/apps/bridge-worker/tasks/fetch-signal-messages.ts @@ -1,6 +1,9 @@ import { db, getWorkerUtils } from "@link-stack/bridge-common"; +import { createLogger } from "@link-stack/logger"; import * as signalApi from "@link-stack/signal-api"; +const logger = createLogger('fetch-signal-messages'); + const { Configuration, MessagesApi, AttachmentsApi } = signalApi; const config = new Configuration({ basePath: process.env.BRIDGE_SIGNAL_URL, @@ -21,8 +24,23 @@ const fetchAttachments = async (attachments: any[] | undefined) => { const arrayBuffer = await blob.arrayBuffer(); const base64Attachment = Buffer.from(arrayBuffer).toString("base64"); + // Generate default filename if not provided by Signal API + let defaultFilename = name; + if (!defaultFilename) { + // Check if id already has an extension + const hasExtension = id.includes('.'); + if (hasExtension) { + // ID already includes extension + defaultFilename = id; + } else { + // Add extension based on content type + const extension = contentType?.split('/')[1] || 'bin'; + defaultFilename = `${id}.${extension}`; + } + } + const formattedAttachment = { - filename: name, + filename: defaultFilename, mimeType: contentType, attachment: base64Attachment, }; @@ -46,21 +64,48 @@ const processMessage = async ({ message: msg, }: ProcessMessageArgs): Promise[]> => { const { envelope } = msg; - console.log(envelope); const { source, sourceUuid, dataMessage } = envelope; + const isGroup = !!( + dataMessage?.groupV2 || + dataMessage?.groupContext || + dataMessage?.groupInfo + ); + if (!dataMessage) return []; const { attachments } = dataMessage; const rawTimestamp = dataMessage?.timestamp; + + logger.debug({ + sourceUuid, + source, + rawTimestamp, + hasGroupV2: !!dataMessage?.groupV2, + hasGroupContext: !!dataMessage?.groupContext, + hasGroupInfo: !!dataMessage?.groupInfo, + isGroup, + groupV2Id: dataMessage?.groupV2?.id, + groupContextType: dataMessage?.groupContext?.type, + groupInfoType: dataMessage?.groupInfo?.type, + }, 'Processing message'); const timestamp = new Date(rawTimestamp); const formattedAttachments = await fetchAttachments(attachments); const primaryAttachment = formattedAttachments[0] ?? {}; const additionalAttachments = formattedAttachments.slice(1); + + const groupId = + dataMessage?.groupV2?.id || + dataMessage?.groupContext?.id || + dataMessage?.groupInfo?.groupId; + const toRecipient = groupId + ? `group.${Buffer.from(groupId).toString("base64")}` + : phoneNumber; + const primaryMessage = { token: id, - to: phoneNumber, + to: toRecipient, from: source, messageId: `${sourceUuid}-${rawTimestamp}`, message: dataMessage?.message, @@ -68,6 +113,7 @@ const processMessage = async ({ attachment: primaryAttachment.attachment, filename: primaryAttachment.filename, mimeType: primaryAttachment.mimeType, + isGroup, }; const formattedMessages = [primaryMessage]; @@ -119,15 +165,25 @@ const fetchSignalMessagesTask = async ({ number: phoneNumber, }); + logger.debug({ botId: id, phoneNumber }, 'Fetching messages for bot'); + for (const message of messages) { const formattedMessages = await processMessage({ id, phoneNumber, message, }); - console.log({ formattedMessages }); for (const formattedMessage of formattedMessages) { if (formattedMessage.to !== formattedMessage.from) { + logger.debug({ + messageId: formattedMessage.messageId, + from: formattedMessage.from, + to: formattedMessage.to, + isGroup: formattedMessage.isGroup, + hasMessage: !!formattedMessage.message, + hasAttachment: !!formattedMessage.attachment, + }, 'Creating job for message'); + await worker.addJob( "signal/receive-signal-message", formattedMessage, diff --git a/apps/bridge-worker/tasks/formstack/create-ticket-from-form.ts b/apps/bridge-worker/tasks/formstack/create-ticket-from-form.ts new file mode 100644 index 0000000..84062dc --- /dev/null +++ b/apps/bridge-worker/tasks/formstack/create-ticket-from-form.ts @@ -0,0 +1,181 @@ +import { createLogger } from "@link-stack/logger"; +import { Zammad, getUser } from "../../lib/zammad.js"; + +const logger = createLogger('create-ticket-from-form'); + +export interface CreateTicketFromFormOptions { + formData: any; + receivedAt: string; +} + +const createTicketFromFormTask = async ( + options: CreateTicketFromFormOptions, +): Promise => { + const { formData, receivedAt } = options; + + logger.info({ + formData, + receivedAt, + formDataKeys: Object.keys(formData), + }, 'Processing Formstack form submission'); + + // Extract data from Formstack payload + const { + FormID, + UniqueID, + name, + signal_number, + type_of_help_requested, + type_of_organization, + urgency_level, + email_address, + phone_number, + address, + description_of_issue, + preferred_contact_method, + available_times_for_contact, + how_did_you_hear_about_us, + preferred_language, + } = formData; + + // Build full name + const fullName = name + ? `${name.first || ''} ${name.last || ''}`.trim() + : 'Unknown'; + + // Get organization name from form data + const organizationName = type_of_organization || ''; + + // Build ticket title - matching ngo-isac-uploader pattern + let title = fullName; + if (organizationName) { + title += ` - ${organizationName}`; + } + if (type_of_help_requested) { + title += ` - ${type_of_help_requested}`; + } + + // Build article body - only description and metadata + // All other fields go into custom Zammad ticket fields + const body = description_of_issue + ? `

${description_of_issue}

+ +
+

Submitted via Formstack | Form ID: ${FormID} | Submission ID: ${UniqueID} | Received: ${receivedAt}

` + : `

No description provided

+ +
+

Submitted via Formstack | Form ID: ${FormID} | Submission ID: ${UniqueID} | Received: ${receivedAt}

`; + + // Get Zammad configuration from environment + const zammadUrl = process.env.ZAMMAD_URL || 'http://zammad-nginx:8080'; + const zammadToken = process.env.ZAMMAD_API_TOKEN; + + if (!zammadToken) { + logger.error('ZAMMAD_API_TOKEN environment variable is not configured'); + throw new Error('ZAMMAD_API_TOKEN is required'); + } + + const zammad = Zammad({ token: zammadToken }, zammadUrl); + + try { + // Get or create user based on contact info + // Priority: signal_number > phone_number > email_address + let customer; + + // Try to find existing user by phone or email + if (signal_number || phone_number) { + const phoneToSearch = signal_number || phone_number; + customer = await getUser(zammad, phoneToSearch); + if (customer) { + logger.info({ customerId: customer.id, method: 'phone' }, 'Found existing user by phone'); + } + } + + if (!customer && email_address) { + // Search by email if phone search didn't work + const emailResults = await zammad.user.search(`email:${email_address}`); + if (emailResults.length > 0) { + customer = emailResults[0]; + logger.info({ customerId: customer.id, method: 'email' }, 'Found existing user by email'); + } + } + + if (!customer) { + // Create new user with all available contact information + logger.info('Creating new user from form submission'); + customer = await zammad.user.create({ + firstname: name?.first || '', + lastname: name?.last || '', + email: email_address || `${UniqueID}@formstack.local`, + phone: signal_number || phone_number || '', + note: `User created from Formstack submission ${UniqueID}`, + }); + } + + logger.info({ + customerId: customer.id, + customerEmail: customer.email, + customerPhone: customer.phone, + }, 'Customer identified/created'); + + // Build address parts + const streetAddress = address?.address || ''; + const cityValue = address?.city || ''; + const stateValue = address?.state || ''; + const zipValue = address?.zip || ''; + + // Create the ticket with custom fields mapped to Zammad attributes + // Following the pattern from ngo-isac-uploader where all form data + // goes into structured fields rather than HTML body + const ticket = await zammad.ticket.create({ + title, + group: "Users", // Default group - you may want to make this configurable + customer_id: customer.id, + + // Custom fields - these will be populated in Zammad's ticket attributes + // NOTE: 'organization', 'formstack_form_id', 'formstack_submission_id' + // fields could not be created due to naming conflicts, so metadata + // is included in the ticket body instead + signal_number: signal_number || undefined, + type_of_help_requested: type_of_help_requested || undefined, + type_of_organization: type_of_organization || undefined, + urgency_level: urgency_level || undefined, + city: cityValue || undefined, + us_state: stateValue || undefined, + zip_code: zipValue || undefined, + street_address: streetAddress || undefined, + preferred_contact_method: preferred_contact_method || undefined, + available_times: available_times_for_contact || undefined, + where_heard: how_did_you_hear_about_us || undefined, + preferred_language: preferred_language || undefined, + + // Article with just the description + article: { + body, + subject: title, + content_type: "text/html", + type: "note", + }, + }); + + logger.info({ + ticketId: ticket.id, + customerId: customer.id, + formId: FormID, + submissionId: UniqueID, + }, 'Zammad ticket created successfully'); + + } catch (error: any) { + logger.error({ + error: error.message, + stack: error.stack, + output: error.output, + formId: FormID, + submissionId: UniqueID, + }, 'Failed to create Zammad ticket'); + throw error; + } +}; + +export default createTicketFromFormTask; diff --git a/apps/bridge-worker/tasks/leafcutter/import-label-studio.ts b/apps/bridge-worker/tasks/leafcutter/import-label-studio.ts index cb1857e..4f50066 100644 --- a/apps/bridge-worker/tasks/leafcutter/import-label-studio.ts +++ b/apps/bridge-worker/tasks/leafcutter/import-label-studio.ts @@ -36,7 +36,6 @@ const getZammadTickets = async ( { headers }, ); const tickets: any = await rawTickets.json(); - console.log({ tickets }); if (!tickets || tickets.length === 0) { return [shouldContinue, docs]; } @@ -49,23 +48,9 @@ const getZammadTickets = async ( shouldContinue = true; if (source_closed_at <= minUpdatedTimestamp) { - console.log(`Skipping ticket`, { - source_id, - source_updated_at, - source_closed_at, - minUpdatedTimestamp, - }); continue; } - - console.log(`Processing ticket`, { - source_id, - source_updated_at, - source_closed_at, - minUpdatedTimestamp, - }); - - const rawArticles = await fetch( + const rawArticles = await fetch( `${zammadApiUrl}/ticket_articles/by_ticket/${source_id}`, { headers }, ); @@ -178,8 +163,6 @@ const sendToLabelStudio = async (tickets: FormattedZammadTicket[]) => { body: JSON.stringify([ticket]), }); const importResult = await res.json(); - - console.log(JSON.stringify(importResult, undefined, 2)); } }; */ @@ -201,7 +184,6 @@ const importLabelStudioTask = async (): Promise => { await sendToLabelStudio(tickets); const lastTicket = tickets.pop(); const newLastTimestamp = lastTicket.data.source_closed_at; - console.log({ newLastTimestamp }); await db.settings.upsert(settingName, { minUpdatedTimestamp: newLastTimestamp, }); diff --git a/apps/bridge-worker/tasks/leafcutter/import-leafcutter.ts b/apps/bridge-worker/tasks/leafcutter/import-leafcutter.ts index a3c3e4e..a0fa171 100644 --- a/apps/bridge-worker/tasks/leafcutter/import-leafcutter.ts +++ b/apps/bridge-worker/tasks/leafcutter/import-leafcutter.ts @@ -2,6 +2,9 @@ /* import { URLSearchParams } from "url"; import { withDb, AppDatabase } from "../../lib/db.js"; +import { createLogger } from "@link-stack/logger"; + +const logger = createLogger('bridge-worker-import-leafcutter'); // import { loadConfig } from "@digiresilience/bridge-config"; const config: any = {}; @@ -43,14 +46,11 @@ const getLabelStudioTickets = async ( page_size: "50", page: `${page}`, }); - console.log({ url: `${labelStudioApiUrl}/projects/1/tasks?${ticketsQuery}` }); const res = await fetch( `${labelStudioApiUrl}/projects/1/tasks?${ticketsQuery}`, { headers }, ); - console.log({ res }); const tasksResult: any = await res.json(); - console.log({ tasksResult }); return tasksResult; }; @@ -63,14 +63,11 @@ const fetchFromLabelStudio = async ( for await (const page of pages) { const docs = await getLabelStudioTickets(page + 1); - console.log({ page, docs }); if (docs && docs.length > 0) { for (const doc of docs) { const updatedAt = new Date(doc.updated_at); - console.log({ updatedAt, minUpdatedTimestamp }); if (updatedAt > minUpdatedTimestamp) { - console.log(`Adding doc`, { doc }); allDocs.push(doc); } } @@ -79,7 +76,6 @@ const fetchFromLabelStudio = async ( } } - console.log({ allDocs }); return allDocs; }; @@ -93,9 +89,7 @@ const sendToLeafcutter = async (tickets: LabelStudioTicket[]) => { }, } = config; - console.log({ tickets }); const filteredTickets = tickets.filter((ticket) => ticket.is_labeled); - console.log({ filteredTickets }); const finalTickets: LeafcutterTicket[] = filteredTickets.map((ticket) => { const { id, @@ -131,8 +125,7 @@ const sendToLeafcutter = async (tickets: LabelStudioTicket[]) => { }; }); - console.log("Sending to Leafcutter"); - console.log({ finalTickets }); + logger.info("Sending to Leafcutter"); const result = await fetch(opensearchApiUrl, { method: "POST", @@ -157,15 +150,7 @@ const importLeafcutterTask = async (): Promise => { ? new Date(res.value.minUpdatedTimestamp as string) : new Date("2023-03-01"); const newLastTimestamp = new Date(); - console.log({ - contributorName, - settingName, - res, - startTimestamp, - newLastTimestamp, - }); const tickets = await fetchFromLabelStudio(startTimestamp); - console.log({ tickets }); await sendToLeafcutter(tickets); await db.settings.upsert(settingName, { minUpdatedTimestamp: newLastTimestamp, diff --git a/apps/bridge-worker/tasks/signal/receive-signal-message.ts b/apps/bridge-worker/tasks/signal/receive-signal-message.ts index b1688ea..a504b3c 100644 --- a/apps/bridge-worker/tasks/signal/receive-signal-message.ts +++ b/apps/bridge-worker/tasks/signal/receive-signal-message.ts @@ -1,4 +1,9 @@ import { db, getWorkerUtils } from "@link-stack/bridge-common"; +import { createLogger } from "@link-stack/logger"; +import * as signalApi from "@link-stack/signal-api"; +const { Configuration, GroupsApi } = signalApi; + +const logger = createLogger('bridge-worker-receive-signal-message'); interface ReceiveSignalMessageTaskOptions { token: string; @@ -10,6 +15,7 @@ interface ReceiveSignalMessageTaskOptions { attachment?: string; filename?: string; mimeType?: string; + isGroup?: boolean; } const receiveSignalMessageTask = async ({ @@ -22,8 +28,17 @@ const receiveSignalMessageTask = async ({ attachment, filename, mimeType, + isGroup, }: ReceiveSignalMessageTaskOptions): Promise => { - console.log({ token, to, from }); + logger.debug({ + messageId, + from, + to, + isGroup, + hasMessage: !!message, + hasAttachment: !!attachment, + token, + }, 'Processing incoming message'); const worker = await getWorkerUtils(); const row = await db .selectFrom("SignalBot") @@ -32,8 +47,170 @@ const receiveSignalMessageTask = async ({ .executeTakeFirstOrThrow(); const backendId = row.id; + let finalTo = to; + let createdInternalId: string | undefined; + + // Check if auto-group creation is enabled and this is NOT already a group message + const enableAutoGroups = process.env.BRIDGE_SIGNAL_AUTO_GROUPS === "true"; + + logger.debug({ + enableAutoGroups, + isGroup, + shouldCreateGroup: enableAutoGroups && !isGroup && from && to, + }, 'Auto-groups config'); + + // If this is already a group message and auto-groups is enabled, + // use group provided in 'to' + if (enableAutoGroups && isGroup && to) { + // Signal sends the internal ID (base64) in group messages + // We should NOT add "group." prefix - that's for sending messages, not receiving + logger.debug('Message is from existing group with internal ID'); + + finalTo = to; + } else if (enableAutoGroups && !isGroup && from && to) { + try { + const config = new Configuration({ + basePath: process.env.BRIDGE_SIGNAL_URL, + }); + const groupsClient = new GroupsApi(config); + + // Always create a new group for direct messages to the helpdesk + // This ensures each conversation gets its own group/ticket + logger.info({ from }, 'Creating new group for user'); + + // Include timestamp to make each group unique + const timestamp = new Date() + .toISOString() + .replace(/[:.]/g, "-") + .substring(0, 19); + const groupName = `Support: ${from} (${timestamp})`; + + // Create new group for this conversation + const createGroupResponse = await groupsClient.v1GroupsNumberPost({ + number: row.phoneNumber, + data: { + name: groupName, + members: [from], + description: "Private support conversation", + }, + }); + + logger.debug({ createGroupResponse }, 'Group creation response from Signal API'); + + if (createGroupResponse.id) { + // The createGroupResponse.id already contains the full group identifier (group.BASE64) + finalTo = createGroupResponse.id; + + // Fetch the group details to get the actual internalId + // The base64 part of the ID is NOT the same as the internalId! + try { + logger.debug('Fetching group details to get internalId'); + const groups = await groupsClient.v1GroupsNumberGet({ + number: row.phoneNumber, + }); + + logger.debug({ groupsSample: groups.slice(0, 3) }, 'Groups for bot'); + + const createdGroup = groups.find((g) => g.id === finalTo); + if (createdGroup) { + logger.debug({ createdGroup }, 'Found created group details'); + } + + if (createdGroup && createdGroup.internalId) { + createdInternalId = createdGroup.internalId; + logger.debug({ createdInternalId }, 'Got actual internalId'); + } else { + // Fallback: extract base64 part from ID + if (finalTo.startsWith("group.")) { + createdInternalId = finalTo.substring(6); + } + } + } catch (fetchError) { + logger.debug('Could not fetch group details, using ID base64 part'); + // Fallback: extract base64 part from ID + if (finalTo.startsWith("group.")) { + createdInternalId = finalTo.substring(6); + } + } + + logger.debug({ + fullGroupId: finalTo, + internalId: createdInternalId, + }, 'Group created successfully'); + logger.debug({ + groupId: finalTo, + internalId: createdInternalId, + groupName, + forPhoneNumber: from, + botNumber: row.phoneNumber, + response: createGroupResponse, + }, 'Created new Signal group'); + } + + // Now handle notifications and message forwarding for both new and existing groups + if (finalTo && finalTo.startsWith("group.")) { + // Forward the user's initial message to the group using quote feature + try { + logger.debug('Forwarding initial message to group using quote feature'); + + const attributionMessage = `Message from ${from}:\n"${message}"\n\n---\nSupport team: Your request has been received. An agent will respond shortly.`; + + await worker.addJob("signal/send-signal-message", { + token: row.token, + to: finalTo, + message: attributionMessage, + conversationId: null, + quoteMessage: message, + quoteAuthor: from, + quoteTimestamp: Date.parse(sentAt), + }); + + logger.debug({ finalTo }, 'Successfully forwarded initial message to group'); + } catch (forwardError) { + logger.error({ error: forwardError }, 'Error forwarding message to group'); + } + + // Send a response to the original DM informing about the group + try { + logger.debug('Sending group notification to original DM'); + + const dmNotification = `Hello! A private support group has been created for your conversation.\n\nGroup name: ${groupName}\n\nPlease look for the new group in your Signal app to continue the conversation. Our support team will respond there shortly.\n\nThank you for contacting support!`; + + await worker.addJob("signal/send-signal-message", { + token: row.token, + to: from, + message: dmNotification, + conversationId: null, + }); + + logger.debug('Successfully sent group notification to user DM'); + } catch (dmError) { + logger.error({ error: dmError }, 'Error sending DM notification'); + } + } + } catch (error: any) { + // Check if error is because group already exists + const errorMessage = + error?.response?.data?.error || error?.message || error; + const isAlreadyExists = + errorMessage?.toString().toLowerCase().includes("already") || + errorMessage?.toString().toLowerCase().includes("exists"); + + if (isAlreadyExists) { + logger.debug({ from }, 'Group might already exist, continuing with original recipient'); + } else { + logger.error({ + error: errorMessage, + from, + to, + botNumber: row.phoneNumber, + }, 'Error creating Signal group'); + } + } + } + const payload = { - to, + to: finalTo, from, message_id: messageId, sent_at: sentAt, @@ -41,6 +218,7 @@ const receiveSignalMessageTask = async ({ attachment, filename, mime_type: mimeType, + is_group: finalTo.startsWith("group"), }; await worker.addJob("common/notify-webhooks", { backendId, payload }); diff --git a/apps/bridge-worker/tasks/signal/send-signal-message.ts b/apps/bridge-worker/tasks/signal/send-signal-message.ts index 4150300..5b00bbc 100644 --- a/apps/bridge-worker/tasks/signal/send-signal-message.ts +++ b/apps/bridge-worker/tasks/signal/send-signal-message.ts @@ -1,19 +1,44 @@ -import { db } from "@link-stack/bridge-common"; +import { db, getWorkerUtils } from "@link-stack/bridge-common"; +import { createLogger } from "@link-stack/logger"; import * as signalApi from "@link-stack/signal-api"; -const { Configuration, MessagesApi } = signalApi; +const { Configuration, MessagesApi, GroupsApi } = signalApi; + +const logger = createLogger("bridge-worker-send-signal-message"); interface SendSignalMessageTaskOptions { token: string; to: string; message: any; + conversationId?: string; // Zammad ticket/conversation ID for callback + quoteMessage?: string; // Optional: message text to quote + quoteAuthor?: string; // Optional: author of quoted message (phone number) + quoteTimestamp?: number; // Optional: timestamp of quoted message in milliseconds + attachments?: Array<{ + data: string; // base64 + filename: string; + mime_type: string; + }>; } const sendSignalMessageTask = async ({ token, to, message, + conversationId, + quoteMessage, + quoteAuthor, + quoteTimestamp, + attachments, }: SendSignalMessageTaskOptions): Promise => { - console.log({ token, to }); + logger.debug( + { + token, + to, + conversationId, + messageLength: message?.length, + }, + "Processing outgoing message", + ); const bot = await db .selectFrom("SignalBot") .selectAll() @@ -25,18 +50,208 @@ const sendSignalMessageTask = async ({ basePath: process.env.BRIDGE_SIGNAL_URL, }); const messagesClient = new MessagesApi(config); + const groupsClient = new GroupsApi(config); + const worker = await getWorkerUtils(); + + let finalTo = to; + let groupCreated = false; try { - const response = await messagesClient.v2SendPost({ - data: { - number, - recipients: [to], - message, + // Check if 'to' is a group ID (UUID format, group.base64 format, or base64) vs phone number + const isUUID = + /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test( + to, + ); + const isGroupPrefix = to.startsWith("group."); + const isBase64 = /^[A-Za-z0-9+/]+=*$/.test(to) && to.length > 20; // Base64 internal_id + const isGroupId = isUUID || isGroupPrefix || isBase64; + const enableAutoGroups = process.env.BRIDGE_SIGNAL_AUTO_GROUPS === "true"; + + logger.debug( + { + to, + isGroupId, + enableAutoGroups, + shouldCreateGroup: + enableAutoGroups && !isGroupId && to && conversationId, }, + "Recipient analysis", + ); + + // If sending to a phone number and auto-groups is enabled, create a group first + if (enableAutoGroups && !isGroupId && to && conversationId) { + try { + const groupName = `DPN Support Request: ${conversationId}`; + const createGroupResponse = await groupsClient.v1GroupsNumberPost({ + number: bot.phoneNumber, + data: { + name: groupName, + members: [to], + description: "Private support conversation", + }, + }); + + if (createGroupResponse.id) { + // The createGroupResponse.id already contains the full group identifier (group.BASE64) + finalTo = createGroupResponse.id; + groupCreated = true; + + // Fetch the group details to get the actual internalId + let internalId: string | undefined; + try { + const groups = await groupsClient.v1GroupsNumberGet({ + number: bot.phoneNumber, + }); + + const createdGroup = groups.find((g) => g.id === finalTo); + if (createdGroup && createdGroup.internalId) { + internalId = createdGroup.internalId; + logger.debug({ internalId }, "Got actual internalId"); + } else { + // Fallback: extract base64 part from ID + if (finalTo.startsWith("group.")) { + internalId = finalTo.substring(6); + } + } + } catch (fetchError) { + logger.debug("Could not fetch group details, using ID base64 part"); + // Fallback: extract base64 part from ID + if (finalTo.startsWith("group.")) { + internalId = finalTo.substring(6); + } + } + logger.debug( + { + groupId: finalTo, + internalId, + groupName, + conversationId, + originalRecipient: to, + botNumber: bot.phoneNumber, + }, + "Created new Signal group", + ); + + // Notify Zammad about the new group ID via webhook + await worker.addJob("common/notify-webhooks", { + backendId: bot.id, + payload: { + event: "group_created", + conversation_id: conversationId, + original_recipient: to, + group_id: finalTo, + internal_group_id: internalId, + timestamp: new Date().toISOString(), + }, + }); + } + } catch (groupError) { + logger.error( + { + error: + groupError instanceof Error ? groupError.message : groupError, + to, + conversationId, + }, + "Error creating Signal group", + ); + // Continue with original recipient if group creation fails + } + } + + logger.debug( + { + fromNumber: number, + toRecipient: finalTo, + originalTo: to, + recipientChanged: to !== finalTo, + groupCreated, + isGroupRecipient: finalTo.startsWith("group."), + }, + "Sending message via API", + ); + + // Build the message data with optional quote parameters + const messageData: signalApi.ApiSendMessageV2 = { + number, + recipients: [finalTo], + message, + }; + + logger.debug( + { + number, + recipients: [finalTo], + message: message.substring(0, 50) + "...", + hasQuoteParams: !!(quoteMessage && quoteAuthor && quoteTimestamp), + }, + "Message data being sent", + ); + + // Add quote parameters if all are provided + if (quoteMessage && quoteAuthor && quoteTimestamp) { + messageData.quoteTimestamp = quoteTimestamp; + messageData.quoteAuthor = quoteAuthor; + messageData.quoteMessage = quoteMessage; + + logger.debug( + { + quoteAuthor, + quoteMessage: quoteMessage.substring(0, 50) + "...", + quoteTimestamp, + }, + "Including quote in message", + ); + } + + // Add attachments if provided + if (attachments && attachments.length > 0) { + messageData.base64Attachments = attachments.map((att) => att.data); + logger.debug( + { + attachmentCount: attachments.length, + attachmentNames: attachments.map((att) => att.filename), + }, + "Including attachments in message", + ); + } + + const response = await messagesClient.v2SendPost({ + data: messageData, }); - console.log({ response }); - } catch (error) { - console.error({ error }); + + logger.debug( + { + to: finalTo, + groupCreated, + response: response?.timestamp || "no timestamp", + }, + "Message sent successfully", + ); + } catch (error: any) { + // Try to get the actual error message from the response + if (error.response) { + try { + const errorBody = await error.response.text(); + logger.error( + { + status: error.response.status, + statusText: error.response.statusText, + body: errorBody, + sentTo: finalTo, + messageDetails: { + fromNumber: number, + toRecipients: [finalTo], + hasQuote: !!quoteMessage, + }, + }, + "Signal API error", + ); + } catch (e) { + logger.error("Could not parse error response"); + } + } + logger.error({ error }, "Full error details"); throw error; } }; diff --git a/apps/bridge-worker/tasks/voice/twilio-recording.ts b/apps/bridge-worker/tasks/voice/twilio-recording.ts index 9372b61..1b11675 100644 --- a/apps/bridge-worker/tasks/voice/twilio-recording.ts +++ b/apps/bridge-worker/tasks/voice/twilio-recording.ts @@ -3,6 +3,9 @@ import { withDb, AppDatabase } from "../../lib/db.js"; import { twilioClientFor } from "../../lib/common.js"; import { CallInstance } from "twilio/lib/rest/api/v2010/account/call"; import workerUtils from "../../lib/utils.js"; +import { createLogger } from "@link-stack/logger"; + +const logger = createLogger('bridge-worker-twilio-recording'); interface WebhookPayload { startTime: string; @@ -20,7 +23,7 @@ const getTwilioRecording = async (url: string) => { const { payload } = await Wreck.get(url); return { recording: payload as Buffer }; } catch (error: any) { - console.error(error.output); + logger.error(error.output); return { error: error.output }; } }; diff --git a/apps/bridge-worker/tasks/whatsapp/receive-whatsapp-message.ts b/apps/bridge-worker/tasks/whatsapp/receive-whatsapp-message.ts index 024c70e..48adc6f 100644 --- a/apps/bridge-worker/tasks/whatsapp/receive-whatsapp-message.ts +++ b/apps/bridge-worker/tasks/whatsapp/receive-whatsapp-message.ts @@ -23,8 +23,6 @@ const receiveWhatsappMessageTask = async ({ filename, mimeType, }: ReceiveWhatsappMessageTaskOptions): Promise => { - console.log({ token, to, from }); - const worker = await getWorkerUtils(); const row = await db .selectFrom("WhatsappBot") diff --git a/apps/bridge-worker/tasks/whatsapp/send-whatsapp-message.ts b/apps/bridge-worker/tasks/whatsapp/send-whatsapp-message.ts index 509371f..8e89d13 100644 --- a/apps/bridge-worker/tasks/whatsapp/send-whatsapp-message.ts +++ b/apps/bridge-worker/tasks/whatsapp/send-whatsapp-message.ts @@ -1,15 +1,24 @@ import { db } from "@link-stack/bridge-common"; +import { createLogger } from "@link-stack/logger"; + +const logger = createLogger("bridge-worker-send-whatsapp-message"); interface SendWhatsappMessageTaskOptions { token: string; to: string; message: any; + attachments?: Array<{ + data: string; + filename: string; + mime_type: string; + }>; } const sendWhatsappMessageTask = async ({ message, to, token, + attachments, }: SendWhatsappMessageTaskOptions): Promise => { const bot = await db .selectFrom("WhatsappBot") @@ -18,16 +27,40 @@ const sendWhatsappMessageTask = async ({ .executeTakeFirstOrThrow(); const url = `${process.env.BRIDGE_WHATSAPP_URL}/api/bots/${bot.id}/send`; - const params = { message, phoneNumber: to }; + const params: any = { message, phoneNumber: to }; + + if (attachments && attachments.length > 0) { + params.attachments = attachments; + logger.debug( + { + attachmentCount: attachments.length, + attachmentNames: attachments.map((att) => att.filename), + }, + "Sending WhatsApp message with attachments", + ); + } + try { const result = await fetch(url, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(params), }); - console.log({ result }); + + if (!result.ok) { + const errorText = await result.text(); + logger.error( + { + status: result.status, + errorText, + url, + }, + "WhatsApp send failed", + ); + throw new Error(`Failed to send message: ${result.status}`); + } } catch (error) { - console.error({ error }); + logger.error({ error }); throw new Error("Failed to send message"); } }; diff --git a/apps/leafcutter/Dockerfile b/apps/leafcutter/Dockerfile index 9103384..99ee7b4 100644 --- a/apps/leafcutter/Dockerfile +++ b/apps/leafcutter/Dockerfile @@ -38,9 +38,7 @@ RUN chown -R node ${APP_DIR}/ USER node WORKDIR ${APP_DIR} -COPY --from=installer ${APP_DIR}/node_modules/ ./node_modules/ -COPY --from=installer ${APP_DIR}/apps/leafcutter/ ./apps/leafcutter/ -COPY --from=installer ${APP_DIR}/package.json ./package.json +COPY --from=installer ${APP_DIR} ./ USER root WORKDIR ${APP_DIR}/apps/leafcutter/ RUN chmod +x docker-entrypoint.sh diff --git a/apps/leafcutter/README.md b/apps/leafcutter/README.md index e03b35c..c644c6a 100644 --- a/apps/leafcutter/README.md +++ b/apps/leafcutter/README.md @@ -1,32 +1,195 @@ -This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app). +# Leafcutter -## Getting Started +Data visualization and analytics platform for the CDR Link ecosystem. -First, run the development server: +## Overview + +Leafcutter provides powerful data visualization capabilities with multiple chart types, trend analysis, and OpenSearch integration. It enables users to create, save, and share custom visualizations of their data with support for multiple languages. + +## Features + +- **Multiple Visualization Types**: + - Vertical/Horizontal Bar Charts (including stacked) + - Line Charts (including stacked) + - Pie/Donut Charts + - Data Tables + - Metrics Display + - Tag Clouds + +- **Data Management**: + - Create and save custom searches + - User-specific visualizations + - Trend analysis and insights + - OpenSearch integration for data queries + +- **User Experience**: + - Internationalization (English, French) + - Responsive design + - Export capabilities + - Preview mode for sharing + +## Development + +### Prerequisites + +- Node.js >= 20 +- npm >= 10 +- OpenSearch instance +- PostgreSQL database (for user data) + +### Setup ```bash +# Install dependencies +npm install + +# Run development server (port 3001) npm run dev + +# Build for production +npm run build + +# Start production server +npm run start ``` -Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. +### Environment Variables -You can start editing the page by modifying `pages/index.js`. The page auto-updates as you edit the file. +Required environment variables: -[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.js`. +- `OPENSEARCH_URL` - OpenSearch endpoint +- `OPENSEARCH_USERNAME` - OpenSearch username +- `OPENSEARCH_PASSWORD` - OpenSearch password +- `DATABASE_URL` - PostgreSQL connection +- `NEXTAUTH_URL` - Application URL +- `NEXTAUTH_SECRET` - NextAuth.js secret +- `GOOGLE_CLIENT_ID` - Google OAuth client ID +- `GOOGLE_CLIENT_SECRET` - Google OAuth client secret -The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages. +### Available Scripts -## Learn More +- `npm run dev` - Development server on port 3001 +- `npm run build` - Build for production +- `npm run start` - Start production server +- `npm run lint` - Run ESLint +- `npm run export` - Export static site +- `npm run aws:*` - AWS deployment utilities +- `npm run kubectl:*` - Kubernetes utilities -To learn more about Next.js, take a look at the following resources: +## Architecture -- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. -- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. +### Page Structure -You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome! +- `/` - Home dashboard +- `/create` - Create new visualizations +- `/visualizations/[id]` - View/edit visualizations +- `/preview/[id]` - Public preview mode +- `/trends` - Trend analysis +- `/about` - About page +- `/faq` - Frequently asked questions +- `/setup` - Initial setup wizard -## Deploy on Vercel +### API Routes -The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. +- `/api/searches/*` - Search management +- `/api/visualizations/*` - Visualization CRUD +- `/api/trends/*` - Trend data +- `/api/upload` - File upload handling +- `/api/link/auth` - Link authentication -Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details. +### Visualization Configuration + +Each visualization type has a JSON configuration in `_config/visualizations/`: +- Chart options +- Data mapping +- Styling preferences +- Aggregation settings + +### Data Flow + +1. User creates search query +2. Query sent to OpenSearch +3. Results processed and aggregated +4. Data rendered in chosen visualization +5. Visualization saved to PostgreSQL + +## Internationalization + +Supported languages: +- English (`_locales/en.json`) +- French (`_locales/fr.json`) + +Language selection available in the UI with automatic persistence. + +## OpenSearch Integration + +### Query Structure + +Leafcutter translates user inputs into OpenSearch queries: +- Full-text search +- Field filtering +- Date ranges +- Aggregations + +### Index Management + +Works with OpenSearch indices for: +- Document storage +- Real-time analytics +- Historical data + +## Visualization Types + +### Bar Charts +- Vertical and horizontal orientations +- Stacked variants for multi-series data +- Customizable colors and labels + +### Line Charts +- Time series visualization +- Multiple series support +- Trend analysis + +### Pie/Donut Charts +- Proportional data display +- Interactive legends +- Percentage calculations + +### Data Tables +- Sortable columns +- Pagination +- Export functionality + +### Metrics +- Single value display +- Comparison indicators +- Real-time updates + +### Tag Clouds +- Word frequency visualization +- Interactive filtering +- Size-based importance + +## Security + +- Authentication via NextAuth.js +- User-scoped data access +- Secure OpenSearch queries +- Input validation + +## Docker Support + +```bash +# Build image +docker build -t link-stack/leafcutter . + +# Run with docker-compose +docker-compose -f docker/compose/leafcutter.yml up +``` + +## Deployment + +Includes utilities for: +- AWS deployment (S3, CloudFront) +- Kubernetes deployment +- Static site export \ No newline at end of file diff --git a/apps/leafcutter/app/(main)/about/page.tsx b/apps/leafcutter/app/(main)/about/page.tsx index b6958b2..1bfad23 100644 --- a/apps/leafcutter/app/(main)/about/page.tsx +++ b/apps/leafcutter/app/(main)/about/page.tsx @@ -1,5 +1,10 @@ +import { Suspense } from "react"; import { About } from "@link-stack/leafcutter-ui"; export default function Page() { - return ; + return ( + + + + ); } diff --git a/apps/leafcutter/app/(main)/faq/page.tsx b/apps/leafcutter/app/(main)/faq/page.tsx index 396a9b5..05b57de 100644 --- a/apps/leafcutter/app/(main)/faq/page.tsx +++ b/apps/leafcutter/app/(main)/faq/page.tsx @@ -1,5 +1,12 @@ +import { Suspense } from "react"; import { FAQ } from "@link-stack/leafcutter-ui"; -export default function Page() { - return ; +export const dynamic = "force-dynamic"; + +export default async function Page() { + return ( + + + + ); } diff --git a/apps/leafcutter/app/(main)/layout.tsx b/apps/leafcutter/app/(main)/layout.tsx index 5b9e9f1..d97fd7b 100644 --- a/apps/leafcutter/app/(main)/layout.tsx +++ b/apps/leafcutter/app/(main)/layout.tsx @@ -2,6 +2,8 @@ import { ReactNode } from "react"; import "app/_styles/global.css"; import { InternalLayout } from "../_components/InternalLayout"; +export const dynamic = "force-dynamic"; + type LayoutProps = { children: ReactNode; }; diff --git a/apps/leafcutter/app/(main)/preview/[...visualizationID]/page.tsx b/apps/leafcutter/app/(main)/preview/[...visualizationID]/page.tsx index 8daaaba..a1a60c6 100644 --- a/apps/leafcutter/app/(main)/preview/[...visualizationID]/page.tsx +++ b/apps/leafcutter/app/(main)/preview/[...visualizationID]/page.tsx @@ -69,19 +69,17 @@ export const getServerSideProps: GetServerSideProps = async ( }); }); } - console.log({ query }); const dataResponse = await client.search({ index: "demo_data", size: 200, body: { query }, }); - console.log({ dataResponse }); res.props.data = dataResponse.body.hits.hits.map((hit) => ({ id: hit._id, ...hit._source, })); - console.log({ data: res.props.data }); - console.log(res.props.data[0]); + + return res; }; diff --git a/apps/leafcutter/app/(main)/setup/_components/Setup.tsx b/apps/leafcutter/app/(main)/setup/_components/Setup.tsx index 1a079ec..7fe08ae 100644 --- a/apps/leafcutter/app/(main)/setup/_components/Setup.tsx +++ b/apps/leafcutter/app/(main)/setup/_components/Setup.tsx @@ -13,7 +13,7 @@ export const Setup: FC = () => { } = useLeafcutterContext(); const router = useRouter(); useLayoutEffect(() => { - setTimeout(() => router.push("/"), 4000); + setTimeout(() => router.push("/"), 2000); }, [router]); return ( diff --git a/apps/leafcutter/app/(main)/setup/page.tsx b/apps/leafcutter/app/(main)/setup/page.tsx index 4e62414..dad2ed0 100644 --- a/apps/leafcutter/app/(main)/setup/page.tsx +++ b/apps/leafcutter/app/(main)/setup/page.tsx @@ -1,5 +1,10 @@ +import { Suspense } from "react"; import { Setup } from "./_components/Setup"; export default function Page() { - return ; + return ( + + + + ); } diff --git a/apps/leafcutter/app/(main)/visualizations/[...visualizationID]/page.tsx b/apps/leafcutter/app/(main)/visualizations/[...visualizationID]/page.tsx index 5ef382b..d840ef4 100644 --- a/apps/leafcutter/app/(main)/visualizations/[...visualizationID]/page.tsx +++ b/apps/leafcutter/app/(main)/visualizations/[...visualizationID]/page.tsx @@ -22,9 +22,9 @@ const getVisualization = async (visualizationID: string) => { ); const hit = hits[0]; const visualization = { - id: hit._id.split(":")[1], - title: hit._source.visualization.title, - description: hit._source.visualization.description, + id: hit?._id.split(":")[1], + title: hit?._source?.visualization.title, + description: hit?._source?.visualization.description, url: `/app/visualize?security_tenant=global#/edit/${ hit._id.split(":")[1] }?embed=true`, @@ -34,12 +34,13 @@ const getVisualization = async (visualizationID: string) => { }; type PageProps = { - params: { + params: Promise<{ visualizationID: string; - }; + }>; }; -export default async function Page({ params: { visualizationID } }: PageProps) { +export default async function Page({ params }: PageProps) { + const { visualizationID } = await params; const visualization = await getVisualization(visualizationID); return ; diff --git a/apps/leafcutter/app/_components/MultiProvider.tsx b/apps/leafcutter/app/_components/MultiProvider.tsx index 67e12b5..f049ce0 100644 --- a/apps/leafcutter/app/_components/MultiProvider.tsx +++ b/apps/leafcutter/app/_components/MultiProvider.tsx @@ -15,7 +15,7 @@ import fr from "@link-stack/leafcutter-ui/locales/fr.json"; import { LicenseInfo } from "@mui/x-license"; LicenseInfo.setLicenseKey( - "c787ac6613c5f2aa0494c4285fe3e9f2Tz04OTY1NyxFPTE3NDYzNDE0ODkwMDAsUz1wcm8sTE09c3Vic2NyaXB0aW9uLEtWPTI=", + "2a7dd73ee59e3e028b96b0d2adee1ad8Tz0xMTMwOTUsRT0xNzc5MDYyMzk5MDAwLFM9cHJvLExNPXN1YnNjcmlwdGlvbixQVj1pbml0aWFsLEtWPTI=", ); const messages: any = { en, fr }; diff --git a/apps/leafcutter/app/_components/Sidebar.tsx b/apps/leafcutter/app/_components/Sidebar.tsx index acdcef3..b3c0a43 100644 --- a/apps/leafcutter/app/_components/Sidebar.tsx +++ b/apps/leafcutter/app/_components/Sidebar.tsx @@ -48,7 +48,6 @@ const MenuItem = ({ return ( new Client({ - node: baseURL, - auth: { - username: process.env.OPENSEARCH_USERNAME!, - password: process.env.OPENSEARCH_PASSWORD!, - }, - ssl: { - rejectUnauthorized: false, - }, -}); +const createClient = () => + new Client({ + node: baseURL, + auth: { + username: process.env.OPENSEARCH_USERNAME!, + password: process.env.OPENSEARCH_PASSWORD!, + }, + ssl: { + rejectUnauthorized: false, + }, + }); -const createUserClient = (username: string, password: string) => new Client({ - node: baseURL, - auth: { - username, - password, - }, - ssl: { - rejectUnauthorized: false, - }, -}); +const createUserClient = (username: string, password: string) => + new Client({ + node: baseURL, + auth: { + username, + password, + }, + ssl: { + rejectUnauthorized: false, + }, + }); export const checkAuth = async (username: string, password: string) => { const client = createUserClient(username, password); @@ -115,7 +120,7 @@ export const getUserMetadata = async (username: string) => { await client.create({ id: username, index: userMetadataIndexName, - body: { username, savedSearches: [] } + body: { username, savedSearches: [] }, }); res = await client.get({ @@ -132,7 +137,7 @@ export const saveUserMetadata = async (username: string, metadata: any) => { await client.update({ id: username, index: userMetadataIndexName, - body: { doc: { username, ...metadata } } + body: { doc: { username, ...metadata } }, }); }; @@ -181,7 +186,7 @@ const getIndexPattern: any = async (index: string) => { sort: ["updated_at:desc"], }); - if (res.body.hits.total.value === 0) { + if (res?.body?.hits?.total?.valueOf() === 0) { // eslint-disable-next-line no-use-before-define return createCurrentUserIndexPattern(index); } @@ -226,7 +231,7 @@ interface createUserVisualizationProps { } export const createUserVisualization = async ( - props: createUserVisualizationProps + props: createUserVisualizationProps, ) => { const { email, query, visualizationID, title, description } = props; const userIndex = await getCurrentUserIndex(email); @@ -279,7 +284,7 @@ interface updateVisualizationProps { } export const updateUserVisualization = async ( - props: updateVisualizationProps + props: updateVisualizationProps, ) => { const { email, id, query, title, description } = props; const userIndex = await getCurrentUserIndex(email); @@ -300,8 +305,7 @@ export const updateUserVisualization = async ( body, }); } catch (e) { - // eslint-disable-next-line no-console - console.log({ e }); + logger.error({ e }); } return id; @@ -469,10 +473,18 @@ export const performQuery = async (searchQuery: any, limit: number) => { const results = hits.map((hit: any) => ({ ...hit._source, id: hit._id, - incident: Array.isArray(hit._source.incident) ? hit._source.incident.join(", ") : hit._source.incident, - technology: Array.isArray(hit._source.technology) ? hit._source.technology.join(", ") : hit._source.technology, - targeted_group: Array.isArray(hit._source.targeted_group) ? hit._source.targeted_group.join(", ") : hit._source.targeted_group, - country: Array.isArray(hit._source.country) ? hit._source.country.join(", ") : hit._source.country, + incident: Array.isArray(hit._source.incident) + ? hit._source.incident.join(", ") + : hit._source.incident, + technology: Array.isArray(hit._source.technology) + ? hit._source.technology.join(", ") + : hit._source.technology, + targeted_group: Array.isArray(hit._source.targeted_group) + ? hit._source.targeted_group.join(", ") + : hit._source.targeted_group, + country: Array.isArray(hit._source.country) + ? hit._source.country.join(", ") + : hit._source.country, })); return results; @@ -570,7 +582,6 @@ export const getTemplates = async (limit: number) => { }, }; - const rawResponse = await client.search({ index: globalIndex, size: limit, diff --git a/apps/leafcutter/app/api/link/auth/route.ts b/apps/leafcutter/app/api/link/auth/route.ts index 37b356e..1960382 100644 --- a/apps/leafcutter/app/api/link/auth/route.ts +++ b/apps/leafcutter/app/api/link/auth/route.ts @@ -1,15 +1,13 @@ import { NextRequest, NextResponse } from "next/server"; export const GET = async (req: NextRequest) => { - const validDomains = "localhost"; - console.log({ req }); + const validDomains = "localhost"; - return NextResponse.json({ response: "ok" }); + return NextResponse.json({ response: "ok" }); }; export const POST = async (req: NextRequest) => { - const validDomains = "localhost"; - console.log({ req }); + const validDomains = "localhost"; - return NextResponse.json({ response: "ok" }); + return NextResponse.json({ response: "ok" }); }; diff --git a/apps/leafcutter/app/api/trends/recent/route.ts b/apps/leafcutter/app/api/trends/recent/route.ts index a635d29..d18b657 100644 --- a/apps/leafcutter/app/api/trends/recent/route.ts +++ b/apps/leafcutter/app/api/trends/recent/route.ts @@ -2,10 +2,9 @@ import { NextResponse } from "next/server"; import { getTrends } from "app/_lib/opensearch"; export const GET = async () => { - const results = await getTrends(5); - console.log({ results }); + const results = await getTrends(5); - NextResponse.json(results); + NextResponse.json(results); }; -export const dynamic = 'force-dynamic'; +export const dynamic = "force-dynamic"; diff --git a/apps/leafcutter/app/api/upload/index.ts b/apps/leafcutter/app/api/upload/index.ts index 4f3223e..dd2ea26 100644 --- a/apps/leafcutter/app/api/upload/index.ts +++ b/apps/leafcutter/app/api/upload/index.ts @@ -4,6 +4,9 @@ import { Client } from "@opensearch-project/opensearch"; import { v4 as uuid } from "uuid"; import taxonomy from "app/_config/taxonomy.json"; import unRegions from "app/_config/unRegions.json"; +import { createLogger } from "@link-stack/logger"; + +const logger = createLogger('leafcutter-index'); export const POST = async (req: NextRequest) => { const { tickets } = await req.json(); @@ -15,8 +18,8 @@ export const POST = async (req: NextRequest) => { rejectUnauthorized: false, }, headers: { - authorization - } + authorization, + }, }); const succeeded = []; @@ -28,11 +31,15 @@ export const POST = async (req: NextRequest) => { const country = ticket.country[0] ?? "none"; // @ts-expect-error const translatedCountry = taxonomy.country[country]?.display ?? "none"; - const countryDetails: any = unRegions.find((c) => c.name === translatedCountry); + const countryDetails: any = unRegions.find( + (c) => c.name === translatedCountry, + ); const augmentedTicket = { ...ticket, - region: countryDetails['sub-region']?.toLowerCase().replace(" ", "-") ?? null, - continent: countryDetails.region?.toLowerCase().replace(" ", "-") ?? null, + region: + countryDetails["sub-region"]?.toLowerCase().replace(" ", "-") ?? null, + continent: + countryDetails.region?.toLowerCase().replace(" ", "-") ?? null, }; const out = await client.create({ id: uuid(), @@ -40,10 +47,9 @@ export const POST = async (req: NextRequest) => { refresh: true, body: augmentedTicket, }); - console.log(out); succeeded.push(id); } catch (e) { - console.log(e); + logger.error(e); failed.push(id); } } @@ -52,4 +58,3 @@ export const POST = async (req: NextRequest) => { return NextResponse.json(results); }; - diff --git a/apps/leafcutter/app/api/visualizations/delete/route.ts b/apps/leafcutter/app/api/visualizations/delete/route.ts index 2200ed7..20e10b5 100644 --- a/apps/leafcutter/app/api/visualizations/delete/route.ts +++ b/apps/leafcutter/app/api/visualizations/delete/route.ts @@ -3,13 +3,13 @@ import { getServerSession } from "next-auth"; import { authOptions } from "app/_lib/auth"; import { deleteUserVisualization } from "app/_lib/opensearch"; -export const POST = async (req: NextRequest, res: NextResponse) => { - const session = await getServerSession(authOptions); - const { user: { email } }: any = session; - const { id } = await req.json(); - await deleteUserVisualization(email as string, id); +export const POST = async (req: NextRequest) => { + const session = await getServerSession(authOptions); + const { + user: { email }, + }: any = session; + const { id } = await req.json(); + await deleteUserVisualization(email as string, id); - return NextResponse.json({ id }); + return NextResponse.json({ id }); }; - - diff --git a/apps/leafcutter/next-env.d.ts b/apps/leafcutter/next-env.d.ts index 725dd6f..36a4fe4 100644 --- a/apps/leafcutter/next-env.d.ts +++ b/apps/leafcutter/next-env.d.ts @@ -1,6 +1,7 @@ /// /// /// +/// // NOTE: This file should not be edited -// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information. +// see https://nextjs.org/docs/app/api-reference/config/typescript for more information. diff --git a/apps/leafcutter/next.config.js b/apps/leafcutter/next.config.js index e8cb203..a57f18f 100644 --- a/apps/leafcutter/next.config.js +++ b/apps/leafcutter/next.config.js @@ -1,8 +1,5 @@ module.exports = { transpilePackages: ["@link-stack/leafcutter-ui", "@link-stack/opensearch-common"], - experimental: { - missingSuspenseWithCSRBailout: false, - }, poweredByHeader: false, rewrites: async () => ({ fallback: [ diff --git a/apps/leafcutter/package.json b/apps/leafcutter/package.json index 9c3e062..b21e711 100644 --- a/apps/leafcutter/package.json +++ b/apps/leafcutter/package.json @@ -1,6 +1,6 @@ { "name": "@link-stack/leafcutter", - "version": "2.2.0", + "version": "3.2.0b3", "scripts": { "dev": "next dev -p 3001", "login": "aws sso login --sso-session cdr", @@ -13,37 +13,38 @@ "lint": "next lint" }, "dependencies": { - "@emotion/cache": "^11.13.1", - "@emotion/react": "^11.13.3", + "@emotion/cache": "^11.14.0", + "@emotion/react": "^11.14.0", "@emotion/server": "^11.11.0", - "@emotion/styled": "^11.13.0", + "@emotion/styled": "^11.14.1", "@link-stack/leafcutter-ui": "*", + "@link-stack/logger": "*", "@link-stack/opensearch-common": "*", - "@mui/icons-material": "^5", - "@mui/material": "^5", - "@mui/material-nextjs": "^5", - "@mui/x-date-pickers-pro": "^7.18.0", - "@opensearch-project/opensearch": "^2.12.0", + "@mui/icons-material": "^6", + "@mui/material": "^6", + "@mui/material-nextjs": "^6", + "@mui/x-date-pickers-pro": "^7", + "@opensearch-project/opensearch": "^3.5.1", "date-fns": "^4.1.0", - "http-proxy-middleware": "^3.0.2", - "material-ui-popup-state": "^5.3.1", - "next": "^14.2.23", - "next-auth": "^4.24.8", - "react": "18.3.1", - "react-cookie": "^7.2.0", + "http-proxy-middleware": "^3.0.5", + "material-ui-popup-state": "^5.3.6", + "next": "15.5.4", + "next-auth": "^4.24.11", + "react": "19.2.0", + "react-cookie": "^8.0.1", "react-cookie-consent": "^9.0.0", - "react-dom": "18.3.1", + "react-dom": "19.2.0", "react-iframe": "^1.8.5", "react-polyglot": "^0.7.2", - "sharp": "^0.33.5", - "uuid": "^10.0.0" + "sharp": "^0.34.4", + "uuid": "^13.0.0" }, "devDependencies": { + "@types/node": "^24.7.0", + "@types/react": "19.2.2", + "@types/uuid": "^11.0.0", "@link-stack/eslint-config": "*", "@link-stack/typescript-config": "*", - "@types/node": "^22.7.3", - "@types/react": "18.3.9", - "@types/uuid": "^10.0.0", - "typescript": "5.6.2" + "typescript": "5.9.3" } } diff --git a/apps/leafcutter/pages/api/proxy/[[...path]].ts b/apps/leafcutter/pages/api/proxy/[[...path]].ts index 913b7cb..fd296d1 100644 --- a/apps/leafcutter/pages/api/proxy/[[...path]].ts +++ b/apps/leafcutter/pages/api/proxy/[[...path]].ts @@ -1,6 +1,9 @@ import { createProxyMiddleware } from "http-proxy-middleware"; import { NextApiRequest, NextApiResponse } from "next"; import { getToken } from "next-auth/jwt"; +import { createLogger } from "@link-stack/logger"; + +const logger = createLogger('leafcutter-[[...path]]'); /* @@ -24,17 +27,17 @@ const withAuthInfo = const requestSignature = req.query.signature; const url = new URL(req.headers.referer as string); const referrerSignature = url.searchParams.get("signature"); - - console.log({ requestSignature, referrerSignature }); const isAppPath = !!req.url?.startsWith("/app"); - const isResourcePath = !!req.url?.match(/\/(api|app|bootstrap|3961|ui|translations|internal|login|node_modules)/); + const isResourcePath = !!req.url?.match( + /\/(api|app|bootstrap|3961|ui|translations|internal|login|node_modules)/, + ); if (requestSignature && isAppPath) { - console.log("Has Signature"); + logger.info("Has Signature"); } if (referrerSignature && isResourcePath) { - console.log("Has Signature"); + logger.info("Has Signature"); } if (!email) { diff --git a/apps/leafcutter/public/robots.txt b/apps/leafcutter/public/robots.txt new file mode 100644 index 0000000..1f53798 --- /dev/null +++ b/apps/leafcutter/public/robots.txt @@ -0,0 +1,2 @@ +User-agent: * +Disallow: / diff --git a/apps/link/Dockerfile b/apps/link/Dockerfile index d894214..833a82a 100644 --- a/apps/link/Dockerfile +++ b/apps/link/Dockerfile @@ -38,10 +38,7 @@ RUN chown -R node ${APP_DIR}/ USER node WORKDIR ${APP_DIR} -COPY --from=installer ${APP_DIR}/node_modules/ ./node_modules/ -COPY --from=installer ${APP_DIR}/apps/link/ ./apps/link/ -COPY --from=installer ${APP_DIR}/apps/bridge-migrations/ ./apps/bridge-migrations/ -COPY --from=installer ${APP_DIR}/package.json ./package.json +COPY --from=installer ${APP_DIR} ./ USER root WORKDIR ${APP_DIR}/apps/link/ RUN chmod +x docker-entrypoint.sh diff --git a/apps/link/README.md b/apps/link/README.md index e03b35c..f080763 100644 --- a/apps/link/README.md +++ b/apps/link/README.md @@ -1,32 +1,109 @@ -This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app). +# CDR Link -## Getting Started +The main CDR (Center for Digital Resilience) Link application - a streamlined helpdesk interface built on top of Zammad with integrated communication channels and data visualization. -First, run the development server: +## Overview + +CDR Link provides a unified dashboard for managing support tickets, communication channels, and data analytics. It integrates multiple services including Zammad (ticketing), Bridge (multi-channel messaging), Leafcutter (data visualization), and OpenSearch. + +## Features + +- **Simplified Helpdesk Interface**: Streamlined UI for Zammad ticket management +- **Multi-Channel Communication**: Integration with Signal, WhatsApp, Facebook, and Voice channels +- **Data Visualization**: Embedded Leafcutter analytics and reporting +- **User Management**: Role-based access control with Google OAuth +- **Search**: Integrated OpenSearch for advanced queries +- **Label Studio Integration**: For data annotation workflows + +## Development + +### Prerequisites + +- Node.js >= 20 +- npm >= 10 +- Running instances of Zammad, PostgreSQL, and Redis +- Configured authentication providers + +### Setup ```bash +# Install dependencies +npm install + +# Run development server npm run dev + +# Build for production +npm run build + +# Start production server +npm run start ``` -Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. +### Environment Variables -You can start editing the page by modifying `pages/index.js`. The page auto-updates as you edit the file. +Key environment variables required: -[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.js`. +- `ZAMMAD_URL` - Zammad instance URL +- `ZAMMAD_API_TOKEN` - Zammad API authentication token +- `DATABASE_URL` - PostgreSQL connection string +- `REDIS_URL` - Redis connection URL +- `NEXTAUTH_URL` - Application URL for authentication +- `NEXTAUTH_SECRET` - Secret for NextAuth.js +- `GOOGLE_CLIENT_ID` - Google OAuth client ID +- `GOOGLE_CLIENT_SECRET` - Google OAuth client secret -The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages. +### Available Scripts -## Learn More +- `npm run dev` - Start development server +- `npm run build` - Build for production +- `npm run start` - Start production server +- `npm run lint` - Run ESLint +- `npm run export` - Export static site -To learn more about Next.js, take a look at the following resources: +## Architecture -- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. -- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. +### Pages Structure -You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome! +- `/` - Main dashboard +- `/overview/[overview]` - Ticket overview pages +- `/tickets/[id]` - Individual ticket view/edit +- `/admin/bridge` - Bridge configuration management +- `/leafcutter` - Data visualization dashboard +- `/opensearch` - Search dashboard +- `/zammad` - Direct Zammad access +- `/profile` - User profile management -## Deploy on Vercel +### API Routes -The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. +- `/api/auth` - NextAuth.js authentication +- `/api/v2/users` - User management API +- `/api/[service]/bots` - Bot management for communication channels +- `/api/[service]/webhooks` - Webhook endpoints -Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details. +### Key Components + +- `ZammadWrapper` - Embeds Zammad UI with authentication +- `SearchBox` - Global search functionality +- `TicketList` / `TicketDetail` - Ticket management components +- `Sidebar` - Navigation and service switching + +## Docker Support + +Build and run with Docker: + +```bash +# Build image +docker build -t link-stack/link . + +# Run with docker-compose +docker-compose -f docker/compose/link.yml up +``` + +## Integration Points + +- **Zammad**: GraphQL queries for ticket data +- **Bridge Services**: REST APIs for channel management +- **Leafcutter**: Embedded iframe integration +- **OpenSearch**: Direct dashboard embedding +- **Redis**: Session and cache storage \ No newline at end of file diff --git a/apps/link/app/(login)/login/_components/Login.tsx b/apps/link/app/(login)/login/_components/Login.tsx index 0cc1234..3207dfd 100644 --- a/apps/link/app/(login)/login/_components/Login.tsx +++ b/apps/link/app/(login)/login/_components/Login.tsx @@ -23,13 +23,15 @@ import { useSearchParams } from "next/navigation"; type LoginProps = { session: any; + baseURL: string; }; -export const Login: FC = ({ session }) => { - const origin = - typeof window !== "undefined" && window.location.origin - ? window.location.origin - : ""; +export const Login: FC = ({ session, baseURL }) => { + let origin = null; + if (typeof window !== "undefined") { + origin = window.location.origin; + } + const callbackUrl = `${origin}/link`; const [provider, setProvider] = useState(undefined); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); @@ -158,7 +160,7 @@ export const Login: FC = ({ session }) => { sx={buttonStyles} onClick={() => signIn("google", { - callbackUrl: `${origin}`, + callbackUrl, }) } > @@ -174,7 +176,7 @@ export const Login: FC = ({ session }) => { sx={buttonStyles} onClick={() => signIn("apple", { - callbackUrl: `${window.location.origin}`, + callbackUrl, }) } > @@ -189,7 +191,7 @@ export const Login: FC = ({ session }) => { sx={buttonStyles} onClick={() => signIn("azure-ad", { - callbackUrl: `${origin}`, + callbackUrl, }) } > @@ -226,13 +228,13 @@ export const Login: FC = ({ session }) => { + onClick={() => { signIn("credentials", { email, password, - callbackUrl: `${origin}/setup`, - }) - } + callbackUrl, + }); + }} > Sign in with Zammad credentials diff --git a/apps/link/app/(login)/login/page.tsx b/apps/link/app/(login)/login/page.tsx index 7bafdbb..ea944ba 100644 --- a/apps/link/app/(login)/login/page.tsx +++ b/apps/link/app/(login)/login/page.tsx @@ -9,10 +9,11 @@ export const metadata: Metadata = { export default async function Page() { const session = await getSession(); + const baseURL = process.env.LINK_URL; return ( Loading...}> - + ); } diff --git a/apps/link/app/(main)/_components/ClientOnly.tsx b/apps/link/app/(main)/_components/ClientOnly.tsx index 46e79e8..b669b4f 100644 --- a/apps/link/app/(main)/_components/ClientOnly.tsx +++ b/apps/link/app/(main)/_components/ClientOnly.tsx @@ -1,8 +1,9 @@ "use client"; +import { ReactNode } from "react"; import dynamic from "next/dynamic"; -type ClientOnlyProps = { children: JSX.Element }; +type ClientOnlyProps = { children: ReactNode }; const ClientOnly = (props: ClientOnlyProps) => { const { children } = props; diff --git a/apps/link/app/(main)/_components/Home.tsx b/apps/link/app/(main)/_components/Home.tsx index 1a404ae..44332f1 100644 --- a/apps/link/app/(main)/_components/Home.tsx +++ b/apps/link/app/(main)/_components/Home.tsx @@ -3,9 +3,10 @@ import { FC } from "react"; import { OpenSearchWrapper } from "@link-stack/leafcutter-ui"; -export const Home: FC = () => ( - +type HomeProps = { + url: string; +}; + +export const Home: FC = ({ url }) => ( + ); diff --git a/apps/link/app/(main)/_components/Sidebar.tsx b/apps/link/app/(main)/_components/Sidebar.tsx index c099c42..1bd7a1c 100644 --- a/apps/link/app/(main)/_components/Sidebar.tsx +++ b/apps/link/app/(main)/_components/Sidebar.tsx @@ -207,10 +207,6 @@ export const Sidebar: FC = ({ return () => clearInterval(interval); }, []); - const logout = () => { - signOut({ callbackUrl: "/login" }); - }; - return ( = ({ {open ? username : username - .split(" ") - .map((name) => name.substring(0, 1)) - .join("")} + .split(" ") + .map((name) => name.substring(0, 1)) + .join("")} @@ -485,7 +481,17 @@ export const Sidebar: FC = ({ selected={pathname.endsWith("/docs")} open={open} /> - {leafcutterEnabled && ( + {roles.includes("admin") && leafcutterEnabled && ( + + )} + {false && leafcutterEnabled && ( = ({ Icon={LogoutIcon} iconSize={20} open={open} - onClick={logout} /> diff --git a/apps/link/app/(main)/_components/ZammadWrapper.tsx b/apps/link/app/(main)/_components/ZammadWrapper.tsx index 1696040..4daec0f 100644 --- a/apps/link/app/(main)/_components/ZammadWrapper.tsx +++ b/apps/link/app/(main)/_components/ZammadWrapper.tsx @@ -41,7 +41,6 @@ export const ZammadWrapper: FC = ({ method: "GET", redirect: "manual", }); - console.log({ res }); if (res.type === "opaqueredirect") { setAuthenticated(true); } else { @@ -69,7 +68,6 @@ export const ZammadWrapper: FC = ({ }, [session]); if (!session || !authenticated) { - console.log("Not authenticated"); return ( = ({ } if (session && authenticated) { - console.log("Session and authenticated"); return (