eggi-ai-db-schema
Version:
Type-safe database schema and ORM client for Eggi.AI with direct RDS connection
145 lines • 6.63 kB
JavaScript
/**
* =============================================================================
* AUTHENTICATED USER OPERATIONS UTILITIES
* =============================================================================
* Utility functions for managing authenticated users table operations
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.findAuthenticatedUserByCognitoId = findAuthenticatedUserByCognitoId;
exports.findAuthenticatedUserById = findAuthenticatedUserById;
exports.findAuthenticatedUserByUserId = findAuthenticatedUserByUserId;
exports.findAuthenticatedUserByLinkedInIdentifier = findAuthenticatedUserByLinkedInIdentifier;
const drizzle_orm_1 = require("drizzle-orm");
const db_1 = require("../lib/db");
const schema_1 = require("../lib/schema");
/**
* Finds an authenticated user record by Cognito User ID
*
* This function queries the authenticated_users table specifically and returns
* the authenticated user record which contains:
* - id: Primary key of authenticated_users table
* - userId: Foreign key linking to the users table (use this for social accounts)
* - cognitoUserId: The Cognito ID for authentication
*
* @param cognitoUserId - The Cognito User ID to search for
* @returns Promise resolving to authenticated user record or null if not found
*/
async function findAuthenticatedUserByCognitoId(cognitoUserId) {
const db = await (0, db_1.getDb)();
const result = await db
.select()
.from(schema_1.authenticatedUsers)
.where((0, drizzle_orm_1.eq)(schema_1.authenticatedUsers.cognitoUserId, cognitoUserId))
.limit(1);
return result.length > 0 ? result[0] : null;
}
/**
* Finds an authenticated user record by authenticated user ID
*
* @param authenticatedUserId - The authenticated_users.id to search for
* @returns Promise resolving to authenticated user record or null if not found
*/
async function findAuthenticatedUserById(authenticatedUserId) {
const db = await (0, db_1.getDb)();
const result = await db
.select()
.from(schema_1.authenticatedUsers)
.where((0, drizzle_orm_1.eq)(schema_1.authenticatedUsers.id, authenticatedUserId))
.limit(1);
return result.length > 0 ? result[0] : null;
}
/**
* Finds an authenticated user record by user ID (foreign key to users table)
*
* @param userId - The authenticated_users.user_id to search for
* @returns Promise resolving to authenticated user record or null if not found
*/
async function findAuthenticatedUserByUserId(userId) {
const db = await (0, db_1.getDb)();
const result = await db
.select()
.from(schema_1.authenticatedUsers)
.where((0, drizzle_orm_1.eq)(schema_1.authenticatedUsers.userId, userId))
.limit(1);
return result.length > 0 ? result[0] : null;
}
/**
* Finds an authenticated user by LinkedIn identifier
*
* This function performs the complete chain lookup:
* LinkedIn identifier → social_account → user → authenticated_user
*
* Used by pre-signup validation to prevent duplicate accounts when a user
* already has an authenticated account linked to their LinkedIn profile.
*
* @param linkedinIdentifier - LinkedIn identifier (ACoA, ACwA, AEMA, or public)
* @returns Promise resolving to authenticated user details or null if not found
*/
async function findAuthenticatedUserByLinkedInIdentifier(linkedinIdentifier) {
const db = await (0, db_1.getDb)();
// Import required schema tables
const { socialAccounts, users } = await Promise.resolve().then(() => __importStar(require("../lib/schema")));
const { and, or } = await Promise.resolve().then(() => __importStar(require("drizzle-orm")));
// Build where clause based on identifier type (same logic as findSocialAccountByLinkedInIdentifier)
let whereClause;
if (linkedinIdentifier.startsWith("ACoA") ||
linkedinIdentifier.startsWith("ACwA") ||
linkedinIdentifier.startsWith("AEMA")) {
// For LinkedIn internal identifiers: Search BOTH internal identifier fields
whereClause = and((0, drizzle_orm_1.eq)(socialAccounts.platform, "linkedin"), or((0, drizzle_orm_1.eq)(socialAccounts.internalIdentifierRegular, linkedinIdentifier), // ACoA* identifiers
(0, drizzle_orm_1.eq)(socialAccounts.internalIdentifier, linkedinIdentifier) // ACwA*/AEMA* identifiers
));
}
else {
// Public identifier → search in public_identifier field
whereClause = and((0, drizzle_orm_1.eq)(socialAccounts.platform, "linkedin"), (0, drizzle_orm_1.eq)(socialAccounts.publicIdentifier, linkedinIdentifier));
}
// Join social_accounts → users → authenticated_users
const result = await db
.select({
user_id: users.id,
social_account_id: socialAccounts.id,
cognito_user_id: schema_1.authenticatedUsers.cognitoUserId,
})
.from(socialAccounts)
.innerJoin(users, (0, drizzle_orm_1.eq)(socialAccounts.userId, users.id))
.innerJoin(schema_1.authenticatedUsers, (0, drizzle_orm_1.eq)(users.id, schema_1.authenticatedUsers.userId))
.where(whereClause)
.limit(1);
return result.length > 0 ? result[0] : null;
}
//# sourceMappingURL=authenticated-user-operations.js.map
;