@culturefy/shared 1.0.18 → 1.0.20

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.
@@ -4,8 +4,9 @@ exports.__esModule = true;
4
4
  exports.AzureSecretKeysEnum = void 0;
5
5
  // Enum for secret keys
6
6
  let AzureSecretKeysEnum = exports.AzureSecretKeysEnum = /*#__PURE__*/function (AzureSecretKeysEnum) {
7
- // STRIPE_PUBLISHABLE_KEY = "Stripe-Publishable-Key",
8
- AzureSecretKeysEnum["STRIPE_SECRET_KEY"] = "Stripe-secret-key";
7
+ AzureSecretKeysEnum["KEYCLOAK_ADMIN_CLIENT_SECRET"] = "KEYCLOAK-ADMIN-CLIENT-SECRET";
8
+ AzureSecretKeysEnum["KEYCLOAK_ADMIN_CLIENT_ID"] = "KEYCLOAK-ADMIN-CLIENT-ID";
9
+ AzureSecretKeysEnum["KEYCLOAK_BASE_URL"] = "KEYCLOAK-BASE-URL";
9
10
  AzureSecretKeysEnum["STRIPE_PAYMENT_WEBHOOK_SECRET_KEY"] = "Stripe-payment-webhook-secret-key";
10
11
  // in-use
11
12
  AzureSecretKeysEnum["STRIPE_PRODUCT_WEBHOOK_SECRET"] = "Stripe-product-webhook-secret-key";
@@ -14,15 +15,20 @@ let AzureSecretKeysEnum = exports.AzureSecretKeysEnum = /*#__PURE__*/function (A
14
15
  // in-use
15
16
  AzureSecretKeysEnum["EMAIL_SERVICE_URL"] = "Email-Service-Url";
16
17
  // in-use,
17
- AzureSecretKeysEnum["STRIPE_WEBHOOK_CUSTOMER_CREATED_SECRET_KEY"] = "Stripe-Webhook-Customer-Created-Secret-Key";
18
- // in-use
19
- AzureSecretKeysEnum["DB_CONNECTING_STRING_BILLING"] = "DB-Connecting-String-Billing";
20
- AzureSecretKeysEnum["STRIPE_PRODUCT_SYNC_WEBHOOK_SECRET"] = "stripe-product-sync-webhook-secret-key";
21
- // in-use
18
+ AzureSecretKeysEnum["DB_CONNECTING_STRING_AUTH"] = "DB-CONNECTION-STRING-AUTH";
19
+ AzureSecretKeysEnum["DB_CONNECTING_STRING_USER"] = "DB-CONNECTION-STRING-USER";
20
+ AzureSecretKeysEnum["DB_CONNECTING_STRING_TENANT_BRIDGE"] = "DB-Connecting-String-Tenant-Bridge";
21
+ AzureSecretKeysEnum["DB_CONNECTING_STRING_PAYMENT"] = "DB-CONNECTION-STRING-PAYMENT";
22
+ AzureSecretKeysEnum["DB_CONNECTING_STRING_CORE"] = "DB-CONNECTION-STRING-CORE";
23
+ AzureSecretKeysEnum["DB_CONNECTING_STRING_BILLING"] = "DB-CONNECTION-STRING-BILLING";
24
+ AzureSecretKeysEnum["DB_CONNECTING_STRING_STAGING"] = "DB-CONNECTION-STRING-STAGING";
25
+ AzureSecretKeysEnum["SERVICE_BUS_CONNECTION_STRING"] = "servicebus-connection-string";
22
26
  AzureSecretKeysEnum["STRIPE_CUSTOMER_SYNC_WEBHOOK_SECRET"] = "stripe-customer-sync-webhook-secret-key";
23
27
  AzureSecretKeysEnum["STRIPE_INVOICE_SYNC_WEBHOOK_SECRET"] = "stripe-invoice-sync-webhook-secret-key";
28
+ AzureSecretKeysEnum["STRIPE_PRODUCT_SYNC_WEBHOOK_SECRET"] = "stripe-product-sync-webhook-secret-key";
29
+ AzureSecretKeysEnum["STRIPE_SECRET_KEY"] = "Stripe-secret-key";
30
+ AzureSecretKeysEnum["STRIPE_WEBHOOK_CUSTOMER_CREATED_SECRET_KEY"] = "Stripe-Webhook-Customer-Created-Secret-Key";
24
31
  AzureSecretKeysEnum["STRIPE_SUBSCRIPTION_SYNC_WEBHOOK_SECRET"] = "stripe-subscription-sync-webhook-secret-key";
25
- AzureSecretKeysEnum["DB_CONNECTING_STRING_CORE"] = "DB-Connecting-String-Core";
26
32
  return AzureSecretKeysEnum;
27
33
  }({});
28
34
  //# sourceMappingURL=secretKeys.enum.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"secretKeys.enum.js","names":["AzureSecretKeysEnum","exports"],"sources":["../../../src/enums/secretKeys.enum.ts"],"sourcesContent":["// Enum for secret keys\nexport enum AzureSecretKeysEnum {\n // STRIPE_PUBLISHABLE_KEY = \"Stripe-Publishable-Key\",\n STRIPE_SECRET_KEY = \"Stripe-secret-key\",\n STRIPE_PAYMENT_WEBHOOK_SECRET_KEY = \"Stripe-payment-webhook-secret-key\", // in-use\n STRIPE_PRODUCT_WEBHOOK_SECRET = \"Stripe-product-webhook-secret-key\", // in-use\n STRIPE_PRICE_WEBHOOK_SECRET = \"Stripe-price-webhook-secret-key\", // in-use\n EMAIL_SERVICE_URL = \"Email-Service-Url\", // in-use,\n STRIPE_WEBHOOK_CUSTOMER_CREATED_SECRET_KEY = \"Stripe-Webhook-Customer-Created-Secret-Key\",// in-use\n DB_CONNECTING_STRING_BILLING = \"DB-Connecting-String-Billing\",\n STRIPE_PRODUCT_SYNC_WEBHOOK_SECRET = \"stripe-product-sync-webhook-secret-key\",// in-use\n STRIPE_CUSTOMER_SYNC_WEBHOOK_SECRET = \"stripe-customer-sync-webhook-secret-key\",\n STRIPE_INVOICE_SYNC_WEBHOOK_SECRET = \"stripe-invoice-sync-webhook-secret-key\",\n STRIPE_SUBSCRIPTION_SYNC_WEBHOOK_SECRET = \"stripe-subscription-sync-webhook-secret-key\",\n DB_CONNECTING_STRING_CORE = \"DB-Connecting-String-Core\",\n}\n"],"mappings":";;;;AAAA;AAAA,IACYA,mBAAmB,GAAAC,OAAA,CAAAD,mBAAA,0BAAnBA,mBAAmB;EAC7B;EADUA,mBAAmB;EAAnBA,mBAAmB;EAG4C;EAH/DA,mBAAmB;EAIwC;EAJ3DA,mBAAmB;EAKoC;EALvDA,mBAAmB;EAMY;EAN/BA,mBAAmB;EAO6D;EAPhFA,mBAAmB;EAAnBA,mBAAmB;EASiD;EATpEA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAA,OAAnBA,mBAAmB;AAAA","ignoreList":[]}
1
+ {"version":3,"file":"secretKeys.enum.js","names":["AzureSecretKeysEnum","exports"],"sources":["../../../src/enums/secretKeys.enum.ts"],"sourcesContent":["// Enum for secret keys\nexport enum AzureSecretKeysEnum {\n KEYCLOAK_ADMIN_CLIENT_SECRET = \"KEYCLOAK-ADMIN-CLIENT-SECRET\",\n KEYCLOAK_ADMIN_CLIENT_ID = \"KEYCLOAK-ADMIN-CLIENT-ID\",\n KEYCLOAK_BASE_URL = \"KEYCLOAK-BASE-URL\",\n STRIPE_PAYMENT_WEBHOOK_SECRET_KEY = \"Stripe-payment-webhook-secret-key\", // in-use\n STRIPE_PRODUCT_WEBHOOK_SECRET = \"Stripe-product-webhook-secret-key\", // in-use\n STRIPE_PRICE_WEBHOOK_SECRET = \"Stripe-price-webhook-secret-key\", // in-use\n EMAIL_SERVICE_URL = \"Email-Service-Url\", // in-use,\n DB_CONNECTING_STRING_AUTH = \"DB-CONNECTION-STRING-AUTH\",\n DB_CONNECTING_STRING_USER = \"DB-CONNECTION-STRING-USER\",\n DB_CONNECTING_STRING_TENANT_BRIDGE = \"DB-Connecting-String-Tenant-Bridge\",\n DB_CONNECTING_STRING_PAYMENT = \"DB-CONNECTION-STRING-PAYMENT\",\n DB_CONNECTING_STRING_CORE = \"DB-CONNECTION-STRING-CORE\",\n DB_CONNECTING_STRING_BILLING = \"DB-CONNECTION-STRING-BILLING\",\n DB_CONNECTING_STRING_STAGING = \"DB-CONNECTION-STRING-STAGING\",\n SERVICE_BUS_CONNECTION_STRING = \"servicebus-connection-string\",\n STRIPE_CUSTOMER_SYNC_WEBHOOK_SECRET = \"stripe-customer-sync-webhook-secret-key\",\n STRIPE_INVOICE_SYNC_WEBHOOK_SECRET = \"stripe-invoice-sync-webhook-secret-key\",\n STRIPE_PRODUCT_SYNC_WEBHOOK_SECRET = \"stripe-product-sync-webhook-secret-key\",\n STRIPE_SECRET_KEY = \"Stripe-secret-key\",\n STRIPE_WEBHOOK_CUSTOMER_CREATED_SECRET_KEY = \"Stripe-Webhook-Customer-Created-Secret-Key\",\n STRIPE_SUBSCRIPTION_SYNC_WEBHOOK_SECRET = \"stripe-subscription-sync-webhook-secret-key\",\n}\n"],"mappings":";;;;AAAA;AAAA,IACYA,mBAAmB,GAAAC,OAAA,CAAAD,mBAAA,0BAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAI4C;EAJ/DA,mBAAmB;EAKwC;EAL3DA,mBAAmB;EAMoC;EANvDA,mBAAmB;EAOY;EAP/BA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAA,OAAnBA,mBAAmB;AAAA","ignoreList":[]}
@@ -19,4 +19,10 @@ Object.keys(_initializers).forEach(function (key) {
19
19
  if (key in exports && exports[key] === _initializers[key]) return;
20
20
  exports[key] = _initializers[key];
21
21
  });
22
+ var _mapper = require("./mapper");
23
+ Object.keys(_mapper).forEach(function (key) {
24
+ if (key === "default" || key === "__esModule") return;
25
+ if (key in exports && exports[key] === _mapper[key]) return;
26
+ exports[key] = _mapper[key];
27
+ });
22
28
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["_secrets","require","Object","keys","forEach","key","exports","_response","_initializers"],"sources":["../../../src/utils/index.ts"],"sourcesContent":["export * from './secrets';\nexport * from './response';\nexport * from './initializers';"],"mappings":";;;AAAA,IAAAA,QAAA,GAAAC,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAH,QAAA,EAAAI,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAL,QAAA,CAAAK,GAAA;EAAAC,OAAA,CAAAD,GAAA,IAAAL,QAAA,CAAAK,GAAA;AAAA;AACA,IAAAE,SAAA,GAAAN,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAI,SAAA,EAAAH,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAE,SAAA,CAAAF,GAAA;EAAAC,OAAA,CAAAD,GAAA,IAAAE,SAAA,CAAAF,GAAA;AAAA;AACA,IAAAG,aAAA,GAAAP,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAK,aAAA,EAAAJ,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAG,aAAA,CAAAH,GAAA;EAAAC,OAAA,CAAAD,GAAA,IAAAG,aAAA,CAAAH,GAAA;AAAA","ignoreList":[]}
1
+ {"version":3,"file":"index.js","names":["_secrets","require","Object","keys","forEach","key","exports","_response","_initializers","_mapper"],"sources":["../../../src/utils/index.ts"],"sourcesContent":["export * from './secrets';\nexport * from './response';\nexport * from './initializers';\nexport * from './mapper';"],"mappings":";;;AAAA,IAAAA,QAAA,GAAAC,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAH,QAAA,EAAAI,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAL,QAAA,CAAAK,GAAA;EAAAC,OAAA,CAAAD,GAAA,IAAAL,QAAA,CAAAK,GAAA;AAAA;AACA,IAAAE,SAAA,GAAAN,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAI,SAAA,EAAAH,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAE,SAAA,CAAAF,GAAA;EAAAC,OAAA,CAAAD,GAAA,IAAAE,SAAA,CAAAF,GAAA;AAAA;AACA,IAAAG,aAAA,GAAAP,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAK,aAAA,EAAAJ,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAG,aAAA,CAAAH,GAAA;EAAAC,OAAA,CAAAD,GAAA,IAAAG,aAAA,CAAAH,GAAA;AAAA;AACA,IAAAI,OAAA,GAAAR,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAM,OAAA,EAAAL,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAI,OAAA,CAAAJ,GAAA;EAAAC,OAAA,CAAAD,GAAA,IAAAI,OAAA,CAAAJ,GAAA;AAAA","ignoreList":[]}
@@ -12,27 +12,80 @@ function WithDb(_target, _propertyKey, descriptor) {
12
12
  if (!descriptor.value) return;
13
13
  const originalMethod = descriptor.value;
14
14
  descriptor.value = async function (...args) {
15
- // Await the shared connection promise
16
- await this.dbConnected;
15
+ await this.ensureConnection();
17
16
  return originalMethod.apply(this, args);
18
17
  };
19
18
  }
20
19
  class Initializers {
21
20
  constructor(context, dbConnectionString) {
21
+ this.context = void 0;
22
+ this.dbConnectionString = void 0;
23
+ this.connectionInitialized = false;
22
24
  this.context = context;
23
25
  this.dbConnectionString = dbConnectionString;
24
- this.dbConnected = void 0;
25
- // Connect once to the default mongoose instance
26
- this.dbConnected = _mongoose.default.connect(dbConnectionString, {
27
- serverSelectionTimeoutMS: 10000
28
- }).then(conn => {
29
- context.info('✅ MongoDB connected');
30
- return _mongoose.default;
31
- }).catch(err => {
32
- context.error('❌ MongoDB connection error', err);
33
- throw err;
34
- });
26
+ }
27
+ async ensureConnection() {
28
+ // Check if we already have a connection for this specific connection string
29
+ const existingConnection = Initializers.connections.get(this.dbConnectionString);
30
+ if (existingConnection && existingConnection.connection.readyState === 1) {
31
+ return existingConnection;
32
+ }
33
+
34
+ // Create a new Mongoose instance for this connection string
35
+ this.context.info(`Initializing database connection... ${this.dbConnectionString}`);
36
+ const newMongoose = new _mongoose.default.Mongoose();
37
+ try {
38
+ await newMongoose.connect(this.dbConnectionString, {
39
+ serverSelectionTimeoutMS: 10000,
40
+ connectTimeoutMS: 10000,
41
+ socketTimeoutMS: 45000
42
+ });
43
+ this.context.info(`✅ MongoDB connected successfully ${this.dbConnectionString}`);
44
+ this.connectionInitialized = true;
45
+
46
+ // Store the connection for this connection string
47
+ Initializers.connections.set(this.dbConnectionString, newMongoose);
48
+ return newMongoose;
49
+ } catch (err) {
50
+ this.context.error(`❌ MongoDB connection error for ${this.dbConnectionString}`, {
51
+ message: err.message,
52
+ name: err.name,
53
+ code: err.code,
54
+ stack: err.stack
55
+ });
56
+ this.connectionInitialized = false;
57
+ throw new Error(`Failed to connect to MongoDB: ${err.message}`);
58
+ }
59
+ }
60
+ getConnection() {
61
+ return Initializers.connections.get(this.dbConnectionString);
62
+ }
63
+ async forceNewConnection() {
64
+ // Disconnect existing connection if any
65
+ await this.disconnect();
66
+
67
+ // Reset connection state
68
+ this.connectionInitialized = false;
69
+
70
+ // Create a new connection
71
+ return this.ensureConnection();
72
+ }
73
+ async disconnect() {
74
+ const connection = Initializers.connections.get(this.dbConnectionString);
75
+ if (connection) {
76
+ try {
77
+ // Properly close the connection
78
+ await connection.disconnect();
79
+ this.context.info(`✅ Disconnected from database: ${this.dbConnectionString}`);
80
+ } catch (error) {
81
+ this.context.error(`❌ Error disconnecting from database: ${this.dbConnectionString}`, error);
82
+ } finally {
83
+ // Always remove from the connections map
84
+ Initializers.connections.delete(this.dbConnectionString);
85
+ }
86
+ }
35
87
  }
36
88
  }
37
89
  exports.Initializers = Initializers;
90
+ Initializers.connections = new Map();
38
91
  //# sourceMappingURL=initializers.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"initializers.js","names":["_mongoose","_interopRequireDefault","require","e","__esModule","default","WithDb","_target","_propertyKey","descriptor","value","originalMethod","args","dbConnected","apply","Initializers","constructor","context","dbConnectionString","mongoose","connect","serverSelectionTimeoutMS","then","conn","info","catch","err","error","exports"],"sources":["../../../src/utils/initializers.ts"],"sourcesContent":["import mongoose from \"mongoose\";\nimport { InvocationContext } from \"@azure/functions\";\n\n/**\n * Decorator that ensures MongoDB is connected before the method runs.\n */\nexport function WithDb(\n _target: any,\n _propertyKey: string | symbol,\n descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<any>>\n): void {\n if (!descriptor.value) return;\n const originalMethod = descriptor.value;\n descriptor.value = async function (this: Initializers, ...args: any[]) {\n // Await the shared connection promise\n await this.dbConnected;\n return originalMethod.apply(this, args);\n };\n}\n\nexport abstract class Initializers {\n protected dbConnected: Promise<typeof mongoose>;\n\n constructor(\n protected context: InvocationContext,\n protected dbConnectionString: string\n ) {\n // Connect once to the default mongoose instance\n this.dbConnected = mongoose\n .connect(dbConnectionString, {\n serverSelectionTimeoutMS: 10000\n })\n .then(conn => {\n context.info('✅ MongoDB connected');\n return mongoose;\n })\n .catch(err => {\n context.error('❌ MongoDB connection error', err);\n throw err;\n });\n }\n}\n"],"mappings":";;;;;AAAA,IAAAA,SAAA,GAAAC,sBAAA,CAAAC,OAAA;AAAgC,SAAAD,uBAAAE,CAAA,WAAAA,CAAA,IAAAA,CAAA,CAAAC,UAAA,GAAAD,CAAA,KAAAE,OAAA,EAAAF,CAAA;AAGhC;AACA;AACA;AACO,SAASG,MAAMA,CACpBC,OAAY,EACZC,YAA6B,EAC7BC,UAAqE,EAC/D;EACN,IAAI,CAACA,UAAU,CAACC,KAAK,EAAE;EACvB,MAAMC,cAAc,GAAGF,UAAU,CAACC,KAAK;EACvCD,UAAU,CAACC,KAAK,GAAG,gBAAoC,GAAGE,IAAW,EAAE;IACrE;IACA,MAAM,IAAI,CAACC,WAAW;IACtB,OAAOF,cAAc,CAACG,KAAK,CAAC,IAAI,EAAEF,IAAI,CAAC;EACzC,CAAC;AACH;AAEO,MAAeG,YAAY,CAAC;EAGjCC,WAAWA,CACCC,OAA0B,EAC1BC,kBAA0B,EACpC;IAAA,KAFUD,OAA0B,GAA1BA,OAA0B;IAAA,KAC1BC,kBAA0B,GAA1BA,kBAA0B;IAAA,KAJ5BL,WAAW;IAMnB;IACA,IAAI,CAACA,WAAW,GAAGM,iBAAQ,CACxBC,OAAO,CAACF,kBAAkB,EAAE;MAC3BG,wBAAwB,EAAE;IAC5B,CAAC,CAAC,CACDC,IAAI,CAACC,IAAI,IAAI;MACZN,OAAO,CAACO,IAAI,CAAC,qBAAqB,CAAC;MACnC,OAAOL,iBAAQ;IACjB,CAAC,CAAC,CACDM,KAAK,CAACC,GAAG,IAAI;MACZT,OAAO,CAACU,KAAK,CAAC,4BAA4B,EAAED,GAAG,CAAC;MAChD,MAAMA,GAAG;IACX,CAAC,CAAC;EACN;AACF;AAACE,OAAA,CAAAb,YAAA,GAAAA,YAAA","ignoreList":[]}
1
+ {"version":3,"file":"initializers.js","names":["_mongoose","_interopRequireDefault","require","e","__esModule","default","WithDb","_target","_propertyKey","descriptor","value","originalMethod","args","ensureConnection","apply","Initializers","constructor","context","dbConnectionString","connectionInitialized","existingConnection","connections","get","connection","readyState","info","newMongoose","mongoose","Mongoose","connect","serverSelectionTimeoutMS","connectTimeoutMS","socketTimeoutMS","set","err","error","message","name","code","stack","Error","getConnection","forceNewConnection","disconnect","delete","exports","Map"],"sources":["../../../src/utils/initializers.ts"],"sourcesContent":["import mongoose from \"mongoose\";\nimport { InvocationContext } from \"@azure/functions\";\n\n/**\n * Decorator that ensures MongoDB is connected before the method runs.\n */\nexport function WithDb(\n _target: any,\n _propertyKey: string | symbol,\n descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<any>>\n): void {\n if (!descriptor.value) return;\n const originalMethod = descriptor.value;\n descriptor.value = async function (this: Initializers, ...args: any[]) {\n await this.ensureConnection();\n return originalMethod.apply(this, args);\n };\n}\n\nexport abstract class Initializers {\n protected static connections: Map<string, mongoose.Mongoose> = new Map();\n protected context: InvocationContext;\n protected dbConnectionString: string;\n private connectionInitialized: boolean = false;\n\n constructor(context: InvocationContext, dbConnectionString: string) {\n this.context = context;\n this.dbConnectionString = dbConnectionString;\n }\n\n protected async ensureConnection(): Promise<mongoose.Mongoose> {\n // Check if we already have a connection for this specific connection string\n const existingConnection = Initializers.connections.get(this.dbConnectionString);\n if (existingConnection && existingConnection.connection.readyState === 1) {\n return existingConnection;\n }\n\n // Create a new Mongoose instance for this connection string\n this.context.info(`Initializing database connection... ${this.dbConnectionString}`);\n const newMongoose = new mongoose.Mongoose();\n \n try {\n await newMongoose.connect(this.dbConnectionString, {\n serverSelectionTimeoutMS: 10000,\n connectTimeoutMS: 10000,\n socketTimeoutMS: 45000,\n });\n \n this.context.info(`✅ MongoDB connected successfully ${this.dbConnectionString}`);\n this.connectionInitialized = true;\n \n // Store the connection for this connection string\n Initializers.connections.set(this.dbConnectionString, newMongoose);\n \n return newMongoose;\n } catch (err: any) {\n this.context.error(`❌ MongoDB connection error for ${this.dbConnectionString}`, {\n message: err.message,\n name: err.name,\n code: err.code,\n stack: err.stack\n });\n this.connectionInitialized = false;\n throw new Error(`Failed to connect to MongoDB: ${err.message}`);\n }\n }\n\n protected getConnection(): mongoose.Mongoose | undefined {\n return Initializers.connections.get(this.dbConnectionString);\n }\n\n async forceNewConnection(): Promise<mongoose.Mongoose> {\n // Disconnect existing connection if any\n await this.disconnect();\n \n // Reset connection state\n this.connectionInitialized = false;\n \n // Create a new connection\n return this.ensureConnection();\n }\n \n async disconnect(): Promise<void> {\n const connection = Initializers.connections.get(this.dbConnectionString);\n if (connection) {\n try {\n // Properly close the connection\n await connection.disconnect();\n this.context.info(`✅ Disconnected from database: ${this.dbConnectionString}`);\n } catch (error) {\n this.context.error(`❌ Error disconnecting from database: ${this.dbConnectionString}`, error);\n } finally {\n // Always remove from the connections map\n Initializers.connections.delete(this.dbConnectionString);\n }\n }\n }\n}\n"],"mappings":";;;;;AAAA,IAAAA,SAAA,GAAAC,sBAAA,CAAAC,OAAA;AAAgC,SAAAD,uBAAAE,CAAA,WAAAA,CAAA,IAAAA,CAAA,CAAAC,UAAA,GAAAD,CAAA,KAAAE,OAAA,EAAAF,CAAA;AAGhC;AACA;AACA;AACO,SAASG,MAAMA,CACpBC,OAAY,EACZC,YAA6B,EAC7BC,UAAqE,EAC/D;EACN,IAAI,CAACA,UAAU,CAACC,KAAK,EAAE;EACvB,MAAMC,cAAc,GAAGF,UAAU,CAACC,KAAK;EACvCD,UAAU,CAACC,KAAK,GAAG,gBAAoC,GAAGE,IAAW,EAAE;IACrE,MAAM,IAAI,CAACC,gBAAgB,CAAC,CAAC;IAC7B,OAAOF,cAAc,CAACG,KAAK,CAAC,IAAI,EAAEF,IAAI,CAAC;EACzC,CAAC;AACH;AAEO,MAAeG,YAAY,CAAC;EAMjCC,WAAWA,CAACC,OAA0B,EAAEC,kBAA0B,EAAE;IAAA,KAJ1DD,OAAO;IAAA,KACPC,kBAAkB;IAAA,KACpBC,qBAAqB,GAAY,KAAK;IAG5C,IAAI,CAACF,OAAO,GAAGA,OAAO;IACtB,IAAI,CAACC,kBAAkB,GAAGA,kBAAkB;EAC9C;EAEA,MAAgBL,gBAAgBA,CAAA,EAA+B;IAC7D;IACA,MAAMO,kBAAkB,GAAGL,YAAY,CAACM,WAAW,CAACC,GAAG,CAAC,IAAI,CAACJ,kBAAkB,CAAC;IAChF,IAAIE,kBAAkB,IAAIA,kBAAkB,CAACG,UAAU,CAACC,UAAU,KAAK,CAAC,EAAE;MACxE,OAAOJ,kBAAkB;IAC3B;;IAEA;IACA,IAAI,CAACH,OAAO,CAACQ,IAAI,CAAC,uCAAuC,IAAI,CAACP,kBAAkB,EAAE,CAAC;IACnF,MAAMQ,WAAW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AY,CAACM,WAAW,CAACC,GAAG,CAAC,IAAI,CAACJ,kBAAkB,CAAC;IACxE,IAAIK,UAAU,EAAE;MACd,IAAI;QACF;QACA,MAAMA,UAAU,CAACoB,UAAU,CAAC,CAAC;QAC7B,IAAI,CAAC1B,OAAO,CAACQ,IAAI,CAAC,iCAAiC,IAAI,CAACP,kBAAkB,EAAE,CAAC;MAC/E,CAAC,CAAC,OAAOiB,KAAK,EAAE;QACd,IAAI,CAAClB,OAAO,CAACkB,KAAK,CAAC,wCAAwC,IAAI,CAACjB,kBAAkB,EAAE,EAAEiB,KAAK,CAAC;MAC9F,CAAC,SAAS;QACR;QACApB,YAAY,CAACM,WAAW,CAACuB,MAAM,CAAC,IAAI,CAAC1B,kBAAkB,CAAC;MAC1D;IACF;EACF;AACF;AAAC2B,OAAA,CAAA9B,YAAA,GAAAA,YAAA;AA9EqBA,YAAY,CACfM,WAAW,GAAmC,IAAIyB,GAAG,CAAC,CAAC","ignoreList":[]}
@@ -1,7 +1,8 @@
1
1
  // Enum for secret keys
2
2
  export let AzureSecretKeysEnum = /*#__PURE__*/function (AzureSecretKeysEnum) {
3
- // STRIPE_PUBLISHABLE_KEY = "Stripe-Publishable-Key",
4
- AzureSecretKeysEnum["STRIPE_SECRET_KEY"] = "Stripe-secret-key";
3
+ AzureSecretKeysEnum["KEYCLOAK_ADMIN_CLIENT_SECRET"] = "KEYCLOAK-ADMIN-CLIENT-SECRET";
4
+ AzureSecretKeysEnum["KEYCLOAK_ADMIN_CLIENT_ID"] = "KEYCLOAK-ADMIN-CLIENT-ID";
5
+ AzureSecretKeysEnum["KEYCLOAK_BASE_URL"] = "KEYCLOAK-BASE-URL";
5
6
  AzureSecretKeysEnum["STRIPE_PAYMENT_WEBHOOK_SECRET_KEY"] = "Stripe-payment-webhook-secret-key";
6
7
  // in-use
7
8
  AzureSecretKeysEnum["STRIPE_PRODUCT_WEBHOOK_SECRET"] = "Stripe-product-webhook-secret-key";
@@ -10,15 +11,20 @@ export let AzureSecretKeysEnum = /*#__PURE__*/function (AzureSecretKeysEnum) {
10
11
  // in-use
11
12
  AzureSecretKeysEnum["EMAIL_SERVICE_URL"] = "Email-Service-Url";
12
13
  // in-use,
13
- AzureSecretKeysEnum["STRIPE_WEBHOOK_CUSTOMER_CREATED_SECRET_KEY"] = "Stripe-Webhook-Customer-Created-Secret-Key";
14
- // in-use
15
- AzureSecretKeysEnum["DB_CONNECTING_STRING_BILLING"] = "DB-Connecting-String-Billing";
16
- AzureSecretKeysEnum["STRIPE_PRODUCT_SYNC_WEBHOOK_SECRET"] = "stripe-product-sync-webhook-secret-key";
17
- // in-use
14
+ AzureSecretKeysEnum["DB_CONNECTING_STRING_AUTH"] = "DB-CONNECTION-STRING-AUTH";
15
+ AzureSecretKeysEnum["DB_CONNECTING_STRING_USER"] = "DB-CONNECTION-STRING-USER";
16
+ AzureSecretKeysEnum["DB_CONNECTING_STRING_TENANT_BRIDGE"] = "DB-Connecting-String-Tenant-Bridge";
17
+ AzureSecretKeysEnum["DB_CONNECTING_STRING_PAYMENT"] = "DB-CONNECTION-STRING-PAYMENT";
18
+ AzureSecretKeysEnum["DB_CONNECTING_STRING_CORE"] = "DB-CONNECTION-STRING-CORE";
19
+ AzureSecretKeysEnum["DB_CONNECTING_STRING_BILLING"] = "DB-CONNECTION-STRING-BILLING";
20
+ AzureSecretKeysEnum["DB_CONNECTING_STRING_STAGING"] = "DB-CONNECTION-STRING-STAGING";
21
+ AzureSecretKeysEnum["SERVICE_BUS_CONNECTION_STRING"] = "servicebus-connection-string";
18
22
  AzureSecretKeysEnum["STRIPE_CUSTOMER_SYNC_WEBHOOK_SECRET"] = "stripe-customer-sync-webhook-secret-key";
19
23
  AzureSecretKeysEnum["STRIPE_INVOICE_SYNC_WEBHOOK_SECRET"] = "stripe-invoice-sync-webhook-secret-key";
24
+ AzureSecretKeysEnum["STRIPE_PRODUCT_SYNC_WEBHOOK_SECRET"] = "stripe-product-sync-webhook-secret-key";
25
+ AzureSecretKeysEnum["STRIPE_SECRET_KEY"] = "Stripe-secret-key";
26
+ AzureSecretKeysEnum["STRIPE_WEBHOOK_CUSTOMER_CREATED_SECRET_KEY"] = "Stripe-Webhook-Customer-Created-Secret-Key";
20
27
  AzureSecretKeysEnum["STRIPE_SUBSCRIPTION_SYNC_WEBHOOK_SECRET"] = "stripe-subscription-sync-webhook-secret-key";
21
- AzureSecretKeysEnum["DB_CONNECTING_STRING_CORE"] = "DB-Connecting-String-Core";
22
28
  return AzureSecretKeysEnum;
23
29
  }({});
24
30
  //# sourceMappingURL=secretKeys.enum.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"secretKeys.enum.js","names":["AzureSecretKeysEnum"],"sources":["../../../src/enums/secretKeys.enum.ts"],"sourcesContent":["// Enum for secret keys\nexport enum AzureSecretKeysEnum {\n // STRIPE_PUBLISHABLE_KEY = \"Stripe-Publishable-Key\",\n STRIPE_SECRET_KEY = \"Stripe-secret-key\",\n STRIPE_PAYMENT_WEBHOOK_SECRET_KEY = \"Stripe-payment-webhook-secret-key\", // in-use\n STRIPE_PRODUCT_WEBHOOK_SECRET = \"Stripe-product-webhook-secret-key\", // in-use\n STRIPE_PRICE_WEBHOOK_SECRET = \"Stripe-price-webhook-secret-key\", // in-use\n EMAIL_SERVICE_URL = \"Email-Service-Url\", // in-use,\n STRIPE_WEBHOOK_CUSTOMER_CREATED_SECRET_KEY = \"Stripe-Webhook-Customer-Created-Secret-Key\",// in-use\n DB_CONNECTING_STRING_BILLING = \"DB-Connecting-String-Billing\",\n STRIPE_PRODUCT_SYNC_WEBHOOK_SECRET = \"stripe-product-sync-webhook-secret-key\",// in-use\n STRIPE_CUSTOMER_SYNC_WEBHOOK_SECRET = \"stripe-customer-sync-webhook-secret-key\",\n STRIPE_INVOICE_SYNC_WEBHOOK_SECRET = \"stripe-invoice-sync-webhook-secret-key\",\n STRIPE_SUBSCRIPTION_SYNC_WEBHOOK_SECRET = \"stripe-subscription-sync-webhook-secret-key\",\n DB_CONNECTING_STRING_CORE = \"DB-Connecting-String-Core\",\n}\n"],"mappings":"AAAA;AACA,WAAYA,mBAAmB,0BAAnBA,mBAAmB;EAC7B;EADUA,mBAAmB;EAAnBA,mBAAmB;EAG4C;EAH/DA,mBAAmB;EAIwC;EAJ3DA,mBAAmB;EAKoC;EALvDA,mBAAmB;EAMY;EAN/BA,mBAAmB;EAO6D;EAPhFA,mBAAmB;EAAnBA,mBAAmB;EASiD;EATpEA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAA,OAAnBA,mBAAmB;AAAA","ignoreList":[]}
1
+ {"version":3,"file":"secretKeys.enum.js","names":["AzureSecretKeysEnum"],"sources":["../../../src/enums/secretKeys.enum.ts"],"sourcesContent":["// Enum for secret keys\nexport enum AzureSecretKeysEnum {\n KEYCLOAK_ADMIN_CLIENT_SECRET = \"KEYCLOAK-ADMIN-CLIENT-SECRET\",\n KEYCLOAK_ADMIN_CLIENT_ID = \"KEYCLOAK-ADMIN-CLIENT-ID\",\n KEYCLOAK_BASE_URL = \"KEYCLOAK-BASE-URL\",\n STRIPE_PAYMENT_WEBHOOK_SECRET_KEY = \"Stripe-payment-webhook-secret-key\", // in-use\n STRIPE_PRODUCT_WEBHOOK_SECRET = \"Stripe-product-webhook-secret-key\", // in-use\n STRIPE_PRICE_WEBHOOK_SECRET = \"Stripe-price-webhook-secret-key\", // in-use\n EMAIL_SERVICE_URL = \"Email-Service-Url\", // in-use,\n DB_CONNECTING_STRING_AUTH = \"DB-CONNECTION-STRING-AUTH\",\n DB_CONNECTING_STRING_USER = \"DB-CONNECTION-STRING-USER\",\n DB_CONNECTING_STRING_TENANT_BRIDGE = \"DB-Connecting-String-Tenant-Bridge\",\n DB_CONNECTING_STRING_PAYMENT = \"DB-CONNECTION-STRING-PAYMENT\",\n DB_CONNECTING_STRING_CORE = \"DB-CONNECTION-STRING-CORE\",\n DB_CONNECTING_STRING_BILLING = \"DB-CONNECTION-STRING-BILLING\",\n DB_CONNECTING_STRING_STAGING = \"DB-CONNECTION-STRING-STAGING\",\n SERVICE_BUS_CONNECTION_STRING = \"servicebus-connection-string\",\n STRIPE_CUSTOMER_SYNC_WEBHOOK_SECRET = \"stripe-customer-sync-webhook-secret-key\",\n STRIPE_INVOICE_SYNC_WEBHOOK_SECRET = \"stripe-invoice-sync-webhook-secret-key\",\n STRIPE_PRODUCT_SYNC_WEBHOOK_SECRET = \"stripe-product-sync-webhook-secret-key\",\n STRIPE_SECRET_KEY = \"Stripe-secret-key\",\n STRIPE_WEBHOOK_CUSTOMER_CREATED_SECRET_KEY = \"Stripe-Webhook-Customer-Created-Secret-Key\",\n STRIPE_SUBSCRIPTION_SYNC_WEBHOOK_SECRET = \"stripe-subscription-sync-webhook-secret-key\",\n}\n"],"mappings":"AAAA;AACA,WAAYA,mBAAmB,0BAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAI4C;EAJ/DA,mBAAmB;EAKwC;EAL3DA,mBAAmB;EAMoC;EANvDA,mBAAmB;EAOY;EAP/BA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAnBA,mBAAmB;EAAA,OAAnBA,mBAAmB;AAAA","ignoreList":[]}
@@ -1,4 +1,5 @@
1
1
  export * from './secrets';
2
2
  export * from './response';
3
3
  export * from './initializers';
4
+ export * from './mapper';
4
5
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../../../src/utils/index.ts"],"sourcesContent":["export * from './secrets';\nexport * from './response';\nexport * from './initializers';"],"mappings":"AAAA,cAAc,WAAW;AACzB,cAAc,YAAY;AAC1B,cAAc,gBAAgB","ignoreList":[]}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../../src/utils/index.ts"],"sourcesContent":["export * from './secrets';\nexport * from './response';\nexport * from './initializers';\nexport * from './mapper';"],"mappings":"AAAA,cAAc,WAAW;AACzB,cAAc,YAAY;AAC1B,cAAc,gBAAgB;AAC9B,cAAc,UAAU","ignoreList":[]}
@@ -6,26 +6,79 @@ export function WithDb(_target, _propertyKey, descriptor) {
6
6
  if (!descriptor.value) return;
7
7
  const originalMethod = descriptor.value;
8
8
  descriptor.value = async function (...args) {
9
- // Await the shared connection promise
10
- await this.dbConnected;
9
+ await this.ensureConnection();
11
10
  return originalMethod.apply(this, args);
12
11
  };
13
12
  }
14
13
  export class Initializers {
15
14
  constructor(context, dbConnectionString) {
15
+ this.context = void 0;
16
+ this.dbConnectionString = void 0;
17
+ this.connectionInitialized = false;
16
18
  this.context = context;
17
19
  this.dbConnectionString = dbConnectionString;
18
- this.dbConnected = void 0;
19
- // Connect once to the default mongoose instance
20
- this.dbConnected = mongoose.connect(dbConnectionString, {
21
- serverSelectionTimeoutMS: 10000
22
- }).then(conn => {
23
- context.info('✅ MongoDB connected');
24
- return mongoose;
25
- }).catch(err => {
26
- context.error('❌ MongoDB connection error', err);
27
- throw err;
28
- });
20
+ }
21
+ async ensureConnection() {
22
+ // Check if we already have a connection for this specific connection string
23
+ const existingConnection = Initializers.connections.get(this.dbConnectionString);
24
+ if (existingConnection && existingConnection.connection.readyState === 1) {
25
+ return existingConnection;
26
+ }
27
+
28
+ // Create a new Mongoose instance for this connection string
29
+ this.context.info(`Initializing database connection... ${this.dbConnectionString}`);
30
+ const newMongoose = new mongoose.Mongoose();
31
+ try {
32
+ await newMongoose.connect(this.dbConnectionString, {
33
+ serverSelectionTimeoutMS: 10000,
34
+ connectTimeoutMS: 10000,
35
+ socketTimeoutMS: 45000
36
+ });
37
+ this.context.info(`✅ MongoDB connected successfully ${this.dbConnectionString}`);
38
+ this.connectionInitialized = true;
39
+
40
+ // Store the connection for this connection string
41
+ Initializers.connections.set(this.dbConnectionString, newMongoose);
42
+ return newMongoose;
43
+ } catch (err) {
44
+ this.context.error(`❌ MongoDB connection error for ${this.dbConnectionString}`, {
45
+ message: err.message,
46
+ name: err.name,
47
+ code: err.code,
48
+ stack: err.stack
49
+ });
50
+ this.connectionInitialized = false;
51
+ throw new Error(`Failed to connect to MongoDB: ${err.message}`);
52
+ }
53
+ }
54
+ getConnection() {
55
+ return Initializers.connections.get(this.dbConnectionString);
56
+ }
57
+ async forceNewConnection() {
58
+ // Disconnect existing connection if any
59
+ await this.disconnect();
60
+
61
+ // Reset connection state
62
+ this.connectionInitialized = false;
63
+
64
+ // Create a new connection
65
+ return this.ensureConnection();
66
+ }
67
+ async disconnect() {
68
+ const connection = Initializers.connections.get(this.dbConnectionString);
69
+ if (connection) {
70
+ try {
71
+ // Properly close the connection
72
+ await connection.disconnect();
73
+ this.context.info(`✅ Disconnected from database: ${this.dbConnectionString}`);
74
+ } catch (error) {
75
+ this.context.error(`❌ Error disconnecting from database: ${this.dbConnectionString}`, error);
76
+ } finally {
77
+ // Always remove from the connections map
78
+ Initializers.connections.delete(this.dbConnectionString);
79
+ }
80
+ }
29
81
  }
30
82
  }
83
+ Initializers.connections = new Map();
31
84
  //# sourceMappingURL=initializers.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"initializers.js","names":["mongoose","WithDb","_target","_propertyKey","descriptor","value","originalMethod","args","dbConnected","apply","Initializers","constructor","context","dbConnectionString","connect","serverSelectionTimeoutMS","then","conn","info","catch","err","error"],"sources":["../../../src/utils/initializers.ts"],"sourcesContent":["import mongoose from \"mongoose\";\nimport { InvocationContext } from \"@azure/functions\";\n\n/**\n * Decorator that ensures MongoDB is connected before the method runs.\n */\nexport function WithDb(\n _target: any,\n _propertyKey: string | symbol,\n descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<any>>\n): void {\n if (!descriptor.value) return;\n const originalMethod = descriptor.value;\n descriptor.value = async function (this: Initializers, ...args: any[]) {\n // Await the shared connection promise\n await this.dbConnected;\n return originalMethod.apply(this, args);\n };\n}\n\nexport abstract class Initializers {\n protected dbConnected: Promise<typeof mongoose>;\n\n constructor(\n protected context: InvocationContext,\n protected dbConnectionString: string\n ) {\n // Connect once to the default mongoose instance\n this.dbConnected = mongoose\n .connect(dbConnectionString, {\n serverSelectionTimeoutMS: 10000\n })\n .then(conn => {\n context.info('✅ MongoDB connected');\n return mongoose;\n })\n .catch(err => {\n context.error('❌ MongoDB connection error', err);\n throw err;\n });\n }\n}\n"],"mappings":"AAAA,OAAOA,QAAQ,MAAM,UAAU;AAG/B;AACA;AACA;AACA,OAAO,SAASC,MAAMA,CACpBC,OAAY,EACZC,YAA6B,EAC7BC,UAAqE,EAC/D;EACN,IAAI,CAACA,UAAU,CAACC,KAAK,EAAE;EACvB,MAAMC,cAAc,GAAGF,UAAU,CAACC,KAAK;EACvCD,UAAU,CAACC,KAAK,GAAG,gBAAoC,GAAGE,IAAW,EAAE;IACrE;IACA,MAAM,IAAI,CAACC,WAAW;IACtB,OAAOF,cAAc,CAACG,KAAK,CAAC,IAAI,EAAEF,IAAI,CAAC;EACzC,CAAC;AACH;AAEA,OAAO,MAAeG,YAAY,CAAC;EAGjCC,WAAWA,CACCC,OAA0B,EAC1BC,kBAA0B,EACpC;IAAA,KAFUD,OAA0B,GAA1BA,OAA0B;IAAA,KAC1BC,kBAA0B,GAA1BA,kBAA0B;IAAA,KAJ5BL,WAAW;IAMnB;IACA,IAAI,CAACA,WAAW,GAAGR,QAAQ,CACxBc,OAAO,CAACD,kBAAkB,EAAE;MAC3BE,wBAAwB,EAAE;IAC5B,CAAC,CAAC,CACDC,IAAI,CAACC,IAAI,IAAI;MACZL,OAAO,CAACM,IAAI,CAAC,qBAAqB,CAAC;MACnC,OAAOlB,QAAQ;IACjB,CAAC,CAAC,CACDmB,KAAK,CAACC,GAAG,IAAI;MACZR,OAAO,CAACS,KAAK,CAAC,4BAA4B,EAAED,GAAG,CAAC;MAChD,MAAMA,GAAG;IACX,CAAC,CAAC;EACN;AACF","ignoreList":[]}
1
+ {"version":3,"file":"initializers.js","names":["mongoose","WithDb","_target","_propertyKey","descriptor","value","originalMethod","args","ensureConnection","apply","Initializers","constructor","context","dbConnectionString","connectionInitialized","existingConnection","connections","get","connection","readyState","info","newMongoose","Mongoose","connect","serverSelectionTimeoutMS","connectTimeoutMS","socketTimeoutMS","set","err","error","message","name","code","stack","Error","getConnection","forceNewConnection","disconnect","delete","Map"],"sources":["../../../src/utils/initializers.ts"],"sourcesContent":["import mongoose from \"mongoose\";\nimport { InvocationContext } from \"@azure/functions\";\n\n/**\n * Decorator that ensures MongoDB is connected before the method runs.\n */\nexport function WithDb(\n _target: any,\n _propertyKey: string | symbol,\n descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<any>>\n): void {\n if (!descriptor.value) return;\n const originalMethod = descriptor.value;\n descriptor.value = async function (this: Initializers, ...args: any[]) {\n await this.ensureConnection();\n return originalMethod.apply(this, args);\n };\n}\n\nexport abstract class Initializers {\n protected static connections: Map<string, mongoose.Mongoose> = new Map();\n protected context: InvocationContext;\n protected dbConnectionString: string;\n private connectionInitialized: boolean = false;\n\n constructor(context: InvocationContext, dbConnectionString: string) {\n this.context = context;\n this.dbConnectionString = dbConnectionString;\n }\n\n protected async ensureConnection(): Promise<mongoose.Mongoose> {\n // Check if we already have a connection for this specific connection string\n const existingConnection = Initializers.connections.get(this.dbConnectionString);\n if (existingConnection && existingConnection.connection.readyState === 1) {\n return existingConnection;\n }\n\n // Create a new Mongoose instance for this connection string\n this.context.info(`Initializing database connection... ${this.dbConnectionString}`);\n const newMongoose = new mongoose.Mongoose();\n \n try {\n await newMongoose.connect(this.dbConnectionString, {\n serverSelectionTimeoutMS: 10000,\n connectTimeoutMS: 10000,\n socketTimeoutMS: 45000,\n });\n \n this.context.info(`✅ MongoDB connected successfully ${this.dbConnectionString}`);\n this.connectionInitialized = true;\n \n // Store the connection for this connection string\n Initializers.connections.set(this.dbConnectionString, newMongoose);\n \n return newMongoose;\n } catch (err: any) {\n this.context.error(`❌ MongoDB connection error for ${this.dbConnectionString}`, {\n message: err.message,\n name: err.name,\n code: err.code,\n stack: err.stack\n });\n this.connectionInitialized = false;\n throw new Error(`Failed to connect to MongoDB: ${err.message}`);\n }\n }\n\n protected getConnection(): mongoose.Mongoose | undefined {\n return Initializers.connections.get(this.dbConnectionString);\n }\n\n async forceNewConnection(): Promise<mongoose.Mongoose> {\n // Disconnect existing connection if any\n await this.disconnect();\n \n // Reset connection state\n this.connectionInitialized = false;\n \n // Create a new connection\n return this.ensureConnection();\n }\n \n async disconnect(): Promise<void> {\n const connection = Initializers.connections.get(this.dbConnectionString);\n if (connection) {\n try {\n // Properly close the connection\n await connection.disconnect();\n this.context.info(`✅ Disconnected from database: ${this.dbConnectionString}`);\n } catch (error) {\n this.context.error(`❌ Error disconnecting from database: ${this.dbConnectionString}`, error);\n } finally {\n // Always remove from the connections map\n Initializers.connections.delete(this.dbConnectionString);\n }\n }\n }\n}\n"],"mappings":"AAAA,OAAOA,QAAQ,MAAM,UAAU;AAG/B;AACA;AACA;AACA,OAAO,SAASC,MAAMA,CACpBC,OAAY,EACZC,YAA6B,EAC7BC,UAAqE,EAC/D;EACN,IAAI,CAACA,UAAU,CAACC,KAAK,EAAE;EACvB,MAAMC,cAAc,GAAGF,UAAU,CAACC,KAAK;EACvCD,UAAU,CAACC,KAAK,GAAG,gBAAoC,GAAGE,IAAW,EAAE;IACrE,MAAM,IAAI,CAACC,gBAAgB,CAAC,CAAC;IAC7B,OAAOF,cAAc,CAACG,KAAK,CAAC,IAAI,EAAEF,IAAI,CAAC;EACzC,CAAC;AACH;AAEA,OAAO,MAAeG,YAAY,CAAC;EAMjCC,WAAWA,CAACC,OAA0B,EAAEC,kBAA0B,EAAE;IAAA,KAJ1DD,OAAO;IAAA,KACPC,kBAAkB;IAAA,KACpBC,qBAAqB,GAAY,KAAK;IAG5C,IAAI,CAACF,OAAO,GAAGA,OAAO;IACtB,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ACE,OAAO,EAAE,CAAC;IACjE;EACF;EAEUK,aAAaA,CAAA,EAAkC;IACvD,OAAOzB,YAAY,CAACM,WAAW,CAACC,GAAG,CAAC,IAAI,CAACJ,kBAAkB,CAAC;EAC9D;EAEA,MAAMuB,kBAAkBA,CAAA,EAA+B;IACrD;IACA,MAAM,IAAI,CAACC,UAAU,CAAC,CAAC;;IAEvB;IACA,IAAI,CAACvB,qBAAqB,GAAG,KAAK;;IAElC;IACA,OAAO,IAAI,CAACN,gBAAgB,CAAC,CAAC;EAChC;EAEA,MAAM6B,UAAUA,CAAA,EAAkB;IAChC,MAAMnB,UAAU,GAAGR,YAAY,CAACM,WAAW,CAACC,GAAG,CAAC,IAAI,CAACJ,kBAAkB,CAAC;IACxE,IAAIK,UAAU,EAAE;MACd,IAAI;QACF;QACA,MAAMA,UAAU,CAACmB,UAAU,CAAC,CAAC;QAC7B,IAAI,CAACzB,OAAO,CAACQ,IAAI,CAAC,iCAAiC,IAAI,CAACP,kBAAkB,EAAE,CAAC;MAC/E,CAAC,CAAC,OAAOgB,KAAK,EAAE;QACd,IAAI,CAACjB,OAAO,CAACiB,KAAK,CAAC,wCAAwC,IAAI,CAAChB,kBAAkB,EAAE,EAAEgB,KAAK,CAAC;MAC9F,CAAC,SAAS;QACR;QACAnB,YAAY,CAACM,WAAW,CAACsB,MAAM,CAAC,IAAI,CAACzB,kBAAkB,CAAC;MAC1D;IACF;EACF;AACF;AA9EsBH,YAAY,CACfM,WAAW,GAAmC,IAAIuB,GAAG,CAAC,CAAC","ignoreList":[]}
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@culturefy/shared",
3
3
  "description": "Shared utilities for culturefy serverless services",
4
- "version": "1.0.18",
4
+ "version": "1.0.20",
5
5
  "main": "build/cjs/index.js",
6
6
  "module": "build/esm/index.js",
7
7
  "types": "build/src/index.d.ts",
@@ -1,14 +1,23 @@
1
1
  export declare enum AzureSecretKeysEnum {
2
- STRIPE_SECRET_KEY = "Stripe-secret-key",
2
+ KEYCLOAK_ADMIN_CLIENT_SECRET = "KEYCLOAK-ADMIN-CLIENT-SECRET",
3
+ KEYCLOAK_ADMIN_CLIENT_ID = "KEYCLOAK-ADMIN-CLIENT-ID",
4
+ KEYCLOAK_BASE_URL = "KEYCLOAK-BASE-URL",
3
5
  STRIPE_PAYMENT_WEBHOOK_SECRET_KEY = "Stripe-payment-webhook-secret-key",// in-use
4
6
  STRIPE_PRODUCT_WEBHOOK_SECRET = "Stripe-product-webhook-secret-key",// in-use
5
7
  STRIPE_PRICE_WEBHOOK_SECRET = "Stripe-price-webhook-secret-key",// in-use
6
8
  EMAIL_SERVICE_URL = "Email-Service-Url",// in-use,
7
- STRIPE_WEBHOOK_CUSTOMER_CREATED_SECRET_KEY = "Stripe-Webhook-Customer-Created-Secret-Key",// in-use
8
- DB_CONNECTING_STRING_BILLING = "DB-Connecting-String-Billing",
9
- STRIPE_PRODUCT_SYNC_WEBHOOK_SECRET = "stripe-product-sync-webhook-secret-key",// in-use
9
+ DB_CONNECTING_STRING_AUTH = "DB-CONNECTION-STRING-AUTH",
10
+ DB_CONNECTING_STRING_USER = "DB-CONNECTION-STRING-USER",
11
+ DB_CONNECTING_STRING_TENANT_BRIDGE = "DB-Connecting-String-Tenant-Bridge",
12
+ DB_CONNECTING_STRING_PAYMENT = "DB-CONNECTION-STRING-PAYMENT",
13
+ DB_CONNECTING_STRING_CORE = "DB-CONNECTION-STRING-CORE",
14
+ DB_CONNECTING_STRING_BILLING = "DB-CONNECTION-STRING-BILLING",
15
+ DB_CONNECTING_STRING_STAGING = "DB-CONNECTION-STRING-STAGING",
16
+ SERVICE_BUS_CONNECTION_STRING = "servicebus-connection-string",
10
17
  STRIPE_CUSTOMER_SYNC_WEBHOOK_SECRET = "stripe-customer-sync-webhook-secret-key",
11
18
  STRIPE_INVOICE_SYNC_WEBHOOK_SECRET = "stripe-invoice-sync-webhook-secret-key",
12
- STRIPE_SUBSCRIPTION_SYNC_WEBHOOK_SECRET = "stripe-subscription-sync-webhook-secret-key",
13
- DB_CONNECTING_STRING_CORE = "DB-Connecting-String-Core"
19
+ STRIPE_PRODUCT_SYNC_WEBHOOK_SECRET = "stripe-product-sync-webhook-secret-key",
20
+ STRIPE_SECRET_KEY = "Stripe-secret-key",
21
+ STRIPE_WEBHOOK_CUSTOMER_CREATED_SECRET_KEY = "Stripe-Webhook-Customer-Created-Secret-Key",
22
+ STRIPE_SUBSCRIPTION_SYNC_WEBHOOK_SECRET = "stripe-subscription-sync-webhook-secret-key"
14
23
  }
@@ -4,18 +4,26 @@ exports.AzureSecretKeysEnum = void 0;
4
4
  // Enum for secret keys
5
5
  var AzureSecretKeysEnum;
6
6
  (function (AzureSecretKeysEnum) {
7
- // STRIPE_PUBLISHABLE_KEY = "Stripe-Publishable-Key",
8
- AzureSecretKeysEnum["STRIPE_SECRET_KEY"] = "Stripe-secret-key";
7
+ AzureSecretKeysEnum["KEYCLOAK_ADMIN_CLIENT_SECRET"] = "KEYCLOAK-ADMIN-CLIENT-SECRET";
8
+ AzureSecretKeysEnum["KEYCLOAK_ADMIN_CLIENT_ID"] = "KEYCLOAK-ADMIN-CLIENT-ID";
9
+ AzureSecretKeysEnum["KEYCLOAK_BASE_URL"] = "KEYCLOAK-BASE-URL";
9
10
  AzureSecretKeysEnum["STRIPE_PAYMENT_WEBHOOK_SECRET_KEY"] = "Stripe-payment-webhook-secret-key";
10
11
  AzureSecretKeysEnum["STRIPE_PRODUCT_WEBHOOK_SECRET"] = "Stripe-product-webhook-secret-key";
11
12
  AzureSecretKeysEnum["STRIPE_PRICE_WEBHOOK_SECRET"] = "Stripe-price-webhook-secret-key";
12
13
  AzureSecretKeysEnum["EMAIL_SERVICE_URL"] = "Email-Service-Url";
13
- AzureSecretKeysEnum["STRIPE_WEBHOOK_CUSTOMER_CREATED_SECRET_KEY"] = "Stripe-Webhook-Customer-Created-Secret-Key";
14
- AzureSecretKeysEnum["DB_CONNECTING_STRING_BILLING"] = "DB-Connecting-String-Billing";
15
- AzureSecretKeysEnum["STRIPE_PRODUCT_SYNC_WEBHOOK_SECRET"] = "stripe-product-sync-webhook-secret-key";
14
+ AzureSecretKeysEnum["DB_CONNECTING_STRING_AUTH"] = "DB-CONNECTION-STRING-AUTH";
15
+ AzureSecretKeysEnum["DB_CONNECTING_STRING_USER"] = "DB-CONNECTION-STRING-USER";
16
+ AzureSecretKeysEnum["DB_CONNECTING_STRING_TENANT_BRIDGE"] = "DB-Connecting-String-Tenant-Bridge";
17
+ AzureSecretKeysEnum["DB_CONNECTING_STRING_PAYMENT"] = "DB-CONNECTION-STRING-PAYMENT";
18
+ AzureSecretKeysEnum["DB_CONNECTING_STRING_CORE"] = "DB-CONNECTION-STRING-CORE";
19
+ AzureSecretKeysEnum["DB_CONNECTING_STRING_BILLING"] = "DB-CONNECTION-STRING-BILLING";
20
+ AzureSecretKeysEnum["DB_CONNECTING_STRING_STAGING"] = "DB-CONNECTION-STRING-STAGING";
21
+ AzureSecretKeysEnum["SERVICE_BUS_CONNECTION_STRING"] = "servicebus-connection-string";
16
22
  AzureSecretKeysEnum["STRIPE_CUSTOMER_SYNC_WEBHOOK_SECRET"] = "stripe-customer-sync-webhook-secret-key";
17
23
  AzureSecretKeysEnum["STRIPE_INVOICE_SYNC_WEBHOOK_SECRET"] = "stripe-invoice-sync-webhook-secret-key";
24
+ AzureSecretKeysEnum["STRIPE_PRODUCT_SYNC_WEBHOOK_SECRET"] = "stripe-product-sync-webhook-secret-key";
25
+ AzureSecretKeysEnum["STRIPE_SECRET_KEY"] = "Stripe-secret-key";
26
+ AzureSecretKeysEnum["STRIPE_WEBHOOK_CUSTOMER_CREATED_SECRET_KEY"] = "Stripe-Webhook-Customer-Created-Secret-Key";
18
27
  AzureSecretKeysEnum["STRIPE_SUBSCRIPTION_SYNC_WEBHOOK_SECRET"] = "stripe-subscription-sync-webhook-secret-key";
19
- AzureSecretKeysEnum["DB_CONNECTING_STRING_CORE"] = "DB-Connecting-String-Core";
20
28
  })(AzureSecretKeysEnum || (exports.AzureSecretKeysEnum = AzureSecretKeysEnum = {}));
21
29
  //# sourceMappingURL=secretKeys.enum.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"secretKeys.enum.js","sourceRoot":"","sources":["../../../src/enums/secretKeys.enum.ts"],"names":[],"mappings":";;;AAAA,uBAAuB;AACvB,IAAY,mBAcX;AAdD,WAAY,mBAAmB;IAC7B,qDAAqD;IACrD,8DAAuC,CAAA;IACvC,8FAAuE,CAAA;IACvE,0FAAmE,CAAA;IACnE,sFAA+D,CAAA;IAC/D,8DAAuC,CAAA;IACvC,gHAAyF,CAAA;IACzF,oFAA6D,CAAA;IAC7D,oGAA6E,CAAA;IAC7E,sGAA+E,CAAA;IAC/E,oGAA6E,CAAA;IAC7E,8GAAuF,CAAA;IACvF,8EAAuD,CAAA;AACzD,CAAC,EAdW,mBAAmB,mCAAnB,mBAAmB,QAc9B"}
1
+ {"version":3,"file":"secretKeys.enum.js","sourceRoot":"","sources":["../../../src/enums/secretKeys.enum.ts"],"names":[],"mappings":";;;AAAA,uBAAuB;AACvB,IAAY,mBAsBX;AAtBD,WAAY,mBAAmB;IAC7B,oFAA6D,CAAA;IAC7D,4EAAqD,CAAA;IACrD,8DAAuC,CAAA;IACvC,8FAAuE,CAAA;IACvE,0FAAmE,CAAA;IACnE,sFAA+D,CAAA;IAC/D,8DAAuC,CAAA;IACvC,8EAAuD,CAAA;IACvD,8EAAuD,CAAA;IACvD,gGAAyE,CAAA;IACzE,oFAA6D,CAAA;IAC7D,8EAAuD,CAAA;IACvD,oFAA6D,CAAA;IAC7D,oFAA8D,CAAA;IAC9D,qFAA8D,CAAA;IAC9D,sGAA+E,CAAA;IAC/E,oGAA6E,CAAA;IAC7E,oGAA6E,CAAA;IAC7E,8DAAuC,CAAA;IACvC,gHAAyF,CAAA;IACzF,8GAAuF,CAAA;AACzF,CAAC,EAtBW,mBAAmB,mCAAnB,mBAAmB,QAsB9B"}
@@ -1,3 +1,4 @@
1
1
  export * from './secrets';
2
2
  export * from './response';
3
3
  export * from './initializers';
4
+ export * from './mapper';
@@ -4,4 +4,5 @@ const tslib_1 = require("tslib");
4
4
  tslib_1.__exportStar(require("./secrets"), exports);
5
5
  tslib_1.__exportStar(require("./response"), exports);
6
6
  tslib_1.__exportStar(require("./initializers"), exports);
7
+ tslib_1.__exportStar(require("./mapper"), exports);
7
8
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":";;;AAAA,oDAA0B;AAC1B,qDAA2B;AAC3B,yDAA+B"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":";;;AAAA,oDAA0B;AAC1B,qDAA2B;AAC3B,yDAA+B;AAC/B,mDAAyB"}
@@ -5,8 +5,13 @@ import { InvocationContext } from "@azure/functions";
5
5
  */
6
6
  export declare function WithDb(_target: any, _propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<any>>): void;
7
7
  export declare abstract class Initializers {
8
+ protected static connections: Map<string, mongoose.Mongoose>;
8
9
  protected context: InvocationContext;
9
10
  protected dbConnectionString: string;
10
- protected dbConnected: Promise<typeof mongoose>;
11
+ private connectionInitialized;
11
12
  constructor(context: InvocationContext, dbConnectionString: string);
13
+ protected ensureConnection(): Promise<mongoose.Mongoose>;
14
+ protected getConnection(): mongoose.Mongoose | undefined;
15
+ forceNewConnection(): Promise<mongoose.Mongoose>;
16
+ disconnect(): Promise<void>;
12
17
  }
@@ -13,30 +13,84 @@ function WithDb(_target, _propertyKey, descriptor) {
13
13
  const originalMethod = descriptor.value;
14
14
  descriptor.value = function (...args) {
15
15
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
16
- // Await the shared connection promise
17
- yield this.dbConnected;
16
+ yield this.ensureConnection();
18
17
  return originalMethod.apply(this, args);
19
18
  });
20
19
  };
21
20
  }
22
21
  class Initializers {
23
22
  constructor(context, dbConnectionString) {
23
+ this.connectionInitialized = false;
24
24
  this.context = context;
25
25
  this.dbConnectionString = dbConnectionString;
26
- // Connect once to the default mongoose instance
27
- this.dbConnected = mongoose_1.default
28
- .connect(dbConnectionString, {
29
- serverSelectionTimeoutMS: 10000
30
- })
31
- .then(conn => {
32
- context.info('✅ MongoDB connected');
33
- return mongoose_1.default;
34
- })
35
- .catch(err => {
36
- context.error('❌ MongoDB connection error', err);
37
- throw err;
26
+ }
27
+ ensureConnection() {
28
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
29
+ // Check if we already have a connection for this specific connection string
30
+ const existingConnection = Initializers.connections.get(this.dbConnectionString);
31
+ if (existingConnection && existingConnection.connection.readyState === 1) {
32
+ return existingConnection;
33
+ }
34
+ // Create a new Mongoose instance for this connection string
35
+ this.context.info(`Initializing database connection... ${this.dbConnectionString}`);
36
+ const newMongoose = new mongoose_1.default.Mongoose();
37
+ try {
38
+ yield newMongoose.connect(this.dbConnectionString, {
39
+ serverSelectionTimeoutMS: 10000,
40
+ connectTimeoutMS: 10000,
41
+ socketTimeoutMS: 45000,
42
+ });
43
+ this.context.info(`✅ MongoDB connected successfully ${this.dbConnectionString}`);
44
+ this.connectionInitialized = true;
45
+ // Store the connection for this connection string
46
+ Initializers.connections.set(this.dbConnectionString, newMongoose);
47
+ return newMongoose;
48
+ }
49
+ catch (err) {
50
+ this.context.error(`❌ MongoDB connection error for ${this.dbConnectionString}`, {
51
+ message: err.message,
52
+ name: err.name,
53
+ code: err.code,
54
+ stack: err.stack
55
+ });
56
+ this.connectionInitialized = false;
57
+ throw new Error(`Failed to connect to MongoDB: ${err.message}`);
58
+ }
59
+ });
60
+ }
61
+ getConnection() {
62
+ return Initializers.connections.get(this.dbConnectionString);
63
+ }
64
+ forceNewConnection() {
65
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
66
+ // Disconnect existing connection if any
67
+ yield this.disconnect();
68
+ // Reset connection state
69
+ this.connectionInitialized = false;
70
+ // Create a new connection
71
+ return this.ensureConnection();
72
+ });
73
+ }
74
+ disconnect() {
75
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
76
+ const connection = Initializers.connections.get(this.dbConnectionString);
77
+ if (connection) {
78
+ try {
79
+ // Properly close the connection
80
+ yield connection.disconnect();
81
+ this.context.info(`✅ Disconnected from database: ${this.dbConnectionString}`);
82
+ }
83
+ catch (error) {
84
+ this.context.error(`❌ Error disconnecting from database: ${this.dbConnectionString}`, error);
85
+ }
86
+ finally {
87
+ // Always remove from the connections map
88
+ Initializers.connections.delete(this.dbConnectionString);
89
+ }
90
+ }
38
91
  });
39
92
  }
40
93
  }
41
94
  exports.Initializers = Initializers;
95
+ Initializers.connections = new Map();
42
96
  //# sourceMappingURL=initializers.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"initializers.js","sourceRoot":"","sources":["../../../src/utils/initializers.ts"],"names":[],"mappings":";;;AAMA,wBAYC;;AAlBD,gEAAgC;AAGhC;;GAEG;AACH,SAAgB,MAAM,CACpB,OAAY,EACZ,YAA6B,EAC7B,UAAqE;IAErE,IAAI,CAAC,UAAU,CAAC,KAAK;QAAE,OAAO;IAC9B,MAAM,cAAc,GAAG,UAAU,CAAC,KAAK,CAAC;IACxC,UAAU,CAAC,KAAK,GAAG,UAAoC,GAAG,IAAW;;YACnE,sCAAsC;YACtC,MAAM,IAAI,CAAC,WAAW,CAAC;YACvB,OAAO,cAAc,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QAC1C,CAAC;KAAA,CAAC;AACJ,CAAC;AAED,MAAsB,YAAY;IAGhC,YACY,OAA0B,EAC1B,kBAA0B;QAD1B,YAAO,GAAP,OAAO,CAAmB;QAC1B,uBAAkB,GAAlB,kBAAkB,CAAQ;QAEpC,gDAAgD;QAChD,IAAI,CAAC,WAAW,GAAG,kBAAQ;aACxB,OAAO,CAAC,kBAAkB,EAAE;YAC3B,wBAAwB,EAAE,KAAK;SAChC,CAAC;aACD,IAAI,CAAC,IAAI,CAAC,EAA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B,GAAG,KAAK,CAAC;YAEnC,0BAA0B;YAC1B,OAAO,IAAI,CAAC,gBAAgB,EAAE,CAAC;QACjC,CAAC;KAAA;IAEK,UAAU;;YACd,MAAM,UAAU,GAAG,YAAY,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAC;YACzE,IAAI,UAAU,EAAE,CAAC;gBACf,IAAI,CAAC;oBACH,gCAAgC;oBAChC,MAAM,UAAU,CAAC,UAAU,EAAE,CAAC;oBAC9B,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,iCAAiC,IAAI,CAAC,kBAAkB,EAAE,CAAC,CAAC;gBAChF,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACf,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,wCAAwC,IAAI,CAAC,kBAAkB,EAAE,EAAE,KAAK,CAAC,CAAC;gBAC/F,CAAC;wBAAS,CAAC;oBACT,yCAAyC;oBACzC,YAAY,CAAC,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAC;gBAC3D,CAAC;YACH,CAAC;QACH,CAAC;KAAA;;AA7EH,oCA8EC;AA7EkB,wBAAW,GAAmC,IAAI,GAAG,EAAE,AAA5C,CAA6C"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@culturefy/shared",
3
3
  "description": "Shared utilities for culturefy serverless services",
4
- "version": "1.0.18",
4
+ "version": "1.0.20",
5
5
  "main": "build/cjs/index.js",
6
6
  "module": "build/esm/index.js",
7
7
  "types": "build/src/index.d.ts",
@@ -1,16 +1,24 @@
1
1
  // Enum for secret keys
2
2
  export enum AzureSecretKeysEnum {
3
- // STRIPE_PUBLISHABLE_KEY = "Stripe-Publishable-Key",
4
- STRIPE_SECRET_KEY = "Stripe-secret-key",
3
+ KEYCLOAK_ADMIN_CLIENT_SECRET = "KEYCLOAK-ADMIN-CLIENT-SECRET",
4
+ KEYCLOAK_ADMIN_CLIENT_ID = "KEYCLOAK-ADMIN-CLIENT-ID",
5
+ KEYCLOAK_BASE_URL = "KEYCLOAK-BASE-URL",
5
6
  STRIPE_PAYMENT_WEBHOOK_SECRET_KEY = "Stripe-payment-webhook-secret-key", // in-use
6
7
  STRIPE_PRODUCT_WEBHOOK_SECRET = "Stripe-product-webhook-secret-key", // in-use
7
8
  STRIPE_PRICE_WEBHOOK_SECRET = "Stripe-price-webhook-secret-key", // in-use
8
9
  EMAIL_SERVICE_URL = "Email-Service-Url", // in-use,
9
- STRIPE_WEBHOOK_CUSTOMER_CREATED_SECRET_KEY = "Stripe-Webhook-Customer-Created-Secret-Key",// in-use
10
- DB_CONNECTING_STRING_BILLING = "DB-Connecting-String-Billing",
11
- STRIPE_PRODUCT_SYNC_WEBHOOK_SECRET = "stripe-product-sync-webhook-secret-key",// in-use
10
+ DB_CONNECTING_STRING_AUTH = "DB-CONNECTION-STRING-AUTH",
11
+ DB_CONNECTING_STRING_USER = "DB-CONNECTION-STRING-USER",
12
+ DB_CONNECTING_STRING_TENANT_BRIDGE = "DB-Connecting-String-Tenant-Bridge",
13
+ DB_CONNECTING_STRING_PAYMENT = "DB-CONNECTION-STRING-PAYMENT",
14
+ DB_CONNECTING_STRING_CORE = "DB-CONNECTION-STRING-CORE",
15
+ DB_CONNECTING_STRING_BILLING = "DB-CONNECTION-STRING-BILLING",
16
+ DB_CONNECTING_STRING_STAGING = "DB-CONNECTION-STRING-STAGING",
17
+ SERVICE_BUS_CONNECTION_STRING = "servicebus-connection-string",
12
18
  STRIPE_CUSTOMER_SYNC_WEBHOOK_SECRET = "stripe-customer-sync-webhook-secret-key",
13
19
  STRIPE_INVOICE_SYNC_WEBHOOK_SECRET = "stripe-invoice-sync-webhook-secret-key",
20
+ STRIPE_PRODUCT_SYNC_WEBHOOK_SECRET = "stripe-product-sync-webhook-secret-key",
21
+ STRIPE_SECRET_KEY = "Stripe-secret-key",
22
+ STRIPE_WEBHOOK_CUSTOMER_CREATED_SECRET_KEY = "Stripe-Webhook-Customer-Created-Secret-Key",
14
23
  STRIPE_SUBSCRIPTION_SYNC_WEBHOOK_SECRET = "stripe-subscription-sync-webhook-secret-key",
15
- DB_CONNECTING_STRING_CORE = "DB-Connecting-String-Core",
16
24
  }
@@ -1,3 +1,4 @@
1
1
  export * from './secrets';
2
2
  export * from './response';
3
- export * from './initializers';
3
+ export * from './initializers';
4
+ export * from './mapper';
@@ -12,31 +12,87 @@ export function WithDb(
12
12
  if (!descriptor.value) return;
13
13
  const originalMethod = descriptor.value;
14
14
  descriptor.value = async function (this: Initializers, ...args: any[]) {
15
- // Await the shared connection promise
16
- await this.dbConnected;
15
+ await this.ensureConnection();
17
16
  return originalMethod.apply(this, args);
18
17
  };
19
18
  }
20
19
 
21
20
  export abstract class Initializers {
22
- protected dbConnected: Promise<typeof mongoose>;
21
+ protected static connections: Map<string, mongoose.Mongoose> = new Map();
22
+ protected context: InvocationContext;
23
+ protected dbConnectionString: string;
24
+ private connectionInitialized: boolean = false;
23
25
 
24
- constructor(
25
- protected context: InvocationContext,
26
- protected dbConnectionString: string
27
- ) {
28
- // Connect once to the default mongoose instance
29
- this.dbConnected = mongoose
30
- .connect(dbConnectionString, {
31
- serverSelectionTimeoutMS: 10000
32
- })
33
- .then(conn => {
34
- context.info('✅ MongoDB connected');
35
- return mongoose;
36
- })
37
- .catch(err => {
38
- context.error('❌ MongoDB connection error', err);
39
- throw err;
26
+ constructor(context: InvocationContext, dbConnectionString: string) {
27
+ this.context = context;
28
+ this.dbConnectionString = dbConnectionString;
29
+ }
30
+
31
+ protected async ensureConnection(): Promise<mongoose.Mongoose> {
32
+ // Check if we already have a connection for this specific connection string
33
+ const existingConnection = Initializers.connections.get(this.dbConnectionString);
34
+ if (existingConnection && existingConnection.connection.readyState === 1) {
35
+ return existingConnection;
36
+ }
37
+
38
+ // Create a new Mongoose instance for this connection string
39
+ this.context.info(`Initializing database connection... ${this.dbConnectionString}`);
40
+ const newMongoose = new mongoose.Mongoose();
41
+
42
+ try {
43
+ await newMongoose.connect(this.dbConnectionString, {
44
+ serverSelectionTimeoutMS: 10000,
45
+ connectTimeoutMS: 10000,
46
+ socketTimeoutMS: 45000,
40
47
  });
48
+
49
+ this.context.info(`✅ MongoDB connected successfully ${this.dbConnectionString}`);
50
+ this.connectionInitialized = true;
51
+
52
+ // Store the connection for this connection string
53
+ Initializers.connections.set(this.dbConnectionString, newMongoose);
54
+
55
+ return newMongoose;
56
+ } catch (err: any) {
57
+ this.context.error(`❌ MongoDB connection error for ${this.dbConnectionString}`, {
58
+ message: err.message,
59
+ name: err.name,
60
+ code: err.code,
61
+ stack: err.stack
62
+ });
63
+ this.connectionInitialized = false;
64
+ throw new Error(`Failed to connect to MongoDB: ${err.message}`);
65
+ }
66
+ }
67
+
68
+ protected getConnection(): mongoose.Mongoose | undefined {
69
+ return Initializers.connections.get(this.dbConnectionString);
70
+ }
71
+
72
+ async forceNewConnection(): Promise<mongoose.Mongoose> {
73
+ // Disconnect existing connection if any
74
+ await this.disconnect();
75
+
76
+ // Reset connection state
77
+ this.connectionInitialized = false;
78
+
79
+ // Create a new connection
80
+ return this.ensureConnection();
81
+ }
82
+
83
+ async disconnect(): Promise<void> {
84
+ const connection = Initializers.connections.get(this.dbConnectionString);
85
+ if (connection) {
86
+ try {
87
+ // Properly close the connection
88
+ await connection.disconnect();
89
+ this.context.info(`✅ Disconnected from database: ${this.dbConnectionString}`);
90
+ } catch (error) {
91
+ this.context.error(`❌ Error disconnecting from database: ${this.dbConnectionString}`, error);
92
+ } finally {
93
+ // Always remove from the connections map
94
+ Initializers.connections.delete(this.dbConnectionString);
95
+ }
96
+ }
41
97
  }
42
98
  }