@helpio/common 1.0.3 → 1.0.5

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.
@@ -0,0 +1,36 @@
1
+ /**
2
+ * City (geo-config) Event Types
3
+ * Shape matches what services currently publish/consume.
4
+ */
5
+ export declare type CityCenter = {
6
+ lat: number;
7
+ lng: number;
8
+ };
9
+ export interface CityCreatedEvent {
10
+ id: string;
11
+ name: string;
12
+ countryId: string;
13
+ countryCode?: string;
14
+ slug?: string;
15
+ center?: CityCenter;
16
+ radiusKm?: number;
17
+ timezone?: string;
18
+ isLive: boolean;
19
+ tags?: string[];
20
+ }
21
+ export interface CityUpdatedEvent {
22
+ id: string;
23
+ name: string;
24
+ countryId: string;
25
+ countryCode?: string;
26
+ slug?: string;
27
+ center?: CityCenter;
28
+ radiusKm?: number;
29
+ timezone?: string;
30
+ isLive: boolean;
31
+ tags?: string[];
32
+ }
33
+ export interface CityStatusChangedEvent {
34
+ id: string;
35
+ isLive: boolean;
36
+ }
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ /**
3
+ * City (geo-config) Event Types
4
+ * Shape matches what services currently publish/consume.
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,11 +1,22 @@
1
1
  import { UserCreatedEvent, UserUpdatedEvent } from "./user-events";
2
2
  import { CountryCreatedEvent, CountryStatusChangedEvent, CountryUpdatedEvent } from "./country-events";
3
+ import { CityCreatedEvent, CityStatusChangedEvent, CityUpdatedEvent } from "./city-events";
4
+ import { KycDocumentDeletedEvent, KycDocumentRejectedEvent, KycDocumentUploadedEvent, KycDocumentVerifiedEvent } from "./kyc-document-events";
3
5
  export interface EventMap {
4
6
  "user.created": UserCreatedEvent;
5
7
  "user.updated": UserUpdatedEvent;
6
8
  "country.created": CountryCreatedEvent;
7
9
  "country.updated": CountryUpdatedEvent;
8
10
  "country.status.changed": CountryStatusChangedEvent;
11
+ "city.created": CityCreatedEvent;
12
+ "city.updated": CityUpdatedEvent;
13
+ "city.status.changed": CityStatusChangedEvent;
14
+ "kyc.document.uploaded": KycDocumentUploadedEvent;
15
+ "kyc.document.verified": KycDocumentVerifiedEvent;
16
+ "kyc.document.rejected": KycDocumentRejectedEvent;
17
+ "kyc.document.deleted": KycDocumentDeletedEvent;
9
18
  }
10
19
  export * from "./user-events";
11
20
  export * from "./country-events";
21
+ export * from "./city-events";
22
+ export * from "./kyc-document-events";
@@ -13,3 +13,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
13
13
  // Export all event types
14
14
  __exportStar(require("./user-events"), exports);
15
15
  __exportStar(require("./country-events"), exports);
16
+ __exportStar(require("./city-events"), exports);
17
+ __exportStar(require("./kyc-document-events"), exports);
@@ -0,0 +1,56 @@
1
+ /**
2
+ * KYC Document Event Types
3
+ * Published by kyc-doc-management-service.
4
+ */
5
+ export declare type KycDocumentStatus = "pending" | "verified" | "rejected" | "expired";
6
+ export declare type KycDocumentType = "national_id" | "passport" | "drivers_license" | "police_clearance" | "certificate" | "proof_of_address" | "selfie" | "other";
7
+ export interface KycDocumentUploadedEvent {
8
+ id: string;
9
+ userId: string;
10
+ userAccountNumber: number;
11
+ documentType: KycDocumentType | string;
12
+ documentNumber?: string;
13
+ fileUrl: string;
14
+ fileName: string;
15
+ fileSize: number;
16
+ mimeType: string;
17
+ issueDate?: Date;
18
+ expiryDate?: Date;
19
+ issuingCountry?: string;
20
+ issuingAuthority?: string;
21
+ status: KycDocumentStatus;
22
+ ipAddress?: string;
23
+ userAgent?: string;
24
+ createdAt: Date;
25
+ }
26
+ export interface KycDocumentVerifiedEvent {
27
+ id: string;
28
+ userId: string;
29
+ userAccountNumber: number;
30
+ documentType: KycDocumentType | string;
31
+ documentNumber?: string;
32
+ status: "verified";
33
+ verifiedAt: Date;
34
+ verifiedBy: string;
35
+ adminNotes?: string;
36
+ }
37
+ export interface KycDocumentRejectedEvent {
38
+ id: string;
39
+ userId: string;
40
+ userAccountNumber: number;
41
+ documentType: KycDocumentType | string;
42
+ documentNumber?: string;
43
+ status: "rejected";
44
+ rejectionReason: string;
45
+ rejectedAt: Date;
46
+ rejectedBy: string;
47
+ adminNotes?: string;
48
+ }
49
+ export interface KycDocumentDeletedEvent {
50
+ id: string;
51
+ userId: string;
52
+ userAccountNumber: number;
53
+ documentType: KycDocumentType | string;
54
+ fileUrl: string;
55
+ deletedAt: Date;
56
+ }
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ /**
3
+ * KYC Document Event Types
4
+ * Published by kyc-doc-management-service.
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -5,14 +5,35 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.currentUser = void 0;
7
7
  const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
8
+ const getJwtSecret = () => {
9
+ return process.env.JWT_SECRET || process.env.JWT_KEY;
10
+ };
11
+ const extractAccessToken = (req) => {
12
+ var _a, _b;
13
+ const authHeader = req.headers.authorization;
14
+ const bearer = typeof authHeader === "string" && authHeader.startsWith("Bearer ")
15
+ ? authHeader.substring(7)
16
+ : undefined;
17
+ const cookieToken = (_a = req.cookies) === null || _a === void 0 ? void 0 : _a.accessToken;
18
+ const legacySessionToken = (_b = req.session) === null || _b === void 0 ? void 0 : _b.jwt;
19
+ const token = bearer || cookieToken || legacySessionToken;
20
+ return typeof token === "string" && token.trim() ? token.trim() : undefined;
21
+ };
22
+ const attachPayload = (req, payload) => {
23
+ req.currentUser = payload;
24
+ req.userId = payload.userId || payload.id;
25
+ req.sessionId = payload.sessionId;
26
+ };
8
27
  const currentUser = (req, res, next) => {
9
- var _a;
10
- if (!((_a = req.session) === null || _a === void 0 ? void 0 : _a.jwt)) {
28
+ const token = extractAccessToken(req);
29
+ if (!token)
30
+ return next();
31
+ const secret = getJwtSecret();
32
+ if (!secret)
11
33
  return next();
12
- }
13
34
  try {
14
- const payload = jsonwebtoken_1.default.verify(req.session.jwt, process.env.JWT_KEY);
15
- req.currentUser = payload;
35
+ const payload = jsonwebtoken_1.default.verify(token, secret);
36
+ attachPayload(req, payload);
16
37
  }
17
38
  catch (err) { }
18
39
  next();
@@ -6,42 +6,47 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.optionalAuth = exports.requireAuth = void 0;
7
7
  const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
8
8
  const not_authorized_error_1 = require("../errors/not-authorized-error");
9
- const JWT_SECRET = process.env.JWT_SECRET || "your-secret-key-change-in-production";
9
+ const getJwtSecret = () => {
10
+ const secret = process.env.JWT_SECRET || process.env.JWT_KEY;
11
+ if (!secret) {
12
+ throw new Error("JWT_SECRET must be defined");
13
+ }
14
+ return secret;
15
+ };
16
+ const extractAccessToken = (req) => {
17
+ var _a, _b;
18
+ const authHeader = req.headers.authorization;
19
+ const bearer = typeof authHeader === "string" && authHeader.startsWith("Bearer ")
20
+ ? authHeader.substring(7)
21
+ : undefined;
22
+ const cookieToken = (_a = req.cookies) === null || _a === void 0 ? void 0 : _a.accessToken;
23
+ const legacySessionToken = (_b = req.session) === null || _b === void 0 ? void 0 : _b.jwt;
24
+ const token = bearer || cookieToken || legacySessionToken;
25
+ return typeof token === "string" && token.trim() ? token.trim() : undefined;
26
+ };
27
+ const attachPayload = (req, payload) => {
28
+ req.currentUser = payload;
29
+ req.userId = payload.userId || payload.id;
30
+ req.sessionId = payload.sessionId;
31
+ };
10
32
  /**
11
33
  * Middleware: Require authentication
12
34
  * Verifies JWT access token and attaches user payload to request
13
35
  * Works across all microservices
14
36
  */
15
37
  const requireAuth = (req, res, next) => {
16
- var _a;
17
38
  try {
18
- // Extract token from Authorization header or cookies
19
- const authHeader = req.headers.authorization;
20
- const token = (authHeader === null || authHeader === void 0 ? void 0 : authHeader.startsWith("Bearer "))
21
- ? authHeader.substring(7)
22
- : (_a = req.cookies) === null || _a === void 0 ? void 0 : _a.accessToken;
39
+ const token = extractAccessToken(req);
23
40
  // Check if token exists
24
41
  if (!token) {
25
- console.log("[requireAuth] No token provided");
26
42
  throw new not_authorized_error_1.NotAuthorizedError();
27
43
  }
28
44
  // Verify token
29
- const payload = jsonwebtoken_1.default.verify(token, JWT_SECRET);
30
- console.log("[requireAuth] Token verified successfully:", {
31
- userId: payload.userId,
32
- id: payload.id,
33
- email: payload.email,
34
- hasAccountNumber: !!payload.accountNumber
35
- });
36
- // Attach user payload to request
37
- req.currentUser = payload;
38
- // Support both legacy 'id' and new 'userId' formats
39
- req.userId = payload.userId || payload.id;
40
- req.sessionId = payload.sessionId;
45
+ const payload = jsonwebtoken_1.default.verify(token, getJwtSecret());
46
+ attachPayload(req, payload);
41
47
  next();
42
48
  }
43
49
  catch (err) {
44
- console.error("[requireAuth] Error:", err.message, err.name);
45
50
  if (err.name === "TokenExpiredError") {
46
51
  return res.status(401).json({
47
52
  error: "TOKEN_EXPIRED",
@@ -67,20 +72,13 @@ exports.requireAuth = requireAuth;
67
72
  * Loads user if token is present, but doesn't require it
68
73
  */
69
74
  const optionalAuth = (req, res, next) => {
70
- var _a;
71
75
  try {
72
- const authHeader = req.headers.authorization;
73
- const token = (authHeader === null || authHeader === void 0 ? void 0 : authHeader.startsWith("Bearer "))
74
- ? authHeader.substring(7)
75
- : (_a = req.cookies) === null || _a === void 0 ? void 0 : _a.accessToken;
76
+ const token = extractAccessToken(req);
76
77
  if (!token) {
77
78
  return next();
78
79
  }
79
- const payload = jsonwebtoken_1.default.verify(token, JWT_SECRET);
80
- req.currentUser = payload;
81
- // Support both legacy 'id' and new 'userId' formats
82
- req.userId = payload.userId || payload.id;
83
- req.sessionId = payload.sessionId;
80
+ const payload = jsonwebtoken_1.default.verify(token, getJwtSecret());
81
+ attachPayload(req, payload);
84
82
  next();
85
83
  }
86
84
  catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@helpio/common",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "common library for Helpio ",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",