Skip to content

Commit b0210e8

Browse files
mutliplr changes but nothing is working as of now
1 parent 1e0c504 commit b0210e8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1365
-1
lines changed

aws/Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Use the official AWS Lambda Node.js 18 runtime base image
2+
FROM public.ecr.aws/lambda/nodejs:18
3+
4+
# Set working directory inside the container
5+
WORKDIR /var/task
6+
7+
# Copy package.json and package-lock.json first for caching dependencies
8+
COPY package*.json ./
9+
10+
# Install all production dependencies
11+
RUN npm install --production
12+
13+
# Copy the full backend source code into container
14+
COPY . .
15+
16+
# (Optional) Build step if using typescript or transpilers
17+
# RUN npm run build or tsc
18+
19+
# Set the Lambda handler (adjust path and exported handler function if different)
20+
# For a single API Lambda serving all routes, this could be main.js exporting handler
21+
# or an index.js that routes calls internally
22+
CMD [ "aws/main.handler" ]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// aws/controllers/admin.controller.js
2+
const db = require('../services/db');
3+
4+
async function listAllUsers() {
5+
return await db.listUsers();
6+
}
7+
8+
async function listAllProjects() {
9+
return await db.listAllProjects();
10+
}
11+
12+
async function updateFeatureFlags(userId, flags) {
13+
return await db.setUserFeatureFlags(userId, flags);
14+
}
15+
16+
module.exports = {
17+
listAllUsers,
18+
listAllProjects,
19+
updateFeatureFlags,
20+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// aws/controllers/analytics.controller.js
2+
const db = require('../services/db');
3+
4+
async function getProjectAnalytics(projectId) {
5+
return await db.fetchAnalyticsByProject(projectId);
6+
}
7+
8+
async function storeFeedback(projectId, userId, feedback) {
9+
return await db.saveUserFeedback({ projectId, userId, ...feedback, createdAt: new Date().toISOString() });
10+
}
11+
12+
module.exports = {
13+
getProjectAnalytics,
14+
storeFeedback,
15+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// aws/controllers/apikeys.controller.js
2+
const { generateApiKey, hashApiKey } = require('../utils/apiKeyManager');
3+
const { AppError } = require('../utils/errorHandler');
4+
const db = require('../services/db');
5+
6+
async function listApiKeys(userId) {
7+
return await db.getApiKeysByUser(userId);
8+
}
9+
10+
async function createApiKey(userId, projectId) {
11+
const { rawKey, hash } = generateApiKey();
12+
13+
const saved = await db.createApiKey({
14+
userId,
15+
projectId,
16+
keyHash: hash,
17+
createdAt: new Date().toISOString(),
18+
});
19+
20+
if (!saved) throw new AppError("Failed to create API key", 500);
21+
22+
// Return only rawKey once to user (can't retrieve later)
23+
return { apiKey: rawKey };
24+
}
25+
26+
async function revokeApiKey(keyId, userId) {
27+
const deleted = await db.deleteApiKey(keyId, userId);
28+
if (!deleted) throw new AppError("Failed to revoke API key", 500);
29+
return true;
30+
}
31+
32+
module.exports = {
33+
listApiKeys,
34+
createApiKey,
35+
revokeApiKey,
36+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// aws/controllers/billing.controller.js
2+
const fetch = require('node-fetch');
3+
const { AppError } = require('../utils/errorHandler');
4+
const { stripeKey } = require('../utils/env'); // (replace if using Polar secrets)
5+
const POLAR_API_BASE = 'https://api.polar.sh/v1';
6+
7+
// Polar Auth token should be added to env vars
8+
const POLAR_API_KEY = process.env.POLAR_API_KEY;
9+
10+
if (!POLAR_API_KEY) {
11+
throw new Error("Missing POLAR_API_KEY in environment variables");
12+
}
13+
14+
// Example: Create a Checkout Session
15+
async function createCheckoutSession(userId, planId) {
16+
const response = await fetch(`${POLAR_API_BASE}/checkout_sessions`, {
17+
method: 'POST',
18+
headers: {
19+
'Authorization': `Bearer ${POLAR_API_KEY}`,
20+
'Content-Type': 'application/json'
21+
},
22+
body: JSON.stringify({
23+
customer: userId, // your user identifier in Polar
24+
plan: planId,
25+
success_url: 'https://yourfrontend.com/success',
26+
cancel_url: 'https://yourfrontend.com/cancel'
27+
})
28+
});
29+
30+
if (!response.ok) {
31+
const errStr = await response.text();
32+
throw new AppError(`Polar Checkout Session creation failed: ${errStr}`, 500);
33+
}
34+
35+
const data = await response.json();
36+
return data; // Contains session URL or ID to send back to frontend
37+
}
38+
39+
// Other billing-related functions: webhook handling, subscription status, etc.
40+
41+
module.exports = {
42+
createCheckoutSession,
43+
};

aws/controllers/chat.controller.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// aws/controllers/chat.controller.js
2+
const { AppError } = require('../utils/errorHandler');
3+
const aiService = require('../ai/index.js');
4+
5+
async function sendChatQuery(userId, projectId, queryText) {
6+
if (!queryText) throw new AppError("Query is required", 400);
7+
8+
// Call AI service pipeline/chat with query
9+
const response = await aiService.chatPipeline(userId, projectId, queryText);
10+
return response;
11+
}
12+
13+
async function sendChatStream(userId, projectId, queryText) {
14+
if (!queryText) throw new AppError("Query is required", 400);
15+
16+
// Call streaming pipeline
17+
const stream = await aiService.chatStreamPipeline(userId, projectId, queryText);
18+
return stream;
19+
}
20+
21+
module.exports = {
22+
sendChatQuery,
23+
sendChatStream,
24+
};

aws/controllers/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// aws/controllers/index.js
2+
3+
const aiController = require('./ai.controller');
4+
const authController = require('./auth.controller');
5+
const billingController = require('./billing.controller');
6+
const projectsController = require('./projects.controller');
7+
const scrapingController = require('./scraping.controller');
8+
9+
// Newly added controllers from our plan
10+
const usersController = require('./users.controller');
11+
const apikeysController = require('./apikeys.controller');
12+
const chatController = require('./chat.controller');
13+
const analyticsController = require('./analytics.controller');
14+
const supportController = require('./support.controller');
15+
const adminController = require('./admin.controller');
16+
17+
module.exports = {
18+
aiController,
19+
authController,
20+
billingController,
21+
projectsController,
22+
scrapingController,
23+
usersController,
24+
apikeysController,
25+
chatController,
26+
analyticsController,
27+
supportController,
28+
adminController
29+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// aws/controllers/support.controller.js
2+
const db = require('../services/db');
3+
const { AppError } = require('../utils/errorHandler');
4+
5+
async function createSupportTicket(userId, subject, description) {
6+
if (!subject || !description) throw new AppError("Subject and description required", 400);
7+
8+
const ticket = {
9+
userId,
10+
subject,
11+
description,
12+
status: 'open',
13+
createdAt: new Date().toISOString(),
14+
};
15+
16+
return await db.insertTicket(ticket);
17+
}
18+
19+
async function listUserTickets(userId) {
20+
return await db.getTicketsByUser(userId);
21+
}
22+
23+
module.exports = {
24+
createSupportTicket,
25+
listUserTickets
26+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// aws/controllers/users.controller.js
2+
const { AppError } = require('../utils/errorHandler');
3+
const db = require('../services/db'); // Your DB query module here
4+
const clerkClient = require('@clerk/clerk-sdk-node'); // Clerk SDK
5+
6+
// Fetch user details by userId (Clerk user id)
7+
async function getUserById(userId) {
8+
const userRecord = await db.getUser(userId);
9+
if (!userRecord) {
10+
// Optionally, fetch from Clerk if not found locally and create local record
11+
const clerkUser = await clerkClient.users.getUser(userId);
12+
if (!clerkUser) throw new AppError('User not found', 404);
13+
14+
const newUser = {
15+
id: clerkUser.id,
16+
email: clerkUser.emailAddresses[0].emailAddress,
17+
plan: 'free', // default plan
18+
createdAt: new Date().toISOString(),
19+
// any other metadata
20+
};
21+
22+
await db.createUser(newUser);
23+
return newUser;
24+
}
25+
return userRecord;
26+
}
27+
28+
// Update user plan and metadata in DB (called from Clerk webhook handler usually)
29+
async function updateUserPlan(userId, plan) {
30+
const updated = await db.updateUser(userId, { plan });
31+
if (!updated) throw new AppError("Failed to update user plan", 500);
32+
return updated;
33+
}
34+
35+
// List all users (for admin)
36+
async function listUsers() {
37+
return await db.listUsers();
38+
}
39+
40+
module.exports = {
41+
getUserById,
42+
updateUserPlan,
43+
listUsers,
44+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM public.ecr.aws/lambda/nodejs:18
2+
WORKDIR /var/task
3+
COPY package*.json ./
4+
RUN npm install --production
5+
COPY . .
6+
CMD [ "aws/handlers/aiChatHandler.handler" ]

0 commit comments

Comments
 (0)