@goplusvn/core 0.1.0 → 0.1.1
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.
- package/package.json +2 -1
- package/src/assets/erp_wallpaper.png +0 -0
- package/src/assets/goeat_logo.png +0 -0
- package/src/audit/audit-manager.ts +139 -0
- package/src/audit/index.ts +11 -0
- package/src/audit/memory-audit-logger.ts +86 -0
- package/src/audit/types.ts +50 -0
- package/src/auth/auth-service.ts +97 -0
- package/src/auth/index.ts +266 -0
- package/src/code-generation/index.ts +69 -0
- package/src/configs/auth-routes.ts +17 -0
- package/src/configs/crud.ts +136 -0
- package/src/configs/data/navigations.ts +781 -0
- package/src/configs/data/oauth-links.ts +10 -0
- package/src/configs/entities/material-categories.config.ts +125 -0
- package/src/configs/i18n.ts +12 -0
- package/src/configs/index.ts +26 -0
- package/src/configs/status.ts +25 -0
- package/src/configs/themes.ts +100 -0
- package/src/crud/components/crud-bulk-actions.tsx +91 -0
- package/src/crud/components/crud-card-view.tsx +241 -0
- package/src/crud/components/crud-context.tsx +122 -0
- package/src/crud/components/crud-delete-dialog.tsx +145 -0
- package/src/crud/components/crud-dialog.tsx +406 -0
- package/src/crud/components/crud-empty-state.tsx +104 -0
- package/src/crud/components/crud-export-button.tsx +170 -0
- package/src/crud/components/crud-field-renderer.tsx +653 -0
- package/src/crud/components/crud-filter-chips.tsx +102 -0
- package/src/crud/components/crud-filters/checkbox-filter.tsx +97 -0
- package/src/crud/components/crud-filters/datetime-filter.tsx +83 -0
- package/src/crud/components/crud-filters/filter-builder.tsx +66 -0
- package/src/crud/components/crud-filters/index.tsx +76 -0
- package/src/crud/components/crud-filters/radio-filter.tsx +86 -0
- package/src/crud/components/crud-filters/select-filter.tsx +141 -0
- package/src/crud/components/crud-filters/text-filter.tsx +86 -0
- package/src/crud/components/crud-form.tsx +642 -0
- package/src/crud/components/crud-import-dialog.tsx +440 -0
- package/src/crud/components/crud-infinite-scroll.tsx +116 -0
- package/src/crud/components/crud-page.tsx +1017 -0
- package/src/crud/components/crud-provider.tsx +277 -0
- package/src/crud/components/crud-row-actions.tsx +189 -0
- package/src/crud/components/crud-search.tsx +82 -0
- package/src/crud/components/crud-sheet.tsx +336 -0
- package/src/crud/components/crud-table-skeleton.tsx +26 -0
- package/src/crud/components/crud-table-toolbar.tsx +91 -0
- package/src/crud/components/crud-table.tsx +352 -0
- package/src/crud/components/crud-virtual-table.tsx +55 -0
- package/src/crud/components/index.tsx +20 -0
- package/src/crud/crud-filters/checkbox-filter.tsx +87 -0
- package/src/crud/crud-filters/datetime-filter.tsx +82 -0
- package/src/crud/crud-filters/filter-builder.tsx +64 -0
- package/src/crud/crud-filters/index.tsx +78 -0
- package/src/crud/crud-filters/radio-filter.tsx +79 -0
- package/src/crud/crud-filters/select-filter.tsx +148 -0
- package/src/crud/crud-filters/text-filter.tsx +81 -0
- package/src/crud/index.ts +43 -0
- package/src/crud/lib/crud-service.test.ts +334 -0
- package/src/crud/lib/crud-service.ts +358 -0
- package/src/crud/lib/crud-utils.test.ts +354 -0
- package/src/crud/lib/crud-utils.ts +299 -0
- package/src/crud/lib/crud-validator.ts +247 -0
- package/src/crud/lib/data-loader.ts +234 -0
- package/src/crud/lib/field-calculator.ts +241 -0
- package/src/crud/lib/field-formatter.ts +240 -0
- package/src/crud/lib/import-export-service.test.ts +290 -0
- package/src/crud/lib/import-export-service.ts +352 -0
- package/src/crud/lib/import-server-utils.ts +109 -0
- package/src/crud/lib/lazy-loader.ts +241 -0
- package/src/crud/lib/parse-filters.ts +85 -0
- package/src/crud/lib/permissions.ts +52 -0
- package/src/crud/lib/serialize-config.ts +60 -0
- package/src/crud/lib/stream-loader.ts +145 -0
- package/src/crud/lib/translate-config.ts +335 -0
- package/src/crud/lib/types.ts +11 -0
- package/src/crud/pages/entity-crud-page.tsx +144 -0
- package/src/crud/server.ts +8 -0
- package/src/home/constants.tsx +142 -0
- package/src/home/feature-showcase.tsx +171 -0
- package/src/home/home-page.tsx +191 -0
- package/src/home/hooks/index.ts +1 -0
- package/src/home/hooks/useWidgetPreferences.ts +167 -0
- package/src/home/index.ts +33 -0
- package/src/home/quick-access-dialog.tsx +271 -0
- package/src/home/quick-access-menu.tsx +267 -0
- package/src/home/types.ts +140 -0
- package/src/home/welcome-card.tsx +92 -0
- package/src/home/widget-container.tsx +258 -0
- package/src/home/widgets/base-widget.tsx +200 -0
- package/src/home/widgets/customers-widget.tsx +74 -0
- package/src/home/widgets/index.ts +6 -0
- package/src/home/widgets/orders-widget.tsx +87 -0
- package/src/home/widgets/revenue-widget.tsx +71 -0
- package/src/home/widgets/stock-widget.tsx +109 -0
- package/src/hooks/index.tsx +598 -0
- package/src/hooks/use-tenant.test.tsx +30 -0
- package/src/hooks/use-tenant.ts +5 -0
- package/src/index.ts +17 -0
- package/src/infrastructure/__tests__/architecture-verification.spec.ts +103 -0
- package/src/infrastructure/api-service.ts +317 -0
- package/src/infrastructure/cache/cache-manager.ts +107 -0
- package/src/infrastructure/cache/cache.ts +120 -0
- package/src/infrastructure/cache/index.ts +8 -0
- package/src/infrastructure/cache/types.ts +48 -0
- package/src/infrastructure/cron/cron-manager.ts +239 -0
- package/src/infrastructure/cron/index.ts +6 -0
- package/src/infrastructure/cron/types.ts +41 -0
- package/src/infrastructure/event-bus/event-bus.ts +145 -0
- package/src/infrastructure/event-bus/index.ts +2 -0
- package/src/infrastructure/event-bus/types.ts +22 -0
- package/src/infrastructure/index.ts +32 -0
- package/src/infrastructure/lock/decorators.ts +67 -0
- package/src/infrastructure/lock/index.ts +2 -0
- package/src/infrastructure/lock/lock-manager.ts +33 -0
- package/src/infrastructure/logger/index.ts +2 -0
- package/src/infrastructure/logger/logger.ts +96 -0
- package/src/infrastructure/logger/types.ts +25 -0
- package/src/layout/index.tsx +185 -0
- package/src/navigation/index.ts +91 -0
- package/src/notification/index.ts +14 -0
- package/src/notification/notification-service.ts +120 -0
- package/src/notification/storage/in-memory.ts +56 -0
- package/src/notification/storage/index.ts +1 -0
- package/src/notification/types.ts +51 -0
- package/src/organization/branch-service.ts +299 -0
- package/src/organization/branches.config.ts +154 -0
- package/src/organization/index.ts +5 -0
- package/src/plugin/apps-registry.ts +97 -0
- package/src/plugin/index.ts +5 -0
- package/src/plugin/types.ts +41 -0
- package/src/providers/index.tsx +109 -0
- package/src/providers/tenant-provider.tsx +45 -0
- package/src/rbac/components/roles/role-card.tsx +158 -0
- package/src/rbac/components/roles/role-stats-cards.tsx +29 -0
- package/src/rbac/components/roles/role-toolbar.tsx +123 -0
- package/src/rbac/hooks/use-role-operations.ts +159 -0
- package/src/rbac/hooks/use-roles-data.ts +59 -0
- package/src/rbac/index.ts +297 -0
- package/src/rbac/lib/permission-helpers.ts +63 -0
- package/src/rbac/pages/action-list-page.tsx +25 -0
- package/src/rbac/pages/resource-list-page.tsx +25 -0
- package/src/rbac/pages/role-list-page.tsx +378 -0
- package/src/rbac/permission-service.ts +140 -0
- package/src/rbac/permissions.ts +135 -0
- package/src/rbac/resource-service.ts +115 -0
- package/src/rbac/resource-validator.ts +119 -0
- package/src/rbac/role-service.ts +165 -0
- package/src/rbac/server.ts +16 -0
- package/src/rbac/types.ts +38 -0
- package/src/schemas/action.schema.ts +66 -0
- package/src/schemas/branch.schema.ts +52 -0
- package/src/schemas/coming-soon-schema.ts +9 -0
- package/src/schemas/company.schema.ts +44 -0
- package/src/schemas/forgot-passward-schema.ts +9 -0
- package/src/schemas/index.ts +30 -0
- package/src/schemas/material-category.schema.ts +43 -0
- package/src/schemas/material-pricing.schema.ts +74 -0
- package/src/schemas/material.schema.ts +76 -0
- package/src/schemas/materials.ts +52 -0
- package/src/schemas/new-passward-schema.ts +15 -0
- package/src/schemas/partner-company.schema.ts +149 -0
- package/src/schemas/register-schema.ts +36 -0
- package/src/schemas/resource.schema.ts +133 -0
- package/src/schemas/role.schema.ts +11 -0
- package/src/schemas/sign-in-schema.ts +24 -0
- package/src/schemas/supplier-pricing.schema.ts +15 -0
- package/src/schemas/supplier.schema.ts +120 -0
- package/src/schemas/system-category-group.schema.ts +67 -0
- package/src/schemas/system-category.schema.ts +77 -0
- package/src/schemas/system-config.schema.ts +118 -0
- package/src/schemas/uom.schema.ts +75 -0
- package/src/schemas/user-supplier.schema.ts +179 -0
- package/src/schemas/user.schema.ts +18 -0
- package/src/schemas/verify-email-schema.ts +9 -0
- package/src/schemas/warehouse.schema.ts +49 -0
- package/src/system/components/categories/category-list.tsx +529 -0
- package/src/system/components/categories/category-manager.tsx +89 -0
- package/src/system/components/categories/group-sidebar.tsx +308 -0
- package/src/system/components/settings/setting-dialogs.tsx +197 -0
- package/src/system/components/settings/setting-field.tsx +291 -0
- package/src/system/components/settings/setting-form-dialog.tsx +308 -0
- package/src/system/components/settings/settings-groups.ts +80 -0
- package/src/system/components/settings/settings-search.tsx +71 -0
- package/src/system/components/settings/settings-section.tsx +74 -0
- package/src/system/components/settings/settings-sidebar.tsx +81 -0
- package/src/system/constants.ts +3 -0
- package/src/system/index.ts +150 -0
- package/src/system/job-manager.ts +176 -0
- package/src/system/pages/components/categories/category-list.tsx +537 -0
- package/src/system/pages/components/categories/category-manager.tsx +90 -0
- package/src/system/pages/components/categories/group-sidebar.tsx +311 -0
- package/src/system/pages/components/settings/sales-rules-settings.tsx +222 -0
- package/src/system/pages/components/settings/setting-dialogs.tsx +197 -0
- package/src/system/pages/components/settings/setting-field.tsx +292 -0
- package/src/system/pages/components/settings/setting-form-dialog.tsx +308 -0
- package/src/system/pages/components/settings/settings-groups.ts +87 -0
- package/src/system/pages/components/settings/settings-page.tsx +372 -0
- package/src/system/pages/components/settings/settings-search.tsx +71 -0
- package/src/system/pages/components/settings/settings-section.tsx +74 -0
- package/src/system/pages/components/settings/settings-sidebar.tsx +81 -0
- package/src/system/pages/components/settings/system-settings.tsx +244 -0
- package/src/system/pages/system-category-page.tsx +15 -0
- package/src/system/pages/system-settings-page.tsx +380 -0
- package/src/system/schemas/system-category-group.schema.ts +46 -0
- package/src/system/schemas/system-category.schema.ts +56 -0
- package/src/system/services/settings-service.ts +127 -0
- package/src/system/services/system-category-service.ts +63 -0
- package/src/system/types.ts +45 -0
- package/src/types/index.ts +703 -0
- package/src/ui/auth/auth-layout.tsx +135 -0
- package/src/ui/auth/forgot-password-form.tsx +98 -0
- package/src/ui/auth/index.tsx +7 -0
- package/src/ui/auth/new-password-form.tsx +107 -0
- package/src/ui/auth/oauth-links.tsx +30 -0
- package/src/ui/auth/register-form.tsx +202 -0
- package/src/ui/auth/sign-in-form.tsx +238 -0
- package/src/ui/auth/verify-email-form.tsx +104 -0
- package/src/ui/crud/index.tsx +10 -0
- package/src/ui/data-display/accordion.tsx +65 -0
- package/src/ui/data-display/aspect-ratio.tsx +11 -0
- package/src/ui/data-display/avatar.tsx +163 -0
- package/src/ui/data-display/bento-grid.tsx +77 -0
- package/src/ui/data-display/carousel.tsx +249 -0
- package/src/ui/data-display/chart.tsx +363 -0
- package/src/ui/data-display/code-block-highlight.tsx +54 -0
- package/src/ui/data-display/collapsible.tsx +42 -0
- package/src/ui/data-display/compact-stat-bar.tsx +149 -0
- package/src/ui/data-display/data-table/data-table-context.tsx +255 -0
- package/src/ui/data-display/data-table/data-table-empty-state.tsx +133 -0
- package/src/ui/data-display/data-table/data-table-skeleton.tsx +145 -0
- package/src/ui/data-display/data-table/data-table-toolbar.tsx +353 -0
- package/src/ui/data-display/data-table/data-table.tsx +597 -0
- package/src/ui/data-display/data-table/index.ts +44 -0
- package/src/ui/data-display/data-table-column-header.tsx +75 -0
- package/src/ui/data-display/data-table-pagination.tsx +130 -0
- package/src/ui/data-display/data-table-view-options.tsx +59 -0
- package/src/ui/data-display/formatted-number-input.tsx +210 -0
- package/src/ui/data-display/highlight.tsx +20 -0
- package/src/ui/data-display/hover-card.tsx +48 -0
- package/src/ui/data-display/index.tsx +50 -0
- package/src/ui/data-display/iphone-15-pro.tsx +114 -0
- package/src/ui/data-display/kanban/index.ts +4 -0
- package/src/ui/data-display/kanban/kanban-board.tsx +192 -0
- package/src/ui/data-display/kanban/kanban-column.tsx +74 -0
- package/src/ui/data-display/kanban/kanban-item.tsx +50 -0
- package/src/ui/data-display/kanban/kanban-types.ts +21 -0
- package/src/ui/data-display/kpi-card.tsx +68 -0
- package/src/ui/data-display/media-grid.tsx +110 -0
- package/src/ui/data-display/safari.tsx +175 -0
- package/src/ui/data-display/show-more-text.tsx +55 -0
- package/src/ui/data-display/tabs.tsx +68 -0
- package/src/ui/data-display/timeline.tsx +256 -0
- package/src/ui/feedback/alert.tsx +60 -0
- package/src/ui/feedback/context-menu.tsx +245 -0
- package/src/ui/feedback/drawer.tsx +132 -0
- package/src/ui/feedback/error-dialog.tsx +273 -0
- package/src/ui/feedback/index.tsx +183 -0
- package/src/ui/feedback/progress.tsx +32 -0
- package/src/ui/feedback/sheet.tsx +148 -0
- package/src/ui/feedback/sonner.tsx +36 -0
- package/src/ui/forms/command.tsx +157 -0
- package/src/ui/forms/date-picker.tsx +73 -0
- package/src/ui/forms/date-range-picker.tsx +76 -0
- package/src/ui/forms/date-time-picker.tsx +109 -0
- package/src/ui/forms/editor/editor-menu-bar.tsx +394 -0
- package/src/ui/forms/editor/index.tsx +130 -0
- package/src/ui/forms/editor/multi-select-example.tsx +1234 -0
- package/src/ui/forms/emoji-picker.tsx +109 -0
- package/src/ui/forms/file-dropzone.tsx +169 -0
- package/src/ui/forms/file-thumbnail.tsx +29 -0
- package/src/ui/forms/index.tsx +201 -0
- package/src/ui/forms/input-file.tsx +99 -0
- package/src/ui/forms/input-group.tsx +46 -0
- package/src/ui/forms/input-otp.tsx +81 -0
- package/src/ui/forms/input-phone.tsx +172 -0
- package/src/ui/forms/input-spin.tsx +116 -0
- package/src/ui/forms/input-tags.tsx +219 -0
- package/src/ui/forms/input-time.tsx +42 -0
- package/src/ui/forms/multi-select.tsx +629 -0
- package/src/ui/forms/multiple-date-picker.tsx +74 -0
- package/src/ui/forms/radio-group.tsx +42 -0
- package/src/ui/forms/rating.tsx +158 -0
- package/src/ui/forms/time-picker.tsx +57 -0
- package/src/ui/index.tsx +17 -0
- package/src/ui/layout/animated-list.tsx +77 -0
- package/src/ui/layout/animated-sidebar.tsx +294 -0
- package/src/ui/layout/command-menu.tsx +355 -0
- package/src/ui/layout/customizer.tsx +324 -0
- package/src/ui/layout/footer.tsx +43 -0
- package/src/ui/layout/full-screen-toggle.tsx +52 -0
- package/src/ui/layout/header-breadcrumb.tsx +77 -0
- package/src/ui/layout/horizontal-layout-header.tsx +83 -0
- package/src/ui/layout/horizontal-layout.tsx +50 -0
- package/src/ui/layout/index.tsx +25 -0
- package/src/ui/layout/language-dropdown.tsx +103 -0
- package/src/ui/layout/logo.tsx +63 -0
- package/src/ui/layout/main-layout.tsx +57 -0
- package/src/ui/layout/mode-dropdown.tsx +58 -0
- package/src/ui/layout/notification-dropdown.tsx +127 -0
- package/src/ui/layout/page-tabs.tsx +306 -0
- package/src/ui/layout/route-cache.tsx +214 -0
- package/src/ui/layout/sidebar-group-icon-menu.tsx +195 -0
- package/src/ui/layout/sidebar.tsx +279 -0
- package/src/ui/layout/tab-content-cache.tsx +201 -0
- package/src/ui/layout/tab-navigation-provider.tsx +536 -0
- package/src/ui/layout/toggle-mobile-sidebar.tsx +33 -0
- package/src/ui/layout/top-bar-header-menubar.tsx +412 -0
- package/src/ui/layout/user-dropdown.tsx +188 -0
- package/src/ui/layout/vertical-layout-header.tsx +65 -0
- package/src/ui/layout/vertical-layout.tsx +47 -0
- package/src/ui/management/audit-log-page.tsx +209 -0
- package/src/ui/management/cache-management.tsx +349 -0
- package/src/ui/management/index.ts +3 -0
- package/src/ui/management/job-management.tsx +308 -0
- package/src/ui/pages/not-found.tsx +30 -0
- package/src/ui/primitives/badge.tsx +66 -0
- package/src/ui/primitives/breadcrumb.tsx +103 -0
- package/src/ui/primitives/button.tsx +129 -0
- package/src/ui/primitives/calendar.tsx +74 -0
- package/src/ui/primitives/card.tsx +86 -0
- package/src/ui/primitives/checkbox.tsx +31 -0
- package/src/ui/primitives/client.ts +30 -0
- package/src/ui/primitives/combobox.tsx +290 -0
- package/src/ui/primitives/dialog.tsx +121 -0
- package/src/ui/primitives/dropdown-menu.tsx +239 -0
- package/src/ui/primitives/dynamic-icon.tsx +24 -0
- package/src/ui/primitives/index.tsx +134 -0
- package/src/ui/primitives/input-number.tsx +131 -0
- package/src/ui/primitives/input.tsx +22 -0
- package/src/ui/primitives/keyboard.tsx +23 -0
- package/src/ui/primitives/label.tsx +24 -0
- package/src/ui/primitives/menubar.tsx +262 -0
- package/src/ui/primitives/navigation-menu.tsx +157 -0
- package/src/ui/primitives/pagination.tsx +118 -0
- package/src/ui/primitives/popover.tsx +56 -0
- package/src/ui/primitives/prefetch-link.tsx +60 -0
- package/src/ui/primitives/resizable.tsx +59 -0
- package/src/ui/primitives/scroll-area.tsx +63 -0
- package/src/ui/primitives/select.tsx +172 -0
- package/src/ui/primitives/separator.tsx +51 -0
- package/src/ui/primitives/sidebar.tsx +844 -0
- package/src/ui/primitives/slider.tsx +27 -0
- package/src/ui/primitives/status-badge.tsx +47 -0
- package/src/ui/primitives/sticky-layout.tsx +50 -0
- package/src/ui/primitives/switch.tsx +29 -0
- package/src/ui/primitives/table.tsx +116 -0
- package/src/ui/primitives/tabs.tsx +55 -0
- package/src/ui/primitives/toggle-group.tsx +70 -0
- package/src/ui/primitives/toggle.tsx +47 -0
- package/src/ui/primitives/tooltip.tsx +59 -0
- package/src/user/components/dangerous-zone.tsx +34 -0
- package/src/user/components/delete-account-form.tsx +40 -0
- package/src/user/components/index.ts +4 -0
- package/src/user/components/profile-info-form.tsx +390 -0
- package/src/user/components/profile-info.tsx +32 -0
- package/src/user/components/unified-profile-dialog.tsx +1019 -0
- package/src/user/components/user-stats.tsx +27 -0
- package/src/user/components/user-toolbar.tsx +137 -0
- package/src/user/components/users-card-view.tsx +253 -0
- package/src/user/index.ts +11 -0
- package/src/user/pages/user-list-page.tsx +234 -0
- package/src/user/pages/users-client-page.tsx +385 -0
- package/src/user/profile-page.tsx +19 -0
- package/src/user/schemas.ts +68 -0
- package/src/user/types.ts +34 -0
- package/src/user/user-service.ts +538 -0
- package/src/utils/index.ts +906 -0
- package/src/workflow/activity-timeline.tsx +412 -0
- package/src/workflow/approval-workflow.tsx +31 -0
- package/src/workflow/index.ts +2 -0
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CronJob,
|
|
3
|
+
CronJobManager as ICronJobManager,
|
|
4
|
+
CronJobOptions,
|
|
5
|
+
} from "./types";
|
|
6
|
+
import { createLogger } from "../logger";
|
|
7
|
+
|
|
8
|
+
const logger = createLogger("CronJobManager");
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Simple interval-based cron job (no external dependencies)
|
|
12
|
+
* Supports basic cron expressions: minute, hour, day, month, weekday
|
|
13
|
+
*/
|
|
14
|
+
class SimpleCronJob implements CronJob {
|
|
15
|
+
name: string;
|
|
16
|
+
cronTime: string;
|
|
17
|
+
private onTick: () => void | Promise<void>;
|
|
18
|
+
private intervalId: NodeJS.Timeout | null = null;
|
|
19
|
+
private running = false;
|
|
20
|
+
private intervalMs: number;
|
|
21
|
+
|
|
22
|
+
constructor(options: CronJobOptions) {
|
|
23
|
+
this.name = options.name;
|
|
24
|
+
this.cronTime = options.cronTime;
|
|
25
|
+
this.onTick = options.onTick;
|
|
26
|
+
this.intervalMs = this.parseInterval(options.cronTime);
|
|
27
|
+
|
|
28
|
+
if (options.start) {
|
|
29
|
+
this.start();
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Parse simple cron expressions to interval
|
|
35
|
+
* Supports:
|
|
36
|
+
* - '* * * * *' - every minute
|
|
37
|
+
* - '0 * * * *' - every hour
|
|
38
|
+
* - '0 0 * * *' - every day at midnight
|
|
39
|
+
* - Also supports simple intervals: '5m', '1h', '1d'
|
|
40
|
+
*/
|
|
41
|
+
private parseInterval(cronTime: string): number {
|
|
42
|
+
// Simple interval format: 5m, 1h, 1d
|
|
43
|
+
const simpleMatch = cronTime.match(/^(\d+)([smhd])$/);
|
|
44
|
+
if (simpleMatch) {
|
|
45
|
+
const value = parseInt(simpleMatch[1], 10);
|
|
46
|
+
const unit = simpleMatch[2];
|
|
47
|
+
switch (unit) {
|
|
48
|
+
case "s":
|
|
49
|
+
return value * 1000;
|
|
50
|
+
case "m":
|
|
51
|
+
return value * 60 * 1000;
|
|
52
|
+
case "h":
|
|
53
|
+
return value * 60 * 60 * 1000;
|
|
54
|
+
case "d":
|
|
55
|
+
return value * 24 * 60 * 60 * 1000;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Parse cron expression
|
|
60
|
+
const parts = cronTime.split(" ");
|
|
61
|
+
if (parts.length >= 5) {
|
|
62
|
+
const [minute, hour, dayOfMonth, ,] = parts;
|
|
63
|
+
|
|
64
|
+
// Every minute
|
|
65
|
+
if (minute === "*" && hour === "*") {
|
|
66
|
+
return 60 * 1000; // 1 minute
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Every hour (minute = 0)
|
|
70
|
+
if (minute !== "*" && hour === "*") {
|
|
71
|
+
return 60 * 60 * 1000; // 1 hour
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Every day (minute and hour specified)
|
|
75
|
+
if (minute !== "*" && hour !== "*" && dayOfMonth === "*") {
|
|
76
|
+
return 24 * 60 * 60 * 1000; // 1 day
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Default: every minute
|
|
81
|
+
logger.warn(
|
|
82
|
+
`Could not parse cron expression "${cronTime}", defaulting to every minute`,
|
|
83
|
+
);
|
|
84
|
+
return 60 * 1000;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
start(): void {
|
|
88
|
+
if (this.running) return;
|
|
89
|
+
|
|
90
|
+
logger.info(`Starting cron job: ${this.name}`, {
|
|
91
|
+
interval: `${this.intervalMs}ms`,
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
this.running = true;
|
|
95
|
+
this.intervalId = setInterval(async () => {
|
|
96
|
+
try {
|
|
97
|
+
await this.onTick();
|
|
98
|
+
} catch (error) {
|
|
99
|
+
logger.error(`Cron job "${this.name}" failed`, {
|
|
100
|
+
error: String(error),
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}, this.intervalMs);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
stop(): void {
|
|
107
|
+
if (!this.running || !this.intervalId) return;
|
|
108
|
+
|
|
109
|
+
logger.info(`Stopping cron job: ${this.name}`);
|
|
110
|
+
clearInterval(this.intervalId);
|
|
111
|
+
this.intervalId = null;
|
|
112
|
+
this.running = false;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
isRunning(): boolean {
|
|
116
|
+
return this.running;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
nextDate(): Date | null {
|
|
120
|
+
if (!this.running) return null;
|
|
121
|
+
return new Date(Date.now() + this.intervalMs);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* CronJobManager - manages scheduled tasks
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```typescript
|
|
130
|
+
* import { cronJobManager } from '@goerp/core/infrastructure';
|
|
131
|
+
*
|
|
132
|
+
* // Add a job that runs every hour
|
|
133
|
+
* cronJobManager.addJob({
|
|
134
|
+
* name: 'cleanup-expired-tokens',
|
|
135
|
+
* cronTime: '1h', // or '0 * * * *'
|
|
136
|
+
* onTick: async () => {
|
|
137
|
+
* await db.token.deleteMany({ where: { expiredAt: { lt: new Date() } } });
|
|
138
|
+
* },
|
|
139
|
+
* start: true
|
|
140
|
+
* });
|
|
141
|
+
*
|
|
142
|
+
* // Stop all jobs on shutdown
|
|
143
|
+
* cronJobManager.stopAll();
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
class CronJobManagerImpl implements ICronJobManager {
|
|
147
|
+
private jobs = new Map<string, CronJob>();
|
|
148
|
+
|
|
149
|
+
addJob(options: CronJobOptions): CronJob {
|
|
150
|
+
if (this.jobs.has(options.name)) {
|
|
151
|
+
logger.warn(`Job "${options.name}" already exists, removing old one`);
|
|
152
|
+
this.removeJob(options.name);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const job = new SimpleCronJob(options);
|
|
156
|
+
this.jobs.set(options.name, job);
|
|
157
|
+
|
|
158
|
+
logger.info(`Added cron job: ${options.name}`, {
|
|
159
|
+
cronTime: options.cronTime,
|
|
160
|
+
});
|
|
161
|
+
return job;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
removeJob(name: string): void {
|
|
165
|
+
const job = this.jobs.get(name);
|
|
166
|
+
if (job) {
|
|
167
|
+
job.stop();
|
|
168
|
+
this.jobs.delete(name);
|
|
169
|
+
logger.info(`Removed cron job: ${name}`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
getJob(name: string): CronJob | undefined {
|
|
174
|
+
return this.jobs.get(name);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
getAllJobs(): CronJob[] {
|
|
178
|
+
return Array.from(this.jobs.values());
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
startAll(): void {
|
|
182
|
+
for (const job of this.jobs.values()) {
|
|
183
|
+
job.start();
|
|
184
|
+
}
|
|
185
|
+
logger.info(`Started ${this.jobs.size} cron jobs`);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
stopAll(): void {
|
|
189
|
+
for (const job of this.jobs.values()) {
|
|
190
|
+
job.stop();
|
|
191
|
+
}
|
|
192
|
+
logger.info(`Stopped ${this.jobs.size} cron jobs`);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** Get info for all jobs (for admin dashboard) */
|
|
196
|
+
getJobsInfo(): Array<{
|
|
197
|
+
name: string;
|
|
198
|
+
cronTime: string;
|
|
199
|
+
isRunning: boolean;
|
|
200
|
+
nextDate: Date | null;
|
|
201
|
+
}> {
|
|
202
|
+
const info: Array<{
|
|
203
|
+
name: string;
|
|
204
|
+
cronTime: string;
|
|
205
|
+
isRunning: boolean;
|
|
206
|
+
nextDate: Date | null;
|
|
207
|
+
}> = [];
|
|
208
|
+
|
|
209
|
+
for (const job of this.jobs.values()) {
|
|
210
|
+
info.push({
|
|
211
|
+
name: job.name,
|
|
212
|
+
cronTime: job.cronTime,
|
|
213
|
+
isRunning: job.isRunning(),
|
|
214
|
+
nextDate: job.nextDate(),
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return info;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** Toggle a job on/off */
|
|
222
|
+
toggleJob(name: string): boolean {
|
|
223
|
+
const job = this.jobs.get(name);
|
|
224
|
+
if (!job) return false;
|
|
225
|
+
|
|
226
|
+
if (job.isRunning()) {
|
|
227
|
+
job.stop();
|
|
228
|
+
} else {
|
|
229
|
+
job.start();
|
|
230
|
+
}
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// Singleton instance
|
|
236
|
+
export const cronJobManager = new CronJobManagerImpl();
|
|
237
|
+
|
|
238
|
+
// Export classes for testing
|
|
239
|
+
export { CronJobManagerImpl, SimpleCronJob };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// CronJob Types
|
|
2
|
+
export interface CronJobOptions {
|
|
3
|
+
/** Job name for identification */
|
|
4
|
+
name: string;
|
|
5
|
+
/** Cron expression (e.g., '0 * * * *' for every hour) */
|
|
6
|
+
cronTime: string;
|
|
7
|
+
/** Function to execute */
|
|
8
|
+
onTick: () => void | Promise<void>;
|
|
9
|
+
/** Start immediately on creation (default: false) */
|
|
10
|
+
start?: boolean;
|
|
11
|
+
/** Timezone (default: system timezone) */
|
|
12
|
+
timezone?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface CronJob {
|
|
16
|
+
name: string;
|
|
17
|
+
cronTime: string;
|
|
18
|
+
/** Start the job */
|
|
19
|
+
start(): void;
|
|
20
|
+
/** Stop the job */
|
|
21
|
+
stop(): void;
|
|
22
|
+
/** Check if job is running */
|
|
23
|
+
isRunning(): boolean;
|
|
24
|
+
/** Get next execution date */
|
|
25
|
+
nextDate(): Date | null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface CronJobManager {
|
|
29
|
+
/** Add a new cron job */
|
|
30
|
+
addJob(options: CronJobOptions): CronJob;
|
|
31
|
+
/** Remove a job by name */
|
|
32
|
+
removeJob(name: string): void;
|
|
33
|
+
/** Get a job by name */
|
|
34
|
+
getJob(name: string): CronJob | undefined;
|
|
35
|
+
/** Get all jobs */
|
|
36
|
+
getAllJobs(): CronJob[];
|
|
37
|
+
/** Start all jobs */
|
|
38
|
+
startAll(): void;
|
|
39
|
+
/** Stop all jobs */
|
|
40
|
+
stopAll(): void;
|
|
41
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
EventBus as IEventBus,
|
|
3
|
+
EventHandler,
|
|
4
|
+
EventSubscription,
|
|
5
|
+
} from "./types";
|
|
6
|
+
import { createLogger } from "../logger";
|
|
7
|
+
|
|
8
|
+
const logger = createLogger("EventBus");
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Simple in-memory event bus for pub/sub pattern
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { eventBus } from '@goerp/core/infrastructure';
|
|
16
|
+
*
|
|
17
|
+
* // Subscribe
|
|
18
|
+
* eventBus.on('order.created', async ({ orderId }) => {
|
|
19
|
+
* await notificationService.notify(orderId);
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* // Publish
|
|
23
|
+
* eventBus.emit('order.created', { orderId: '123' });
|
|
24
|
+
*
|
|
25
|
+
* // Async emit (wait for handlers)
|
|
26
|
+
* await eventBus.emitAsync('order.approved', { orderId: '123' });
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
class EventBusImpl implements IEventBus {
|
|
30
|
+
private handlers = new Map<string, Set<EventHandler<unknown>>>();
|
|
31
|
+
private onceHandlers = new Map<string, Set<EventHandler<unknown>>>();
|
|
32
|
+
|
|
33
|
+
on<T>(event: string, handler: EventHandler<T>): EventSubscription {
|
|
34
|
+
if (!this.handlers.has(event)) {
|
|
35
|
+
this.handlers.set(event, new Set());
|
|
36
|
+
}
|
|
37
|
+
this.handlers.get(event)!.add(handler as EventHandler<unknown>);
|
|
38
|
+
|
|
39
|
+
logger.debug(`Subscribed to event: ${event}`);
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
unsubscribe: () => {
|
|
43
|
+
this.handlers.get(event)?.delete(handler as EventHandler<unknown>);
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
once<T>(event: string, handler: EventHandler<T>): EventSubscription {
|
|
49
|
+
if (!this.onceHandlers.has(event)) {
|
|
50
|
+
this.onceHandlers.set(event, new Set());
|
|
51
|
+
}
|
|
52
|
+
this.onceHandlers.get(event)!.add(handler as EventHandler<unknown>);
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
unsubscribe: () => {
|
|
56
|
+
this.onceHandlers.get(event)?.delete(handler as EventHandler<unknown>);
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
emit<T>(event: string, data: T): void {
|
|
62
|
+
logger.debug(`Emitting event: ${event}`);
|
|
63
|
+
|
|
64
|
+
const handlers = this.handlers.get(event);
|
|
65
|
+
const onceHandlers = this.onceHandlers.get(event);
|
|
66
|
+
|
|
67
|
+
if (handlers) {
|
|
68
|
+
for (const handler of handlers) {
|
|
69
|
+
try {
|
|
70
|
+
handler(data);
|
|
71
|
+
} catch (error) {
|
|
72
|
+
logger.error(`Error in event handler for ${event}`, {
|
|
73
|
+
error: String(error),
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (onceHandlers) {
|
|
80
|
+
for (const handler of onceHandlers) {
|
|
81
|
+
try {
|
|
82
|
+
handler(data);
|
|
83
|
+
} catch (error) {
|
|
84
|
+
logger.error(`Error in once handler for ${event}`, {
|
|
85
|
+
error: String(error),
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
this.onceHandlers.delete(event);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async emitAsync<T>(event: string, data: T): Promise<void> {
|
|
94
|
+
logger.debug(`Emitting async event: ${event}`);
|
|
95
|
+
|
|
96
|
+
const handlers = this.handlers.get(event);
|
|
97
|
+
const onceHandlers = this.onceHandlers.get(event);
|
|
98
|
+
const promises: Promise<void>[] = [];
|
|
99
|
+
|
|
100
|
+
if (handlers) {
|
|
101
|
+
for (const handler of handlers) {
|
|
102
|
+
promises.push(
|
|
103
|
+
Promise.resolve(handler(data)).catch((error) => {
|
|
104
|
+
logger.error(`Error in async handler for ${event}`, {
|
|
105
|
+
error: String(error),
|
|
106
|
+
});
|
|
107
|
+
}),
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (onceHandlers) {
|
|
113
|
+
for (const handler of onceHandlers) {
|
|
114
|
+
promises.push(
|
|
115
|
+
Promise.resolve(handler(data)).catch((error) => {
|
|
116
|
+
logger.error(`Error in async once handler for ${event}`, {
|
|
117
|
+
error: String(error),
|
|
118
|
+
});
|
|
119
|
+
}),
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
this.onceHandlers.delete(event);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
await Promise.all(promises);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
off(event: string): void {
|
|
129
|
+
this.handlers.delete(event);
|
|
130
|
+
this.onceHandlers.delete(event);
|
|
131
|
+
logger.debug(`Removed all handlers for event: ${event}`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
removeAllListeners(): void {
|
|
135
|
+
this.handlers.clear();
|
|
136
|
+
this.onceHandlers.clear();
|
|
137
|
+
logger.debug("Removed all event listeners");
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Singleton instance
|
|
142
|
+
export const eventBus = new EventBusImpl();
|
|
143
|
+
|
|
144
|
+
// Export class for testing
|
|
145
|
+
export { EventBusImpl };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// EventBus Types
|
|
2
|
+
export type EventHandler<T = unknown> = (data: T) => void | Promise<void>;
|
|
3
|
+
|
|
4
|
+
export interface EventSubscription {
|
|
5
|
+
/** Unsubscribe from the event */
|
|
6
|
+
unsubscribe(): void;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface EventBus {
|
|
10
|
+
/** Subscribe to an event */
|
|
11
|
+
on<T>(event: string, handler: EventHandler<T>): EventSubscription;
|
|
12
|
+
/** Subscribe to an event (fires only once) */
|
|
13
|
+
once<T>(event: string, handler: EventHandler<T>): EventSubscription;
|
|
14
|
+
/** Emit an event with data */
|
|
15
|
+
emit<T>(event: string, data: T): void;
|
|
16
|
+
/** Emit an event and wait for all handlers to complete */
|
|
17
|
+
emitAsync<T>(event: string, data: T): Promise<void>;
|
|
18
|
+
/** Remove all handlers for an event */
|
|
19
|
+
off(event: string): void;
|
|
20
|
+
/** Remove all handlers */
|
|
21
|
+
removeAllListeners(): void;
|
|
22
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// @goerp/core/infrastructure
|
|
2
|
+
// Infrastructure utilities for GoERP applications
|
|
3
|
+
|
|
4
|
+
// Logger
|
|
5
|
+
export { createLogger, logger } from "./logger";
|
|
6
|
+
export type { Logger, LoggerOptions, LogContext, LogLevel } from "./logger";
|
|
7
|
+
|
|
8
|
+
// Cache
|
|
9
|
+
export { cacheManager, CacheManagerImpl, MemoryCache } from "./cache";
|
|
10
|
+
export type {
|
|
11
|
+
Cache,
|
|
12
|
+
CacheOptions,
|
|
13
|
+
CacheManager,
|
|
14
|
+
CacheManagerOptions,
|
|
15
|
+
} from "./cache";
|
|
16
|
+
|
|
17
|
+
// EventBus
|
|
18
|
+
export { eventBus, EventBusImpl } from "./event-bus";
|
|
19
|
+
export type { EventBus, EventHandler, EventSubscription } from "./event-bus";
|
|
20
|
+
|
|
21
|
+
// CronJob
|
|
22
|
+
export { cronJobManager, CronJobManagerImpl, SimpleCronJob } from "./cron";
|
|
23
|
+
export type { CronJob, CronJobOptions, CronJobManager } from "./cron";
|
|
24
|
+
|
|
25
|
+
// API Service Layer (Inspired by Plane's Abstract APIService pattern)
|
|
26
|
+
export { APIService, defaultClientOptions, isAPIError } from "./api-service";
|
|
27
|
+
export type {
|
|
28
|
+
APIRequestConfig,
|
|
29
|
+
APIResponse,
|
|
30
|
+
APIError,
|
|
31
|
+
APIServiceOptions,
|
|
32
|
+
} from "./api-service";
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { LockOptions } from "./lock-manager";
|
|
2
|
+
import { LockManager } from "./lock-manager";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Decorator to acquire a lock before executing a method.
|
|
6
|
+
* Requires the class to have a `lockManager` property.
|
|
7
|
+
*/
|
|
8
|
+
export function WithLock(keyPath: string, options?: LockOptions) {
|
|
9
|
+
return function (
|
|
10
|
+
target: any,
|
|
11
|
+
propertyKey: string,
|
|
12
|
+
descriptor: PropertyDescriptor,
|
|
13
|
+
) {
|
|
14
|
+
const originalMethod = descriptor.value;
|
|
15
|
+
|
|
16
|
+
descriptor.value = async function (...args: any[]) {
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
|
+
const self = this as any;
|
|
19
|
+
|
|
20
|
+
if (!self.lockManager) {
|
|
21
|
+
throw new Error(
|
|
22
|
+
`Class '${target.constructor.name}' must have a 'lockManager' property to use @WithLock`,
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const resolvedKey = resolveKey(keyPath, args);
|
|
27
|
+
// Prefix lock key to avoid collisions
|
|
28
|
+
const lockKey = `lock:${resolvedKey}`;
|
|
29
|
+
|
|
30
|
+
const acquired = await self.lockManager.acquire(lockKey, options);
|
|
31
|
+
|
|
32
|
+
if (!acquired) {
|
|
33
|
+
throw new Error(`Could not acquire lock for key: ${lockKey}`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
return await originalMethod.apply(this, args);
|
|
38
|
+
} finally {
|
|
39
|
+
await self.lockManager.release(lockKey);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
return descriptor;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function resolveKey(path: string, args: any[]): string {
|
|
48
|
+
// If path format is "{0}" or "{0}.id"
|
|
49
|
+
const match = path.match(/\{(\d+)\}(.*)/);
|
|
50
|
+
if (match) {
|
|
51
|
+
const argIndex = parseInt(match[1]);
|
|
52
|
+
let value = args[argIndex];
|
|
53
|
+
const subPath = match[2]; // e.g., ".id"
|
|
54
|
+
|
|
55
|
+
if (!subPath) return String(value);
|
|
56
|
+
|
|
57
|
+
const props = subPath.replace(/^\./, "").split(".");
|
|
58
|
+
for (const prop of props) {
|
|
59
|
+
if (value === undefined || value === null) break;
|
|
60
|
+
value = value[prop];
|
|
61
|
+
}
|
|
62
|
+
return String(value);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Legacy/Simple support: direct string
|
|
66
|
+
return path;
|
|
67
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Cache } from "../cache/types";
|
|
2
|
+
|
|
3
|
+
export interface LockOptions {
|
|
4
|
+
ttl?: number;
|
|
5
|
+
retryTimes?: number;
|
|
6
|
+
retryDelay?: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export class LockManager {
|
|
10
|
+
constructor(private cache: Cache) {}
|
|
11
|
+
|
|
12
|
+
async acquire(key: string, options?: LockOptions): Promise<boolean> {
|
|
13
|
+
const ttl = options?.ttl || 10;
|
|
14
|
+
const retryTimes = options?.retryTimes || 1;
|
|
15
|
+
const retryDelay = options?.retryDelay || 100;
|
|
16
|
+
|
|
17
|
+
for (let i = 0; i < retryTimes; i++) {
|
|
18
|
+
const existing = await this.cache.get(key);
|
|
19
|
+
if (!existing) {
|
|
20
|
+
await this.cache.set(key, "LOCKED", ttl);
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
if (i < retryTimes - 1) {
|
|
24
|
+
await new Promise((resolve) => setTimeout(resolve, retryDelay));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async release(key: string): Promise<void> {
|
|
31
|
+
await this.cache.del(key);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { Logger, LoggerOptions, LogContext, LogLevel } from "./types";
|
|
2
|
+
|
|
3
|
+
const LOG_LEVELS: Record<LogLevel, number> = {
|
|
4
|
+
trace: 0,
|
|
5
|
+
debug: 1,
|
|
6
|
+
info: 2,
|
|
7
|
+
warn: 3,
|
|
8
|
+
error: 4,
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
function getLogLevel(): LogLevel {
|
|
12
|
+
const envLevel = process.env.LOG_LEVEL?.toLowerCase() as LogLevel;
|
|
13
|
+
if (envLevel && LOG_LEVELS[envLevel] !== undefined) {
|
|
14
|
+
return envLevel;
|
|
15
|
+
}
|
|
16
|
+
return process.env.NODE_ENV === "production" ? "info" : "debug";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function formatMessage(
|
|
20
|
+
level: LogLevel,
|
|
21
|
+
name: string,
|
|
22
|
+
message: string,
|
|
23
|
+
context?: LogContext,
|
|
24
|
+
): string {
|
|
25
|
+
const timestamp = new Date().toISOString();
|
|
26
|
+
const contextStr = context ? ` ${JSON.stringify(context)}` : "";
|
|
27
|
+
return `${timestamp} [${level.toUpperCase()}] [${name}] ${message}${contextStr}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function shouldLog(level: LogLevel, minLevel: LogLevel): boolean {
|
|
31
|
+
return LOG_LEVELS[level] >= LOG_LEVELS[minLevel];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
class ConsoleLogger implements Logger {
|
|
35
|
+
private name: string;
|
|
36
|
+
private level: LogLevel;
|
|
37
|
+
|
|
38
|
+
constructor(options: LoggerOptions = {}) {
|
|
39
|
+
this.name = options.name || "App";
|
|
40
|
+
this.level = options.level || getLogLevel();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
trace(message: string, context?: LogContext): void {
|
|
44
|
+
if (shouldLog("trace", this.level)) {
|
|
45
|
+
console.log(formatMessage("trace", this.name, message, context));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
debug(message: string, context?: LogContext): void {
|
|
50
|
+
if (shouldLog("debug", this.level)) {
|
|
51
|
+
console.log(formatMessage("debug", this.name, message, context));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
info(message: string, context?: LogContext): void {
|
|
56
|
+
if (shouldLog("info", this.level)) {
|
|
57
|
+
console.info(formatMessage("info", this.name, message, context));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
warn(message: string, context?: LogContext): void {
|
|
62
|
+
if (shouldLog("warn", this.level)) {
|
|
63
|
+
console.warn(formatMessage("warn", this.name, message, context));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
error(message: string, context?: LogContext): void {
|
|
68
|
+
if (shouldLog("error", this.level)) {
|
|
69
|
+
console.error(formatMessage("error", this.name, message, context));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Create a logger instance with the given name/options
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* import { createLogger } from '@goerp/core/infrastructure';
|
|
80
|
+
*
|
|
81
|
+
* const logger = createLogger('OrderService');
|
|
82
|
+
* logger.info('Order created', { orderId: '123' });
|
|
83
|
+
* logger.error('Payment failed', { error: err.message });
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export function createLogger(
|
|
87
|
+
nameOrOptions: string | LoggerOptions = {},
|
|
88
|
+
): Logger {
|
|
89
|
+
const options =
|
|
90
|
+
typeof nameOrOptions === "string" ? { name: nameOrOptions } : nameOrOptions;
|
|
91
|
+
|
|
92
|
+
return new ConsoleLogger(options);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Default app logger
|
|
96
|
+
export const logger = createLogger("App");
|