@clidey/dory 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/README.md +1 -1
  2. package/docs/api-playground-demo.mdx +119 -0
  3. package/docs/api-reference/openapi.json +366 -0
  4. package/docs/api-reference/operations/create.mdx +5 -0
  5. package/docs/api-reference/operations/delete.mdx +5 -0
  6. package/docs/api-reference/operations/get.mdx +5 -0
  7. package/docs/api-reference/operations/update.mdx +5 -0
  8. package/docs/assets/dory.jpeg +0 -0
  9. package/docs/dory.json +12 -131
  10. package/docs/favicon.svg +75 -0
  11. package/docs/features.mdx +440 -0
  12. package/docs/introduction.mdx +44 -0
  13. package/docs/logo/logo.png +0 -0
  14. package/package.json +3 -2
  15. package/src/mdx/source.tsx +15 -4
  16. package/docs/README.md +0 -2
  17. package/docs/configuration/app-settings/feature-flags.mdx +0 -157
  18. package/docs/configuration/app-settings/user-settings.mdx +0 -201
  19. package/docs/configuration/installation-setup/env-vars.mdx +0 -198
  20. package/docs/configuration/installation-setup/installation.mdx +0 -127
  21. package/docs/integrations/analytics-monitoring/error-tracking.mdx +0 -199
  22. package/docs/integrations/analytics-monitoring/notifications.mdx +0 -189
  23. package/docs/integrations/analytics-monitoring/user-analytics.mdx +0 -182
  24. package/docs/integrations/shopify-integration/shopify-setup.mdx +0 -226
  25. package/docs/integrations/shopify-integration/shopify-webhooks.mdx +0 -207
  26. package/docs/overview/developer-overview/dev-architecture.mdx +0 -292
  27. package/docs/overview/product-intro/app-overview.mdx +0 -167
  28. package/docs/overview/product-intro/architecture.mdx +0 -203
  29. package/docs/overview/product-intro/core-concepts.mdx +0 -267
  30. package/docs/platform-admin/billing-subscriptions/billing.mdx +0 -176
  31. package/docs/platform-admin/system-maintenance/db-migrations.mdx +0 -184
  32. package/docs/platform-admin/system-maintenance/health-monitoring.mdx +0 -222
  33. package/docs/platform-admin/system-maintenance/queue-management.mdx +0 -234
  34. package/docs/platform-admin/user-team-management/teams.mdx +0 -170
  35. package/docs/platform-admin/user-team-management/users.mdx +0 -185
  36. package/docs/support/legal/dependency-analysis.mdx +0 -132
  37. package/docs/support/legal/privacy-terms.mdx +0 -73
  38. package/docs/support/troubleshooting/common-issues.mdx +0 -159
  39. package/docs/usage/api-usage/api-inventory.mdx +0 -213
  40. package/docs/usage/api-usage/api-products.mdx +0 -249
  41. package/docs/usage/api-usage/api-purchase-orders.mdx +0 -289
  42. package/docs/usage/api-usage/api-reports.mdx +0 -227
  43. package/docs/usage/api-usage/api-suppliers.mdx +0 -171
  44. package/docs/usage/api-usage/api-user-management.mdx +0 -171
  45. package/docs/usage/ui-workflows/bundles.mdx +0 -157
  46. package/docs/usage/ui-workflows/demand-planning.mdx +0 -210
  47. package/docs/usage/ui-workflows/favorites.mdx +0 -167
  48. package/docs/usage/ui-workflows/inventory-management.mdx +0 -179
  49. package/docs/usage/ui-workflows/onboarding.mdx +0 -215
  50. package/docs/usage/ui-workflows/purchase-orders.mdx +0 -200
  51. package/docs/usage/ui-workflows/reports.mdx +0 -257
@@ -1,222 +0,0 @@
1
- ---
2
- title: 'Health & Monitoring'
3
- description: 'Health checks and system testing utilities for Genie App components.'
4
- ---
5
-
6
- # Health & Monitoring
7
-
8
- ## Overview
9
-
10
- The **Health & Monitoring** module ensures the ongoing stability, availability, and operational integrity of key system components within the Genie platform. It provides mechanisms for health checks, scheduled maintenance tasks, system synchronization, and alerts through integrated channels.
11
-
12
- This module plays a critical role in maintaining service reliability, enabling early detection of potential issues, and facilitating timely operational actions.
13
-
14
- ## Purpose and Scope
15
-
16
- This module encompasses several backend services and REST API controllers, primarily responsible for:
17
-
18
- - Exposing health endpoints to verify system status.
19
- - Running scheduled tasks such as data cleanup and system health checks.
20
- - Synchronizing inventory insights and sales data.
21
- - Sending notifications about system performance and health check results.
22
-
23
- It operates within the backend API service boundary and integrates tightly with Shopify, Slack, sales order management, and insightful analytics services.
24
-
25
- ## Technical Overview
26
-
27
- The Health & Monitoring features are implemented mainly within the API backend service using the NestJS framework. Key components include:
28
-
29
- - **HealthController:** Provides basic health check endpoints.
30
- - **ScheduledTasksService:** Runs scheduled cron jobs for maintenance and health validation.
31
- - **SyncController:** Offers admin endpoints to trigger manual synchronization of insights and sales data.
32
- - **SlackService:** Responsible for sending system alerts and notifications over Slack channels.
33
-
34
- ### Architectural Context
35
-
36
- - The HealthController is a public REST endpoint responding with the system's current status.
37
- - ScheduledTasksService uses cron-based jobs to automate repetitive maintenance tasks, leveraging date-time utilities for timezone awareness.
38
- - SyncController facilitates administrative operations for syncing data, impacting multiple teams.
39
- - SlackService acts as an integration client with Slack, automating alerts and reporting critical system events.
40
-
41
- These components collaborate to maintain system health proactively and inform relevant stakeholders.
42
-
43
- ```mermaid
44
- flowchart TD
45
- subgraph Backend API Service
46
- direction TB
47
- HealthController["HealthController"]:::controller
48
- ScheduledTasksService["ScheduledTasksService"]:::service
49
- SyncController["SyncController"]:::controller
50
- SlackService["SlackService"]:::service
51
- end
52
-
53
- subgraph External Systems
54
- Shopify["Shopify"]:::external
55
- Slack["Slack"]:::external
56
- end
57
-
58
- HealthController -->|"Responds to Health Check Requests"| users["Clients / Monitoring Tools"]
59
- ScheduledTasksService -->|"Runs Scheduled Jobs"| SalesOrdersService["SalesOrdersService"]
60
- ScheduledTasksService -->|"Sends Notifications"| SlackService
61
- SyncController -->|"Triggers Data Syncs"| InsightsSyncService["InsightsSyncService"]
62
- SyncController -->|"Triggers Data Syncs"| SalesOrdersService
63
- SyncController -->|"Triggers Shopify Sync"| Shopify
64
- SlackService -->|"Sends Alerts"| Slack
65
-
66
- %% Style for components
67
- classDef controller fill:#d1e7fd,stroke:#1e40af,color:#1e40af,stroke-width:1px
68
- classDef service fill:#c7f0d8,stroke:#047857,color:#047857,stroke-width:1px
69
- classDef external fill:#fef3c7,stroke:#78350f,color:#78350f,stroke-width:1px
70
-
71
- %% Click events
72
- click HealthController "apps/api/src/health/controllers/health.controller.ts"
73
- click ScheduledTasksService "apps/api/src/sales-order/services/scheduled-tasks.service.ts"
74
- click SyncController "apps/api/src/admin/controllers/sync.controller.ts"
75
- click SlackService "apps/api/src/slack/services/slack.service.ts"
76
- ```
77
-
78
- ## Core Concepts and Responsibilities
79
-
80
- ### Components
81
-
82
- - **Health Check Endpoint:** Provides a simple status response `{ status: "OK" }` via a public GET endpoint to facilitate uptime monitoring.
83
- - **Scheduled Tasks:** Automated jobs running process-heavy or periodic cleanup and health verification tasks at predefined cron schedules.
84
- - **Data Synchronization:** Manual admin-triggered endpoints to synchronize inventory insights, sales data, and shop settings on demand.
85
- - **Slack Notifications:** Automated messages to designated Slack channels for system alerts, data sync reports, and health check summaries.
86
-
87
- ### Responsibilities
88
-
89
- - Ensure that the backend services report their operational status accurately.
90
- - Clean up old sales orders to maintain database hygiene.
91
- - Monitor sales synchronization health and report discrepancies.
92
- - Allow admin users to trigger data syncs for insights and sales as needed.
93
- - Generate Slack notifications that highlight the status and health of core system operations.
94
-
95
- ### Design Decisions
96
-
97
- - Use of **NestJS controllers** to encapsulate HTTP API endpoints.
98
- - Scheduled tasks annotated with `@EnhancedCron` decorator for declarative cron job scheduling.
99
- - Dependency injection for clean integration with services such as Slack and SalesOrders.
100
- - Use of timezone-aware date handling via `date-fns-tz` to maintain consistent timing across regions.
101
-
102
- ### Ownership and Monitoring
103
-
104
- - Owned and maintained by the backend API development team.
105
- - Logs primarily written via NestJS Logger with identifiable class scopes.
106
- - Slack messages sent to system alerts and user requests channels for operational monitoring.
107
-
108
- ## Code Structure
109
-
110
- ### Key Files
111
-
112
- - [`apps/api/src/health/controllers/health.controller.ts`](apps/api/src/health/controllers/health.controller.ts): Health check controller exposing the `/health` REST endpoint.
113
- - [`apps/api/src/sales-order/services/scheduled-tasks.service.ts`](apps/api/src/sales-order/services/scheduled-tasks.service.ts): Contains scheduled jobs for routine maintenance and health checks.
114
- - [`apps/api/src/admin/controllers/sync.controller.ts`](apps/api/src/admin/controllers/sync.controller.ts): Admin controller with POST endpoints for manual data sync operations.
115
- - [`apps/api/src/slack/services/slack.service.ts`](apps/api/src/slack/services/slack.service.ts): Slack integration service handling message dispatch and channel management.
116
-
117
- ### Example: HealthController
118
-
119
- ```ts
120
- import { Controller, Get, Logger } from "@nestjs/common";
121
- import { Public } from "~/user-management/decorators/decorators";
122
-
123
- @Controller("health")
124
- export class HealthController {
125
- private readonly logger = new Logger(HealthController.name);
126
-
127
- @Public()
128
- @Get()
129
- async getHealth() {
130
- return { status: "OK" };
131
- }
132
- }
133
- ```
134
-
135
- This controller exposes a public endpoint returning a simple `{ status: "OK" }` JSON response to indicate that the system is operational.
136
-
137
- ### ScheduledTasksService Highlights
138
-
139
- - Schedules jobs using the `@EnhancedCron` decorator with cron time set to specific EST times.
140
- - Runs daily cleanup of old sales orders.
141
- - Performs sales synchronization health checks on Mondays and Fridays at midnight.
142
- - Sends Slack notifications with summaries of health checks.
143
-
144
- ```ts
145
- @EnhancedCron({
146
- cronTime: "0 12 * * *", // every day at 12pm
147
- taskName: "deleteOldSalesOrdersTask",
148
- })
149
- async deleteOldSalesOrdersTask() {
150
- this.logger.log("Deleting old sales orders for all teams...");
151
- await this.salesOrdersService.deleteOldSalesOrders();
152
- this.logger.log("Deleted old sales orders for all teams");
153
- }
154
- ```
155
-
156
- ### SyncController Responsibilities
157
-
158
- - Provides POST endpoints like `/sync-inventory-insights-for-all-teams` and `/sync-weekly-sales-for-last-year-for-team` to trigger manual syncs.
159
- - Uses request validation via Zod schemas to accept well-typed request bodies.
160
- - Logs detailed performance summaries upon sync completion.
161
-
162
- ## API and Interfaces
163
-
164
- ### Health API
165
-
166
- - **GET /health**
167
- - Publicly accessible endpoint.
168
- - Returns `{ status: "OK" }` to indicate service health.
169
-
170
- ### Admin Sync APIs
171
-
172
- | Endpoint | Method | Request Body | Description |
173
- |----------------------------------------------|--------|-------------------------------|-----------------------------------------------|
174
- | /admin/sync-inventory-insights-for-all-teams | POST | None | Triggers sync of inventory insights for all teams.
175
- | /admin/sync-inventory-insights-for-team | POST | `{ teamId: string }` | Triggers sync of inventory insights for a specific team.
176
- | /admin/sync-daily-inventory-insights-for-all-teams | POST | None | Triggers daily sync of inventory insights for all teams.
177
- | /admin/sync-daily-inventory-insights-for-team | POST | `{ teamId: string }` | Triggers daily sync for a specific team.
178
- | /admin/sync-insights-and-ideas-for-all-teams | POST | None | Triggers full sync of insights and ideas for all teams.
179
- | /admin/sync-shop-settings-for-team | POST | `{ teamId: string }` | Syncs shop settings for a specific team.
180
- | /admin/update-sales-and-order-metrics-for-team | POST | `{ teamId: string }` | Updates sales and order metrics for a team.
181
- | /admin/sync-weekly-sales-for-last-year-for-team | POST | `{ teamId: string }` | Syncs weekly sales data for last year.
182
- | /admin/sales-health-check-for-past-days | POST | `{ days: number }` | Returns sales sync health check info for given past days.
183
-
184
- ### SlackService
185
-
186
- - Provides async methods to send different types of notifications:
187
- - New user report requests
188
- - Feature suggestions
189
- - Daily sync performance summaries
190
- - Product bundle import/onboarding notifications
191
- - Purchase order import requests
192
- - Sales sync health check notifications
193
-
194
- - Ensures app is a member of Slack channels before sending messages.
195
- - Retrieves Slack app user ID for membership validation.
196
-
197
- ## Environment and Configuration
198
-
199
- - **Slack tokens:** `SLACK_APP_TOKEN` for API access.
200
- - **Slack Channel IDs:**
201
- - `SlACK_APP_USER_REQUESTS_CHANNEL_ID` for user request-related notifications.
202
- - `SLACK_APP_SYSTEM_ALERTS_CHANNEL_ID` for system alerts and health notifications.
203
- - **Timezone settings:** `DATA_SYNC_TIMEZONE` used for scheduled tasks to align cron job execution times.
204
- - **API environment:** `API_ENV` variable used in Slack notification messages to indicate deployment environment.
205
-
206
- ## Testing, Monitoring, and Deployment
207
-
208
- - Logging and debugging are handled through NestJS `Logger` instances per class.
209
- - Scheduled tasks log start and completion times.
210
- - Performance tracking integrated with `DailySyncPerformanceTrackerService` to monitor sync job durations.
211
-
212
- ## Troubleshooting & Known Issues
213
-
214
- - No explicit troubleshooting or known issues documented in the current context.
215
-
216
- ## Summary
217
-
218
- The Health & Monitoring module is fundamental to maintaining Genie’s backend system reliability. Through health endpoint exposure, scheduled operations, manual sync triggers, and Slack alerting, it ensures the platform remains operational and timely alerts notify stakeholders of potential issues.
219
-
220
- This module’s design leverages NestJS controllers and services, cron scheduling decorators, and external API integrations for a scalable and maintainable infrastructure.
221
-
222
- For detailed implementation, refer to the respective code files provided in the repository.
@@ -1,234 +0,0 @@
1
- ---
2
- title: 'Queue Management'
3
- description: 'Overview of queue system powered by BullMQ, its UI and configuration.'
4
- ---
5
-
6
- ## Queue Management
7
-
8
- ### Purpose and Scope
9
-
10
- This module handles the management and processing of queues within the Genie App backend system. It orchestrates various asynchronous background jobs, responsible for syncing data, processing events, maintaining inventory history snapshots, forecasting, demand planning, and onboarding workflows.
11
-
12
- Queue management enables scalable, reliable, and fault-tolerant processing of tasks by leveraging BullMQ and its NestJS integration in a modular fashion. The service ensures jobs are queued, processed, deduplicated, retried, and cleaned up efficiently.
13
-
14
- This queue system operates primarily as part of the backend services, interfacing with core business logic in inventory, forecasting, Shopify integration, and user onboarding, ensuring smooth asynchronous workflows.
15
-
16
- ### Technical Overview
17
-
18
- The system uses [BullMQ](https://docs.bullmq.io/) queues within NestJS, enhanced by `@bull-board` to provide UI monitoring of the queues' state and jobs.
19
-
20
- #### Components:
21
-
22
- - **Queues and Jobs**: Defined centrally in `const.ts`, grouping four main queues:
23
- - `insights-sync`
24
- - `shopify-webhooks`
25
- - `inventory-item-history`
26
- - `product-variant-quantity-snapshot`
27
-
28
- - **Queue Registration**: Each queue is registered with BullModule, including default job options like retry attempts and removal after completion.
29
-
30
- - **Queue Dashboard**: Each queue is registered with BullBoardModule using the `BullMQAdapter`, enabling web UI monitoring.
31
-
32
- - **Job Processors (Consumers)**: Each queue has corresponding processors that execute jobs, e.g. `OnboardingConsumer` for onboarding sync jobs, `InventoryItemHisotryConsumer` for inventory snapshot sync, etc.
33
-
34
- - **Workflow Service**: Manages complex flows (multi-job workflows) for demand planning and forecast updates, coordinating job creation and job dependencies using BullMQ flow producers.
35
-
36
-
37
- ```mermaid
38
- flowchart TD
39
- subgraph "Queues"
40
- INSIGHTS_SYNC["Insights Sync Queue"]:::queue
41
- SHOPIFY_WEBHOOKS["Shopify Webhooks Queue"]:::queue
42
- INVENTORY_ITEM_HISTORY["Inventory Item History Queue"]:::queue
43
- PRODUCT_VARIANT_QTY_SNAPSHOT["Product Variant Quantity Snapshot Queue"]:::queue
44
- end
45
-
46
- subgraph "Job Processors"
47
- OnboardingConsumer["OnboardingConsumer"]:::processor
48
- InventoryItemHistoryConsumer["InventoryItemHistoryConsumer"]:::processor
49
- ProductVariantSnapshotConsumer["ProductVariantSnapshotConsumer"]:::processor
50
- InsightsSyncConsumer["InsightsSyncConsumer"]:::processor
51
- end
52
-
53
- subgraph "Workflow Services & Producers"
54
- WorkflowService["WorkflowService
55
- (Generates demand plan & forecast workflows)"]:::service
56
- end
57
-
58
- style INSIGHTS_SYNC fill:#d1e7dd,stroke:#0f5132,stroke-width:2px
59
- style SHOPIFY_WEBHOOKS fill:#cff4fc,stroke:#055160,stroke-width:2px
60
- style INVENTORY_ITEM_HISTORY fill:#f8d7da,stroke:#842029,stroke-width:2px
61
- style PRODUCT_VARIANT_QTY_SNAPSHOT fill:#fff3cd,stroke:#664d03,stroke-width:2px
62
- style OnboardingConsumer fill:#e2e3e5,stroke:#41464b,stroke-width:2px
63
- style InventoryItemHistoryConsumer fill:#e2e3e5,stroke:#41464b,stroke-width:2px
64
- style ProductVariantSnapshotConsumer fill:#e2e3e5,stroke:#41464b,stroke-width:2px
65
- style InsightsSyncConsumer fill:#e2e3e5,stroke:#41464b,stroke-width:2px
66
- style WorkflowService fill:#dbeafe,stroke:#2563eb,stroke-width:2px
67
-
68
- INSIGHTS_SYNC -->|"processes job: SYNC_SHOPIFY_DATA"| InsightsSyncConsumer
69
- SHOPIFY_WEBHOOKS -->|"processes job: PROCESS_EVENT"| OnboardingConsumer
70
- INVENTORY_ITEM_HISTORY -->|"processes job: TAKE_SNAPSHOT"| InventoryItemHistoryConsumer
71
- PRODUCT_VARIANT_QTY_SNAPSHOT -->|"processes jobs: WEEKLY_SNAPSHOT, MONTHLY_SNAPSHOT"| ProductVariantSnapshotConsumer
72
-
73
- WorkflowService -->|"creates complex demand plan workflows"| INSIGHTS_SYNC
74
- WorkflowService -->|"creates demand plan and forecast jobs"| INVENTORY_ITEM_HISTORY
75
-
76
- click OnboardingConsumer "apps/api/src/setup/services/onboarding.consumer.ts"
77
- click InventoryItemHistoryConsumer "apps/api/src/pim/services/inventory-item-history.consumer.ts"
78
- click ProductVariantSnapshotConsumer "apps/api/src/pim/services/product-variant-snapshot.consumer.ts"
79
- click InsightsSyncConsumer "apps/api/src/pim/services/insights-sync.consumer.ts"
80
- click WorkflowService "apps/api/src/forecast/services/workflow.service.ts"
81
- click INSIGHTS_SYNC "apps/api/src/pim/const.ts"
82
- click SHOPIFY_WEBHOOKS "apps/api/src/pim/const.ts"
83
- click INVENTORY_ITEM_HISTORY "apps/api/src/pim/const.ts"
84
- click PRODUCT_VARIANT_QTY_SNAPSHOT "apps/api/src/pim/const.ts"
85
- ```
86
-
87
-
88
- ### Core Concepts and Responsibilities
89
-
90
- #### Queues and Jobs
91
- - Queues are named namespaces for logically grouping related jobs.
92
- - Jobs represent atomic units of asynchronous work identified by a unique name.
93
- - Jobs support deduplication using unique IDs to prevent double processing.
94
- - Default options include removing completed jobs after 7 days and retrying failed jobs twice.
95
-
96
- #### Flow Producers
97
- - Used for managing complex job workflows with children jobs and dependencies.
98
- - Used mainly for demand planning workflows involving multiple dependent jobs across different queues (`demand-plan`, `forecast`, `historical-sales`).
99
-
100
- #### Job Consumers
101
- - Implemented as NestJS `@EnhancedProcessor` classes, each responsible for processing jobs from a specific queue.
102
- - Include concurrency control for managing parallel job executions.
103
- - Ensure jobs are processed within a max 23-hour timeout to prevent indefinite execution.
104
-
105
- #### Key Design Decisions
106
- - BullMQ chosen for robustness and rich features for background job management.
107
- - BullBoard integration enables an out-of-the-box dashboard for queue health monitoring.
108
- - Centralized queue and job name constants avoid naming collisions and improve maintainability.
109
-
110
- #### Ownership
111
- - The backend engineering team owns these queue modules.
112
- - Logging is performed using NestJS's native `Logger` with clear contextual naming.
113
- - Error capturing integrates with centralized exception tracking via `GenieException` and sentry-like reporting.
114
-
115
- #### Non-functional Requirements (NFRs)
116
- - High concurrency support (e.g., 10-20 concurrent workers).
117
- - Job deduplication to avoid redundant processing.
118
- - Job retries with exponential backoff for transient failures.
119
- - Timely removal of completed jobs to maintain Redis memory hygiene.
120
- - Stable fault tolerance to avoid crashing on unhandled job names.
121
-
122
- ### Code Structure
123
-
124
- Key files and their responsibilities:
125
-
126
- | File | Description |
127
- |-------|-------------|
128
- | `apps/api/src/pim/const.ts` | Declares all queue and job names, registers queues with BullModule and BullBoardModule to configure default options and dashboard UI |
129
- | `apps/api/src/forecast/services/workflow.service.ts` | Manages multi-job workflows via BullMQ's flow producers for demand plan generation, actual updates, and forecast refresh workflows |
130
- | `apps/api/src/setup/services/onboarding.consumer.ts` | Consumer for onboarding-related jobs from the `onboarding` queue; processes shop settings sync, PIM data sync, and orders sync jobs |
131
- | `apps/api/src/pim/services/inventory-item-history.consumer.ts` | Processes inventory item history snapshot jobs to sync historical inventory data per team |
132
- | `apps/api/src/pim/services/product-variant-snapshot.consumer.ts` | Handles weekly and monthly inventory quantity snapshot jobs for product variants by updating relevant database columns efficiently |
133
- | `apps/api/src/pim/services/insights-sync.consumer.ts` | Processes Shopify data synchronization jobs per team, includes concurrency control and job delay if duplicate jobs are active |
134
-
135
-
136
- ### Selected Code Snippets
137
-
138
- #### Queue and Job Constants Registration
139
- ```ts
140
- export const QUEUE = {
141
- INSIGHTS_SYNC: { name: "insights-sync", jobs: { SYNC_SHOPIFY_DATA: "insights-sync:sync-shopify-data" } },
142
- SHOPIFY_WEBHOOKS: { name: "shopify-webhooks", jobs: { PROCESS_EVENT: "shopify-webhooks:process-event" } },
143
- INVENTORY_ITEM_HISTORY: { name: "inventory-item-history", jobs: { TAKE_SNAPSHOT: "inventory-item-history:take-snapshot" } },
144
- PRODUCT_VARIANT_QUANTITY_SNAPSHOT: {
145
- name: "product-variant-quantity-snapshot",
146
- jobs: {
147
- WEEKLY_SNAPSHOT: "product-variant-quantity-snapshot:weekly-snapshot",
148
- MONTHLY_SNAPSHOT: "product-variant-quantity-snapshot:monthly-snapshot",
149
- },
150
- },
151
- } as const;
152
-
153
- export const BULLMQ_QUEUES = [
154
- BullModule.registerQueue({ name: QUEUE.INSIGHTS_SYNC.name, defaultJobOptions: { removeOnComplete: { age: 604800 }, attempts: 2 } }),
155
- BullBoardModule.forFeature({ name: QUEUE.INSIGHTS_SYNC.name, adapter: BullMQAdapter }),
156
- // similarly for other queues
157
- ];
158
- ```
159
-
160
- #### Job Processing Example - OnboardingConsumer
161
- ```ts
162
- @EnhancedProcessor(QUEUE.ONBOARDING.name, { concurrency: 10 })
163
- export class OnboardingConsumer extends GenieWorkerHost {
164
- async process(job: Job) {
165
- const jobName = job.name as string; // one of onboarding jobs
166
- const { teamId } = z.object({ teamId: z.string().nonempty() }).parse(job.data);
167
-
168
- const JOB_HANDLERS = {
169
- [QUEUE.ONBOARDING.jobs.SYNC_SHOP_SETTINGS]: async () => {
170
- await this.shopifyStoreService.syncShopSettings(teamId);
171
- await this.onboardingProgressRepository.update({ teamId }, { hasShopSettingsDataSynced: true });
172
- },
173
- // Other handlers...
174
- };
175
-
176
- const jobHandler = JOB_HANDLERS[jobName];
177
- if (!jobHandler) {
178
- throw new UnreachableError(jobName as never, `Unhandled job name for queue: onboarding`);
179
- }
180
-
181
- // Enforce max 23 hour timeout
182
- const timeoutPromise = new Promise((_, reject) => {
183
- setTimeout(() => reject(new GenieException(`Job ${jobName} timed out after 23 hours`, { id: job.id })), 23 * 3600 * 1000);
184
- });
185
-
186
- await Promise.race([jobHandler.call(this), timeoutPromise]);
187
- }
188
- }
189
- ```
190
-
191
- #### Workflow Service Job Addition Example
192
- ```ts
193
- async startGenerateDemandPlanWorkflow({ teamId, demandPlanId, demandPlanStartDate }: { teamId: string; demandPlanId: string; demandPlanStartDate: Date; }) {
194
- const historyStartDate = subYears(demandPlanStartDate, 1);
195
-
196
- await this.generateDemandPlanFlowProducer.add({
197
- name: FLOW_PRODUCER.GENERATE_DEMAND_PLAN,
198
- queueName: QUEUE.DEMAND_PLAN.name,
199
- data: { teamId, demandPlanId },
200
- opts: {
201
- deduplication: { id: `${FLOW_PRODUCER.GENERATE_DEMAND_PLAN}-${teamId}-${demandPlanId}` },
202
- },
203
- children: [ /*...child jobs with deduplication and priorities...*/ ],
204
- });
205
- }
206
- ```
207
-
208
- ### Environment & Configuration
209
-
210
- - No specific environment variables are detailed for queue management except a feature flag check for disabling Shopify scheduled tasks (`DISABLE_SHOPIFY_SCHEDULED_TASKS`).
211
- - Queues use Redis as backend via BullMQ, configured elsewhere in the application.
212
-
213
- ### Testing, Monitoring, and Deployment
214
-
215
- - Each consumer implements detailed error handling and logs job execution details.
216
- - Timeouts are enforced at 23 hours per job to avoid slow/hung processing.
217
- - Jobs leverage retry logic with exponential backoff configured as default.
218
- - Queue dashboard integration via BullBoard enables real-time monitoring of queue states.
219
- - Exception capturing employs `GenieException` and utilities to integrate with centralized error tracking.
220
-
221
- ### Troubleshooting & Known Issues
222
-
223
- - If a job name is unhandled in a consumer, an `UnreachableError` is thrown, ensuring missing handlers are caught during development or deployment.
224
- - Long-running jobs are timed out and fail gracefully after 23 hours to prevent queue blockage.
225
- - Duplicate jobs for the same team are deferred (e.g., in `InsightsSyncConsumer`) to avoid race conditions.
226
-
227
- ### References
228
-
229
- - [Queue and Job Constants](apps/api/src/pim/const.ts)
230
- - [Onboarding Consumer](apps/api/src/setup/services/onboarding.consumer.ts)
231
- - [Inventory Item History Consumer](apps/api/src/pim/services/inventory-item-history.consumer.ts)
232
- - [Product Variant Snapshot Consumer](apps/api/src/pim/services/product-variant-snapshot.consumer.ts)
233
- - [Insights Sync Consumer](apps/api/src/pim/services/insights-sync.consumer.ts)
234
- - [Workflow Service](apps/api/src/forecast/services/workflow.service.ts)
@@ -1,170 +0,0 @@
1
- ---
2
- title: 'Teams Management'
3
- description: 'Creating, managing, and configuring teams and their members.'
4
- ---
5
-
6
- ## Teams Management Service - Internal Developer Documentation
7
-
8
- ### 1. Purpose and Scope
9
-
10
- The Teams Management service is a core backend module responsible for handling operations related to teams within the Genie platform. It manages the storage, retrieval, and update of team settings, including general business information, insights configurations, and Shopify-related settings. Additionally, it provides API endpoints for team membership and invitation management.
11
-
12
- This service acts as a boundary between the user/team management domain and other subsystems such as purchase orders, product inventory, and onboarding workflows. It is a crucial part of the user collaboration and team configuration capabilities.
13
-
14
- ### 2. Technical Overview
15
-
16
- The `SettingsService` and `TeamsController` work closely to enable team configuration and management functionalities. Teams are represented by `TeamEntity` in the database using TypeORM ORM.
17
-
18
- #### Architectural Role
19
- - Implements core business logic for team settings management.
20
- - Offers REST API endpoints via `TeamsController` to expose these operations.
21
- - Handles validation and error management, throwing domain-specific exceptions (`GenieException`) for consistent error reporting.
22
-
23
- #### Dependencies
24
- - Uses TypeORM repository for data access to `TeamEntity`.
25
- - Depends on shared DTOs and enums from `@genie/shared` (e.g., `TeamSettingsDto`).
26
- - Uses utility functions such as timezone validation.
27
- - Works together with `TeamsService` which provides user and membership logic.
28
-
29
- #### Design Pattern
30
- - Follows NestJS provider and controller patterns.
31
- - Separation between controller (request handling) and service (business logic).
32
-
33
- ### Mermaid Diagram
34
-
35
- ```mermaid
36
- flowchart TD
37
- subgraph "User & Team Management"
38
- SettingsService["SettingsService"]:::service
39
- TeamsController["TeamsController"]:::controller
40
- TeamsService["TeamsService"]:::service
41
- TeamEntity["TeamEntity DB":::db]
42
- end
43
-
44
- TeamsController -->|"calls"| SettingsService
45
- TeamsController -->|"uses"| TeamsService
46
- SettingsService -->|"DB access via TypeORM"| TeamEntity
47
- TeamsService -->|"DB & business logic"| TeamEntity
48
-
49
- classDef service fill:#f9f,stroke:#333,stroke-width:1px,color:#000
50
- classDef controller fill:#bbf,stroke:#333,stroke-width:1px,color:#000
51
- classDef db fill:#fab,stroke:#333,stroke-width:2px,color:#000
52
- ```
53
-
54
- ### 3. Core Concepts and Responsibilities
55
-
56
- - **Team Settings** encompass general business info (e.g., business name), insights configuration (e.g., inventory grading), and Shopify-specific shop settings (currency, timezone).
57
- - **Inventory Item Grading Strategy:** Currently, only the `SalesRevenue` strategy is supported (the `SalesFrequency` strategy explicitly throws a `GenieException`).
58
- - **General Settings DTOs:** The API consumes and returns structured DTOs like `TeamGeneralSettingsDto`, `InsightsSettingsDto`, and `TeamSettingsDto`.
59
- - **Authorization:** Controllers enforce user membership (`user.teamId` presence) to restrict operations.
60
- - **Invitation Management:** Supports sending invitations, accepting invitations, and deactivating team users, with admin role enforcement.
61
-
62
- ### Key Domain Terms
63
- - **TeamGeneralSettingsDto:** Business-facing settings such as business name, VAT number, contact email.
64
- - **InsightsSettingsDto:** Configurations for sales analysis and inventory grading.
65
- - **Shopify Shop Settings:** Currency and timezone data linked to Shopify integration.
66
- - **GenieException:** Centralized exception class for domain errors.
67
-
68
- ### Ownership
69
- - Maintained primarily by the User and Team Management backend team.
70
- - Logs are generated via NestJS's internal logging and passed through to centralized error monitoring.
71
-
72
- ### 4. Code Structure
73
-
74
- - **`apps/api/src/user-management/services/settings.service.ts`**
75
- - Implements `SettingsService` providing methods:
76
- - `getTeamGeneralSettings(teamId)`
77
- - `getTeamInsightsSettings(teamId)`
78
- - `updateTeamGeneralSettings(teamId, settings)`
79
- - `updateTeamInsightsSettings(teamId, settings)`
80
- - `updateTeamSettings(teamId, settings)`
81
- - `getShopSettings(teamId)`
82
- - `saveShopSettings(teamId, shopSettings)`
83
- - Uses `teamRepository` injected via TypeORM.
84
- - Throws `GenieException` for error scenarios.
85
-
86
- - **`apps/api/src/user-management/controllers/teams.controller.ts`**
87
- - Defines REST endpoints prefixed with `/teams`.
88
- - Endpoints include GET and PUT for team settings, management of team members, invitations, and deactivation.
89
- - Uses request validation through `ZodValidationPipe` with strict DTO schemas.
90
- - Checks for user membership and admin role authorization.
91
-
92
- - **`apps/api/src/user-management/entities/team.entity.ts`**
93
- - Defines the `TeamEntity` mapped to the underlying database `team` table.
94
- - Stores business and insights settings as JSON columns.
95
- - Fields include shopify integration credentials, timezone, currency, onboarding progress, enabled status.
96
- - Provides methods `getCompleteGeneralSettings()` and `hasOnboarded()`.
97
-
98
- #### Example snippet from `SettingsService` update:
99
- ```ts
100
- async updateTeamSettings(teamId: string, settings: TeamSettingsDto) {
101
- const { businessName: name, ...generalSettings } = settings.general;
102
- const { averageDailySalesPeriodDays, stockLevels, grading } = settings.insights;
103
-
104
- await this.teamRepository.update(teamId, {
105
- name,
106
- generalSettings,
107
- averageDailySalesPeriodDaysSetting: averageDailySalesPeriodDays,
108
- stockLevelsSettings: stockLevels,
109
- ...(grading ? {
110
- inventoryItemGradingPeriodDaysSetting: grading.periodDays,
111
- inventoryItemGradingStrategySetting: grading.strategy,
112
- inventoryItemGradingLevelAThresholdSetting: grading.levelAThreshold,
113
- inventoryItemGradingLevelBThresholdSetting: grading.levelBThreshold,
114
- inventoryItemGradingLevelCThresholdSetting: grading.levelCThreshold,
115
- } : {}),
116
- });
117
- }
118
- ```
119
-
120
- ### 5. API and Interfaces
121
-
122
- - **Endpoints exposed by `TeamsController`:**
123
-
124
- | Method | Path | Description | Auth Required | Body DTO |
125
- |--------|--------------------------|-------------------------------------------|-----------------------|---------------------------------------|
126
- | PUT | `/teams/settings` | Update complete team settings (general + insights) | User must belong to team | `TeamSettingsDto` |
127
- | PUT | `/teams/settings/general` | Update general team settings only | User must belong to team | `TeamGeneralSettingsDto` |
128
- | GET | `/teams/settings/general` | Retrieve general settings | User must belong to team | None |
129
- | PUT | `/teams/settings/insights`| Update insights settings | User must belong to team | `InsightsSettingsDto` |
130
- | GET | `/teams/settings/insights`| Retrieve insights settings | User must belong to team | None |
131
- | GET | `/teams/settings/shop` | Fetch shop currency and timezone settings | User must belong to team | None |
132
- | POST | `/teams/send-invitations` | Send team member invitations | User must belong to team | `SendInvitationsRequestDto` |
133
- | PATCH | `/teams/user/:id/deactivate` | Deactivate a team user (admin only) | User must be Admin | None |
134
- | DELETE | `/teams/invitations/:email` | Remove invitation by email | User must belong to team | None |
135
- | POST | `/teams/accept-invitation` | Accept a team invitation (public) | Public | `AcceptInvitationRequestDto` |
136
-
137
- - **Error Modes:**
138
- - Throws `GenieException` with contextual info.
139
- - Specific cases: user without team, unsupported grading strategy.
140
-
141
- ### 6. Environment & Configuration
142
-
143
- - No direct environment variables are required solely for this service.
144
- - Depends on database connection configured globally for TypeORM.
145
- - Uses shared DTOs and enums from the `@genie/shared` package.
146
-
147
- ### 7. Testing, Monitoring, and Deployment
148
-
149
- - The service is tested through unit and integration tests leveraging NestJS testing modules (not shown in detail).
150
- - Logs exceptions via NestJS Logger and captures errors with `captureException` utility.
151
- - Throws domain-specific exceptions to simplify error handling.
152
-
153
- ### 8. Troubleshooting & Known Issues
154
-
155
- - **SalesFrequency Grading Strategy Unsupported:** Attempting to use the `SalesFrequency` inventory grading strategy will throw an exception.
156
- - **User Without Team:** All team-related endpoints require authenticated users associated with a team; missing this triggers an exception.
157
- - **Only Admins Can Deactivate Users:** User deactivation requires admin role check enforced in controller.
158
-
159
- ### 9. Style and Terminology Guidelines
160
-
161
- - Use `TeamEntity` when referring to the database team record.
162
- - Settings are split into `generalSettings`, `insightsSettings`, and `shopSettings`.
163
- - DTOs follow strict Zod schemas to enforce API contracts.
164
-
165
- ---
166
-
167
- ### References
168
- - [SettingsService Source](apps/api/src/user-management/services/settings.service.ts)
169
- - [TeamsController Source](apps/api/src/user-management/controllers/teams.controller.ts)
170
- - [TeamEntity Source](apps/api/src/user-management/entities/team.entity.ts)