Files
SerpentRace/SerpentRace_Backend/dist/Api/routers/organizationRouter.js
T

179 lines
8.5 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const AuthMiddleware_1 = require("../../Application/Services/AuthMiddleware");
const DIContainer_1 = require("../../Application/Services/DIContainer");
const ErrorResponseService_1 = require("../../Application/Services/ErrorResponseService");
const Generalsearch_1 = require("../../Application/Search/Generalsearch");
const Logger_1 = require("../../Application/Services/Logger");
const organizationRouter = (0, express_1.Router)();
// Create search service that isn't in the container yet
const searchService = new Generalsearch_1.GeneralSearchService(DIContainer_1.container.userRepository, DIContainer_1.container.organizationRepository, DIContainer_1.container.deckRepository);
// Auth routes - Get organizations with pagination (RECOMMENDED)
organizationRouter.get('/page/:from/:to', AuthMiddleware_1.authRequired, async (req, res) => {
try {
const from = parseInt(req.params.from);
const to = parseInt(req.params.to);
if (isNaN(from) || isNaN(to) || from < 0 || to < from) {
return res.status(400).json({ error: 'Invalid page parameters. "from" and "to" must be valid numbers with to >= from >= 0' });
}
(0, Logger_1.logRequest)('Get organizations by page endpoint accessed', req, res, { from, to });
const result = await DIContainer_1.container.getOrganizationsByPageQueryHandler.execute({ from, to });
(0, Logger_1.logRequest)('Organizations page retrieved successfully', req, res, {
from,
to,
count: result.organizations.length,
totalCount: result.totalCount
});
res.json(result);
}
catch (error) {
(0, Logger_1.logError)('Get organizations by page endpoint error', error, req, res);
res.status(500).json({ error: 'Internal server error' });
}
});
organizationRouter.get('/search', AuthMiddleware_1.authRequired, async (req, res) => {
try {
const { q: query, limit, offset } = req.query;
(0, Logger_1.logRequest)('Search organizations endpoint accessed', req, res, { query, limit, offset });
if (!query || typeof query !== 'string') {
(0, Logger_1.logWarning)('Organization search attempted without query', { query, hasQuery: !!query }, req, res);
return res.status(400).json({ error: 'Search query is required' });
}
const searchQuery = {
query: query.trim(),
limit: limit ? parseInt(limit) : 20,
offset: offset ? parseInt(offset) : 0
};
// Validate pagination parameters
if (searchQuery.limit < 1 || searchQuery.limit > 100) {
(0, Logger_1.logWarning)('Invalid organization search limit parameter', { limit: searchQuery.limit }, req, res);
return res.status(400).json({ error: 'Limit must be between 1 and 100' });
}
if (searchQuery.offset < 0) {
(0, Logger_1.logWarning)('Invalid organization search offset parameter', { offset: searchQuery.offset }, req, res);
return res.status(400).json({ error: 'Offset must be non-negative' });
}
const result = await searchService.searchFromUrl(req.originalUrl, searchQuery);
(0, Logger_1.logRequest)('Organization search completed successfully', req, res, {
query: searchQuery.query,
resultCount: Array.isArray(result) ? result.length : 0
});
res.json(result);
}
catch (error) {
(0, Logger_1.logError)('Search organizations endpoint error', error, req, res);
res.status(500).json({ error: 'Internal server error' });
}
});
// Get organization login URL
organizationRouter.get('/:orgId/login-url', AuthMiddleware_1.authRequired, async (req, res) => {
try {
const userId = req.user.userId;
const { orgId } = req.params;
(0, Logger_1.logRequest)('Get organization login URL endpoint accessed', req, res, {
userId,
organizationId: orgId
});
const result = await DIContainer_1.container.getOrganizationLoginUrlQueryHandler.execute({
organizationId: orgId
});
if (!result) {
(0, Logger_1.logWarning)('Organization login URL not found', {
organizationId: orgId,
userId
}, req, res);
return ErrorResponseService_1.ErrorResponseService.sendNotFound(res, 'Organization login URL not found');
}
(0, Logger_1.logRequest)('Organization login URL retrieved successfully', req, res, {
organizationId: orgId,
organizationName: result.organizationName,
hasUrl: !!result.loginUrl,
userId
});
res.json(result);
}
catch (error) {
(0, Logger_1.logError)('Get organization login URL endpoint error', error, req, res);
return ErrorResponseService_1.ErrorResponseService.sendInternalServerError(res);
}
});
// Process third-party authentication callback
organizationRouter.post('/auth-callback', AuthMiddleware_1.authRequired, async (req, res) => {
try {
const userId = req.user.userId;
const { organizationId, status, authToken } = req.body;
(0, Logger_1.logRequest)('Organization auth callback endpoint accessed', req, res, {
userId,
organizationId,
status,
hasAuthToken: !!authToken
});
// Validate required fields
if (!organizationId || !status) {
(0, Logger_1.logWarning)('Missing required fields for organization auth callback', {
organizationId: !!organizationId,
status: !!status,
userId
}, req, res);
return ErrorResponseService_1.ErrorResponseService.sendBadRequest(res, 'organizationId and status are required');
}
if (status !== 'ok' && status !== 'not_ok') {
(0, Logger_1.logWarning)('Invalid status value for organization auth callback', {
status,
userId,
organizationId
}, req, res);
return ErrorResponseService_1.ErrorResponseService.sendBadRequest(res, 'status must be either "ok" or "not_ok"');
}
const result = await DIContainer_1.container.processOrgAuthCallbackCommandHandler.execute({
organizationId,
userId,
status,
authToken
});
if (!result.success) {
if (result.message.includes('not found')) {
(0, Logger_1.logWarning)('Organization auth callback failed - entity not found', {
userId,
organizationId,
message: result.message
}, req, res);
return ErrorResponseService_1.ErrorResponseService.sendNotFound(res, result.message);
}
if (result.message.includes('does not belong')) {
(0, Logger_1.logWarning)('Organization auth callback failed - authorization error', {
userId,
organizationId,
message: result.message
}, req, res);
return ErrorResponseService_1.ErrorResponseService.sendForbidden(res, result.message);
}
if (result.message.includes('authentication failed')) {
(0, Logger_1.logAuth)('Organization authentication failed via callback', userId, {
organizationId,
status
}, req, res);
return ErrorResponseService_1.ErrorResponseService.sendUnauthorized(res, result.message);
}
(0, Logger_1.logError)('Organization auth callback internal error', new Error(result.message), req, res);
return ErrorResponseService_1.ErrorResponseService.sendInternalServerError(res);
}
(0, Logger_1.logAuth)('Organization auth callback processed successfully', userId, {
organizationId,
status,
updatedFields: result.updatedFields
}, req, res);
res.json({
success: result.success,
message: result.message,
updatedFields: result.updatedFields
});
}
catch (error) {
(0, Logger_1.logError)('Organization auth callback endpoint error', error, req, res);
return ErrorResponseService_1.ErrorResponseService.sendInternalServerError(res);
}
});
exports.default = organizationRouter;
//# sourceMappingURL=organizationRouter.js.map