@axova/shared 1.0.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 (112) hide show
  1. package/CONFIGURATION_GUIDE.md +1 -0
  2. package/README.md +384 -0
  3. package/SCHEMA_ORGANIZATION.md +209 -0
  4. package/dist/configs/index.d.ts +85 -0
  5. package/dist/configs/index.js +555 -0
  6. package/dist/events/kafka.d.ts +40 -0
  7. package/dist/events/kafka.js +311 -0
  8. package/dist/index.d.ts +13 -0
  9. package/dist/index.js +41 -0
  10. package/dist/interfaces/customer-events.d.ts +85 -0
  11. package/dist/interfaces/customer-events.js +2 -0
  12. package/dist/interfaces/inventory-events.d.ts +453 -0
  13. package/dist/interfaces/inventory-events.js +3 -0
  14. package/dist/interfaces/inventory-types.d.ts +894 -0
  15. package/dist/interfaces/inventory-types.js +3 -0
  16. package/dist/interfaces/order-events.d.ts +320 -0
  17. package/dist/interfaces/order-events.js +3 -0
  18. package/dist/lib/auditLogger.d.ts +162 -0
  19. package/dist/lib/auditLogger.js +626 -0
  20. package/dist/lib/authOrganization.d.ts +24 -0
  21. package/dist/lib/authOrganization.js +110 -0
  22. package/dist/lib/db.d.ts +6 -0
  23. package/dist/lib/db.js +88 -0
  24. package/dist/middleware/serviceAuth.d.ts +60 -0
  25. package/dist/middleware/serviceAuth.js +272 -0
  26. package/dist/middleware/storeOwnership.d.ts +15 -0
  27. package/dist/middleware/storeOwnership.js +156 -0
  28. package/dist/middleware/storeValidationMiddleware.d.ts +44 -0
  29. package/dist/middleware/storeValidationMiddleware.js +180 -0
  30. package/dist/middleware/userAuth.d.ts +27 -0
  31. package/dist/middleware/userAuth.js +218 -0
  32. package/dist/schemas/admin/admin-schema.d.ts +741 -0
  33. package/dist/schemas/admin/admin-schema.js +111 -0
  34. package/dist/schemas/ai-moderation/ai-moderation-schema.d.ts +648 -0
  35. package/dist/schemas/ai-moderation/ai-moderation-schema.js +88 -0
  36. package/dist/schemas/common/common-schemas.d.ts +436 -0
  37. package/dist/schemas/common/common-schemas.js +94 -0
  38. package/dist/schemas/compliance/compliance-schema.d.ts +3388 -0
  39. package/dist/schemas/compliance/compliance-schema.js +472 -0
  40. package/dist/schemas/compliance/kyc-schema.d.ts +2642 -0
  41. package/dist/schemas/compliance/kyc-schema.js +361 -0
  42. package/dist/schemas/customer/customer-schema.d.ts +2727 -0
  43. package/dist/schemas/customer/customer-schema.js +399 -0
  44. package/dist/schemas/index.d.ts +27 -0
  45. package/dist/schemas/index.js +138 -0
  46. package/dist/schemas/inventory/inventory-tables.d.ts +9476 -0
  47. package/dist/schemas/inventory/inventory-tables.js +1470 -0
  48. package/dist/schemas/inventory/lot-tables.d.ts +3281 -0
  49. package/dist/schemas/inventory/lot-tables.js +608 -0
  50. package/dist/schemas/order/order-schema.d.ts +5825 -0
  51. package/dist/schemas/order/order-schema.js +954 -0
  52. package/dist/schemas/product/discount-relations.d.ts +15 -0
  53. package/dist/schemas/product/discount-relations.js +34 -0
  54. package/dist/schemas/product/discount-schema.d.ts +1975 -0
  55. package/dist/schemas/product/discount-schema.js +297 -0
  56. package/dist/schemas/product/product-relations.d.ts +41 -0
  57. package/dist/schemas/product/product-relations.js +133 -0
  58. package/dist/schemas/product/product-schema.d.ts +4544 -0
  59. package/dist/schemas/product/product-schema.js +671 -0
  60. package/dist/schemas/store/store-audit-schema.d.ts +4135 -0
  61. package/dist/schemas/store/store-audit-schema.js +556 -0
  62. package/dist/schemas/store/store-schema.d.ts +3100 -0
  63. package/dist/schemas/store/store-schema.js +381 -0
  64. package/dist/schemas/store/store-settings-schema.d.ts +665 -0
  65. package/dist/schemas/store/store-settings-schema.js +141 -0
  66. package/dist/schemas/types.d.ts +50 -0
  67. package/dist/schemas/types.js +3 -0
  68. package/dist/types/events.d.ts +2396 -0
  69. package/dist/types/events.js +505 -0
  70. package/dist/utils/errorHandler.d.ts +12 -0
  71. package/dist/utils/errorHandler.js +36 -0
  72. package/dist/utils/subdomain.d.ts +6 -0
  73. package/dist/utils/subdomain.js +20 -0
  74. package/nul +8 -0
  75. package/package.json +43 -0
  76. package/src/configs/index.ts +654 -0
  77. package/src/events/kafka.ts +429 -0
  78. package/src/index.ts +26 -0
  79. package/src/interfaces/customer-events.ts +106 -0
  80. package/src/interfaces/inventory-events.ts +545 -0
  81. package/src/interfaces/inventory-types.ts +1004 -0
  82. package/src/interfaces/order-events.ts +381 -0
  83. package/src/lib/auditLogger.ts +1117 -0
  84. package/src/lib/authOrganization.ts +153 -0
  85. package/src/lib/db.ts +64 -0
  86. package/src/middleware/serviceAuth.ts +328 -0
  87. package/src/middleware/storeOwnership.ts +199 -0
  88. package/src/middleware/storeValidationMiddleware.ts +247 -0
  89. package/src/middleware/userAuth.ts +248 -0
  90. package/src/schemas/admin/admin-schema.ts +208 -0
  91. package/src/schemas/ai-moderation/ai-moderation-schema.ts +180 -0
  92. package/src/schemas/common/common-schemas.ts +108 -0
  93. package/src/schemas/compliance/compliance-schema.ts +927 -0
  94. package/src/schemas/compliance/kyc-schema.ts +649 -0
  95. package/src/schemas/customer/customer-schema.ts +576 -0
  96. package/src/schemas/index.ts +189 -0
  97. package/src/schemas/inventory/inventory-tables.ts +1927 -0
  98. package/src/schemas/inventory/lot-tables.ts +799 -0
  99. package/src/schemas/order/order-schema.ts +1400 -0
  100. package/src/schemas/product/discount-relations.ts +44 -0
  101. package/src/schemas/product/discount-schema.ts +464 -0
  102. package/src/schemas/product/product-relations.ts +187 -0
  103. package/src/schemas/product/product-schema.ts +955 -0
  104. package/src/schemas/store/ethiopian_business_api.md.resolved +212 -0
  105. package/src/schemas/store/store-audit-schema.ts +1257 -0
  106. package/src/schemas/store/store-schema.ts +661 -0
  107. package/src/schemas/store/store-settings-schema.ts +231 -0
  108. package/src/schemas/types.ts +67 -0
  109. package/src/types/events.ts +646 -0
  110. package/src/utils/errorHandler.ts +44 -0
  111. package/src/utils/subdomain.ts +19 -0
  112. package/tsconfig.json +21 -0
@@ -0,0 +1,231 @@
1
+ import { createId } from "@paralleldrive/cuid2";
2
+ import { relations } from "drizzle-orm";
3
+ import {
4
+ boolean,
5
+ decimal,
6
+ index,
7
+ jsonb,
8
+ pgTable,
9
+ text,
10
+ timestamp,
11
+ unique,
12
+ varchar,
13
+ } from "drizzle-orm/pg-core";
14
+ import { stores } from "./store-schema";
15
+
16
+ export const storeSettings = pgTable(
17
+ "store_settings",
18
+ {
19
+ id: text("id")
20
+ .primaryKey()
21
+ .$defaultFn(() => createId()),
22
+ storeId: text("store_id")
23
+ .notNull()
24
+ .references(() => stores.id, { onDelete: "cascade" }),
25
+
26
+ storeCountry: varchar("store_country", { length: 100 })
27
+ .notNull()
28
+ .default("Ethiopia"),
29
+
30
+ storeCurrency: varchar("store_currency", { length: 3 })
31
+ .notNull()
32
+ .default("ETB"),
33
+
34
+ storeLanguage: varchar("store_language", { length: 10 })
35
+ .notNull()
36
+ .default("en"),
37
+
38
+ supportedLanguages: jsonb("supported_languages")
39
+ .$type<string[]>()
40
+ .notNull()
41
+ .default(["en"]),
42
+
43
+ supportedCurrencies: jsonb("supported_currencies")
44
+ .$type<string[]>()
45
+ .notNull()
46
+ .default(["ETB"]),
47
+
48
+ localizationSettings: jsonb("localization_settings")
49
+ .$type<{
50
+ dateFormat?: string;
51
+ timeFormat?: string;
52
+ numberFormat?: string;
53
+ firstDayOfWeek?: number;
54
+ weightUnit?: "kg" | "lb";
55
+ dimensionUnit?: "cm" | "inch";
56
+ }>()
57
+ .default({
58
+ dateFormat: "DD/MM/YYYY",
59
+ timeFormat: "HH:mm",
60
+ numberFormat: "1,234.56",
61
+ firstDayOfWeek: 0,
62
+ weightUnit: "kg",
63
+ dimensionUnit: "cm",
64
+ }),
65
+
66
+ generalSettings: jsonb("general_settings")
67
+ .$type<{
68
+ autoApplyTax?: boolean;
69
+ pricesIncludeTax?: boolean;
70
+ enableMultiCurrency?: boolean;
71
+ enableMultiLanguage?: boolean;
72
+ currencyDisplayFormat?: "symbol" | "code" | "both";
73
+ }>()
74
+ .default({
75
+ autoApplyTax: true,
76
+ pricesIncludeTax: false,
77
+ enableMultiCurrency: false,
78
+ enableMultiLanguage: false,
79
+ currencyDisplayFormat: "symbol",
80
+ }),
81
+
82
+ metadata: jsonb("metadata").$type<Record<string, unknown>>().default({}),
83
+
84
+ createdAt: timestamp("created_at", { withTimezone: true })
85
+ .defaultNow()
86
+ .notNull(),
87
+ updatedAt: timestamp("updated_at", { withTimezone: true })
88
+ .defaultNow()
89
+ .notNull(),
90
+ },
91
+ (table) => ({
92
+ storeIdIndex: index("idx_store_settings_store_id").on(table.storeId),
93
+ storeIdUnique: unique("idx_store_settings_store_unique").on(table.storeId),
94
+ countryIndex: index("idx_store_settings_country").on(table.storeCountry),
95
+ }),
96
+ );
97
+
98
+ export const marketRegions = pgTable(
99
+ "market_regions",
100
+ {
101
+ id: text("id")
102
+ .primaryKey()
103
+ .$defaultFn(() => createId()),
104
+ storeId: text("store_id")
105
+ .notNull()
106
+ .references(() => stores.id, { onDelete: "cascade" }),
107
+
108
+ regionName: varchar("region_name", { length: 100 }).notNull(),
109
+ regionCode: varchar("region_code", { length: 10 }).notNull(),
110
+
111
+ country: varchar("country", { length: 100 }).notNull().default("Ethiopia"),
112
+ countryCode: varchar("country_code", { length: 2 })
113
+ .notNull()
114
+ .default("ET"),
115
+
116
+ currency: varchar("currency", { length: 3 }).notNull().default("ETB"),
117
+ language: varchar("language", { length: 10 }).notNull().default("en"),
118
+
119
+ taxEnabled: boolean("tax_enabled").notNull().default(true),
120
+ taxPercentage: decimal("tax_percentage", { precision: 5, scale: 2 })
121
+ .notNull()
122
+ .default("15.00"),
123
+
124
+ taxName: varchar("tax_name", { length: 50 }).default("VAT"),
125
+
126
+ taxConfiguration: jsonb("tax_configuration")
127
+ .$type<{
128
+ taxType?: "VAT" | "GST" | "SALES_TAX" | "CUSTOM";
129
+ taxNumber?: string;
130
+ taxRegistrationDate?: string;
131
+ includeTaxInPrice?: boolean;
132
+ compoundTax?: boolean;
133
+ exemptCategories?: string[];
134
+ reducedRates?: Array<{
135
+ categoryId: string;
136
+ categoryName: string;
137
+ rate: number;
138
+ }>;
139
+ }>()
140
+ .default({
141
+ taxType: "VAT",
142
+ includeTaxInPrice: false,
143
+ compoundTax: false,
144
+ exemptCategories: [],
145
+ reducedRates: [],
146
+ }),
147
+
148
+ isDefault: boolean("is_default").notNull().default(false),
149
+ isActive: boolean("is_active").notNull().default(true),
150
+
151
+ shippingSettings: jsonb("shipping_settings")
152
+ .$type<{
153
+ enableShipping?: boolean;
154
+ freeShippingThreshold?: number;
155
+ defaultShippingRate?: number;
156
+ shippingZones?: Array<{
157
+ name: string;
158
+ rate: number;
159
+ estimatedDays: number;
160
+ }>;
161
+ }>()
162
+ .default({
163
+ enableShipping: true,
164
+ freeShippingThreshold: 0,
165
+ defaultShippingRate: 0,
166
+ shippingZones: [],
167
+ }),
168
+
169
+ paymentSettings: jsonb("payment_settings")
170
+ .$type<{
171
+ enabledPaymentMethods?: string[];
172
+ preferredPaymentMethod?: string;
173
+ paymentProcessingFee?: number;
174
+ }>()
175
+ .default({
176
+ enabledPaymentMethods: ["cash", "card"],
177
+ preferredPaymentMethod: "cash",
178
+ paymentProcessingFee: 0,
179
+ }),
180
+
181
+ metadata: jsonb("metadata").$type<Record<string, unknown>>().default({}),
182
+
183
+ createdAt: timestamp("created_at", { withTimezone: true })
184
+ .defaultNow()
185
+ .notNull(),
186
+ updatedAt: timestamp("updated_at", { withTimezone: true })
187
+ .defaultNow()
188
+ .notNull(),
189
+ },
190
+ (table) => ({
191
+ storeIdIndex: index("idx_market_regions_store_id").on(table.storeId),
192
+ regionCodeIndex: index("idx_market_regions_code").on(
193
+ table.storeId,
194
+ table.regionCode,
195
+ ),
196
+ defaultIndex: index("idx_market_regions_default").on(
197
+ table.storeId,
198
+ table.isDefault,
199
+ ),
200
+ activeIndex: index("idx_market_regions_active").on(
201
+ table.storeId,
202
+ table.isActive,
203
+ ),
204
+ uniqueRegionCode: unique("idx_market_regions_unique_code").on(
205
+ table.storeId,
206
+ table.regionCode,
207
+ ),
208
+ }),
209
+ );
210
+
211
+ export const storeSettingsRelations = relations(storeSettings, ({ one, many }) => ({
212
+ store: one(stores, {
213
+ fields: [storeSettings.storeId],
214
+ references: [stores.id],
215
+ }),
216
+ }));
217
+
218
+ export const marketRegionsRelations = relations(marketRegions, ({ one }) => ({
219
+ store: one(stores, {
220
+ fields: [marketRegions.storeId],
221
+ references: [stores.id],
222
+ }),
223
+ }));
224
+
225
+ export const storesRelationsExtended = relations(stores, ({ one, many }) => ({
226
+ settings: one(storeSettings, {
227
+ fields: [stores.id],
228
+ references: [storeSettings.storeId],
229
+ }),
230
+ marketRegions: many(marketRegions),
231
+ }));
@@ -0,0 +1,67 @@
1
+ // Core types used across all schemas
2
+
3
+ // Common result type for all operations
4
+ export interface OperationResult<T = unknown> {
5
+ success: boolean;
6
+ data?: T;
7
+ error?: string;
8
+ metadata?: Record<string, unknown>;
9
+ }
10
+
11
+ // Paginated result wrapper
12
+ export interface PaginatedResult<T> {
13
+ items: T[];
14
+ pagination: {
15
+ page: number;
16
+ limit: number;
17
+ total: number;
18
+ pages: number;
19
+ hasNext: boolean;
20
+ hasPrev: boolean;
21
+ };
22
+ }
23
+
24
+ // Multi-tenancy context
25
+ export interface TenantContext {
26
+ tenantId: string;
27
+ storeId: string;
28
+ userId?: string;
29
+ userRole?: string;
30
+ }
31
+
32
+ // User context for operations
33
+ export interface UserContext {
34
+ userId: string;
35
+ userName: string;
36
+ userRole: string;
37
+ permissions: string[];
38
+ tenantId: string;
39
+ storeId: string;
40
+ }
41
+
42
+ // Base filter interface
43
+ export interface BaseFilter {
44
+ page?: number;
45
+ limit?: number;
46
+ sortBy?: string;
47
+ sortOrder?: "asc" | "desc";
48
+ search?: string;
49
+ }
50
+
51
+ // Date range filter
52
+ export interface DateRangeFilter {
53
+ startDate?: string;
54
+ endDate?: string;
55
+ }
56
+
57
+ // Status filter
58
+ export interface StatusFilter {
59
+ status?: string | string[];
60
+ }
61
+
62
+ // Location filter
63
+ export interface LocationFilter {
64
+ locationId?: string;
65
+ locationType?: "warehouse" | "pos";
66
+ locationIds?: string[];
67
+ }